Skip to content

Commit

Permalink
added more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 21, 2024
1 parent d06ba4c commit 0e4c978
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 38 deletions.
7 changes: 6 additions & 1 deletion demo/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions docs/src/content/docs/reference/scripts/imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -57,4 +67,4 @@ script(...)
export default async function() {
$`Write a poem.`
}
```
```
7 changes: 6 additions & 1 deletion genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<YAML>({
stringify: YAMLStringify,
Expand Down Expand Up @@ -175,7 +175,7 @@ export function createPromptContext(
if (fn) appendPromptChild(createOutputProcessor(fn))
}

const promptHost: PromptHost = {
const promptHost: PromptHost = Object.freeze<PromptHost>({
askUser: (question) =>
host.askUser({
prompt: question,
Expand All @@ -191,13 +191,13 @@ export function createPromptContext(
const res = await host.container({ ...(options || {}), trace })
return res
},
}
})

const ctx = Object.freeze<PromptContext & RunPromptContextNode>({
const ctx: PromptContext & RunPromptContextNode = {
...createChatGenerationContext(options, trace),
script: () => {},
system: () => {},
env,
env: undefined, // set later
path,
fs: workspace,
workspace,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -55,7 +54,6 @@ async function resolveExpansionVars(
} else trace.error(`secret \`${secret}\` not found`)
}
const res: Partial<ExpansionVariables> = {
...staticVars(),
spec: {
filename: relativePath(host.projectFolder(), file.filename),
content: file.content,
Expand Down
8 changes: 0 additions & 8 deletions packages/core/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,6 @@ export function parsePromptScriptMeta(jsSource: string) {
return meta
}

export function staticVars(): Omit<ExpansionVariables, "template"> {
return {
spec: { filename: "spec.gpspec.md", content: "" } as WorkspaceFile,
files: [] as WorkspaceFile[],
vars: {} as Record<string, string>,
}
}

async function parsePromptTemplateCore(
filename: string,
content: string,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>

/**
* Root prompt generation context
*/
generator: ChatGenerationContext
}

type MakeOptional<T, P extends keyof T> = Partial<Pick<T, P>> & Omit<T, P>
Expand Down
7 changes: 6 additions & 1 deletion packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sample/genaisrc/mjs.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 6 additions & 1 deletion packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/sample/genaisrc/summarizer.mjs
Original file line number Diff line number Diff line change
@@ -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.`
}
7 changes: 6 additions & 1 deletion packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/src/tla/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/sample/src/vision/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion slides/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e4c978

Please sign in to comment.