2026-04-05 21:09:33 -05:00
|
|
|
/**
|
|
|
|
|
* Anthropic Claude text completion service.
|
|
|
|
|
*
|
|
|
|
|
* Python reference: trustgraph-flow/trustgraph/model/text_completion/claude/llm.py
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Anthropic from "@anthropic-ai/sdk";
|
2026-06-01 16:22:25 -05:00
|
|
|
import {
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
makeLlmService,
|
2026-06-01 16:22:25 -05:00
|
|
|
makeFlowProcessorProgram,
|
|
|
|
|
makeLlmServiceShape,
|
|
|
|
|
makeLlmSpecs,
|
2026-06-01 20:26:47 -05:00
|
|
|
type LlmProvider,
|
2026-06-01 16:22:25 -05:00
|
|
|
type ProcessorConfig,
|
|
|
|
|
type LlmResult,
|
|
|
|
|
type LlmChunk,
|
|
|
|
|
tooManyRequestsError,
|
|
|
|
|
} from "@trustgraph/base";
|
|
|
|
|
import { Effect, Layer } from "effect";
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export type ClaudeProcessorConfig = ProcessorConfig & {
|
|
|
|
|
model?: string;
|
|
|
|
|
apiKey?: string;
|
|
|
|
|
temperature?: number;
|
|
|
|
|
maxOutput?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function makeClaudeProvider(config: ClaudeProcessorConfig): LlmProvider {
|
|
|
|
|
const defaultModel = config.model ?? "claude-sonnet-4-20250514";
|
|
|
|
|
const defaultTemperature = config.temperature ?? 0.0;
|
|
|
|
|
const maxOutput = config.maxOutput ?? 8192;
|
2026-04-05 21:09:33 -05:00
|
|
|
const apiKey = config.apiKey ?? process.env.CLAUDE_KEY;
|
2026-05-12 08:06:58 -05:00
|
|
|
if (apiKey === undefined || apiKey.length === 0) {
|
|
|
|
|
throw new Error("Claude API key not specified");
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const client = new Anthropic({ apiKey });
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
console.log("[Claude] LLM service initialized");
|
2026-06-01 20:26:47 -05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
generateContent: async (
|
|
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
|
|
|
|
): Promise<LlmResult> => {
|
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await client.messages.create({
|
|
|
|
|
model: modelName,
|
|
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
temperature: temp,
|
|
|
|
|
system,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const text = response.content[0].type === "text"
|
|
|
|
|
? response.content[0].text
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
text,
|
|
|
|
|
inToken: response.usage.input_tokens,
|
|
|
|
|
outToken: response.usage.output_tokens,
|
|
|
|
|
model: modelName,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof Anthropic.RateLimitError) {
|
|
|
|
|
throw tooManyRequestsError();
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
throw err;
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
supportsStreaming: () => true,
|
|
|
|
|
generateContentStream: async function* (
|
|
|
|
|
system: string,
|
|
|
|
|
prompt: string,
|
|
|
|
|
model?: string,
|
|
|
|
|
temperature?: number,
|
|
|
|
|
): AsyncGenerator<LlmChunk> {
|
|
|
|
|
const modelName = model ?? defaultModel;
|
|
|
|
|
const temp = temperature ?? defaultTemperature;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const stream = client.messages.stream({
|
|
|
|
|
model: modelName,
|
|
|
|
|
max_tokens: maxOutput,
|
|
|
|
|
temperature: temp,
|
|
|
|
|
system,
|
|
|
|
|
messages: [
|
|
|
|
|
{ role: "user", content: prompt },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for await (const event of stream) {
|
|
|
|
|
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
|
|
|
yield {
|
|
|
|
|
text: event.delta.text,
|
|
|
|
|
inToken: null,
|
|
|
|
|
outToken: null,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
const finalMessage = await stream.finalMessage();
|
|
|
|
|
yield {
|
|
|
|
|
text: "",
|
|
|
|
|
inToken: finalMessage.usage.input_tokens,
|
|
|
|
|
outToken: finalMessage.usage.output_tokens,
|
|
|
|
|
model: modelName,
|
|
|
|
|
isFinal: true,
|
|
|
|
|
};
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof Anthropic.RateLimitError) {
|
|
|
|
|
throw tooManyRequestsError();
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
2026-06-01 20:26:47 -05:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ClaudeProcessor = ReturnType<typeof makeClaudeProcessor>;
|
|
|
|
|
|
|
|
|
|
export function makeClaudeProcessor(config: ClaudeProcessorConfig): ReturnType<typeof makeLlmService> {
|
|
|
|
|
return makeLlmService(config, makeClaudeProvider(config));
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:26:47 -05:00
|
|
|
export const ClaudeProcessor = makeClaudeProcessor;
|
|
|
|
|
|
2026-06-01 16:22:25 -05:00
|
|
|
export const program = makeFlowProcessorProgram<ProcessorConfig, never, Llm>({
|
2026-05-12 08:06:58 -05:00
|
|
|
id: "text-completion",
|
2026-06-01 16:22:25 -05:00
|
|
|
specs: () => makeLlmSpecs(),
|
|
|
|
|
layer: (config) =>
|
|
|
|
|
Layer.succeed(
|
|
|
|
|
Llm,
|
2026-06-01 20:26:47 -05:00
|
|
|
Llm.of(makeLlmServiceShape(makeClaudeProvider(config))),
|
2026-06-01 16:22:25 -05:00
|
|
|
),
|
2026-05-12 08:06:58 -05:00
|
|
|
});
|
|
|
|
|
|
2026-04-05 21:09:33 -05:00
|
|
|
export async function run(): Promise<void> {
|
2026-06-01 16:22:25 -05:00
|
|
|
await Effect.runPromise(program);
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|