Embed and search

Embeddings turn text into numeric vectors. Similar meanings land close together in vector space, so a user question can find the chunks that are semantically near it, not just the ones that share keywords.

src/app/api/rag2/route.ts
typescript
import { MistralAIEmbeddings } from '@langchain/mistralai';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';

const embeddings = new MistralAIEmbeddings({ model: 'mistral-embed' });
const vectorStore = new MemoryVectorStore(embeddings);

await vectorStore.addDocuments(chunkDocuments);

const relatedDocs = await vectorStore.similaritySearch(userInput);

const mergedRelatedDocs = relatedDocs.map((doc) => doc.pageContent).join('\n');

MemoryVectorStore is a simple in-process store. Perfect for a workshop, not for production. similaritySearch returns the top-k chunks closest to the question in vector space.

You would not, at least not for multi-user or cold-start scenarios. It is a great teaching store because it has zero setup, and it is useful for request-scoped caches like this workshop. In production you swap it for Qdrant, pgvector, Chroma, or similar. The swap is often a one line change because the interface is the same.

Fill in the blanks: Complete the retrieval step

Loading practice…

Quiz: Quiz

Loading practice…