UNPKG

@agenite/anthropic

Version:
190 lines (188 loc) 5.6 kB
import Anthropic from '@anthropic-ai/sdk'; import { BaseLLMProvider, convertStringToMessages, iterateFromMethods } from '@agenite/llm'; // src/provider.ts function mapStopReason(stopReason) { if (!stopReason) return void 0; const stopReasonMap = { end_turn: "endTurn", max_tokens: "maxTokens", stop_sequence: "stopSequence", tool_use: "toolUse" }; return stopReasonMap[stopReason] || void 0; } function mapContent(content) { return content.map((block) => { switch (block.type) { case "text": return { type: "text", text: block.text }; case "image": return { type: "image", source: block.source }; case "tool_use": return { type: "toolUse", id: block.id, name: block.name, input: block.input }; default: throw new Error( `Unsupported content block type: ${JSON.stringify(block, null, 2)}` ); } }); } function convertContentBlock(block) { if (!block) { return ""; } if (typeof block === "string") { return block; } switch (block.type) { case "text": return { type: "text", text: block.text }; case "image": return { type: "image", source: block.source.type === "base64" ? { type: "base64", data: block.source.data, media_type: block.source.media_type } : { type: "url", url: block.source.url } }; case "toolUse": return { type: "tool_use", id: block.id, name: block.name, input: block.input }; case "toolResult": return { type: "tool_result", tool_use_id: block.toolUseId, content: typeof block.content === "string" ? block.content : block.content?.map( (block2) => convertContentBlock(block2) ) || "", is_error: block.isError }; default: console.log("block", block); throw new Error( `Unsupported content block type: ${JSON.stringify(block, null, 2)}` ); } } function convertMessages(messages) { return messages.filter( (msg) => msg.role !== "system" ).map((msg) => ({ role: msg.role, content: msg.content.map((block) => { return convertContentBlock(block); }) })); } var AnthropicProvider = class extends BaseLLMProvider { client; model; name = "Claude"; version = "3"; constructor(config) { super(); this.client = new Anthropic({ apiKey: config.apiKey, baseURL: config.baseURL }); this.model = config.model ?? "claude-3-opus-20240229"; } async generate(input, options) { try { const messageArray = convertStringToMessages(input); const transformedMessages = convertMessages(messageArray); const response = await this.client.messages.create({ model: this.model, messages: transformedMessages, system: options?.systemPrompt, max_tokens: options?.maxTokens ?? 4096, temperature: options?.temperature, stop_sequences: options?.stopSequences }); return { content: mapContent(response.content), stopReason: mapStopReason(response.stop_reason), tokenUsage: { model: response.model, inputTokens: response.usage.input_tokens, outputTokens: response.usage.output_tokens, inputCost: 0, outputCost: 0 } }; } catch (error) { console.error("Anthropic generation failed:", error); throw error instanceof Error ? new Error(`Anthropic generation failed: ${error.message}`) : new Error("Anthropic generation failed with unknown error"); } } async *stream(input, options) { try { const messageArray = convertStringToMessages(input); const transformedMessages = convertMessages(messageArray); const messageStream = await this.client.messages.stream({ model: this.model, messages: transformedMessages, system: options?.systemPrompt, max_tokens: options?.maxTokens ?? 4096, temperature: options?.temperature, stop_sequences: options?.stopSequences }); let buffer = ""; for await (const chunk of messageStream) { if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") { buffer += chunk.delta.text; if (buffer.length > 10) { yield { type: "text", text: buffer }; buffer = ""; } } } if (buffer.length > 0) { yield { type: "text", text: buffer }; } const finalMessage = await messageStream.finalMessage(); return { content: mapContent(finalMessage.content), stopReason: mapStopReason(finalMessage.stop_reason), tokenUsage: { model: finalMessage.model, inputTokens: finalMessage.usage.input_tokens, outputTokens: finalMessage.usage.output_tokens, inputCost: 0, outputCost: 0 } }; } catch (error) { console.error("Anthropic generation failed:", error); throw error instanceof Error ? new Error(`Anthropic generation failed: ${error.message}`) : new Error("Anthropic generation failed with unknown error"); } } async *iterate(input, options) { return yield* iterateFromMethods(this, input, options); } }; export { AnthropicProvider }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map