Vector Databases and Vector Embeddings in Generative AI

Vector Databases and Vector Embeddings in Generative AI

·

2 min read

What Are Vector Embeddings?

Vector embeddings are numerical representations of data. In Generative AI, they convert text, images, or other data into a list of numbers (vectors). These vectors help us understand and compare different pieces of data.

Example:

  • Text: "Hello world"

  • Vector: [0.1, -0.2, 0.3, ...]

What is a Vector Database?

A vector database stores these vectors and helps you find similar data. Unlike traditional databases, it’s designed for comparing vectors.

Example Use: Finding similar documents or products.

Using MongoDB for Vector Embeddings

MongoDB can store vector embeddings and allow you to retrieve them. Here’s a simple way to use MongoDB for this purpose.

1. Setup MongoDB

Make sure MongoDB is installed and running.

2. Connect to MongoDB and Create a Collection

const { MongoClient } = require('mongodb');

async function run() {
    const client = new MongoClient('mongodb://localhost:27017');
    await client.connect();
    const db = client.db('genai_database');
    const collection = db.collection('embeddings');
    console.log('Connected to MongoDB!');
}

run().catch(console.dir);

3. Store a Vector Embedding

async function storeEmbedding(text, vector) {
    const client = new MongoClient('mongodb://localhost:27017');
    await client.connect();
    const db = client.db('genai_database');
    const collection = db.collection('embeddings');
    await collection.insertOne({ text, vector });
    console.log('Embedding stored!');
}

storeEmbedding('Hello world', [0.1, -0.2, 0.3]).catch(console.dir);

4. Find Similar Vectors

const { MongoClient } = require('mongodb');

async function vectorSearch(queryVector) {
    const client = new MongoClient('mongodb://localhost:27017');
    await client.connect();
    const db = client.db('genai_database');
    const collection = db.collection('embeddings');

    const results = await collection.aggregate([
        {
            $vectorSearch: {
                vector: queryVector,
                field: 'vector',
                k: 5,  // Number of similar vectors to return
                metric: 'cosineSimilarity'  // Similarity metric
            }
        },
    ]).toArray();

    console.log('Search Results:', results);
    await client.close();
}

// Example usage
vectorSearch([0.1, -0.2, 0.3]).catch(console.dir);

Summary

Vector embeddings are used to represent data as vectors. A vector database helps you find similar vectors, and MongoDB can be used to store and manage these embeddings.

That’s it! You now have a basic understanding of vector embeddings and how to use MongoDB for them. 🚀

Connect with me on Linkedin and Twitter