feat: add Anthropic LLM provider
This commit is contained in:
34
packages/llm/src/providers/anthropic.ts
Normal file
34
packages/llm/src/providers/anthropic.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import Anthropic from "@anthropic-ai/sdk";
|
||||||
|
import type { LLMMessage, LLMOptions } from "@codeboard/shared";
|
||||||
|
import type { LLMProvider } from "./base.js";
|
||||||
|
|
||||||
|
export class AnthropicProvider implements LLMProvider {
|
||||||
|
name = "anthropic";
|
||||||
|
private client: Anthropic;
|
||||||
|
private defaultModel: string;
|
||||||
|
|
||||||
|
constructor(apiKey: string, model?: string) {
|
||||||
|
this.client = new Anthropic({ apiKey });
|
||||||
|
this.defaultModel = model ?? "claude-sonnet-4-20250514";
|
||||||
|
}
|
||||||
|
|
||||||
|
async chat(messages: LLMMessage[], options?: LLMOptions): Promise<string> {
|
||||||
|
const systemMessage = messages.find((m) => m.role === "system");
|
||||||
|
const nonSystemMessages = messages
|
||||||
|
.filter((m) => m.role !== "system")
|
||||||
|
.map((m) => ({
|
||||||
|
role: m.role as "user" | "assistant",
|
||||||
|
content: m.content,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const response = await this.client.messages.create({
|
||||||
|
model: options?.model ?? this.defaultModel,
|
||||||
|
max_tokens: options?.maxTokens ?? 4096,
|
||||||
|
system: systemMessage?.content,
|
||||||
|
messages: nonSystemMessages,
|
||||||
|
});
|
||||||
|
|
||||||
|
const textBlock = response.content.find((b) => b.type === "text");
|
||||||
|
return textBlock?.type === "text" ? textBlock.text : "";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user