// One plain-text answer from either provider — the sweep's extractor call // (release 27, S5; the stopgap's `claude_api.messages` with EXTRACTOR_SYSTEM). // Metered on the run's meter. A local model's prompt is cut to fit its cap // before it is sent (the 8k rule): the caller says what was cut. import { AnthropicClient, anthropicConfigOf, isStrictModel, ollamaChat, ollamaConfigOf, promptCapOf, providerOf, textOf, type LlmConfig, type Meter, type StructuredCallMeta, } from "../../../llm"; import { lazyKeyRedactor, recordModelCall } from "../../../llm/callLog-server"; // Characters a token takes in the ollama estimate (lib/llm/ollama.ts). const CHARS_PER_TOKEN = 3.5; export async function plainAnswer(o: { llm: LlmConfig; model: string; system: string; user: string; meter: Meter; signal?: AbortSignal; maxTokens?: number; // The model log (lib/llm/callLog.ts): the call's phase, label and slot. call?: StructuredCallMeta; }): Promise { const route = providerOf(o.model); const t0 = Date.now(); let redact: ((s: string) => string) | undefined = lazyKeyRedactor(o.llm.anthropic); const record = (answer: string, tokens: { tokensIn?: number; tokensOut?: number }, error?: unknown) => recordModelCall( { ...(o.call?.phase ? { phase: o.call.phase } : {}), ...(o.call?.label ? { label: o.call.label } : {}), model: o.model, route, system: o.system, prompt: o.user, answer, seconds: (Date.now() - t0) / 1000, ...tokens, ...(error !== undefined ? { error: (error as Error)?.message ?? String(error) } : {}), ...(redact ? { redact } : {}), }, o.call?.slot, ); try { const configured = redact; return await plainAnswerOn(o, record, (r) => (redact = (s) => r(configured ? configured(s) : s))); } catch (err) { record("", {}, err); throw err; } } async function plainAnswerOn( o: Parameters[0], record: (answer: string, tokens: { tokensIn?: number; tokensOut?: number }) => void, setRedact: (r: (s: string) => string) => void, ): Promise { if (providerOf(o.model) === "api") { const client = new AnthropicClient(anthropicConfigOf(o.llm, o.model)); setRedact((s) => client.redactText(s)); const res = await client.messages( { model: o.model, max_tokens: o.maxTokens ?? 4000, ...(isStrictModel(o.model) ? {} : { temperature: 0 }), system: o.system, messages: [{ role: "user", content: o.user }], }, { meter: o.meter, signal: o.signal }, ); const u = res.usage ?? {}; const text = textOf(res); record(text, { tokensIn: (Number(u.input_tokens) || 0) + (Number(u.cache_read_input_tokens) || 0) + (Number(u.cache_creation_input_tokens) || 0), tokensOut: Number(u.output_tokens) || 0 }); return text; } const cfg = ollamaConfigOf(o.llm, o.model); const reply = await ollamaChat(cfg, { model: o.model, messages: [ { role: "system", content: o.system }, { role: "user", content: o.user }, ], signal: o.signal, }); o.meter.add(o.model, { input_tokens: reply.promptTokens, output_tokens: reply.evalTokens }); const text = reply.message.content ?? ""; record(text, { tokensIn: reply.promptTokens, tokensOut: reply.evalTokens }); return text; } // How many characters of user text fit a model's one call beside `system`: // unbounded on the API (the caller's own cap applies), the prompt cap less a // margin on ollama. export function userCharsFor(llm: LlmConfig, model: string, system: string, apiCap: number): number { if (providerOf(model) === "api") return apiCap; const cap = promptCapOf(ollamaConfigOf(llm, model)); return Math.max(1000, Math.floor((cap - 300) * CHARS_PER_TOKEN) - system.length); }