Skip to content

RetrieveAndGenerate

Source: src/AWS/Bedrock/RetrieveAndGenerate.ts

Runtime binding for bedrock-agent-runtime:RetrieveAndGenerate — the fully managed RAG operation: retrieve relevant passages from a KnowledgeBase and generate a grounded answer with a foundation model in one call.

Bind a knowledge base and one or more generation models inside a function runtime. The binding grants bedrock:Retrieve and bedrock:RetrieveAndGenerate scoped to the knowledge base, plus bedrock:InvokeModel scoped to the bound models (or all foundation models and cross-region inference profiles when none are named).

// init
const rag = yield* Bedrock.RetrieveAndGenerate(
knowledgeBase,
"us.anthropic.claude-3-5-sonnet-20240620-v1:0",
);
// runtime
const result = yield* rag({
input: { text: "How do I rotate credentials?" },
retrieveAndGenerateConfiguration: {
type: "KNOWLEDGE_BASE",
knowledgeBaseConfiguration: {
knowledgeBaseId: yield* knowledgeBase.knowledgeBaseId,
modelArn: "us.anthropic.claude-3-5-sonnet-20240620-v1:0",
},
},
});
const answer = result.output.text;