Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prompt... syntax #717

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 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.

18 changes: 17 additions & 1 deletion genaisrc/genaiscript.d.ts

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

18 changes: 17 additions & 1 deletion packages/auto/genaiscript.d.ts

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

2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,5 @@
export const PLAYWRIGHT_DEFAULT_BROWSER = "chromium"
export const MAX_TOKENS_ELLIPSE = "..."
export const ESTIMATE_TOKEN_OVERHEAD = 2

export const DEDENT_INSPECT_MAX_DEPTH = 3
18 changes: 17 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.

1 change: 1 addition & 0 deletions packages/core/src/importprompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function importPrompt(
"env",
"retrieval",
"runPrompt",
"prompt",
]

const oldGlb: any = {}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/indent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import tsDedent from "ts-dedent"
import { inspect } from "./logging"
import { DEDENT_INSPECT_MAX_DEPTH } from "./constants"

export function indent(text: string, indentation: string) {
return text
Expand All @@ -8,3 +10,23 @@
}

export const dedent = tsDedent

export async function dedentAsync(
strings: Awaitable<TemplateStringsArray | string>,
...args: any[]
) {
const template = await strings
const resolvedArgs: any[] = []
for (const arg of args) {
let resolvedArg = await arg
if (typeof resolvedArg === "function") resolvedArg = resolvedArg()
// render objects
if (typeof resolvedArg === "object" || Array.isArray(resolvedArg))
resolvedArg = inspect(resolvedArg, {
maxDepth: DEDENT_INSPECT_MAX_DEPTH,
})
resolvedArgs.push(resolvedArg ?? "")
}
const value = dedent(template, ...resolvedArgs)
return value
}

Check failure on line 32 in packages/core/src/indent.ts

View workflow job for this annotation

GitHub Actions / build

The function `dedentAsync` is declared as async but doesn't have any await expression inside it. This might lead to unexpected behavior.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import { Project } from "./ast"
import { resolveSystems } from "./systems"
import { shellParse } from "./shell"
import { dedent, dedentAsync } from "./indent"

export async function createPromptContext(
prj: Project,
Expand Down Expand Up @@ -233,6 +234,25 @@
defFileMerge: (fn) => {
appendPromptChild(createFileMerge(fn))
},
prompt: (template, ...args): RunPromptResultPromiseWithOptions => {
const options: PromptGeneratorOptions = {}
const p: RunPromptResultPromiseWithOptions =
new Promise<RunPromptResult>(async (resolve, reject) => {
try {
const text = await dedentAsync(template, ...args)
// data race for options
const res = await runPrompt(text, options)
resolve(res)
} catch (e) {
reject(e)
}
}) as any
p.options = (v) => {
if (v !== undefined) Object.assign(options, v)
return p
}
return p
},

Check failure on line 255 in packages/core/src/promptcontext.ts

View workflow job for this annotation

GitHub Actions / build

In the `prompt` function, the promise rejection is not handled. This could lead to unhandled promise rejections which are deprecated.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
runPrompt: async (generator, runOptions): Promise<RunPromptResult> => {
try {
const { label } = runOptions || {}
Expand Down
21 changes: 2 additions & 19 deletions packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { toChatCompletionUserMessage } from "./chat"
import { errorMessage } from "./error"
import { tidyData } from "./tidy"
import { inspect } from "./logging"
import { dedent } from "./indent"
import { dedent, dedentAsync } from "./indent"
import {
ChatCompletionAssistantMessageParam,
ChatCompletionMessageParam,
Expand Down Expand Up @@ -483,24 +483,7 @@ async function resolvePromptNode(
stringTemplate: async (n) => {
const { strings, args } = n
try {
const resolvedArgs: any[] = []
for (const arg of args) {
let resolvedArg = await arg
if (typeof resolvedArg === "function")
resolvedArg = resolvedArg()
// render objects
if (
typeof resolvedArg === "object" ||
Array.isArray(resolvedArg)
)
resolvedArg = inspect(resolvedArg, {
maxDepth: 3,
})
resolvedArgs.push(resolvedArg ?? "")
}
let value = dedent(strings, ...resolvedArgs)

// apply transforms
let value = await dedentAsync(strings, ...args)
if (n.transforms?.length)
for (const transform of n.transforms)
value = await transform(value)
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,10 @@ interface FileUpdate {
validation?: JSONSchemaValidation
}

interface RunPromptResultPromiseWithOptions extends Promise<RunPromptResult> {
options(values?: PromptGeneratorOptions): RunPromptResultPromiseWithOptions
}

interface ChatGenerationContext extends ChatTurnGenerationContext {
defSchema(
name: string,
Expand Down Expand Up @@ -1576,6 +1580,10 @@ interface ChatGenerationContext extends ChatTurnGenerationContext {
generator: string | PromptGenerator,
options?: PromptGeneratorOptions
): Promise<RunPromptResult>
prompt(
strings: TemplateStringsArray,
...args: any[]
): RunPromptResultPromiseWithOptions
}

interface GenerationOutput {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ declare function defSchema(
* @param options
*/
declare function defImages(
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
options?: DefImagesOptions
): void

Expand Down Expand Up @@ -235,6 +235,14 @@ declare function runPrompt(
options?: PromptGeneratorOptions
): Promise<RunPromptResult>

/**
* Expands and executes the prompt
*/
declare function prompt(
strings: TemplateStringsArray,
...args: any[]
): RunPromptResultPromiseWithOptions

/**
* Registers a callback to process the LLM output
* @param fn
Expand Down
18 changes: 17 additions & 1 deletion packages/sample/genaisrc/blog/genaiscript.d.ts

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

Loading
Loading