UNPKG

jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

239 lines (238 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnthropicProvider = void 0; const sdk_1 = require("@anthropic-ai/sdk"); const bedrock_sdk_1 = require("@anthropic-ai/bedrock-sdk"); const providers_1 = require("../../providers"); const convert_llm_message_1 = require("./convert-llm-message"); const shared_1 = require("../../shared"); const tools_1 = require("../../tools"); /** Provides access to OpenAI and other compatible services */ class AnthropicProvider { constructor({ apiKey, bedrock, name } = {}) { this.name = name || "anthropic"; if (bedrock) { const region = bedrock.awsRegion || process.env.AWS_REGION; const accessKeyId = bedrock.awsAccessKey || process.env.AWS_ACCESS_KEY_ID; const secretAccessKey = bedrock.awsSecretKey || process.env.AWS_SECRET_ACCESS_KEY; if (!region) throw new Error("[AnthropicProvider]: Missing AWS region. Either pass it as config.region or set the AWS_REGION environment variable"); if (!accessKeyId) throw new Error("[AnthropicProvider]: Missing AWS access key id. Either pass it as config.accessKeyId or set the AWS_ACCESS_KEY_ID environment variable"); if (!secretAccessKey) throw new Error("[AnthropicProvider]: Missing AWS secret access key. Either pass it as config.secretAccessKey or set the AWS_SECRET_ACCESS_KEY environment variable"); this.client = new bedrock_sdk_1.AnthropicBedrock({ awsRegion: region, awsAccessKey: accessKeyId, awsSecretKey: secretAccessKey, }); } else { const _apiKey = apiKey || process.env.ANTHROPIC_API_KEY; if (!_apiKey) throw new Error("[AnthropicProvider]: Missing API key. Either pass it as config.apiKey or set the ANTHROPIC_API_KEY environment variable"); this.client = new sdk_1.default({ apiKey: _apiKey, }); } } async generateResponse(model, messages, config = {}) { const start = Date.now(); const { chatMessages, systemMessage } = await (0, convert_llm_message_1.convertLlmMessagesToAnthropicMessages)(messages); const temperature = config.temperature ?? undefined; const response = await this.client.messages.create({ model, messages: chatMessages, temperature, max_tokens: config.maxTokens || 4096, system: systemMessage, tool_choice: config.toolChoice === "none" || !config.tools || !config.tools.hasTools ? undefined : config.toolChoice === "any" ? { type: "auto", disable_parallel_tool_use: config.tools?.allowParallelCalls, } : config.toolChoice === "required" ? { type: "auto", disable_parallel_tool_use: config.tools?.allowParallelCalls, } : config.toolChoice ? { type: "tool", name: config.toolChoice, disable_parallel_tool_use: config.tools?.allowParallelCalls, } : undefined, tools: config.toolChoice === "none" ? undefined : config.tools?.asLlmFunctions?.map((tool) => ({ name: tool.function.name, input_schema: { ...tool.function.parameters?.properties, type: "object", }, description: tool.function.description, })), }); const durationMs = Date.now() - start; const inputTokens = response.usage.input_tokens; const outputTokens = response.usage.output_tokens; const content = response.content .map((c) => (c.type === "text" ? c.text : "")) .join("") .trim(); const toolCalls = response.content .filter((c) => c.type === "tool_use") .map((c) => ({ id: (0, shared_1.generateUniqueId)(), request: { id: c.id, function: { name: c.name, arguments: c.input && typeof c.input === "object" ? c.input : {}, }, }, approvalState: config.tools?.getTool(c.name)?.requiresConfirmation ? "requiresApproval" : "noApprovalRequired", executionState: "pending", result: null, error: null, })); const provider = this.name; return { ...(0, providers_1.generateAssistantMessage)(content, toolCalls), meta: { model, provider, temperature, durationMs, inputTokens, outputTokens, }, }; } async *generateResponseStream(model, messages, config = {}) { const start = Date.now(); const { chatMessages, systemMessage } = await (0, convert_llm_message_1.convertLlmMessagesToAnthropicMessages)(messages); const temperature = config.temperature ?? undefined; const responseStream = await this.client.messages.create({ model, messages: chatMessages, temperature, max_tokens: config.maxTokens || 4096, system: systemMessage, stream: true, tool_choice: config.toolChoice === "none" || !config.tools || !config.tools.hasTools ? undefined : config.toolChoice === "any" ? { type: "auto", disable_parallel_tool_use: config.tools?.allowParallelCalls, } : config.toolChoice === "required" ? { type: "auto", disable_parallel_tool_use: config.tools?.allowParallelCalls, } : config.toolChoice ? { type: "tool", name: config.toolChoice, disable_parallel_tool_use: config.tools?.allowParallelCalls, } : undefined, tools: config.toolChoice === "none" ? undefined : config.tools?.asLlmFunctions?.map((tool) => ({ name: tool.function.name, input_schema: { ...tool.function.parameters?.properties, type: "object", }, description: tool.function.description, })), }); let inputTokens = undefined; let outputTokens = undefined; let content = ""; const _toolCalls = {}; for await (const chunk of responseStream) { if (chunk.type === "content_block_delta") { if (chunk.delta.type === "text_delta") { content += chunk.delta.text; yield { type: "chunk", content: chunk.delta.text }; } } if (chunk.type === "message_start") { inputTokens = (inputTokens || 0) + chunk.message.usage.input_tokens; outputTokens = (outputTokens || 0) + chunk.message.usage.output_tokens; } if (chunk.type === "content_block_start") { if (chunk.content_block.type === "tool_use") { _toolCalls[chunk.index] = { ...chunk.content_block, arguments: "" }; } } if (chunk.type === "content_block_delta") { if (chunk.delta.type === "input_json_delta") { const index = chunk.index; const toolCall = _toolCalls[index]; toolCall.arguments += chunk.delta.partial_json; } } if (chunk.type === "message_delta") { outputTokens = (outputTokens || 0) + chunk.usage.output_tokens; } } const durationMs = Date.now() - start; const provider = this.name; const meta = { model, provider, temperature, durationMs, inputTokens, outputTokens, }; const toolCalls = Object.values(_toolCalls).map((c) => ({ id: (0, shared_1.generateUniqueId)(), request: { id: c.id, function: { name: c.name, arguments: tools_1.LlmToolKit.deserialize(c.arguments), }, }, approvalState: config.tools?.getTool(c.name)?.requiresConfirmation ? "requiresApproval" : "noApprovalRequired", executionState: "pending", result: null, error: null, })); if (toolCalls && toolCalls.length > 0) { yield { type: "response", role: "assistant_with_tools", content, toolCalls, meta, }; } else { yield { type: "response", role: "assistant", content, meta, }; } } async getAvailableModels() { const response = (await this.client.get("/v1/models")); return response.data.map((model) => model.id); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async createEmbedding(model, text) { throw new Error("Embeddings are not yet supported for Anthropic"); } } exports.AnthropicProvider = AnthropicProvider;