35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
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 : "";
|
|
}
|
|
}
|