{"version":3,"file":"tagging.cjs","names":["PromptTemplate","JsonOutputFunctionsParser","LLMChain"],"sources":["../../../src/chains/openai_functions/tagging.ts"],"sourcesContent":["import { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { BaseFunctionCallOptions } from \"@langchain/core/language_models/base\";\nimport { AIMessageChunk } from \"@langchain/core/messages\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { InteropZodObject } from \"@langchain/core/utils/types\";\nimport {\n  type JsonSchema7ObjectType,\n  toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n  FunctionParameters,\n  JsonOutputFunctionsParser,\n} from \"../../output_parsers/openai_functions.js\";\nimport { LLMChain, LLMChainInput } from \"../llm_chain.js\";\n\n/**\n * Type representing the options for creating a tagging chain.\n */\nexport type TaggingChainOptions = {\n  prompt?: PromptTemplate;\n} & Omit<LLMChainInput<object>, \"prompt\" | \"llm\">;\n\n/**\n * Function that returns an array of tagging functions. These functions\n * are used to extract relevant information from a passage.\n * @param schema The schema defining the structure of function parameters.\n * @returns An array of tagging functions.\n */\nfunction getTaggingFunctions(schema: FunctionParameters) {\n  return [\n    {\n      name: \"information_extraction\",\n      description: \"Extracts the relevant information from the passage.\",\n      parameters: schema,\n    },\n  ];\n}\n\nconst TAGGING_TEMPLATE = `Extract the desired information from the following passage.\n\nPassage:\n{input}\n`;\n\n/**\n * Function that creates a tagging chain using the provided schema,\n * LLM, and options. It constructs the LLM with the necessary\n * functions, prompt, output parser, and tags.\n * @param schema The schema defining the structure of function parameters.\n * @param llm LLM to use in the chain. Must support function calling.\n * @param options Options for creating the tagging chain.\n * @returns A new instance of LLMChain configured for tagging.\n *\n * @deprecated\n * Switch to expression language: https://js.langchain.com/docs/expression_language/\n * Will be removed in 0.2.0\n */\nexport function createTaggingChain(\n  schema: FunctionParameters,\n  llm: BaseChatModel<BaseFunctionCallOptions>,\n  options: TaggingChainOptions = {}\n): LLMChain<object, BaseChatModel<BaseFunctionCallOptions, AIMessageChunk>> {\n  const { prompt = PromptTemplate.fromTemplate(TAGGING_TEMPLATE), ...rest } =\n    options;\n  const functions = getTaggingFunctions(schema);\n  const outputParser = new JsonOutputFunctionsParser();\n  return new LLMChain({\n    llm,\n    prompt,\n    llmKwargs: { functions },\n    outputParser,\n    tags: [\"openai_functions\", \"tagging\"],\n    ...rest,\n  });\n}\n\n/**\n * Function that creates a tagging chain from a Zod schema. It converts\n * the Zod schema to a JSON schema using the zodToJsonSchema function and\n * then calls createTaggingChain with the converted schema.\n * @param schema The Zod schema which extracted data should match.\n * @param llm LLM to use in the chain. Must support function calling.\n * @param options Options for creating the tagging chain.\n * @returns A new instance of LLMChain configured for tagging.\n *\n * @deprecated\n * Switch to expression language: https://js.langchain.com/docs/expression_language/\n * Will be removed in 0.2.0\n */\nexport function createTaggingChainFromZod(\n  schema: InteropZodObject,\n  llm: BaseChatModel<BaseFunctionCallOptions>,\n  options?: TaggingChainOptions\n): LLMChain<object, BaseChatModel<BaseFunctionCallOptions, AIMessageChunk>> {\n  return createTaggingChain(\n    toJsonSchema(schema) as JsonSchema7ObjectType,\n    llm,\n    options\n  );\n}\n"],"mappings":";;;;;;;;;;;;AA4BA,SAAS,oBAAoB,QAA4B;AACvD,QAAO,CACL;EACE,MAAM;EACN,aAAa;EACb,YAAY;EACb,CACF;;AAGH,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;AAmBzB,SAAgB,mBACd,QACA,KACA,UAA+B,EAAE,EACyC;CAC1E,MAAM,EAAE,SAASA,wBAAAA,eAAe,aAAa,iBAAiB,EAAE,GAAG,SACjE;CACF,MAAM,YAAY,oBAAoB,OAAO;CAC7C,MAAM,eAAe,IAAIC,yBAAAA,2BAA2B;AACpD,QAAO,IAAIC,kBAAAA,SAAS;EAClB;EACA;EACA,WAAW,EAAE,WAAW;EACxB;EACA,MAAM,CAAC,oBAAoB,UAAU;EACrC,GAAG;EACJ,CAAC;;;;;;;;;;;;;;;AAgBJ,SAAgB,0BACd,QACA,KACA,SAC0E;AAC1E,QAAO,oBAAA,GAAA,kCAAA,cACQ,OAAO,EACpB,KACA,QACD"}