| 1 | import type * as bedrock from "@distilled.cloud/aws/bedrock-agent-runtime"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { KnowledgeBase } from "./KnowledgeBase.ts"; |
| 5 | |
| 6 | /** |
| 7 | * The `Retrieve` request with the binding-injected `knowledgeBaseId` removed — |
| 8 | * it is supplied automatically from the bound {@link KnowledgeBase}. |
| 9 | */ |
| 10 | export interface RetrieveRequest extends Omit< |
| 11 | bedrock.RetrieveRequest, |
| 12 | "knowledgeBaseId" |
| 13 | > {} |
| 14 | |
| 15 | /** |
| 16 | * Runtime binding for `bedrock-agent-runtime:Retrieve` — query a |
| 17 | * {@link KnowledgeBase} for the passages most relevant to a natural-language |
| 18 | * query, without generating an answer. |
| 19 | * |
| 20 | * Bind a knowledge base inside a function runtime to get a callable that runs |
| 21 | * semantic retrieval. The binding grants the function `bedrock:Retrieve` |
| 22 | * scoped to exactly that knowledge base. Use this when you want the raw |
| 23 | * retrieved chunks (to build your own prompt); use {@link RetrieveAndGenerate} |
| 24 | * for a fully managed RAG answer. |
| 25 | * |
| 26 | * ### Retrieving Passages |
| 27 | * **Example:** Retrieve Relevant Chunks |
| 28 | * ```typescript |
| 29 | * // init |
| 30 | * const retrieve = yield* Bedrock.Retrieve(knowledgeBase); |
| 31 | * |
| 32 | * // runtime |
| 33 | * const result = yield* retrieve({ |
| 34 | * retrievalQuery: { text: "How do I rotate credentials?" }, |
| 35 | * retrievalConfiguration: { |
| 36 | * vectorSearchConfiguration: { numberOfResults: 5 }, |
| 37 | * }, |
| 38 | * }); |
| 39 | * const passages = result.retrievalResults.map((r) => r.content?.text); |
| 40 | * ``` |
| 41 | * |
| 42 | * @binding |
| 43 | */ |
| 44 | export interface Retrieve extends Binding.Service< |
| 45 | Retrieve, |
| 46 | "AWS.Bedrock.Retrieve", |
| 47 | <K extends KnowledgeBase>( |
| 48 | knowledgeBase: K, |
| 49 | ) => Effect.Effect< |
| 50 | ( |
| 51 | request: RetrieveRequest, |
| 52 | ) => Effect.Effect<bedrock.RetrieveResponse, bedrock.RetrieveError> |
| 53 | > |
| 54 | > {} |
| 55 | export const Retrieve = Binding.Service<Retrieve>("AWS.Bedrock.Retrieve"); |