ExamplesRAGRetrieve Results

Retrieving Top-K Results

After storing embeddings in a vector database, you need to query them to find similar content.

The query method returns the most semantically similar chunks to your input embedding, ranked by relevance. The topK parameter allows you to specify the number of results to return.

This example shows how to retrieve similar chunks from a Pinecone vector database.

import { openai } from "@ai-sdk/openai";
import { PineconeVector } from "@mastra/pinecone";
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 pinecone = new PineconeVector("your-api-key");
 
await pinecone.createIndex("test_index", 1536);
 
await pinecone.upsert(
  "test_index",
  embeddings,
  chunks?.map((chunk: any) => ({ text: chunk.text })),
);
 
const topK = 10;
 
const results = await pinecone.query("test_index", embeddings[0], topK);
 
console.log(results);





View Example on GitHub