Anvia
Retrieval

RAG Context

Add retrieval context to agent and completion workflows.

RAG context searches an index for each prompt and injects the matched documents into the model request.

1. Preprocess First

Build the index before the prompt runs.

const embedded = await embedDocuments(embeddings, documents, {
  id: (doc) => doc.id,
  content: (doc) => `${doc.title}\n${doc.body}`,
  metadata: (doc) => ({ product: doc.product }),
});

const index = InMemoryVectorStore.fromDocuments(embedded).index(embeddings);

Do not rebuild the index on every user prompt in production.

2. Attach Dynamic Context

const agent = new AgentBuilder("support", model)
  .instructions("Use retrieved context when answering support questions.")
  .dynamicContext(index, {
    topK: 2,
  })
  .build();

On each prompt, Anvia searches the index using the prompt text and adds matching documents to the completion request.

3. Format Retrieved Documents

const agent = new AgentBuilder("support", model)
  .dynamicContext(index, {
    topK: 2,
    format: (result) => ({
      id: result.id,
      text: `${result.document.title}\n${result.document.body}`,
    }),
  })
  .build();

Use format(...) when your stored document is an object and the model should receive a specific text shape.

4. Add a Threshold

const agent = new AgentBuilder("support", model)
  .dynamicContext(index, {
    topK: 3,
    threshold: 0.75,
  })
  .build();

Thresholds prevent weak matches from entering the prompt.

5. Run the Agent

const response = await agent
  .prompt("How long does a password reset link last?")
  .send();

Use dynamic context for automatic retrieval. Use a search tool when the model should decide whether to search.