ExamplesRAGInsert Embedding in LibSQL

Insert Embedding in LibSQL

After generating embeddings, you need to store them in a vector database for similarity search. The DefaultVectorDB class provides methods to create collections and insert embeddings into LibSQL, a fork of SQLite with vector extensions. This example shows how to store embeddings in LibSQL for later retrieval.

import { openai } from "@ai-sdk/openai";
import { DefaultVectorDB } from "@mastra/core/storage";
import { MDocument } from "@mastra/rag";
import { embedMany } from "ai";
 
const doc = MDocument.fromText("Your text content...");
 
const chunks = await doc.chunk();
 
const { embeddings } = await embedMany({
  values: chunks.map((chunk) => chunk.text),
  model: openai.embedding("text-embedding-3-small"),
});
 
const libsql = new DefaultVectorDB(process.env.DATABASE_URL);
 
await libsql.createIndex("test_collection", 1536);
 
await libsql.upsert(
  "test_collection",
  embeddings,
  chunks?.map((chunk) => ({ text: chunk.text })),
);





View Example on GitHub