|
| 1 | +import { type Node } from "@commontools/common-builder"; |
| 2 | +import { cell, CellImpl, ReactivityLog } from "../cell.js"; |
| 3 | +import { sendValueToBinding, findAllAliasedCells } from "../utils.js"; |
| 4 | +import { schedule, Action } from "../scheduler.js"; |
| 5 | +import { mapBindingsToCell } from "../utils.js"; |
| 6 | +import { makeClient, SimpleMessage, SimpleContent } from "../llm-client.js"; |
| 7 | + |
| 8 | +// TODO(ja): investigate if generateText should be replaced by |
| 9 | +// fetchData with streaming support |
| 10 | + |
| 11 | +/** |
| 12 | + * Generate data via an LLM. |
| 13 | + * |
| 14 | + * Returns the complete result as `result` and the incremental result as |
| 15 | + * `partial`. `pending` is true while a request is pending. |
| 16 | + * |
| 17 | + * @param prompt - A cell to store the prompt message - if you only have a single message |
| 18 | + * @param messages - list of messages to send to the LLM. - alternating user and assistant messages. |
| 19 | + * - if you end with an assistant message, the LLM will continue from there. |
| 20 | + * - if both prompt and messages are empty, no LLM call will be made, |
| 21 | + * result and partial will be undefined. |
| 22 | + * @param system - A cell to store the system message. |
| 23 | + * @param stop - A cell to store (optional) stop sequence. |
| 24 | + * @param max_tokens - A cell to store the maximum number of tokens to generate. |
| 25 | + * |
| 26 | + * @returns { pending: boolean, result?: string, partial?: string } - As individual |
| 27 | + * cells, representing `pending` state, final `result` and incrementally |
| 28 | + * updating `partial` result. |
| 29 | + */ |
| 30 | +export function llm( |
| 31 | + recipeCell: CellImpl<any>, |
| 32 | + { inputs, outputs }: Node |
| 33 | +) { |
| 34 | + const inputBindings = mapBindingsToCell(inputs, recipeCell) as { |
| 35 | + messages?: SimpleContent[] | SimpleMessage[]; |
| 36 | + prompt?: SimpleContent; |
| 37 | + stop?: string; |
| 38 | + system?: string; |
| 39 | + max_tokens?: number; |
| 40 | + }; |
| 41 | + const inputsCell = cell(inputBindings); |
| 42 | + |
| 43 | + const pending = cell(false); |
| 44 | + const result = cell<string | undefined>(undefined); |
| 45 | + const partial = cell<string | undefined>(undefined); |
| 46 | + |
| 47 | + const outputCell = cell({ pending, result, partial }); |
| 48 | + |
| 49 | + const outputBindings = mapBindingsToCell(outputs, recipeCell) as any[]; |
| 50 | + sendValueToBinding(recipeCell, outputBindings, outputCell); |
| 51 | + |
| 52 | + let currentRun = 0; |
| 53 | + |
| 54 | + const startGeneration: Action = (log: ReactivityLog) => { |
| 55 | + const thisRun = ++currentRun; |
| 56 | + |
| 57 | + const { system, messages, prompt, stop, max_tokens } = inputsCell.getAsProxy([], log) ?? {}; |
| 58 | + |
| 59 | + result.setAtPath([], undefined, log); |
| 60 | + partial.setAtPath([], undefined, log); |
| 61 | + |
| 62 | + if (((prompt === undefined || prompt.length === 0) && (messages === undefined || messages.length === 0)) || system === undefined) { |
| 63 | + pending.setAtPath([], false, log); |
| 64 | + return; |
| 65 | + } |
| 66 | + pending.setAtPath([], true, log); |
| 67 | + |
| 68 | + const updatePartial = (text: string) => { |
| 69 | + if (thisRun != currentRun) return; |
| 70 | + partial.setAtPath([], text, log); |
| 71 | + } |
| 72 | + |
| 73 | + let resultPromise = makeClient().sendRequest({ |
| 74 | + messages: messages || [prompt as SimpleContent], |
| 75 | + system, |
| 76 | + model: "claude-3-5-sonnet-20240620", |
| 77 | + max_tokens: max_tokens || 4096, |
| 78 | + stop |
| 79 | + }, updatePartial) |
| 80 | + |
| 81 | + resultPromise |
| 82 | + .then((text) => { |
| 83 | + if (thisRun !== currentRun) return; |
| 84 | + // normalizeToCells(result, undefined, log); |
| 85 | + |
| 86 | + pending.setAtPath([], false, log); |
| 87 | + result.setAtPath([], text, log); |
| 88 | + partial.setAtPath([], text, log); |
| 89 | + }).catch((error) => { |
| 90 | + if (thisRun !== currentRun) return; |
| 91 | + |
| 92 | + console.error("Error generating data", error); |
| 93 | + pending.setAtPath([], false, log); |
| 94 | + result.setAtPath([], undefined, log); |
| 95 | + partial.setAtPath([], undefined, log); |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + schedule(startGeneration, { |
| 100 | + reads: findAllAliasedCells(inputBindings, recipeCell), |
| 101 | + writes: findAllAliasedCells(outputBindings, recipeCell), |
| 102 | + }); |
| 103 | +} |
0 commit comments