9a61041e509576e9fee0f44c8616d59e7918617ff478d992bd209b78fa16a7f5.json 18 KB

1
  1. {"ast":null,"code":"import mustache from \"mustache\";\nimport { addLangChainErrorFields } from \"../errors/index.js\";\nfunction configureMustache() {\n // Use unescaped HTML\n // https://github.com/janl/mustache.js?tab=readme-ov-file#variables\n mustache.escape = text => text;\n}\nexport const parseFString = template => {\n // Core logic replicated from internals of pythons built in Formatter class.\n // https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706\n const chars = template.split(\"\");\n const nodes = [];\n const nextBracket = (bracket, start) => {\n for (let i = start; i < chars.length; i += 1) {\n if (bracket.includes(chars[i])) {\n return i;\n }\n }\n return -1;\n };\n let i = 0;\n while (i < chars.length) {\n if (chars[i] === \"{\" && i + 1 < chars.length && chars[i + 1] === \"{\") {\n nodes.push({\n type: \"literal\",\n text: \"{\"\n });\n i += 2;\n } else if (chars[i] === \"}\" && i + 1 < chars.length && chars[i + 1] === \"}\") {\n nodes.push({\n type: \"literal\",\n text: \"}\"\n });\n i += 2;\n } else if (chars[i] === \"{\") {\n const j = nextBracket(\"}\", i);\n if (j < 0) {\n throw new Error(\"Unclosed '{' in template.\");\n }\n nodes.push({\n type: \"variable\",\n name: chars.slice(i + 1, j).join(\"\")\n });\n i = j + 1;\n } else if (chars[i] === \"}\") {\n throw new Error(\"Single '}' in template.\");\n } else {\n const next = nextBracket(\"{}\", i);\n const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join(\"\");\n nodes.push({\n type: \"literal\",\n text\n });\n i = next < 0 ? chars.length : next;\n }\n }\n return nodes;\n};\n/**\n * Convert the result of mustache.parse into an array of ParsedTemplateNode,\n * to make it compatible with other LangChain string parsing template formats.\n *\n * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.\n * @returns {ParsedTemplateNode[]}\n */\nconst mustacheTemplateToNodes = template => template.map(temp => {\n if (temp[0] === \"name\") {\n const name = temp[1].includes(\".\") ? temp[1].split(\".\")[0] : temp[1];\n return {\n type: \"variable\",\n name\n };\n } else if ([\"#\", \"&\", \"^\", \">\"].includes(temp[0])) {\n // # represents a section, \"&\" represents an unescaped variable.\n // These should both be considered variables.\n return {\n type: \"variable\",\n name: temp[1]\n };\n } else {\n return {\n type: \"literal\",\n text: temp[1]\n };\n }\n});\nexport const parseMustache = template => {\n configureMustache();\n const parsed = mustache.parse(template);\n return mustacheTemplateToNodes(parsed);\n};\nexport const interpolateFString = (template, values) => {\n return parseFString(template).reduce((res, node) => {\n if (node.type === \"variable\") {\n if (node.name in values) {\n const stringValue = typeof values[node.name] === \"string\" ? values[node.name] : JSON.stringify(values[node.name]);\n return res + stringValue;\n }\n throw new Error(`(f-string) Missing value for input ${node.name}`);\n }\n return res + node.text;\n }, \"\");\n};\nexport const interpolateMustache = (template, values) => {\n configureMustache();\n return mustache.render(template, values);\n};\nexport const DEFAULT_FORMATTER_MAPPING = {\n \"f-string\": interpolateFString,\n mustache: interpolateMustache\n};\nexport const DEFAULT_PARSER_MAPPING = {\n \"f-string\": parseFString,\n mustache: parseMustache\n};\nexport const renderTemplate = (template, templateFormat, inputValues) => {\n try {\n return DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);\n } catch (e) {\n const error = addLangChainErrorFields(e, \"INVALID_PROMPT_INPUT\");\n throw error;\n }\n};\nexport const parseTemplate = (template, templateFormat) => DEFAULT_PARSER_MAPPING[templateFormat](template);\nexport const checkValidTemplate = (template, templateFormat, inputVariables) => {\n if (!(templateFormat in DEFAULT_FORMATTER_MAPPING)) {\n const validFormats = Object.keys(DEFAULT_FORMATTER_MAPPING);\n throw new Error(`Invalid template format. Got \\`${templateFormat}\\`;\n should be one of ${validFormats}`);\n }\n try {\n const dummyInputs = inputVariables.reduce((acc, v) => {\n acc[v] = \"foo\";\n return acc;\n }, {});\n if (Array.isArray(template)) {\n template.forEach(message => {\n if (message.type === \"text\") {\n renderTemplate(message.text, templateFormat, dummyInputs);\n } else if (message.type === \"image_url\") {\n if (typeof message.image_url === \"string\") {\n renderTemplate(message.image_url, templateFormat, dummyInputs);\n } else {\n const imageUrl = message.image_url.url;\n renderTemplate(imageUrl, templateFormat, dummyInputs);\n }\n } else {\n throw new Error(`Invalid message template received. ${JSON.stringify(message, null, 2)}`);\n }\n });\n } else {\n renderTemplate(template, templateFormat, dummyInputs);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e) {\n throw new Error(`Invalid prompt schema: ${e.message}`);\n }\n};","map":{"version":3,"names":["mustache","addLangChainErrorFields","configureMustache","escape","text","parseFString","template","chars","split","nodes","nextBracket","bracket","start","i","length","includes","push","type","j","Error","name","slice","join","next","mustacheTemplateToNodes","map","temp","parseMustache","parsed","parse","interpolateFString","values","reduce","res","node","stringValue","JSON","stringify","interpolateMustache","render","DEFAULT_FORMATTER_MAPPING","DEFAULT_PARSER_MAPPING","renderTemplate","templateFormat","inputValues","e","error","parseTemplate","checkValidTemplate","inputVariables","validFormats","Object","keys","dummyInputs","acc","v","Array","isArray","forEach","message","image_url","imageUrl","url"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/@langchain/core/dist/prompts/template.js"],"sourcesContent":["import mustache from \"mustache\";\nimport { addLangChainErrorFields } from \"../errors/index.js\";\nfunction configureMustache() {\n // Use unescaped HTML\n // https://github.com/janl/mustache.js?tab=readme-ov-file#variables\n mustache.escape = (text) => text;\n}\nexport const parseFString = (template) => {\n // Core logic replicated from internals of pythons built in Formatter class.\n // https://github.com/python/cpython/blob/135ec7cefbaffd516b77362ad2b2ad1025af462e/Objects/stringlib/unicode_format.h#L700-L706\n const chars = template.split(\"\");\n const nodes = [];\n const nextBracket = (bracket, start) => {\n for (let i = start; i < chars.length; i += 1) {\n if (bracket.includes(chars[i])) {\n return i;\n }\n }\n return -1;\n };\n let i = 0;\n while (i < chars.length) {\n if (chars[i] === \"{\" && i + 1 < chars.length && chars[i + 1] === \"{\") {\n nodes.push({ type: \"literal\", text: \"{\" });\n i += 2;\n }\n else if (chars[i] === \"}\" &&\n i + 1 < chars.length &&\n chars[i + 1] === \"}\") {\n nodes.push({ type: \"literal\", text: \"}\" });\n i += 2;\n }\n else if (chars[i] === \"{\") {\n const j = nextBracket(\"}\", i);\n if (j < 0) {\n throw new Error(\"Unclosed '{' in template.\");\n }\n nodes.push({\n type: \"variable\",\n name: chars.slice(i + 1, j).join(\"\"),\n });\n i = j + 1;\n }\n else if (chars[i] === \"}\") {\n throw new Error(\"Single '}' in template.\");\n }\n else {\n const next = nextBracket(\"{}\", i);\n const text = (next < 0 ? chars.slice(i) : chars.slice(i, next)).join(\"\");\n nodes.push({ type: \"literal\", text });\n i = next < 0 ? chars.length : next;\n }\n }\n return nodes;\n};\n/**\n * Convert the result of mustache.parse into an array of ParsedTemplateNode,\n * to make it compatible with other LangChain string parsing template formats.\n *\n * @param {mustache.TemplateSpans} template The result of parsing a mustache template with the mustache.js library.\n * @returns {ParsedTemplateNode[]}\n */\nconst mustacheTemplateToNodes = (template) => template.map((temp) => {\n if (temp[0] === \"name\") {\n const name = temp[1].includes(\".\") ? temp[1].split(\".\")[0] : temp[1];\n return { type: \"variable\", name };\n }\n else if ([\"#\", \"&\", \"^\", \">\"].includes(temp[0])) {\n // # represents a section, \"&\" represents an unescaped variable.\n // These should both be considered variables.\n return { type: \"variable\", name: temp[1] };\n }\n else {\n return { type: \"literal\", text: temp[1] };\n }\n});\nexport const parseMustache = (template) => {\n configureMustache();\n const parsed = mustache.parse(template);\n return mustacheTemplateToNodes(parsed);\n};\nexport const interpolateFString = (template, values) => {\n return parseFString(template).reduce((res, node) => {\n if (node.type === \"variable\") {\n if (node.name in values) {\n const stringValue = typeof values[node.name] === \"string\"\n ? values[node.name]\n : JSON.stringify(values[node.name]);\n return res + stringValue;\n }\n throw new Error(`(f-string) Missing value for input ${node.name}`);\n }\n return res + node.text;\n }, \"\");\n};\nexport const interpolateMustache = (template, values) => {\n configureMustache();\n return mustache.render(template, values);\n};\nexport const DEFAULT_FORMATTER_MAPPING = {\n \"f-string\": interpolateFString,\n mustache: interpolateMustache,\n};\nexport const DEFAULT_PARSER_MAPPING = {\n \"f-string\": parseFString,\n mustache: parseMustache,\n};\nexport const renderTemplate = (template, templateFormat, inputValues) => {\n try {\n return DEFAULT_FORMATTER_MAPPING[templateFormat](template, inputValues);\n }\n catch (e) {\n const error = addLangChainErrorFields(e, \"INVALID_PROMPT_INPUT\");\n throw error;\n }\n};\nexport const parseTemplate = (template, templateFormat) => DEFAULT_PARSER_MAPPING[templateFormat](template);\nexport const checkValidTemplate = (template, templateFormat, inputVariables) => {\n if (!(templateFormat in DEFAULT_FORMATTER_MAPPING)) {\n const validFormats = Object.keys(DEFAULT_FORMATTER_MAPPING);\n throw new Error(`Invalid template format. Got \\`${templateFormat}\\`;\n should be one of ${validFormats}`);\n }\n try {\n const dummyInputs = inputVariables.reduce((acc, v) => {\n acc[v] = \"foo\";\n return acc;\n }, {});\n if (Array.isArray(template)) {\n template.forEach((message) => {\n if (message.type === \"text\") {\n renderTemplate(message.text, templateFormat, dummyInputs);\n }\n else if (message.type === \"image_url\") {\n if (typeof message.image_url === \"string\") {\n renderTemplate(message.image_url, templateFormat, dummyInputs);\n }\n else {\n const imageUrl = message.image_url.url;\n renderTemplate(imageUrl, templateFormat, dummyInputs);\n }\n }\n else {\n throw new Error(`Invalid message template received. ${JSON.stringify(message, null, 2)}`);\n }\n });\n }\n else {\n renderTemplate(template, templateFormat, dummyInputs);\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n }\n catch (e) {\n throw new Error(`Invalid prompt schema: ${e.message}`);\n }\n};\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,UAAU;AAC/B,SAASC,uBAAuB,QAAQ,oBAAoB;AAC5D,SAASC,iBAAiBA,CAAA,EAAG;EACzB;EACA;EACAF,QAAQ,CAACG,MAAM,GAAIC,IAAI,IAAKA,IAAI;AACpC;AACA,OAAO,MAAMC,YAAY,GAAIC,QAAQ,IAAK;EACtC;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