Knowledge Base RAG API
Query knowledge bases directly from your agent code to retrieve relevant documents and generate responses with citations.
Overview
The Knowledge Base RAG API provides two functions for interacting with knowledge bases:
retrieveFromKnowledgeBase: Retrieve relevant documents based on a queryretrieveAndGenerateFromKnowledgeBase: Retrieve documents and generate a response with citations
Both functions support metadata filtering, result count control, and automatic agent/environment isolation.
Default Knowledge Base Selection
The knowledgeBaseId parameter is optional. If omitted, the backend will:
Exactly 1 KB for the agent → automatically use it
0 KBs → throw an error prompting you to create a knowledge base
>1 KBs → throw an error requiring you to pass
knowledgeBaseIdexplicitly to disambiguate
This simplifies usage for agents with a single knowledge base.
Before you start: Create a knowledge base and upload documents in the platform dashboard. For agents with multiple knowledge bases, copy the
knowledgeBaseIdto pass explicitly. See Knowledge Bases.
retrieveFromKnowledgeBase
Retrieve relevant documents from a knowledge base.
Function Signature
Parameters
params.knowledgeBaseId
string
ID of the knowledge base to query. If omitted, backend auto-selects the KB if exactly one exists; errors if 0 or >1 KBs exist.
params.environment
string
✓
Environment (e.g., 'development', 'production')
params.query
string
✓
Search query string
params.numberOfResults
number
Number of results to return (default: 5)
params.filters
KnowledgeBaseFilter
Metadata filters for the query
timeoutMs
number
Query timeout (default: 30000ms)
Return Value
Returns a Promise with:
retrievalResults: Array of retrieval results, each containing:content.text: Text content of the document chunklocation.s3Location.uri: URI of the source documentmetadata: Associated metadata as key-value pairsscore: Relevance score (0-1)
nextToken: Optional token for pagination
Examples
With explicit knowledgeBaseId:
retrieveAndGenerateFromKnowledgeBase
Retrieve documents and generate a response using an LLM, with citations referencing source documents.
Function Signature
Parameters
params.knowledgeBaseId
string
ID of the knowledge base to query. If omitted, backend auto-selects the KB if exactly one exists; errors if 0 or >1 KBs exist.
params.environment
string
✓
Environment (e.g., 'development', 'production')
params.query
string
✓
Search query string
params.modelArn
string
Model ARN for generation (uses default if not specified)
params.numberOfResults
number
Number of documents to retrieve (default: 5)
params.filters
KnowledgeBaseFilter
Metadata filters for the query
timeoutMs
number
Query timeout (default: 60000ms)
Return Value
Returns a Promise with:
output: Generated text response (null if no relevant documents found)citations: Array of citations, each containing:generatedResponsePart.textResponsePart.text: The part of the response that cites this sourcegeneratedResponsePart.textResponsePart.span: Start/end positions in the responseretrievedReferences: Array of source documents referenced
sessionId: Optional session ID for conversation continuity
Examples
With explicit knowledgeBaseId:
Filtering
Both functions support metadata filters for precise document retrieval.
Filter Types
Filter Examples
Last updated