Skip to content

Commit bcadd07

Browse files
authored
Implement generateText built-in (#2010)
* Implement `generateText` built-in * Usage example in `note.tsx` * Add `mise.toml` to specify `deno` version Probably only useful for me... for now * Lint + format * Lint + format again * Clear intermediate results to prevent stale output
1 parent 4ebf35c commit bcadd07

File tree

8 files changed

+273
-79
lines changed

8 files changed

+273
-79
lines changed

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tools]
2+
deno = "2.5.2"

packages/api/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,20 @@ export interface BuiltInGenerateObjectParams {
704704
metadata?: Record<string, string | undefined | object>;
705705
}
706706

707+
export interface BuiltInGenerateTextParams {
708+
prompt: string;
709+
system?: string;
710+
model?: string;
711+
maxTokens?: number;
712+
}
713+
714+
export interface BuiltInGenerateTextState {
715+
pending: boolean;
716+
result?: string;
717+
partial?: string;
718+
requestHash?: string;
719+
}
720+
707721
export interface BuiltInCompileAndRunParams<T> {
708722
files: Array<{ name: string; contents: string }>;
709723
main: string;
@@ -883,6 +897,10 @@ export type GenerateObjectFunction = <T = any>(
883897
params: Opaque<BuiltInGenerateObjectParams>,
884898
) => OpaqueRef<BuiltInLLMGenerateObjectState<T>>;
885899

900+
export type GenerateTextFunction = (
901+
params: Opaque<BuiltInGenerateTextParams>,
902+
) => OpaqueRef<BuiltInGenerateTextState>;
903+
886904
export type FetchOptions = {
887905
body?: JSONValue;
888906
headers?: Record<string, string>;
@@ -989,6 +1007,7 @@ export declare const ifElse: IfElseFunction;
9891007
export declare const llm: LLMFunction;
9901008
export declare const llmDialog: LLMDialogFunction;
9911009
export declare const generateObject: GenerateObjectFunction;
1010+
export declare const generateText: GenerateTextFunction;
9921011
export declare const fetchData: FetchDataFunction;
9931012
export declare const streamData: StreamDataFunction;
9941013
export declare const compileAndRun: CompileAndRunFunction;

packages/patterns/note.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/// <cts-enable />
22
import {
3-
type BuiltInLLMMessage,
43
type Cell,
54
cell,
65
type Default,
76
derive,
7+
generateText,
88
handler,
9-
llm,
109
NAME,
1110
navigateTo,
1211
type Opaque,
@@ -175,15 +174,9 @@ const Note = recipe<Input, Output>(
175174
content: string;
176175
},
177176
) => {
178-
const result = llm({
177+
const result = generateText({
179178
system: str`Translate the content to ${language}.`,
180-
messages: derive(content, (c) =>
181-
[
182-
{
183-
role: "user",
184-
content: c,
185-
},
186-
] satisfies BuiltInLLMMessage[]),
179+
prompt: str`<to_translate>${content}</to_translate>`,
187180
});
188181

189182
return derive(result, ({ pending, result }) => {

packages/runner/src/builder/built-in.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type {
1515
BuiltInCompileAndRunParams,
1616
BuiltInCompileAndRunState,
1717
BuiltInGenerateObjectParams,
18+
BuiltInGenerateTextParams,
19+
BuiltInGenerateTextState,
1820
BuiltInLLMGenerateObjectState,
1921
BuiltInLLMParams,
2022
BuiltInLLMState,
@@ -50,6 +52,13 @@ export const generateObject = createNodeFactory({
5052
params: Opaque<BuiltInGenerateObjectParams>,
5153
) => OpaqueRef<BuiltInLLMGenerateObjectState<T>>;
5254

55+
export const generateText = createNodeFactory({
56+
type: "ref",
57+
implementation: "generateText",
58+
}) as (
59+
params: Opaque<BuiltInGenerateTextParams>,
60+
) => OpaqueRef<BuiltInGenerateTextState>;
61+
5362
export const fetchData = createNodeFactory({
5463
type: "ref",
5564
implementation: "fetchData",

packages/runner/src/builder/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
compileAndRun,
2727
fetchData,
2828
generateObject,
29+
generateText,
2930
ifElse,
3031
llm,
3132
llmDialog,
@@ -117,6 +118,7 @@ export const createBuilder = (
117118
llm,
118119
llmDialog,
119120
generateObject,
121+
generateText,
120122
fetchData,
121123
streamData,
122124
compileAndRun,

packages/runner/src/builder/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
DeriveFunction,
1313
FetchDataFunction,
1414
GenerateObjectFunction,
15+
GenerateTextFunction,
1516
GetRecipeEnvironmentFunction,
1617
HandlerFunction,
1718
HFunction,
@@ -282,6 +283,7 @@ export interface BuilderFunctionsAndConstants {
282283
llm: LLMFunction;
283284
llmDialog: LLMDialogFunction;
284285
generateObject: GenerateObjectFunction;
286+
generateText: GenerateTextFunction;
285287
fetchData: FetchDataFunction;
286288
streamData: StreamDataFunction;
287289
compileAndRun: CompileAndRunFunction;

packages/runner/src/builtins/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { raw } from "../module.ts";
22
import { map } from "./map.ts";
33
import { fetchData } from "./fetch-data.ts";
44
import { streamData } from "./stream-data.ts";
5-
import { generateObject, llm } from "./llm.ts";
5+
import { generateObject, generateText, llm } from "./llm.ts";
66
import { ifElse } from "./if-else.ts";
77
import type { IRuntime } from "../runtime.ts";
88
import { compileAndRun } from "./compile-and-run.ts";
99
import { navigateTo } from "./navigate-to.ts";
1010
import { wish } from "./wish.ts";
1111
import type { Cell } from "../cell.ts";
12-
import type { BuiltInGenerateObjectParams } from "@commontools/api";
12+
import type {
13+
BuiltInGenerateObjectParams,
14+
BuiltInGenerateTextParams,
15+
} from "@commontools/api";
1316
import { llmDialog } from "./llm-dialog.ts";
1417

1518
/**
@@ -34,6 +37,15 @@ export function registerBuiltins(runtime: IRuntime) {
3437
requestHash: Cell<string | undefined>;
3538
}>(generateObject),
3639
);
40+
moduleRegistry.addModuleByRef(
41+
"generateText",
42+
raw<BuiltInGenerateTextParams, {
43+
pending: Cell<boolean>;
44+
result: Cell<string | undefined>;
45+
partial: Cell<string | undefined>;
46+
requestHash: Cell<string | undefined>;
47+
}>(generateText),
48+
);
3749
moduleRegistry.addModuleByRef("navigateTo", raw(navigateTo));
3850
moduleRegistry.addModuleByRef("wish", raw(wish));
3951
}

0 commit comments

Comments
 (0)