AWS.Bedrock reference
Source:
src/AWS/Bedrock/Agent.ts
An Amazon Bedrock agent — a foundation model driven by natural-language instructions that can orchestrate multi-step tasks.
Agent owns the lifecycle of the agent’s DRAFT version. An IAM execution
role is created automatically (trusted by bedrock.amazonaws.com, granted
bedrock:InvokeModel on the foundation model) unless an explicit
agentResourceRoleArn is supplied. After every create/update the agent is
prepared (unless prepare: false) so it is immediately invocable and can
back an AgentAlias.
Agent: Creating Agents
Section titled “Agent: Creating Agents”Minimal Agent
import * as Bedrock from "alchemy/AWS/Bedrock";
const agent = yield* Bedrock.Agent("assistant", { foundationModel: "us.anthropic.claude-3-5-sonnet-20240620-v1:0", instruction: "You are a helpful assistant that answers questions concisely.",});Agent with a Guardrail and Custom Session TTL
const agent = yield* Bedrock.Agent("assistant", { foundationModel: "us.anthropic.claude-3-5-sonnet-20240620-v1:0", instruction: "You are a careful, policy-compliant support agent.", idleSessionTTL: "30 minutes", guardrailConfiguration: { guardrailIdentifier: guardrail.guardrailId, guardrailVersion: "DRAFT", },});Agent with Long-Term Memory
// Session summaries are retained for 30 days and readable at runtime// through the GetAgentMemory binding.const agent = yield* Bedrock.Agent("assistant", { foundationModel: "us.anthropic.claude-3-5-sonnet-20240620-v1:0", instruction: "You are a helpful assistant that remembers past sessions.", memoryConfiguration: { enabledMemoryTypes: ["SESSION_SUMMARY"], storage: "30 days", },});AgentAlias
Section titled “AgentAlias”Source:
src/AWS/Bedrock/AgentAlias.ts
An alias for an Amazon Bedrock Agent — a stable, invocable pointer
to one or more agent versions.
An alias is what applications invoke (via bedrock-agent-runtime
InvokeAgent). Creating an alias with no routingConfiguration snapshots
the agent’s current DRAFT into a new immutable version and routes the alias
to it, so redeploying an updated + prepared agent and recreating the alias
publishes a new version.
AgentAlias: Creating Aliases
Section titled “AgentAlias: Creating Aliases”Alias Pointing at the Current Agent
import * as Bedrock from "alchemy/AWS/Bedrock";
const agent = yield* Bedrock.Agent("assistant", { foundationModel: "us.anthropic.claude-3-5-sonnet-20240620-v1:0", instruction: "You are a helpful assistant.",});
const alias = yield* Bedrock.AgentAlias("prod", { agentId: agent.agentId,});Alias Pinned to a Specific Version
const alias = yield* Bedrock.AgentAlias("prod", { agentId: agent.agentId, routingConfiguration: [{ agentVersion: "3" }],});Converse
Section titled “Converse”Source:
src/AWS/Bedrock/Converse.ts
Runtime binding for bedrock-runtime:Converse — Amazon Bedrock’s unified
messages API that works across all conversational foundation models.
Bind one or more model references inside a function runtime to get a
callable that sends messages to the model. The binding grants the function
bedrock:InvokeModel scoped to exactly the bound models. A model reference
may be a foundation-model id, a cross-region inference profile id (e.g.
us.amazon.nova-micro-v1:0), or a full Bedrock ARN (application inference
profile, imported model, prompt version, …).
Model access is an account entitlement — enable the model in the Bedrock
console (Model access) before invoking, otherwise calls fail with
AccessDeniedException. Many newer models are only invocable through a
cross-region inference profile id, not their bare foundation-model id.
Converse: Conversing with a Model
Section titled “Converse: Conversing with a Model”Send a Single Prompt
// initconst converse = yield* Bedrock.Converse("us.amazon.nova-micro-v1:0");
// runtimeconst result = yield* converse({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], inferenceConfig: { maxTokens: 64 },});const text = result.output.message.content[0]?.text;Bind Multiple Models and Pick Per Call
const converse = yield* Bedrock.Converse( "us.amazon.nova-micro-v1:0", "us.anthropic.claude-sonnet-4-20250514-v1:0",);
const result = yield* converse({ modelId: "us.anthropic.claude-sonnet-4-20250514-v1:0", messages: [{ role: "user", content: [{ text: "Summarize this." }] }],});System Prompt and Inference Config
const result = yield* converse({ system: [{ text: "You answer in exactly one word." }], messages: [{ role: "user", content: [{ text: "What color is the sky?" }] }], inferenceConfig: { maxTokens: 16, temperature: 0 },});ConverseStream
Section titled “ConverseStream”Source:
src/AWS/Bedrock/ConverseStream.ts
Runtime binding for bedrock-runtime:ConverseStream — the streaming
variant of Converse. The response arrives as an event Stream of
ConverseStreamOutput events (messageStart, contentBlockDelta,
messageStop, metadata, …) instead of a single message.
The binding grants the function bedrock:InvokeModelWithResponseStream
(the IAM action streaming operations authorize against) scoped to exactly
the bound models. A model reference may be a foundation-model id, a
cross-region inference profile id (e.g. us.amazon.nova-micro-v1:0), or a
full Bedrock ARN.
Model access is an account entitlement — enable the model in the Bedrock
console (Model access) before invoking, otherwise calls fail with
AccessDeniedException.
ConverseStream: Streaming a Conversation
Section titled “ConverseStream: Streaming a Conversation”// initconst converseStream = yield* Bedrock.ConverseStream("us.amazon.nova-micro-v1:0");
// runtimeconst result = yield* converseStream({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], inferenceConfig: { maxTokens: 64 },});const events = yield* Stream.runCollect(result.stream ?? Stream.empty);const text = events .map((event) => event.contentBlockDelta?.delta.text ?? "") .join("");CountTokens
Section titled “CountTokens”Source:
src/AWS/Bedrock/CountTokens.ts
Runtime binding for bedrock-runtime:CountTokens — count the input tokens
a Converse or InvokeModel request would consume, using the bound
model’s tokenizer, without invoking the model (and without inference
cost).
The binding grants the function bedrock:CountTokens scoped to exactly
the bound models.
Only a subset of models support token counting, addressed by their BARE
foundation-model id (e.g. anthropic.claude-haiku-4-5-20251001-v1:0) —
Amazon Nova models and cross-region inference-profile ids are rejected
with a ValidationException (“The provided model doesn’t support
counting tokens”).
CountTokens: Counting Tokens
Section titled “CountTokens: Counting Tokens”Count Tokens for a Converse Request
// initconst countTokens = yield* Bedrock.CountTokens( "anthropic.claude-haiku-4-5-20251001-v1:0",);
// runtimeconst result = yield* countTokens({ input: { converse: { messages: [{ role: "user", content: [{ text: "Say hello." }] }], }, },});const tokens = result.inputTokens;Count Tokens for a Raw InvokeModel Payload
const result = yield* countTokens({ input: { invokeModel: { body: JSON.stringify({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], }), }, },});DataSource
Section titled “DataSource”Source:
src/AWS/Bedrock/DataSource.ts
A data source attached to an Amazon Bedrock KnowledgeBase — the
origin of the documents the knowledge base embeds and indexes.
The most common source is an S3 bucket. After the data source is created,
start an ingestion job (bedrock-agent:StartIngestionJob) to crawl the
source, chunk + embed the documents, and write them to the vector store.
Ingestion is not part of the desired-state lifecycle — trigger it whenever
the underlying documents change.
DataSource: Creating Data Sources
Section titled “DataSource: Creating Data Sources”import * as Bedrock from "alchemy/AWS/Bedrock";
const source = yield* Bedrock.DataSource("docs-bucket", { knowledgeBaseId: kb.knowledgeBaseId, dataSourceConfiguration: { type: "S3", s3Configuration: { bucketArn: bucket.bucketArn }, }, dataDeletionPolicy: "DELETE",});DeleteAgentMemory
Section titled “DeleteAgentMemory”Source:
src/AWS/Bedrock/DeleteAgentMemory.ts
Runtime binding for bedrock-agent-runtime:DeleteAgentMemory — delete the
memory an agent has stored, for one session, one memory id, or everything.
Bind an AgentAlias inside a function runtime to get a callable
that clears the agent’s long-term memory. The binding grants the function
bedrock:DeleteAgentMemory scoped to exactly that alias. Deletion is
idempotent — deleting a session or memory id that holds no memory
succeeds.
DeleteAgentMemory: Deleting Agent Memory
Section titled “DeleteAgentMemory: Deleting Agent Memory”Forget One Session
// initconst deleteAgentMemory = yield* Bedrock.DeleteAgentMemory(alias);
// runtimeyield* deleteAgentMemory({ sessionId });Forget Everything for a Memory Id
yield* deleteAgentMemory({ memoryId: userId });DeleteKnowledgeBaseDocuments
Section titled “DeleteKnowledgeBaseDocuments”Source:
src/AWS/Bedrock/DeleteKnowledgeBaseDocuments.ts
Runtime binding for bedrock-agent:DeleteKnowledgeBaseDocuments — remove
specific documents from the bound DataSource’s knowledge base
index.
The binding grants the function bedrock:DeleteKnowledgeBaseDocuments
scoped to the data source’s parent knowledge base.
DeleteKnowledgeBaseDocuments: Direct Document Ingestion
Section titled “DeleteKnowledgeBaseDocuments: Direct Document Ingestion”// initconst deleteDocuments = yield* Bedrock.DeleteKnowledgeBaseDocuments(dataSource);
// runtimeyield* deleteDocuments({ documentIdentifiers: [ { dataSourceType: "CUSTOM", custom: { id: "welcome-doc" } }, ],});GetAgentMemory
Section titled “GetAgentMemory”Source:
src/AWS/Bedrock/GetAgentMemory.ts
Runtime binding for bedrock-agent-runtime:GetAgentMemory — retrieve the
session summaries an agent has stored for a memory id.
Bind an AgentAlias inside a function runtime to get a callable
that reads the agent’s long-term memory. The binding grants the function
bedrock:GetAgentMemory scoped to exactly that alias. The agent must have
memory enabled (see Agent’s memoryConfiguration prop); summaries are
generated asynchronously after a session ends.
GetAgentMemory: Reading Agent Memory
Section titled “GetAgentMemory: Reading Agent Memory”// initconst getAgentMemory = yield* Bedrock.GetAgentMemory(alias);
// runtimeconst result = yield* getAgentMemory({ memoryType: "SESSION_SUMMARY", memoryId: userId, maxItems: 10,});const summaries = (result.memoryContents ?? []).map( (memory) => memory.sessionSummary?.summaryText,);GetIngestionJob
Section titled “GetIngestionJob”Source:
src/AWS/Bedrock/GetIngestionJob.ts
Runtime binding for bedrock-agent:GetIngestionJob — read the status and
statistics of an ingestion job started on the bound DataSource.
The binding grants the function bedrock:GetIngestionJob scoped to the
data source’s parent knowledge base.
GetIngestionJob: Syncing a Data Source
Section titled “GetIngestionJob: Syncing a Data Source”// initconst getIngestionJob = yield* Bedrock.GetIngestionJob(dataSource);
// runtimeconst { ingestionJob } = yield* getIngestionJob({ ingestionJobId: jobId,}).pipe( Effect.repeat({ schedule: Schedule.spaced("5 seconds"), until: (r) => r.ingestionJob.status === "COMPLETE" || r.ingestionJob.status === "FAILED", times: 36, }),);GetKnowledgeBaseDocuments
Section titled “GetKnowledgeBaseDocuments”Source:
src/AWS/Bedrock/GetKnowledgeBaseDocuments.ts
Runtime binding for bedrock-agent:GetKnowledgeBaseDocuments — read the
ingestion status of specific documents in the bound DataSource.
The binding grants the function bedrock:GetKnowledgeBaseDocuments
scoped to the data source’s parent knowledge base.
GetKnowledgeBaseDocuments: Direct Document Ingestion
Section titled “GetKnowledgeBaseDocuments: Direct Document Ingestion”// initconst getDocuments = yield* Bedrock.GetKnowledgeBaseDocuments(dataSource);
// runtimeconst { documentDetails } = yield* getDocuments({ documentIdentifiers: [ { dataSourceType: "CUSTOM", custom: { id: "welcome-doc" } }, ],});const status = documentDetails?.[0]?.status; // e.g. "INDEXED"IngestKnowledgeBaseDocuments
Section titled “IngestKnowledgeBaseDocuments”Source:
src/AWS/Bedrock/IngestKnowledgeBaseDocuments.ts
Runtime binding for bedrock-agent:IngestKnowledgeBaseDocuments — ingest
documents directly into the bound DataSource’s knowledge base
(inline text or S3 references) without running a full ingestion job.
The data source must be of type CUSTOM for inline content.
The binding grants the function bedrock:IngestKnowledgeBaseDocuments
scoped to the data source’s parent knowledge base.
IngestKnowledgeBaseDocuments: Direct Document Ingestion
Section titled “IngestKnowledgeBaseDocuments: Direct Document Ingestion”// initconst ingestDocuments = yield* Bedrock.IngestKnowledgeBaseDocuments(dataSource);
// runtimeconst { documentDetails } = yield* ingestDocuments({ documents: [ { content: { dataSourceType: "CUSTOM", custom: { customDocumentIdentifier: { id: "welcome-doc" }, sourceType: "IN_LINE", inlineContent: { type: "TEXT", textContent: { data: "Alchemy is an IaE framework." }, }, }, }, }, ],});InvokeAgent
Section titled “InvokeAgent”Source:
src/AWS/Bedrock/InvokeAgent.ts
Runtime binding for bedrock-agent-runtime:InvokeAgent — send user input
to a Bedrock agent through one of its aliases and receive the agent’s
response as an event stream.
Bind an AgentAlias inside a function runtime to get a callable
that invokes the agent. The binding grants the function
bedrock:InvokeAgent scoped to exactly that alias. The response’s
completion is an event Stream of chunks (and traces when
enableTrace is set); concatenate the chunk bytes to recover the answer.
InvokeAgent: Invoking an Agent
Section titled “InvokeAgent: Invoking an Agent”Invoke and Aggregate the Completion
// initconst invokeAgent = yield* Bedrock.InvokeAgent(alias);
// runtimeconst result = yield* invokeAgent({ sessionId: crypto.randomUUID(), inputText: "What is the capital of France?",});const events = yield* Stream.runCollect(result.completion);const decoder = new TextDecoder();const answer = events .map((event) => event.chunk?.bytes !== undefined ? decoder.decode( Redacted.isRedacted(event.chunk.bytes) ? Redacted.value(event.chunk.bytes) : event.chunk.bytes, ) : "", ) .join("");Continue a Session
// Reuse the same sessionId across calls to keep conversational context.const followUp = yield* invokeAgent({ sessionId, inputText: "And its population?",});InvokeModel
Section titled “InvokeModel”Source:
src/AWS/Bedrock/InvokeModel.ts
Runtime binding for bedrock-runtime:InvokeModel — run inference with a
model-specific request body (text, image, or embedding models).
Bind one or more model references inside a function runtime to get a
callable that invokes the model with a raw payload. The binding grants the
function bedrock:InvokeModel scoped to exactly the bound models. A model
reference may be a foundation-model id, a cross-region inference profile
id (e.g. us.amazon.nova-micro-v1:0), or a full Bedrock ARN.
Prefer Converse for conversational models — it is model-agnostic.
InvokeModel is for model-native payloads (embeddings, image generation,
or provider-specific request features).
Model access is an account entitlement — enable the model in the Bedrock
console (Model access) before invoking, otherwise calls fail with
AccessDeniedException.
InvokeModel: Invoking a Model
Section titled “InvokeModel: Invoking a Model”// initconst invokeModel = yield* Bedrock.InvokeModel("us.amazon.nova-micro-v1:0");
// runtime — body is the raw Nova messages-v1 payloadconst result = yield* invokeModel({ contentType: "application/json", accept: "application/json", body: JSON.stringify({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], inferenceConfig: { maxTokens: 64 }, }),});// result.body is a byte Stream of the JSON responseconst json = JSON.parse( yield* Stream.mkString(Stream.decodeText(result.body)),);InvokeModelWithResponseStream
Section titled “InvokeModelWithResponseStream”Source:
src/AWS/Bedrock/InvokeModelWithResponseStream.ts
Runtime binding for bedrock-runtime:InvokeModelWithResponseStream — the
streaming variant of InvokeModel. The response arrives as an event
Stream of chunk events carrying model-specific payload bytes.
The binding grants the function bedrock:InvokeModelWithResponseStream
(the IAM action streaming operations authorize against) scoped to exactly
the bound models. A model reference may be a foundation-model id, a
cross-region inference profile id (e.g. us.amazon.nova-micro-v1:0), or a
full Bedrock ARN.
Prefer ConverseStream for conversational models — it is
model-agnostic and its events are typed.
Model access is an account entitlement — enable the model in the Bedrock
console (Model access) before invoking, otherwise calls fail with
AccessDeniedException.
InvokeModelWithResponseStream: Streaming a Model Response
Section titled “InvokeModelWithResponseStream: Streaming a Model Response”// initconst invokeModelStream = yield* Bedrock.InvokeModelWithResponseStream( "us.amazon.nova-micro-v1:0",);
// runtime — body is the raw Nova messages-v1 payloadconst result = yield* invokeModelStream({ contentType: "application/json", body: JSON.stringify({ messages: [{ role: "user", content: [{ text: "Say hello." }] }], inferenceConfig: { maxTokens: 64 }, }),});const events = yield* Stream.runCollect(result.body);// each chunk's bytes is a model-specific JSON eventKnowledgeBase
Section titled “KnowledgeBase”Source:
src/AWS/Bedrock/KnowledgeBase.ts
An Amazon Bedrock knowledge base — a managed RAG index that embeds source documents into a vector store for retrieval.
KnowledgeBase owns the index configuration; attach one or more
DataSources (e.g. an S3 bucket) to feed it documents, then trigger
ingestion. Query it at runtime with the Retrieve and
RetrieveAndGenerate bindings, or attach it to an Agent.
The roleArn must grant Bedrock access to the embedding model, the vector
store, and the source data. The vector store (storageConfiguration) must
already exist — provision an OpenSearch Serverless collection (with a
vector index) or another supported store first.
KnowledgeBase: Creating Knowledge Bases
Section titled “KnowledgeBase: Creating Knowledge Bases”import * as Bedrock from "alchemy/AWS/Bedrock";
const kb = yield* Bedrock.KnowledgeBase("docs", { roleArn: role.roleArn, knowledgeBaseConfiguration: { type: "VECTOR", vectorKnowledgeBaseConfiguration: { embeddingModelArn: "arn:aws:bedrock:us-west-2::foundation-model/amazon.titan-embed-text-v2:0", }, }, storageConfiguration: { type: "OPENSEARCH_SERVERLESS", opensearchServerlessConfiguration: { collectionArn: collection.arn, vectorIndexName: "bedrock-index", fieldMapping: { vectorField: "bedrock-vector", textField: "bedrock-text", metadataField: "bedrock-metadata", }, }, },});LanguageModel
Section titled “LanguageModel”Source:
src/AWS/Bedrock/LanguageModel.ts
Runtime binding that turns an Amazon Bedrock model into an
effect/unstable/ai AiLanguageModel.LanguageModel Layer, so any
Effect AI program (LanguageModel.generateText, streamText, Chat,
toolkits, …) runs against Bedrock without code changes.
Calls are translated to the Bedrock Converse API — Bedrock’s unified
messages API that works across all conversational foundation models
(Amazon Nova, Anthropic Claude, Meta Llama, Mistral, …) — so one binding
covers every model. Bind one model or a list of models: the function is
granted bedrock:InvokeModel and bedrock:InvokeModelWithResponseStream
scoped to exactly those models, the first is the default, and runtime code
picks between them (and tunes inference parameters) per call with
withModelParameters. A model reference may be a foundation-model
id, a cross-region inference profile id (e.g. us.amazon.nova-micro-v1:0),
or a full Bedrock ARN.
Model access is an account entitlement — enable the model in the Bedrock
console (Model access) before invoking, otherwise calls fail with
AccessDeniedException. Many newer models are only invocable through a
cross-region inference profile id, not their bare foundation-model id.
LanguageModel: Effect AI on Bedrock
Section titled “LanguageModel: Effect AI on Bedrock”Generate Text
import { LanguageModel } from "effect/unstable/ai";
// init: bind the model and get a LanguageModel Layerconst model = yield* Bedrock.LanguageModel("us.amazon.nova-micro-v1:0", { parameters: { maxTokens: 1024, temperature: 0.7 },});
// runtime: any Effect AI program works against Bedrockconst response = yield* LanguageModel.generateText({ prompt: "Say hello.",}).pipe(Effect.provide(model));Stream Text
const parts = LanguageModel.streamText({ prompt }).pipe( Stream.provide(model),);// parts is a Stream of text-start / text-delta / ... / finish partsLanguageModel: Runtime Configuration
Section titled “LanguageModel: Runtime Configuration”Override Parameters Per Call
The binding’s parameters are only defaults — scope overrides onto any
call with withModelParameters.
const response = yield* LanguageModel.generateText({ prompt }).pipe( Bedrock.withModelParameters({ temperature: 0, maxTokens: 64 }),);Bind Multiple Models and Pick Per Call
IAM access is fixed at deploy time (scoped to the bound list); which of those models serves a given request is a runtime decision.
// init: one Layer, IAM for both models, Nova Micro is the defaultconst model = yield* Bedrock.LanguageModel([ "us.amazon.nova-micro-v1:0", "us.anthropic.claude-sonnet-4-20250514-v1:0",]);
// runtime: route this call to Claudeconst response = yield* LanguageModel.generateText({ prompt }).pipe( Bedrock.withModelParameters({ modelId: "us.anthropic.claude-sonnet-4-20250514-v1:0", }),);LanguageModel: Tool Calling
Section titled “LanguageModel: Tool Calling”import { Tool, Toolkit } from "effect/unstable/ai";import * as Schema from "effect/Schema";
const GetWeather = Tool.make("get_weather", { description: "Get the current weather for a city.", parameters: Schema.Struct({ city: Schema.String }), success: Schema.Struct({ temperatureF: Schema.Number }),});const WeatherToolkit = Toolkit.make(GetWeather);
const response = yield* LanguageModel.generateText({ prompt: "What's the weather in Seattle?", toolkit: WeatherToolkit,}).pipe( Effect.provide(WeatherToolkit.toLayer({ get_weather: ({ city }) => Effect.succeed({ temperatureF: 72 }), })), Effect.provide(model),);ListIngestionJobs
Section titled “ListIngestionJobs”Source:
src/AWS/Bedrock/ListIngestionJobs.ts
Runtime binding for bedrock-agent:ListIngestionJobs — list the ingestion
jobs that have run against the bound DataSource, optionally
filtered and sorted.
The binding grants the function bedrock:ListIngestionJobs scoped to the
data source’s parent knowledge base.
ListIngestionJobs: Syncing a Data Source
Section titled “ListIngestionJobs: Syncing a Data Source”// initconst listIngestionJobs = yield* Bedrock.ListIngestionJobs(dataSource);
// runtimeconst { ingestionJobSummaries } = yield* listIngestionJobs({ sortBy: { attribute: "STARTED_AT", order: "DESCENDING" }, maxResults: 10,});ListKnowledgeBaseDocuments
Section titled “ListKnowledgeBaseDocuments”Source:
src/AWS/Bedrock/ListKnowledgeBaseDocuments.ts
Runtime binding for bedrock-agent:ListKnowledgeBaseDocuments — list the
documents tracked in the bound DataSource together with their
ingestion status.
The binding grants the function bedrock:ListKnowledgeBaseDocuments
scoped to the data source’s parent knowledge base.
ListKnowledgeBaseDocuments: Direct Document Ingestion
Section titled “ListKnowledgeBaseDocuments: Direct Document Ingestion”// initconst listDocuments = yield* Bedrock.ListKnowledgeBaseDocuments(dataSource);
// runtimeconst { documentDetails } = yield* listDocuments({ maxResults: 25 });Rerank
Section titled “Rerank”Source:
src/AWS/Bedrock/Rerank.ts
Runtime binding for bedrock-agent-runtime:Rerank — re-order a list of
candidate documents by semantic relevance to a query using a Bedrock
reranker model (e.g. amazon.rerank-v1:0, cohere.rerank-v3-5:0).
Bind one or more reranker model references inside a function runtime. The
binding grants the function bedrock:Rerank (which AWS authorizes only
against *) plus bedrock:InvokeModel scoped to exactly the bound
models.
Rerank: Reranking Documents
Section titled “Rerank: Reranking Documents”// initconst rerank = yield* Bedrock.Rerank("amazon.rerank-v1:0");
// runtimeconst result = yield* rerank({ queries: [{ type: "TEXT", textQuery: { text: "What is Alchemy?" } }], sources: docs.map((text) => ({ type: "INLINE", inlineDocumentSource: { type: "TEXT", textDocument: { text } }, })), rerankingConfiguration: { type: "BEDROCK_RERANKING_MODEL", bedrockRerankingConfiguration: { modelConfiguration: { modelArn: `arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0`, }, }, },});const best = result.results[0]; // { index, relevanceScore }Retrieve
Section titled “Retrieve”Source:
src/AWS/Bedrock/Retrieve.ts
Runtime binding for bedrock-agent-runtime:Retrieve — query a
KnowledgeBase for the passages most relevant to a natural-language
query, without generating an answer.
Bind a knowledge base inside a function runtime to get a callable that runs
semantic retrieval. The binding grants the function bedrock:Retrieve
scoped to exactly that knowledge base. Use this when you want the raw
retrieved chunks (to build your own prompt); use RetrieveAndGenerate
for a fully managed RAG answer.
Retrieve: Retrieving Passages
Section titled “Retrieve: Retrieving Passages”// initconst retrieve = yield* Bedrock.Retrieve(knowledgeBase);
// runtimeconst result = yield* retrieve({ retrievalQuery: { text: "How do I rotate credentials?" }, retrievalConfiguration: { vectorSearchConfiguration: { numberOfResults: 5 }, },});const passages = result.retrievalResults.map((r) => r.content?.text);RetrieveAndGenerate
Section titled “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).
RetrieveAndGenerate: Retrieving and Generating
Section titled “RetrieveAndGenerate: Retrieving and Generating”// initconst rag = yield* Bedrock.RetrieveAndGenerate( knowledgeBase, "us.anthropic.claude-3-5-sonnet-20240620-v1:0",);
// runtimeconst 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;RetrieveAndGenerateStream
Section titled “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).
RetrieveAndGenerateStream: Streaming a Grounded Answer
Section titled “RetrieveAndGenerateStream: Streaming a Grounded Answer”// initconst ragStream = yield* Bedrock.RetrieveAndGenerateStream( knowledgeBase, "us.amazon.nova-micro-v1:0",);
// runtimeconst 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("");StartIngestionJob
Section titled “StartIngestionJob”Source:
src/AWS/Bedrock/StartIngestionJob.ts
Runtime binding for bedrock-agent:StartIngestionJob — kick off an
ingestion (sync) job that reads the bound DataSource’s content
and indexes it into its knowledge base.
The binding grants the function bedrock:StartIngestionJob scoped to the
data source’s parent knowledge base. Poll the returned job with
GetIngestionJob until its status settles.
StartIngestionJob: Syncing a Data Source
Section titled “StartIngestionJob: Syncing a Data Source”// initconst startIngestionJob = yield* Bedrock.StartIngestionJob(dataSource);
// runtimeconst { ingestionJob } = yield* startIngestionJob({ description: "nightly refresh",});const jobId = ingestionJob.ingestionJobId;StopIngestionJob
Section titled “StopIngestionJob”Source:
src/AWS/Bedrock/StopIngestionJob.ts
Runtime binding for bedrock-agent:StopIngestionJob — stop an in-flight
ingestion job on the bound DataSource.
The binding grants the function bedrock:StopIngestionJob scoped to the
data source’s parent knowledge base.
StopIngestionJob: Syncing a Data Source
Section titled “StopIngestionJob: Syncing a Data Source”// initconst stopIngestionJob = yield* Bedrock.StopIngestionJob(dataSource);
// runtimeconst { ingestionJob } = yield* stopIngestionJob({ ingestionJobId: jobId,});