From a1d100cacd9030aa2b9221ab153c165c49029575 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 10 Jun 2024 11:57:04 +0000 Subject: [PATCH] move runPrompt to global scope only --- docs/genaisrc/genaiscript.d.ts | 8 +- genaisrc/genaiscript.d.ts | 8 +- packages/core/src/genaisrc/genaiscript.d.ts | 8 +- packages/core/src/promptcontext.ts | 108 +++++++++++++++++- packages/core/src/runpromptcontext.ts | 99 +--------------- packages/core/src/types/prompt_template.d.ts | 8 +- packages/sample/genaisrc/genaiscript.d.ts | 8 +- .../sample/genaisrc/node/genaiscript.d.ts | 8 +- .../sample/genaisrc/python/genaiscript.d.ts | 8 +- .../sample/genaisrc/style/genaiscript.d.ts | 8 +- packages/sample/src/aici/genaiscript.d.ts | 8 +- packages/sample/src/errors/genaiscript.d.ts | 8 +- packages/sample/src/makecode/genaiscript.d.ts | 8 +- packages/sample/src/tla/genaiscript.d.ts | 8 +- packages/sample/src/vision/genaiscript.d.ts | 8 +- slides/genaisrc/genaiscript.d.ts | 8 +- 16 files changed, 159 insertions(+), 160 deletions(-) diff --git a/docs/genaisrc/genaiscript.d.ts b/docs/genaisrc/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/docs/genaisrc/genaiscript.d.ts +++ b/docs/genaisrc/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/genaisrc/genaiscript.d.ts b/genaisrc/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/genaisrc/genaiscript.d.ts +++ b/genaisrc/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/core/src/genaisrc/genaiscript.d.ts b/packages/core/src/genaisrc/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/core/src/genaisrc/genaiscript.d.ts +++ b/packages/core/src/genaisrc/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/core/src/promptcontext.ts b/packages/core/src/promptcontext.ts index 7385924d21..1f46355247 100644 --- a/packages/core/src/promptcontext.ts +++ b/packages/core/src/promptcontext.ts @@ -1,4 +1,10 @@ -import { ChatCompletionsOptions, LanguageModel } from "./chat" +import { + ChatCompletionMessageParam, + ChatCompletionsOptions, + executeChatSession, + LanguageModel, + mergeGenerationOptions, +} from "./chat" import { HTMLEscape, arrayify, logVerbose } from "./util" import { host } from "./host" import { MarkdownTrace } from "./trace" @@ -9,25 +15,27 @@ import { readText } from "./fs" import { PromptNode, appendChild, - createChatParticipant, createFileMergeNode, - createImageNode, createOutputProcessor, + createTextNode, + renderPromptNode, } from "./promptdom" import { bingSearch } from "./websearch" -import { CancellationToken } from "./cancellation" +import { CancellationToken, checkCancelled } from "./cancellation" import { RunPromptContextNode, createChatGenerationContext, } from "./runpromptcontext" import { CSVParse, CSVToMarkdown } from "./csv" import { INIParse, INIStringify } from "./ini" -import { CancelError } from "./error" +import { CancelError, isCancelError, serializeError } from "./error" import { createFetch } from "./fetch" -import { resolveFileDataUri } from "./file" import { XMLParse } from "./xml" import { GenerationStats } from "./expander" import { fuzzSearch } from "./fuzzsearch" +import { parseModelIdentifier, resolveModelConnectionInfo } from "./models" +import { renderAICI } from "./aici" +import { MODEL_PROVIDER_AICI } from "./constants" function stringLikeToFileName(f: string | WorkspaceFile) { return typeof f === "string" ? f : f?.filename @@ -39,6 +47,7 @@ export function createPromptContext( options: GenerationOptions, model: string ) { + const { cancellationToken, infoCb } = options || {} const env = new Proxy(vars, { get: (target: any, prop, recv) => { const v = target[prop] @@ -211,6 +220,93 @@ export function createPromptContext( cancel: (reason?: string) => { throw new CancelError(reason || "user cancelled") }, + runPrompt: async (generator, runOptions): Promise => { + try { + const { label } = runOptions || {} + trace.startDetails(`🎁 run prompt ${label || ""}`) + infoCb?.({ text: `run prompt ${label || ""}` }) + + const genOptions = mergeGenerationOptions(options, runOptions) + const ctx = createChatGenerationContext(genOptions, vars, trace) + if (typeof generator === "string") + ctx.node.children.push(createTextNode(generator)) + else await generator(ctx) + const node = ctx.node + + checkCancelled(cancellationToken) + + let messages: ChatCompletionMessageParam[] = [] + let functions: ChatFunctionCallback[] = undefined + let schemas: Record = undefined + let chatParticipants: ChatParticipant[] = undefined + // expand template + const { provider } = parseModelIdentifier(genOptions.model) + if (provider === MODEL_PROVIDER_AICI) { + const { aici } = await renderAICI("prompt", node) + // todo: output processor? + messages.push(aici) + } else { + const { + errors, + schemas: scs, + functions: fns, + messages: msgs, + chatParticipants: cps, + } = await renderPromptNode(genOptions.model, node, { + trace, + }) + + schemas = scs + functions = fns + chatParticipants = cps + messages.push(...msgs) + + if (errors?.length) + throw new Error("errors while running prompt") + } + + const connection = await resolveModelConnectionInfo( + genOptions, + { trace, token: true } + ) + if (!connection.configuration) + throw new Error("model connection error " + connection.info) + const { completer } = await host.resolveLanguageModel( + genOptions, + connection.configuration + ) + if (!completer) + throw new Error( + "model driver not found for " + connection.info + ) + const resp = await executeChatSession( + connection.configuration, + cancellationToken, + messages, + vars, + functions, + schemas, + completer, + chatParticipants, + genOptions + ) + const { json, text } = resp + if (resp.json) + trace.detailsFenced("📩 json (parsed)", json, "json") + else if (text) + trace.detailsFenced(`🔠 output`, text, `markdown`) + return resp + } catch (e) { + trace.error(e) + return { + text: undefined, + finishReason: isCancelError(e) ? "cancel" : "fail", + error: serializeError(e), + } + } finally { + trace.endDetails() + } + }, fetchText: async (urlOrFile, fetchOptions) => { if (typeof urlOrFile === "string") { urlOrFile = { diff --git a/packages/core/src/runpromptcontext.ts b/packages/core/src/runpromptcontext.ts index 3642fcc60d..2842031862 100644 --- a/packages/core/src/runpromptcontext.ts +++ b/packages/core/src/runpromptcontext.ts @@ -11,24 +11,13 @@ import { createSchemaNode, createStringTemplateNode, createTextNode, - renderPromptNode, } from "./promptdom" import { MarkdownTrace } from "./trace" -import { - ChatCompletionMessageParam, - executeChatSession, - mergeGenerationOptions, -} from "./chat" import { GenerationOptions } from "./promptcontext" -import { parseModelIdentifier, resolveModelConnectionInfo } from "./models" -import { renderAICI } from "./aici" -import { CancelError, isCancelError, serializeError } from "./error" -import { checkCancelled } from "./cancellation" -import { MODEL_PROVIDER_AICI } from "./constants" +import { CancelError } from "./error" import { promptParametersSchemaToJSONSchema } from "./parameters" import { isJSONSchema } from "./schema" import { consoleLogFormat } from "./logging" -import { host } from "./host" import { resolveFileDataUri } from "./file" export function createChatTurnGenerationContext( @@ -114,92 +103,6 @@ export function createChatTurnGenerationContext( ctx.def("", body, options) return undefined }, - runPrompt: async (generator, runOptions) => { - try { - const { label } = runOptions || {} - trace.startDetails(`🎁 run prompt ${label || ""}`) - infoCb?.({ text: `run prompt ${label || ""}` }) - - const genOptions = mergeGenerationOptions(options, runOptions) - const ctx = createChatGenerationContext(genOptions, vars, trace) - if (typeof generator === "string") - ctx.node.children.push(createTextNode(generator)) - else await generator(ctx) - const node = ctx.node - - checkCancelled(cancellationToken) - - let messages: ChatCompletionMessageParam[] = [] - let functions: ChatFunctionCallback[] = undefined - let schemas: Record = undefined - let chatParticipants: ChatParticipant[] = undefined - // expand template - const { provider } = parseModelIdentifier(genOptions.model) - if (provider === MODEL_PROVIDER_AICI) { - const { aici } = await renderAICI("prompt", node) - // todo: output processor? - messages.push(aici) - } else { - const { - errors, - schemas: scs, - functions: fns, - messages: msgs, - chatParticipants: cps, - } = await renderPromptNode(genOptions.model, node, { - trace, - }) - - schemas = scs - functions = fns - chatParticipants = cps - messages.push(...msgs) - - if (errors?.length) - throw new Error("errors while running prompt") - } - - const connection = await resolveModelConnectionInfo( - genOptions, - { trace, token: true } - ) - if (!connection.configuration) - throw new Error("model connection error " + connection.info) - const { completer } = await host.resolveLanguageModel( - genOptions, - connection.configuration - ) - if (!completer) - throw new Error( - "model driver not found for " + connection.info - ) - const resp = await executeChatSession( - connection.configuration, - cancellationToken, - messages, - vars, - functions, - schemas, - completer, - chatParticipants, - genOptions - ) - const { json, text } = resp - if (resp.json) - trace.detailsFenced("📩 json (parsed)", json, "json") - else if (text) - trace.detailsFenced(`🔠 output`, text, `markdown`) - return resp - } catch (e) { - trace.error(e) - return { - finishReason: isCancelError(e) ? "cancel" : "fail", - error: serializeError(e), - } - } finally { - trace.endDetails() - } - }, console, } diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index d296cd9386..ef72903dfe 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -1233,10 +1233,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1513,6 +1509,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/genaisrc/genaiscript.d.ts b/packages/sample/genaisrc/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/genaisrc/genaiscript.d.ts +++ b/packages/sample/genaisrc/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/genaisrc/node/genaiscript.d.ts b/packages/sample/genaisrc/node/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/genaisrc/node/genaiscript.d.ts +++ b/packages/sample/genaisrc/node/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/genaisrc/python/genaiscript.d.ts b/packages/sample/genaisrc/python/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/genaisrc/python/genaiscript.d.ts +++ b/packages/sample/genaisrc/python/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/genaisrc/style/genaiscript.d.ts b/packages/sample/genaisrc/style/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/genaisrc/style/genaiscript.d.ts +++ b/packages/sample/genaisrc/style/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/src/aici/genaiscript.d.ts b/packages/sample/src/aici/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/src/aici/genaiscript.d.ts +++ b/packages/sample/src/aici/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/src/errors/genaiscript.d.ts b/packages/sample/src/errors/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/src/errors/genaiscript.d.ts +++ b/packages/sample/src/errors/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/src/makecode/genaiscript.d.ts b/packages/sample/src/makecode/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/src/makecode/genaiscript.d.ts +++ b/packages/sample/src/makecode/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/src/tla/genaiscript.d.ts b/packages/sample/src/tla/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/src/tla/genaiscript.d.ts +++ b/packages/sample/src/tla/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/packages/sample/src/vision/genaiscript.d.ts b/packages/sample/src/vision/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/packages/sample/src/vision/genaiscript.d.ts +++ b/packages/sample/src/vision/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions diff --git a/slides/genaisrc/genaiscript.d.ts b/slides/genaisrc/genaiscript.d.ts index 1645206b7b..530f3de426 100644 --- a/slides/genaisrc/genaiscript.d.ts +++ b/slides/genaisrc/genaiscript.d.ts @@ -1267,10 +1267,6 @@ interface ChatTurnGenerationContext { data: object[] | object, options?: DefDataOptions ): string - runPrompt( - generator: string | PromptGenerator, - options?: PromptGeneratorOptions - ): Promise console: PromptGenerationConsole } @@ -1547,6 +1543,10 @@ interface PromptContext extends ChatGenerationContext { system(options: PromptSystemArgs): void defFileMerge(fn: FileMergeHandler): void defOutputProcessor(fn: PromptOutputProcessorHandler): void + runPrompt( + generator: string | PromptGenerator, + options?: PromptGeneratorOptions + ): Promise fetchText( urlOrFile: string | WorkspaceFile, options?: FetchTextOptions