| 1 | import type * as bedrock from "@distilled.cloud/aws/bedrock-runtime"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | |
| 5 | /** |
| 6 | * The `InvokeModel` request with the binding-injected `modelId` made |
| 7 | * optional: it defaults to the first bound model id and may be overridden |
| 8 | * per call with any of the bound model ids. |
| 9 | * |
| 10 | * `body` is the raw, model-specific request payload (e.g. the Anthropic |
| 11 | * Messages API shape for Claude models, the Nova messages-v1 shape for |
| 12 | * Amazon Nova) — there is no auto-marshalling. The response `body` is a |
| 13 | * byte `Stream` of the model-specific JSON response. |
| 14 | */ |
| 15 | export interface InvokeModelRequest extends Omit< |
| 16 | bedrock.InvokeModelRequest, |
| 17 | "modelId" |
| 18 | > { |
| 19 | /** |
| 20 | * The model to run inference on for this call. Must be one of the model |
| 21 | * ids the binding was created with (IAM is scoped to exactly those). |
| 22 | * @default the first bound model id |
| 23 | */ |
| 24 | modelId?: string; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Runtime binding for `bedrock-runtime:InvokeModel` — run inference with a |
| 29 | * model-specific request body (text, image, or embedding models). |
| 30 | * |
| 31 | * Bind one or more model references inside a function runtime to get a |
| 32 | * callable that invokes the model with a raw payload. The binding grants the |
| 33 | * function `bedrock:InvokeModel` scoped to exactly the bound models. A model |
| 34 | * reference may be a foundation-model id, a cross-region inference profile |
| 35 | * id (e.g. `us.amazon.nova-micro-v1:0`), or a full Bedrock ARN. |
| 36 | * |
| 37 | * Prefer {@link Converse} for conversational models — it is model-agnostic. |
| 38 | * `InvokeModel` is for model-native payloads (embeddings, image generation, |
| 39 | * or provider-specific request features). |
| 40 | * |
| 41 | * Model access is an account entitlement — enable the model in the Bedrock |
| 42 | * console (Model access) before invoking, otherwise calls fail with |
| 43 | * `AccessDeniedException`. |
| 44 | * |
| 45 | * ### Invoking a Model |
| 46 | * **Example:** Invoke with a Model-Native Payload |
| 47 | * ```typescript |
| 48 | * // init |
| 49 | * const invokeModel = yield* Bedrock.InvokeModel("us.amazon.nova-micro-v1:0"); |
| 50 | * |
| 51 | * // runtime — body is the raw Nova messages-v1 payload |
| 52 | * const result = yield* invokeModel({ |
| 53 | * contentType: "application/json", |
| 54 | * accept: "application/json", |
| 55 | * body: JSON.stringify({ |
| 56 | * messages: [{ role: "user", content: [{ text: "Say hello." }] }], |
| 57 | * inferenceConfig: { maxTokens: 64 }, |
| 58 | * }), |
| 59 | * }); |
| 60 | * // result.body is a byte Stream of the JSON response |
| 61 | * const json = JSON.parse( |
| 62 | * yield* Stream.mkString(Stream.decodeText(result.body)), |
| 63 | * ); |
| 64 | * ``` |
| 65 | * |
| 66 | * @binding |
| 67 | */ |
| 68 | export interface InvokeModel extends Binding.Service< |
| 69 | InvokeModel, |
| 70 | "AWS.Bedrock.InvokeModel", |
| 71 | ( |
| 72 | model: string, |
| 73 | ...additionalModels: string[] |
| 74 | ) => Effect.Effect< |
| 75 | ( |
| 76 | request: InvokeModelRequest, |
| 77 | ) => Effect.Effect<bedrock.InvokeModelResponse, bedrock.InvokeModelError> |
| 78 | > |
| 79 | > {} |
| 80 | export const InvokeModel = Binding.Service<InvokeModel>( |
| 81 | "AWS.Bedrock.InvokeModel", |
| 82 | ); |