UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

84 lines (83 loc) • 2.74 kB
const hasDoGenerate = (value) => typeof value === "object" && value !== null && typeof value.doGenerate === "function"; /** * v3 reports `inputTokens` / `outputTokens` as objects carrying a `total`, * while `SingleShotResult` — and `extractTokenUsage` downstream — accept only * numbers. Passing the nested shape straight through recorded 0/0/0 for every * caller of this helper, which is the whole of the video-frame formatting * path's usage accounting. */ const readCount = (value) => { if (typeof value === "number") { return value; } if (typeof value === "object" && value !== null && typeof value.total === "number") { return value.total; } return undefined; }; const normalizeUsage = (usage) => { if (typeof usage !== "object" || usage === null) { return undefined; } const shaped = usage; const input = readCount(shaped.inputTokens); const output = readCount(shaped.outputTokens); if (input === undefined && output === undefined) { return undefined; } return { inputTokens: input ?? 0, outputTokens: output ?? 0, totalTokens: (input ?? 0) + (output ?? 0), }; }; const textFromContent = (content) => Array.isArray(content) ? content .filter((part) => typeof part === "object" && part !== null && part.type === "text" && typeof part.text === "string") .map((part) => part.text) .join("") : typeof content === "string" ? content : ""; export async function generateOnceNative(model, request) { if (!hasDoGenerate(model)) { throw new Error("generateOnceNative: model handle exposes no doGenerate()"); } const prompt = []; if (request.system) { prompt.push({ role: "system", content: request.system }); } prompt.push({ role: "user", content: [{ type: "text", text: request.prompt }], }); const raw = await model.doGenerate({ prompt, ...(request.maxOutputTokens ? { maxOutputTokens: request.maxOutputTokens } : {}), ...(request.temperature !== undefined ? { temperature: request.temperature } : {}), ...(request.abortSignal ? { abortSignal: request.abortSignal } : {}), }); const shaped = raw; return { text: typeof shaped.text === "string" ? shaped.text : textFromContent(shaped.content), ...(normalizeUsage(shaped.usage) ? { usage: normalizeUsage(shaped.usage) } : {}), ...(typeof shaped.finishReason === "string" ? { finishReason: shaped.finishReason } : {}), }; }