diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 525427e59e..0bb5b9dfdb 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -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 \ No newline at end of file +export const OPENAI_RETRY_DEFAULT_DEFAULT = 1000 + +export const TEMPLATE_ARG_FILE_MAX_TOKENS = 1000 +export const TEMPLATE_ARG_DATA_SLICE_SAMPLE = 1000 \ No newline at end of file diff --git a/packages/core/src/promptdom.ts b/packages/core/src/promptdom.ts index 56197feea2..2808c24e79 100644 --- a/packages/core/src/promptdom.ts +++ b/packages/core/src/promptdom.ts @@ -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" @@ -448,6 +450,17 @@ async function resolvePromptNode( ): Promise<{ errors: number }> { const encoder = await resolveTokenEncoder(model) let err = 0 + const names = new Set() + 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++ @@ -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) @@ -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 @@ -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) {