UNPKG

@llumiverse/drivers

Version:

LLM driver implementations. Currently supported are: openai, huggingface, bedrock, replicate.

138 lines 7.12 kB
import { Bedrock, type FoundationModelSummary } from "@aws-sdk/client-bedrock"; import { BedrockRuntime, type ConverseRequest, type ConverseResponse, type ConverseStreamOutput, type Message } from "@aws-sdk/client-bedrock-runtime"; import type { AwsCredentialIdentity, Provider } from "@aws-sdk/types"; import { AbstractDriver, type AIModel, type Completion, type CompletionChunkObject, type DataSource, type DriverOptions, type EmbeddingsOptions, type EmbeddingsResult, type ExecutionOptions, LlumiverseError, type LlumiverseErrorContext, type PromptSegment, type TrainingJob, type TrainingOptions } from "@llumiverse/core"; import { type NovaMessagesPrompt } from "@llumiverse/core/formatters"; import { type TwelvelabsPegasusRequest } from "./twelvelabs.js"; export interface BedrockModelCapabilities { name: string; canStream: boolean; } export interface BedrockDriverOptions extends DriverOptions { /** * The AWS region */ region: string; /** * The bucket name to be used for training. * It will be created if does not already exist. */ training_bucket?: string; /** * The role ARN to be used for training */ training_role_arn?: string; /** * The credentials to use to access AWS (IAM access key + secret) */ credentials?: AwsCredentialIdentity | Provider<AwsCredentialIdentity>; } export type BedrockPrompt = NovaMessagesPrompt | ConverseRequest | TwelvelabsPegasusRequest; export declare class BedrockDriver extends AbstractDriver<BedrockDriverOptions, BedrockPrompt> { static PROVIDER: string; provider: string; private _executor?; private _service?; private _service_region?; constructor(options: BedrockDriverOptions); getExecutor(): BedrockRuntime; getService(region?: string): Bedrock; protected formatPrompt(segments: PromptSegment[], opts: ExecutionOptions): Promise<BedrockPrompt>; /** * Format AWS Bedrock errors into LlumiverseError with proper status codes and retryability. * * AWS SDK errors provide: * - error.name: The exception type (e.g., "ThrottlingException") * - error.$metadata.httpStatusCode: The HTTP status code * - error.$metadata.requestId: The AWS request ID for tracking * - error.$fault: "client" or "server" indicating error category * * @param error - The AWS SDK error * @param context - Context about where the error occurred * @returns A standardized LlumiverseError */ formatLlumiverseError(error: unknown, context: LlumiverseErrorContext): LlumiverseError; /** * Determine if a Bedrock error is retryable based on error type and status. * * Retryable errors: * - ThrottlingException: Rate limit exceeded, retry with backoff * - ServiceUnavailableException: Service temporarily down * - InternalServerException: Server-side error * - ServiceQuotaExceededException: Quota exhausted, may recover * - 5xx status codes: Server errors * - 429, 408 status codes: Rate limit, timeout * * Non-retryable errors: * - ValidationException: Invalid request parameters * - AccessDeniedException: Authentication/authorization failure * - ResourceNotFoundException: Resource doesn't exist * - ConflictException: Resource state conflict * - ResourceInUseException: Resource locked by another operation * - 4xx status codes (except 429, 408): Client errors * * @param errorName - The AWS error name (e.g., "ThrottlingException") * @param httpStatusCode - The HTTP status code if available * @param fault - The fault type ("client" or "server") * @returns True if retryable, false if not retryable, undefined if unknown */ private isBedrockErrorRetryable; getExtractedExecution(result: ConverseResponse, _prompt?: BedrockPrompt, options?: ExecutionOptions): CompletionChunkObject; getExtractedStream(result: ConverseStreamOutput, _prompt?: BedrockPrompt, options?: ExecutionOptions, streamingToolBlocks?: Map<number, { id: string; name: string; }>): CompletionChunkObject; extractRegion(modelString: string, defaultRegion: string): string; private getCanStream; protected canStream(options: ExecutionOptions): Promise<boolean>; /** * Build conversation context after streaming completion. * Reconstructs the assistant message from accumulated results and applies stripping. */ buildStreamingConversation(prompt: BedrockPrompt, result: unknown[], toolUse: unknown[] | undefined, options: ExecutionOptions): ConverseRequest | undefined; requestTextCompletion(prompt: BedrockPrompt, options: ExecutionOptions): Promise<Completion>; private requestTwelvelabsPegasusCompletion; private requestTwelvelabsPegasusCompletionStream; requestTextCompletionStream(prompt: BedrockPrompt, options: ExecutionOptions): Promise<AsyncIterable<CompletionChunkObject>>; preparePayload(prompt: ConverseRequest, options: ExecutionOptions): ConverseRequest; protected isImageModel(model: string): boolean; requestImageGeneration(prompt: NovaMessagesPrompt, options: ExecutionOptions): Promise<Completion>; startTraining(dataset: DataSource, options: TrainingOptions): Promise<TrainingJob>; cancelTraining(jobId: string): Promise<TrainingJob>; getTrainingJob(jobId: string): Promise<TrainingJob>; validateConnection(): Promise<boolean>; listTrainableModels(): Promise<AIModel<string>[]>; listModels(): Promise<AIModel[]>; _listModels(foundationFilter?: (m: FoundationModelSummary) => boolean): Promise<AIModel[]>; generateEmbeddings({ text, image, model }: EmbeddingsOptions): Promise<EmbeddingsResult>; private generateTwelvelabsMarengoEmbeddings; /** * Cleanup AWS SDK clients when the driver is evicted from the cache. */ destroy(): void; } /** * Checks whether any message contains toolUse or toolResult content blocks. */ export declare function messagesContainToolBlocks(messages: Message[]): boolean; /** * Converts toolUse and toolResult content blocks to text representations. * This preserves the tool call information in the conversation while removing * the structured tool blocks that require Bedrock's toolConfig to be set. * * Used when no tools are provided (e.g. checkpoint summary calls) but the * conversation history contains tool interactions from prior turns. */ export declare function convertToolBlocksToText(messages: Message[]): Message[]; /** * Fix orphaned toolUse blocks in the conversation. * * When an agent is stopped mid-tool-execution, the assistant message contains toolUse blocks * but no corresponding toolResult was added. The AWS Converse API requires that every toolUse * must be followed by a toolResult in the next user message. * * This function detects such cases and injects synthetic toolResult blocks indicating * the tools were interrupted, allowing the conversation to continue. */ export declare function fixOrphanedToolUse(messages: Message[]): Message[]; //# sourceMappingURL=index.d.ts.map