{"version":3,"sources":["../../../src/adapters/shared/llmChatTemplates.ts"],"names":["messagesToPromptFactory","rolesOverride","roles","pickBy","system","user","assistant","isDefined","template","toBoundedFunction","messages","render","map","message","Object","fromEntries","entries","key","role","text","name","value","templateSchemaFactory","z","object","array","mapToObj","string","llama31","PromptTemplate","schema","messagesToPrompt","ipython","parameters","stop_sequence","llama33","llama3","granite31Instruct","tools","tool_response","tool_call","LLMChatTemplates","registry","register","model","override","ValueError","has","Boolean","get","context","validModels","keys"],"mappings":";;;;;;;;;;AAiCO,SAASA,uBAAAA,CAAwBC,aAAoD,GAAA,EAAE,EAAA;AAC5F,EAAA,MAAMC,QAAgCC,aACpC,CAAA;IACEC,MAAQ,EAAA,QAAA;IACRC,IAAM,EAAA,MAAA;IACNC,SAAW,EAAA,WAAA;IACX,GAAGL;AACL,GAAA,EACAM,gBAAAA,CAAAA;AAGF,EAAA,OAAO,CAACC,QAAAA,KAAAA;AACN,IAAOC,OAAAA,2BAAAA,CACL,CAACC,QAAAA,KAAAA;AACC,MAAA,OAAOF,SAASG,MAAO,CAAA;AACrBD,QAAAA,QAAAA,EAAUA,SAASE,GAAI,CAAA,CAACC,YACtBC,MAAOC,CAAAA,WAAAA,CACLD,OAAOE,OAAQd,CAAAA,KAAAA,CAAOU,CAAAA,GAAAA,CAAI,CAAC,CAACK,GAAAA,EAAKC,IAAAA,CAC/BL,KAAAA,OAAAA,CAAQK,SAASA,IAAO,GAAA;AAACD,UAAAA,GAAAA;AAAK,UAAA;YAACJ,OAAQM,CAAAA;;AAAS,SAAA,GAAA;AAACF,UAAAA,GAAAA;UAAK;AAAG,SAAA,CAAA,CAAA;OAIjE,CAAA;KAEF,EAAA;AACE,MAAA;QACEG,IAAM,EAAA,UAAA;QACNC,KAAOb,EAAAA;AACT,OAAA;AACA,MAAA;QACEY,IAAM,EAAA,OAAA;QACNC,KAAOnB,EAAAA;AACT;AACD,KAAA,CAAA;AAEL,GAAA;AACF;AApCgBF,MAAAA,CAAAA,uBAA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AAA;AACpB,MAAA,MAAM,IAAIE,qBAAW,CAAA,CAAA,oBAAA,EAAuBF,KAAAA,CAAAA,YAAAA,CAAAA,EAAqB,EAAI,EAAA;QACnEM,OAAS,EAAA;UACPC,WAAarC,EAAAA,MAAAA,CAAOsC,IAAK,CAAA,IAAA,CAAKV,QAAQ;AACxC;OACF,CAAA;AACF;AACA,IAAO,OAAA,IAAA,CAAKA,SAASE,KAAAA,CAAAA;AACvB;AACF","file":"llmChatTemplates.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate } from \"@/template.js\";\nimport { BaseMessage } from \"@/llms/primitives/message.js\";\nimport { ValueError } from \"@/errors.js\";\nimport { isDefined, mapToObj, pickBy } from \"remeda\";\nimport { z } from \"zod\";\nimport { toBoundedFunction } from \"@/serializer/utils.js\";\n\nexport type LLMChatPromptTemplate = PromptTemplate.infer<{ messages: Record<string, string[]>[] }>;\n\nexport interface LLMChatTemplate {\n  template: LLMChatPromptTemplate;\n  messagesToPrompt: (template: LLMChatPromptTemplate) => (messages: BaseMessage[]) => string;\n  parameters: {\n    stop_sequence: string[];\n  };\n}\n\nexport function messagesToPromptFactory(rolesOverride: Record<string, string | undefined> = {}) {\n  const roles: Record<string, string> = pickBy(\n    {\n      system: \"system\",\n      user: \"user\",\n      assistant: \"assistant\",\n      ...rolesOverride,\n    },\n    isDefined,\n  );\n\n  return (template: LLMChatPromptTemplate) => {\n    return toBoundedFunction(\n      (messages: BaseMessage[]) => {\n        return template.render({\n          messages: messages.map((message) =>\n            Object.fromEntries(\n              Object.entries(roles).map(([key, role]) =>\n                message.role === role ? [key, [message.text]] : [key, []],\n              ),\n            ),\n          ),\n        });\n      },\n      [\n        {\n          name: \"template\",\n          value: template,\n        },\n        {\n          name: \"roles\",\n          value: roles,\n        },\n      ],\n    );\n  };\n}\n\nexport function templateSchemaFactory(roles: readonly string[]) {\n  return z.object({\n    messages: z.array(z.object(mapToObj(roles, (role) => [role, z.array(z.string())] as const))),\n  });\n}\n\nconst llama31: LLMChatTemplate = {\n  template: new PromptTemplate({\n    schema: templateSchemaFactory([\"system\", \"user\", \"assistant\", \"ipython\"] as const),\n    template: `{{#messages}}{{#system}}<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{{system}}<|eot_id|>{{/system}}{{#user}}<|start_header_id|>user<|end_header_id|>\n\n{{user}}<|eot_id|>{{/user}}{{#assistant}}<|start_header_id|>assistant<|end_header_id|>\n\n{{assistant}}<|eot_id|>{{/assistant}}{{#ipython}}<|start_header_id|>ipython<|end_header_id|>\n\n{{ipython}}<|eot_id|>{{/ipython}}{{/messages}}<|start_header_id|>assistant<|end_header_id|>\n\n`,\n  }),\n  messagesToPrompt: messagesToPromptFactory({ ipython: \"ipython\" }),\n  parameters: {\n    stop_sequence: [\"<|eot_id|>\"],\n  },\n};\n\nconst llama33: LLMChatTemplate = llama31;\n\nconst llama3: LLMChatTemplate = {\n  template: new PromptTemplate({\n    schema: templateSchemaFactory([\"system\", \"user\", \"assistant\"] as const),\n    template: `{{#messages}}{{#system}}<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{{system}}<|eot_id|>{{/system}}{{#user}}<|start_header_id|>user<|end_header_id|>\n\n{{user}}<|eot_id|>{{/user}}{{#assistant}}<|start_header_id|>assistant<|end_header_id|>\n\n{{assistant}}<|eot_id|>{{/assistant}}{{/messages}}<|start_header_id|>assistant<|end_header_id|>\n`,\n  }),\n  messagesToPrompt: messagesToPromptFactory(),\n  parameters: {\n    stop_sequence: [\"<|eot_id|>\"],\n  },\n};\n\nconst granite31Instruct: LLMChatTemplate = {\n  template: new PromptTemplate({\n    schema: templateSchemaFactory([\n      \"system\",\n      \"user\",\n      \"assistant\",\n      \"tools\",\n      \"tool_call\",\n      \"tool_response\",\n    ] as const),\n    template: `{{#messages}}{{#system}}<|start_of_role|>system<|end_of_role|>\n{{system}}<|end_of_text|>\n{{ end }}{{/system}}{{#tools}}<|start_of_role|>tools<|end_of_role|>\n{{tools}}<|end_of_text|>\n{{ end }}{{/tools}}{{#user}}<|start_of_role|>user<|end_of_role|>\n{{user}}<|end_of_text|>\n{{ end }}{{/user}}{{#assistant}}<|start_of_role|>assistant<|end_of_role|>\n{{assistant}}<|end_of_text|>\n{{ end }}{{/assistant}}{{#tool_call}}<|start_of_role|>assistant<|end_of_role|><|tool_call|>\n{{tool_call}}<|end_of_text|>\n{{ end }}{{/tool_call}}{{#tool_response}}<|start_of_role|>tool_response<|end_of_role|>\n{{tool_response}}<|end_of_text|>\n{{ end }}{{/tool_response}}{{/messages}}<|start_of_role|>assistant<|end_of_role|>\n`,\n  }),\n  messagesToPrompt: messagesToPromptFactory({\n    tools: \"tools\",\n    tool_response: \"tool_response\",\n    tool_call: \"tool_call\",\n  }),\n  parameters: {\n    stop_sequence: [\"<|end_of_text|>\"],\n  },\n};\n\nexport class LLMChatTemplates {\n  protected static readonly registry = {\n    \"llama3.3\": llama33,\n    \"llama3.1\": llama31,\n    \"llama3\": llama3,\n    \"granite3.1-Instruct\": granite31Instruct,\n  };\n\n  static register(model: string, template: LLMChatTemplate, override = false) {\n    if (model in this.registry && !override) {\n      throw new ValueError(`Template for model '${model}' already exists!`);\n    }\n    this.registry[model as keyof typeof this.registry] = template;\n  }\n\n  static has(model: string): boolean {\n    return Boolean(model && model in this.registry);\n  }\n\n  static get(model: keyof typeof LLMChatTemplates.registry): LLMChatTemplate;\n  // eslint-disable-next-line @typescript-eslint/unified-signatures\n  static get(model: string): LLMChatTemplate;\n  static get(model: string): LLMChatTemplate {\n    if (!this.has(model)) {\n      throw new ValueError(`Template for model '${model}' not found!`, [], {\n        context: {\n          validModels: Object.keys(this.registry),\n        },\n      });\n    }\n    return this.registry[model as keyof typeof this.registry];\n  }\n}\n"]}