@paroicms/site-generator-plugin
Version:
ParoiCMS Site Generator Plugin
34 lines (33 loc) • 1.46 kB
JavaScript
import { LlmError } from "./llm-utils.js";
export async function invokeClaude(ctx, options) {
const { anthropic, anthropicModelName } = ctx;
const { prompt, llmTaskName, maxTokens, systemInstruction, temperature } = options;
const system = systemInstruction === "beFast"
? "Please provide brief, direct answers without extensive deliberation. Focus on giving the most immediately useful information quickly."
: "Take max 20 seconds to think and provide a thorough response.";
const startTime = Date.now();
const response = await anthropic.messages.create({
model: anthropicModelName,
system,
messages: [{ role: "user", content: prompt }],
max_tokens: maxTokens,
temperature: temperature,
});
const llmReport = {
llmTaskName,
modelName: anthropicModelName,
inputTokenCount: response.usage.input_tokens,
durationMs: Date.now() - startTime,
outputTokenCount: response.usage.output_tokens,
};
const [first] = response.content;
if (first.type === "text") {
return {
messageContent: first.text,
report: llmReport,
};
}
ctx.logger.error(`Unexpected response type from Claude: ${JSON.stringify(response.content, null, 2)}`);
llmReport.errorMessage = `Unexpected response type: ${first.type}`;
throw new LlmError(`Unexpected response type: "${first.type}"`, llmReport);
}