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

flexible argument insertion #720

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,7 @@

export const OPENAI_MAX_RETRY_DELAY = 10000
export const OPENAI_MAX_RETRY_COUNT = 10
export const OPENAI_RETRY_DEFAULT_DEFAULT = 1000
export const OPENAI_RETRY_DEFAULT_DEFAULT = 1000

export const TEMPLATE_ARG_FILE_MAX_TOKENS = 4000
export const TEMPLATE_ARG_DATA_SLICE_SAMPLE = 2000

Check failure on line 243 in packages/core/src/constants.ts

View workflow job for this annotation

GitHub Actions / build

Missing newline at the end of file. It's a common convention to end a file with a newline. 📝
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 32 additions & 6 deletions packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
MARKDOWN_PROMPT_FENCE,
MAX_TOKENS_ELLIPSE,
PROMPT_FENCE,
TEMPLATE_ARG_DATA_SLICE_SAMPLE,
TEMPLATE_ARG_FILE_MAX_TOKENS,
} from "./constants"
import { parseModelIdentifier } from "./models"
import { toChatCompletionUserMessage } from "./chat"
Expand Down Expand Up @@ -448,6 +450,17 @@
): Promise<{ errors: number }> {
const encoder = await resolveTokenEncoder(model)
let err = 0
const names = new Set<string>()
const uniqueName = (n_: string) => {
let i = 1
let n = n_
while (names.has(n)) {
n = `${n_}${i++}`
}
names.add(n)
return n

Check failure on line 461 in packages/core/src/promptdom.ts

View workflow job for this annotation

GitHub Actions / build

The `uniqueName` function could potentially result in an infinite loop if all possible names are already in the `names` set. Consider adding a limit to the number of iterations. 🔄
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
}

await visitNode(root, {
error: () => {
err++
Expand All @@ -463,6 +476,7 @@
},
def: async (n) => {
try {
names.add(n.name)
const value = await n.value
n.resolved = value
const rendered = renderDefNode(n)
Expand All @@ -486,6 +500,7 @@
try {
const resolvedStrings = await strings
const resolvedArgs = []

for (const arg of args) {
try {
let ra: any = await arg
Expand All @@ -499,30 +514,41 @@
...(n.children ?? []),
createDef(ra.filename, ra, {
ignoreEmpty: true,
maxTokens: TEMPLATE_ARG_FILE_MAX_TOKENS,
}),

Check failure on line 518 in packages/core/src/promptdom.ts

View workflow job for this annotation

GitHub Actions / build

The `maxTokens` argument is missing in the `createDef` function call. This could lead to unexpected behavior. Please make sure to pass all required arguments. 🧐
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
]
ra = ra.filename
} else if (
// env.files
Array.isArray(ra) &&
ra.every(
(r) => typeof r === "object" && r.filename
)
) {
// env.files
const fname = uniqueName("FILES")
n.children = n.children ?? []
for (const r of ra) {
n.children.push(
createDef("FILES", r, {
createDef(fname, r, {
ignoreEmpty: true,
maxTokens:
TEMPLATE_ARG_FILE_MAX_TOKENS,
})
)
}
ra = "FILES"
ra = fname
} else {
const dname = uniqueName("DATA")
n.children = [
...(n.children ?? []),
createDefData(dname, ra, {
sliceSample:
TEMPLATE_ARG_DATA_SLICE_SAMPLE,
}),
]
ra = dname
}
} else {
ra = inspect(ra, {
maxDepth: DEDENT_INSPECT_MAX_DEPTH,
})
}
resolvedArgs.push(ra ?? "")
} catch (e) {
Expand Down
10 changes: 8 additions & 2 deletions packages/sample/genaisrc/nested-args.genai.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script({
title: "summarize with nested arguments",
model: "openai:gpt-3.5-turbo",
files: "src/rag/markdown.md",
files: ["src/rag/markdown.md", "src/penguins.csv"],
tests: [
{
files: "src/rag/markdown.md",
Expand All @@ -10,4 +10,10 @@ script({
],
})

$`Summarize ${env.files}.`
const data = [
{ a: 1, b: 2 },
{ a: 3, b: 4 },
]

$`Summarize ${env.files[0]} and ${env.files.slice(1)} ${"in"} ${3} ${async () => "sentence"}.
${data}`