repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import Anthropic from "@anthropic-ai/sdk"; |
| #2 | import { LLM, LLMResponse } from "./base"; |
| #3 | import { LLMConfig, Message } from "../types"; |
| #4 | |
| #5 | export class AnthropicLLM implements LLM { |
| #6 | private client: Anthropic; |
| #7 | private model: string; |
| #8 | |
| #9 | constructor(config: LLMConfig) { |
| #10 | const apiKey = config.apiKey || process.env.ANTHROPIC_API_KEY; |
| #11 | if (!apiKey) { |
| #12 | throw new Error("Anthropic API key is required"); |
| #13 | } |
| #14 | this.client = new Anthropic({ apiKey }); |
| #15 | this.model = config.model || "claude-3-sonnet-20240229"; |
| #16 | } |
| #17 | |
| #18 | async generateResponse( |
| #19 | messages: Message[], |
| #20 | responseFormat?: { type: string }, |
| #21 | ): Promise<string> { |
| #22 | // Extract system message if present |
| #23 | const systemMessage = messages.find((msg) => msg.role === "system"); |
| #24 | const otherMessages = messages.filter((msg) => msg.role !== "system"); |
| #25 | |
| #26 | const response = await this.client.messages.create({ |
| #27 | model: this.model, |
| #28 | messages: otherMessages.map((msg) => ({ |
| #29 | role: msg.role as "user" | "assistant", |
| #30 | content: |
| #31 | typeof msg.content === "string" |
| #32 | ? msg.content |
| #33 | : msg.content.image_url.url, |
| #34 | })), |
| #35 | system: |
| #36 | typeof systemMessage?.content === "string" |
| #37 | ? systemMessage.content |
| #38 | : undefined, |
| #39 | max_tokens: 4096, |
| #40 | }); |
| #41 | |
| #42 | const firstBlock = response.content[0]; |
| #43 | if (firstBlock.type === "text") { |
| #44 | return firstBlock.text; |
| #45 | } else { |
| #46 | throw new Error("Unexpected response type from Anthropic API"); |
| #47 | } |
| #48 | } |
| #49 | |
| #50 | async generateChat(messages: Message[]): Promise<LLMResponse> { |
| #51 | const response = await this.generateResponse(messages); |
| #52 | return { |
| #53 | content: response, |
| #54 | role: "assistant", |
| #55 | }; |
| #56 | } |
| #57 | } |
| #58 |