{"version":3,"file":"extraction.cjs","names":["PromptTemplate","JsonKeyOutputFunctionsParser","LLMChain"],"sources":["../../../src/chains/openai_functions/extraction.ts"],"sourcesContent":["import { BaseChatModel } from \"@langchain/core/language_models/chat_models\";\nimport { BaseFunctionCallOptions } from \"@langchain/core/language_models/base\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { InteropZodObject } from \"@langchain/core/utils/types\";\nimport type { AIMessageChunk } from \"@langchain/core/messages\";\nimport {\n  type JsonSchema7ObjectType,\n  toJsonSchema,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n  FunctionParameters,\n  JsonKeyOutputFunctionsParser,\n} from \"../../output_parsers/openai_functions.js\";\nimport { LLMChain } from \"../llm_chain.js\";\n\n/**\n * Function that returns an array of extraction functions. These functions\n * are used to extract relevant information from a passage.\n * @param schema The schema of the function parameters.\n * @returns An array of extraction functions.\n */\nfunction getExtractionFunctions(schema: FunctionParameters) {\n  return [\n    {\n      name: \"information_extraction\",\n      description: \"Extracts the relevant information from the passage.\",\n      parameters: {\n        type: \"object\",\n        properties: {\n          info: {\n            type: \"array\",\n            items: {\n              type: schema.type,\n              properties: schema.properties,\n              required: schema.required,\n            },\n          },\n        },\n        required: [\"info\"],\n      },\n    },\n  ];\n}\n\nconst _EXTRACTION_TEMPLATE = `Extract and save the relevant entities mentioned in the following passage together with their properties.\n\nPassage:\n{input}\n`;\n\n/**\n * Function that creates an extraction chain using the provided JSON schema.\n * It sets up the necessary components, such as the prompt, output parser, and tags.\n * @param schema JSON schema of the function parameters.\n * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.\n * @returns A LLMChain instance configured to return data matching the schema.\n */\nexport function createExtractionChain(\n  schema: FunctionParameters,\n  llm: BaseChatModel<BaseFunctionCallOptions>\n): LLMChain<object, BaseChatModel<BaseFunctionCallOptions, AIMessageChunk>> {\n  const functions = getExtractionFunctions(schema);\n  const prompt = PromptTemplate.fromTemplate(_EXTRACTION_TEMPLATE);\n  const outputParser = new JsonKeyOutputFunctionsParser({ attrName: \"info\" });\n  return new LLMChain({\n    llm,\n    prompt,\n    llmKwargs: { functions },\n    outputParser,\n    tags: [\"openai_functions\", \"extraction\"],\n  });\n}\n\n/**\n * Function that creates an extraction chain from a Zod schema. It\n * converts the Zod schema to a JSON schema using before creating\n * the extraction chain.\n * @param schema The Zod schema which extracted data should match\n * @param llm Must be a ChatOpenAI or AnthropicFunctions model that supports function calling.\n * @returns A LLMChain instance configured to return data matching the schema.\n */\nexport function createExtractionChainFromZod(\n  schema: InteropZodObject,\n  llm: BaseChatModel<BaseFunctionCallOptions>\n): LLMChain<object, BaseChatModel<BaseFunctionCallOptions, AIMessageChunk>> {\n  return createExtractionChain(\n    toJsonSchema(schema) as JsonSchema7ObjectType,\n    llm\n  );\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,SAAS,uBAAuB,QAA4B;AAC1D,QAAO,CACL;EACE,MAAM;EACN,aAAa;EACb,YAAY;GACV,MAAM;GACN,YAAY,EACV,MAAM;IACJ,MAAM;IACN,OAAO;KACL,MAAM,OAAO;KACb,YAAY,OAAO;KACnB,UAAU,OAAO;KAClB;IACF,EACF;GACD,UAAU,CAAC,OAAO;GACnB;EACF,CACF;;AAGH,MAAM,uBAAuB;;;;;;;;;;;;AAa7B,SAAgB,sBACd,QACA,KAC0E;CAC1E,MAAM,YAAY,uBAAuB,OAAO;CAChD,MAAM,SAASA,wBAAAA,eAAe,aAAa,qBAAqB;CAChE,MAAM,eAAe,IAAIC,yBAAAA,6BAA6B,EAAE,UAAU,QAAQ,CAAC;AAC3E,QAAO,IAAIC,kBAAAA,SAAS;EAClB;EACA;EACA,WAAW,EAAE,WAAW;EACxB;EACA,MAAM,CAAC,oBAAoB,aAAa;EACzC,CAAC;;;;;;;;;;AAWJ,SAAgB,6BACd,QACA,KAC0E;AAC1E,QAAO,uBAAA,GAAA,kCAAA,cACQ,OAAO,EACpB,IACD"}