A Complete Developer Guide to Vector Embeddings!
Tags: developer, embeddings, ai, machinelearning
Published: 2024-04-02 06:43:15

In this rapidly growing digital age, the way we represent and process information has become increasingly sophisticated, with vector data standing at the forefront of this evolution. Vector data, fundamentally an array of numbers or elements, encapsulates the essence of objects and phenomena across various domains, from the intricacies of natural language in text analysis to the vibrant details of images and the nuanced tones of audio. Today, we will see everything about vector data with a simple tutorial on storing and retrieving vector data. 

## What is Vector Data? 
Traditional databases excel in storing data in simple formats but the challenge comes when the data is unstructured and high-dimensional? In this case, the data gets converted into vector format, in layman's terms you can say in a numerical format and this data is known as vector data. This is where vector databases shine. These vector databases store the vector data efficiently. 

![vector data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia3trsgeafl5nddn0hik.png)

In the context of machine learning and artificial intelligence, vector data often refers to an array or sequence of numbers or elements that represent characteristics or features of objects or entities. These vectors can be used to perform calculations, support machine learning algorithms, or facilitate searches and recommendations.

The vector data is stored in a vector database in a 3-dimensional space. This form of data representation allows for the efficient encoding of features and characteristics, enabling advanced computations, machine learning algorithms, and data analysis techniques. Whether it's powering semantic search engines, facilitating complex geographic information systems (GIS), or driving the latest advancements in artificial intelligence, vector data serves as a cornerstone for modern computational tasks. Its precision and adaptability make it an invaluable resource in our quest to understand and interact with the world around us in more meaningful ways.

## Vector Database: Storing Vector Data
![vector database store](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcfhgc4hnxepxi2hetl7.png)

Vector data is stored in vector databases, designed to efficiently manage and query vector data representations. These databases are tailored to handle the complexities and unique characteristics of vector data, enabling rapid, flexible, and scalable searches across large datasets. Unlike traditional databases that rely on exact matches or keyword searches, [vector databases](https://www.singlestore.com/built-in-vector-database/) excel in understanding the semantic context of the data, supporting operations like similarity search, nearest neighbor search, and pattern recognition. 

This makes them particularly useful in fields such as machine learning, recommendation systems, and natural language processing, where the ability to find similar items or concepts based on their vector representations is crucial. By leveraging advanced indexing and query mechanisms, vector databases provide a robust infrastructure for applications requiring high-performance analysis and retrieval of vector data, thereby facilitating more nuanced and contextually aware insights.

The robust vector database capabilities of SingleStoreDB are tailored to seamlessly serve AI-driven applications, chatbots, image recognition systems and more. With SingleStoreDB, the necessity for maintaining a dedicated vector database for your vector-intensive workloads becomes obsolete.

If you want to have an in-depth understanding of vector databases, read my article “[A Deep Dive into Vector Databases](https://www.singlestore.com/blog/a-complete-guide-to-vector-databases/)”.

{% embed https://dev.to/pavanbelagatti/wtf-is-a-vector-database-a-beginners-guide-16p %}

### Tutorial: Storing and Retrieving Vector Data using SingleStore

We will use SingleStore DB as a vector database to store our vector data. 
[Sign up to SingleStore](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=vecembd) for free to get started.

Once you sign up, create a workspace.

![create workspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or8a1xoraq7wb2yzyhzx.png)

Next, create a database under the workspace you just created. 

![db creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xn3k5rcuncxamt4i40s.png)

Click on ‘Create Database’ to create a new database.

I created my database ‘_vectordata_’. 

Now, let’s create a table inside our newly created database to store vector data.

Open the SQL Editor and select the workspace and database we created.

![sql code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igeucgnqcbmcwn838eei.png)

The table name is reviews. Use the below SQL query to create the table.

```
CREATE TABLE reviews(id INT not null PRIMARY KEY,
  review TEXT,
  position vector(4),
  category VARCHAR(256));
```

Let's add some parameters like id, pos (vector embeddings), rev (review) and cat (category).

```
SET @_id = 1;
SET @pos = '[0.1, 0.19, 0.37, 0.04]';
SET @rev = "The namaste cafe in HSR has a great choco bar";
SET @cat = "Food";
```
Note: The vector embeddings under @pos are just fictional for the tutorial purpose only. You can create real embeddings using different embedding models from OpenAI, Cohere or HuggingFace.


Insert into the reviews table.

```
INSERT INTO reviews VALUES (@_id, @rev, @pos, @cat);
```

Similarly, let’s add some more vector data.

```
SET @_id = 2;
SET @pos = '[0.2, 0.13, 0.26, 0.39]';
SET @rev = "The asha cafe in BTM has a great milk bar";
SET @cat = "Food";


INSERT INTO reviews VALUES (@_id, @rev, @pos, @cat);


SET @_id = 3;
SET @pos = '[0.7, 0.38, 0.10, 0.97]';
SET @rev = "The empire resobar in JP Nagar has a great Dosa bar";
SET @cat = "Food";


INSERT INTO reviews VALUES (@_id, @rev, @pos, @cat);


SET @_id = 4;
SET @pos = '[0.2, 0.25, 0.58, 0.49]';
SET @rev = "The MTR in Madivala has a great Upma bar";
SET @cat = "Food";


INSERT INTO reviews VALUES (@_id, @rev, @pos, @cat);
```

Let’s consider our query having the vector data representation as below,

```
SET @query_vec = '[0.7, 0.38, 0.10, 0.97]' :> VECTOR(4);
```

Using the vector similarity search, we will be able to find more similar results. 

```
SELECT id, review, category, dot_product(position, @query_vec) 
AS score
   FROM reviews
   ORDER BY score DESC
   LIMIT 2
   \G
```

![similarity score](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bisgjf7nalikt2jrs57n.png)

This way, we can easily store and retrieve vector data from SingleStore Database. 

Let’s see one more tutorial example where you can create vector embeddings from SingleStore Notebook for real text and store the vector data in a SingleStore database.

![convert to embeddings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1v34a50uvcvduwt4aoh.png)

[Complete Notebook code link is here for you to try](https://github.com/pavanbelagatti/vector-embeddings)

Once you create these vector embeddings, storing them in a database is easy.

Create a database and a table inside that database to store these embeddings

![database creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr16pgnzq9gejax9r0p0.png)

Then, create a table using the SQL editor. Make sure to select the workspace and database you created.

```
CREATE TABLE IF NOT EXISTS <your table name> (
  text TEXT,
  vector BLOB
);
```

Next, add these embeddings into the table created.

```
INSERT INTO <table name> (text, vector) VALUES ("Hello World", JSON_ARRAY_PACK("<add the embedding values generated>"))
```

You can see the vector embeddings stored in your database under the table you created.

![vector embeddings creation example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq1g52h60e7scyp0sdix.png)

In conclusion, vector data plays a pivotal role in the contemporary landscape of data management and analysis, offering an effective means to represent, store, and retrieve complex, high-dimensional data. With advanced databases like SingleStore, you can efficiently manage vector data. This enables organizations to harness the full potential of their unstructured data, facilitating enhanced insights, faster query responses, and more intelligent decision-making processes. 

[Sign up to SingleStore Database](https://www.singlestore.com/cloud-trial/?utm_medium=referral&utm_source=pavan&utm_term=devto&utm_content=vecembd) and experience the power of efficient vector data storage today.