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 ellipse on token limit exceed and flex layout options in PromptTemplateString #694

Merged
merged 7 commits into from
Sep 6, 2024
26 changes: 25 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.

26 changes: 25 additions & 1 deletion genaisrc/genaiscript.d.ts

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

9 changes: 5 additions & 4 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,22 +467,23 @@

const ctx = createChatTurnGenerationContext(options, trace)
await generator(ctx, structuredClone(messages))
const node = ctx.node
checkCancelled(cancellationToken)
// expand template
const { errors, prompt } = await renderPromptNode(
const { errors, userPrompt } = await renderPromptNode(
options.model,
node,
{
flexTokens: options.flexTokens,
trace,
}
)
if (prompt?.trim().length) {
trace.detailsFenced(`💬 message`, prompt, "markdown")
messages.push({ role: "user", content: prompt })
if (userPrompt?.trim().length) {
trace.detailsFenced(`💬 message`, userPrompt, "markdown")
messages.push({ role: "user", content: userPrompt })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'prompt' has been renamed to 'userPrompt'. This could potentially break any code that relies on the original 'prompt' variable.

generated by pr-review-commit variable_renaming

needsNewTurn = true
} else trace.item("no message")
if (errors?.length) {

Check failure on line 486 in packages/core/src/chat.ts

View workflow job for this annotation

GitHub Actions / build

The variable 'prompt' has been renamed to 'userPrompt'. This could potentially break any code that relies on the 'prompt' variable.

Check failure on line 486 in packages/core/src/chat.ts

View workflow job for this annotation

GitHub Actions / build

The variable 'prompt' has been renamed to 'userPrompt'. This could potentially break any code that relies on the 'prompt' variable.

Check failure on line 486 in packages/core/src/chat.ts

View workflow job for this annotation

GitHub Actions / build

The variable 'prompt' has been renamed to 'userPrompt'. This could potentially break any code that relies on the 'prompt' variable.
for (const error of errors) trace.error(undefined, error)
needsNewTurn = false
break
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@ export const CONSOLE_COLOR_WARNING = 95
export const CONSOLE_COLOR_ERROR = 91

export const PLAYWRIGHT_DEFAULT_BROWSER = "chromium"
export const MAX_TOKENS_ELLIPSE = "..."
export const ESTIMATE_TOKEN_OVERHEAD = 2
31 changes: 24 additions & 7 deletions packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function callExpander(
const node = ctx.node
if (provider !== MODEL_PROVIDER_AICI) {
const {
prompt,
userPrompt,
assistantPrompt,
images: imgs,
errors,
Expand All @@ -83,8 +83,11 @@ export async function callExpander(
outputProcessors: ops,
chatParticipants: cps,
fileOutputs: fos,
} = await renderPromptNode(model, node, { trace })
text = prompt
} = await renderPromptNode(model, node, {
flexTokens: options.flexTokens,
trace,
})
text = userPrompt
assistantText = assistantPrompt
images = imgs
schemas = schs
Expand Down Expand Up @@ -175,7 +178,7 @@ export async function expandTemplate(
const systems = resolveSystems(prj, template)
const systemTemplates = systems.map((s) => prj.getTemplate(s))
// update options
options.lineNumbers =
const lineNumbers =
options.lineNumbers ??
template.lineNumbers ??
systemTemplates.some((s) => s?.lineNumbers)
Expand All @@ -186,7 +189,7 @@ export async function expandTemplate(
host.defaultModelOptions.temperature
const topP =
options.topP ?? normalizeFloat(env.vars["top_p"]) ?? template.topP
const max_tokens =
const maxTokens =
options.maxTokens ??
normalizeInt(env.vars["maxTokens"]) ??
normalizeInt(env.vars["max_tokens"]) ??
Expand All @@ -197,6 +200,11 @@ export async function expandTemplate(
normalizeInt(env.vars["max_tool_calls"]) ??
template.maxToolCalls ??
MAX_TOOL_CALLS
const flexTokens =
options.flexTokens ??
normalizeInt(env.vars["flexTokens"]) ??
normalizeInt(env.vars["flex_tokens"]) ??
template.flexTokens
let seed = options.seed ?? normalizeInt(env.vars["seed"]) ?? template.seed
if (seed !== undefined) seed = seed >> 0

Expand All @@ -207,7 +215,16 @@ export async function expandTemplate(
trace.startDetails("🧬 prompt")
trace.detailsFenced("📓 script source", template.jsSource, "js")

const prompt = await callExpander(prj, template, env, trace, options)
const prompt = await callExpander(prj, template, env, trace, {
...options,
maxTokens,
maxToolCalls,
flexTokens,
seed,
topP,
temperature,
lineNumbers,
})

const images = prompt.images
const schemas = prompt.schemas
Expand Down Expand Up @@ -339,7 +356,7 @@ ${schemaTs}
model,
temperature,
topP,
max_tokens,
maxTokens,
maxToolCalls,
seed,
responseType,
Expand Down
26 changes: 25 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/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export async function createPromptContext(
messages: msgs,
chatParticipants: cps,
} = await renderPromptNode(genOptions.model, node, {
flexTokens: genOptions.flexTokens,
trace,
})

Expand Down
Loading