Skip to content

Commit

Permalink
Add template argument constants and unique name handling in prompt re…
Browse files Browse the repository at this point in the history
…solution
  • Loading branch information
pelikhan committed Sep 20, 2024
1 parent 9938676 commit ad9927b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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 DEDENT_INSPECT_MAX_DEPTH = 3

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 = 1000
export const TEMPLATE_ARG_DATA_SLICE_SAMPLE = 1000
35 changes: 30 additions & 5 deletions packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
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 @@ async function resolvePromptNode(
): Promise<{ errors: number }> {
const encoder = await resolveTokenEncoder(model)
let err = 0
const names = new Set<string>()
const uniqueName = (name: string) => {
let i = 1
let n = name
while (names.has(n)) {
n = `${name}${i++}`
}
names.add(n)
return n
}

await visitNode(root, {
error: () => {
err++
Expand All @@ -463,6 +476,7 @@ async function resolvePromptNode(
},
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 @@ async function resolvePromptNode(
try {
const resolvedStrings = await strings
const resolvedArgs = []

for (const arg of args) {
try {
let ra: any = await arg
Expand All @@ -499,30 +514,40 @@ async function resolvePromptNode(
...(n.children ?? []),
createDef(ra.filename, ra, {
ignoreEmpty: true,
maxTokens: TEMPLATE_ARG_FILE_MAX_TOKENS,
}),
]
ra = ra.filename
} else if (
// env.files
Array.isArray(ra) &&
ra.every(
(r) => typeof r === "object" && r.filename
)
) {
// env.files
const name = uniqueName("FILES")
n.children = n.children ?? []
for (const r of ra) {
n.children.push(
createDef("FILES", r, {
createDef(name, r, {
ignoreEmpty: true,
maxTokens:
TEMPLATE_ARG_FILE_MAX_TOKENS,
})
)
}
ra = "FILES"
ra = name
}
} else {
ra = inspect(ra, {
maxDepth: DEDENT_INSPECT_MAX_DEPTH,
})
const name = uniqueName("DATA")
n.children = [
...(n.children ?? []),
createDefData(name, ra, {
sliceSample: TEMPLATE_ARG_DATA_SLICE_SAMPLE,
}),
]
ra = name
}
resolvedArgs.push(ra ?? "")
} catch (e) {
Expand Down

0 comments on commit ad9927b

Please sign in to comment.