Skip to content

RetrieveAndGenerateStream

Source: src/AWS/Bedrock/RetrieveAndGenerateStream.ts

Runtime binding for bedrock-agent-runtime:RetrieveAndGenerateStream — the streaming variant of RetrieveAndGenerate. The grounded answer arrives as an event Stream of output text deltas, citation events, and guardrail events instead of a single response.

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 ragStream = yield* Bedrock.RetrieveAndGenerateStream(
knowledgeBase,
"us.amazon.nova-micro-v1:0",
);
// runtime
const result = yield* ragStream({
input: { text: "How do I rotate credentials?" },
retrieveAndGenerateConfiguration: {
type: "KNOWLEDGE_BASE",
knowledgeBaseConfiguration: {
knowledgeBaseId: yield* knowledgeBase.knowledgeBaseId,
modelArn: "us.amazon.nova-micro-v1:0",
},
},
});
const events = yield* Stream.runCollect(result.stream);
const answer = events.map((event) => event.output?.text ?? "").join("");