From 0e4c978ce5768f09fd15b06d523d7657af2c715c Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 21 Jun 2024 17:06:30 +0000 Subject: [PATCH] added more docs --- demo/genaisrc/genaiscript.d.ts | 7 ++++++- docs/genaisrc/genaiscript.d.ts | 7 ++++++- .../src/content/docs/reference/scripts/imports.mdx | 14 ++++++++++++-- genaisrc/genaiscript.d.ts | 7 ++++++- packages/core/src/genaisrc/genaiscript.d.ts | 7 ++++++- packages/core/src/promptcontext.ts | 14 ++++++++------ packages/core/src/promptrunner.ts | 2 -- packages/core/src/template.ts | 8 -------- packages/core/src/types/prompt_template.d.ts | 7 ++++++- packages/sample/genaisrc/genaiscript.d.ts | 7 ++++++- packages/sample/genaisrc/mjs.genai.mjs | 2 +- packages/sample/genaisrc/node/genaiscript.d.ts | 7 ++++++- packages/sample/genaisrc/python/genaiscript.d.ts | 7 ++++++- packages/sample/genaisrc/style/genaiscript.d.ts | 7 ++++++- packages/sample/genaisrc/summarizer.mjs | 8 ++++---- packages/sample/src/aici/genaiscript.d.ts | 7 ++++++- packages/sample/src/errors/genaiscript.d.ts | 7 ++++++- packages/sample/src/makecode/genaiscript.d.ts | 7 ++++++- packages/sample/src/tla/genaiscript.d.ts | 7 ++++++- packages/sample/src/vision/genaiscript.d.ts | 7 ++++++- slides/genaisrc/genaiscript.d.ts | 7 ++++++- 21 files changed, 115 insertions(+), 38 deletions(-) diff --git a/demo/genaisrc/genaiscript.d.ts b/demo/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/demo/genaisrc/genaiscript.d.ts +++ b/demo/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/docs/genaisrc/genaiscript.d.ts b/docs/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/docs/genaisrc/genaiscript.d.ts +++ b/docs/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/docs/src/content/docs/reference/scripts/imports.mdx b/docs/src/content/docs/reference/scripts/imports.mdx index e7db72046b..8f873dc68f 100644 --- a/docs/src/content/docs/reference/scripts/imports.mdx +++ b/docs/src/content/docs/reference/scripts/imports.mdx @@ -43,9 +43,19 @@ export function summarize(files) { ```js title="summarize.genai.mjs" import { summarize } from "./summarizer.mjs" -summarize(env.files) +summarize(env.generator, env.files) ``` +## `env.generator` + +The `env.generator` references the root prompt generator context, the top level `$`, `def` functions... It can be used to create function that can be used with those function or also with `runPrompt`. + +```js title="summarizer.mjs" "_" +export function summarize(_, files) { + _.def("FILE", files) + _.$`Summarize each file. Be concise.` +} +``` ## Default function export @@ -57,4 +67,4 @@ script(...) export default async function() { $`Write a poem.` } -``` \ No newline at end of file +``` diff --git a/genaisrc/genaiscript.d.ts b/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/genaisrc/genaiscript.d.ts +++ b/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/core/src/genaisrc/genaiscript.d.ts b/packages/core/src/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/core/src/genaisrc/genaiscript.d.ts +++ b/packages/core/src/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/core/src/promptcontext.ts b/packages/core/src/promptcontext.ts index 3b826a0095..f1773fc901 100644 --- a/packages/core/src/promptcontext.ts +++ b/packages/core/src/promptcontext.ts @@ -49,7 +49,7 @@ export function createPromptContext( model: string ) { const { cancellationToken, infoCb } = options || {} - const env = Object.freeze(vars) + const env = structuredClone(vars) const parsers = createParsers({ trace, model }) const YAML = Object.freeze({ stringify: YAMLStringify, @@ -175,7 +175,7 @@ export function createPromptContext( if (fn) appendPromptChild(createOutputProcessor(fn)) } - const promptHost: PromptHost = { + const promptHost: PromptHost = Object.freeze({ askUser: (question) => host.askUser({ prompt: question, @@ -191,13 +191,13 @@ export function createPromptContext( const res = await host.container({ ...(options || {}), trace }) return res }, - } + }) - const ctx = Object.freeze({ + const ctx: PromptContext & RunPromptContextNode = { ...createChatGenerationContext(options, trace), script: () => {}, system: () => {}, - env, + env: undefined, // set later path, fs: workspace, workspace, @@ -342,7 +342,9 @@ export function createPromptContext( file, } }, - }) + } + env.generator = ctx + ctx.env = Object.freeze(env) const appendPromptChild = (node: PromptNode) => { if (!ctx.node) throw new Error("Prompt closed") appendChild(ctx.node, node) diff --git a/packages/core/src/promptrunner.ts b/packages/core/src/promptrunner.ts index 3b9551ee5b..ed651ed590 100644 --- a/packages/core/src/promptrunner.ts +++ b/packages/core/src/promptrunner.ts @@ -2,7 +2,6 @@ import { executeChatSession } from "./chat" import { Fragment, Project, PromptScript } from "./ast" import { stringToPos } from "./parser" import { arrayify, assert, logVerbose, relativePath } from "./util" -import { staticVars } from "./template" import { host } from "./host" import { applyLLMDiff, applyLLMPatch, parseLLMDiffs } from "./diff" import { MarkdownTrace } from "./trace" @@ -55,7 +54,6 @@ async function resolveExpansionVars( } else trace.error(`secret \`${secret}\` not found`) } const res: Partial = { - ...staticVars(), spec: { filename: relativePath(host.projectFolder(), file.filename), content: file.content, diff --git a/packages/core/src/template.ts b/packages/core/src/template.ts index b82795cb54..67fc60f429 100644 --- a/packages/core/src/template.ts +++ b/packages/core/src/template.ts @@ -196,14 +196,6 @@ export function parsePromptScriptMeta(jsSource: string) { return meta } -export function staticVars(): Omit { - return { - spec: { filename: "spec.gpspec.md", content: "" } as WorkspaceFile, - files: [] as WorkspaceFile[], - vars: {} as Record, - } -} - async function parsePromptTemplateCore( filename: string, content: string, diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index c25bcb9db4..7a7613ba20 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -525,9 +525,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/genaisrc/genaiscript.d.ts b/packages/sample/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/genaisrc/genaiscript.d.ts +++ b/packages/sample/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/genaisrc/mjs.genai.mjs b/packages/sample/genaisrc/mjs.genai.mjs index 3c4321bef2..0e22ad3d9a 100644 --- a/packages/sample/genaisrc/mjs.genai.mjs +++ b/packages/sample/genaisrc/mjs.genai.mjs @@ -13,5 +13,5 @@ import { summarize } from "./summarizer.mjs" $`You are an export at analyzing data.` export default async function () { - summarize(env.files) + summarize(env.generator, env.files) } diff --git a/packages/sample/genaisrc/node/genaiscript.d.ts b/packages/sample/genaisrc/node/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/genaisrc/node/genaiscript.d.ts +++ b/packages/sample/genaisrc/node/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/genaisrc/python/genaiscript.d.ts b/packages/sample/genaisrc/python/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/genaisrc/python/genaiscript.d.ts +++ b/packages/sample/genaisrc/python/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/genaisrc/style/genaiscript.d.ts b/packages/sample/genaisrc/style/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/genaisrc/style/genaiscript.d.ts +++ b/packages/sample/genaisrc/style/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/genaisrc/summarizer.mjs b/packages/sample/genaisrc/summarizer.mjs index 52b8d8879b..dd03b41e66 100644 --- a/packages/sample/genaisrc/summarizer.mjs +++ b/packages/sample/genaisrc/summarizer.mjs @@ -1,5 +1,5 @@ -export function summarize(files) { - def("FILE", files) - $`...` - $`Summarize each file. Be concise.` +export function summarize(_, files) { + _.def("FILE", files) + _.$`...` + _.$`Summarize each file. Be concise.` } \ No newline at end of file diff --git a/packages/sample/src/aici/genaiscript.d.ts b/packages/sample/src/aici/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/src/aici/genaiscript.d.ts +++ b/packages/sample/src/aici/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/src/errors/genaiscript.d.ts b/packages/sample/src/errors/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/src/errors/genaiscript.d.ts +++ b/packages/sample/src/errors/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/src/makecode/genaiscript.d.ts b/packages/sample/src/makecode/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/src/makecode/genaiscript.d.ts +++ b/packages/sample/src/makecode/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/src/tla/genaiscript.d.ts b/packages/sample/src/tla/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/src/tla/genaiscript.d.ts +++ b/packages/sample/src/tla/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/packages/sample/src/vision/genaiscript.d.ts b/packages/sample/src/vision/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/packages/sample/src/vision/genaiscript.d.ts +++ b/packages/sample/src/vision/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit diff --git a/slides/genaisrc/genaiscript.d.ts b/slides/genaisrc/genaiscript.d.ts index d139be1f76..c34d06ee1c 100644 --- a/slides/genaisrc/genaiscript.d.ts +++ b/slides/genaisrc/genaiscript.d.ts @@ -559,9 +559,14 @@ interface ExpansionVariables { vars: PromptParameters /** - * List of secrets used by the prompt, must be registred in `genaiscript`. + * List of secrets used by the prompt, must be registered in `genaiscript`. */ secrets?: Record + + /** + * Root prompt generation context + */ + generator: ChatGenerationContext } type MakeOptional = Partial> & Omit