@langchain/classic 1.0.34 → 1.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/agents/chat/index.cjs.map +1 -1
  3. package/dist/agents/chat/index.js.map +1 -1
  4. package/dist/agents/chat/outputParser.cjs.map +1 -1
  5. package/dist/agents/chat/outputParser.d.cts +2 -2
  6. package/dist/agents/chat/outputParser.js.map +1 -1
  7. package/dist/agents/chat_convo/index.cjs.map +1 -1
  8. package/dist/agents/chat_convo/index.js.map +1 -1
  9. package/dist/agents/chat_convo/outputParser.cjs.map +1 -1
  10. package/dist/agents/chat_convo/outputParser.js.map +1 -1
  11. package/dist/agents/executor.cjs.map +1 -1
  12. package/dist/agents/executor.js.map +1 -1
  13. package/dist/agents/format_scratchpad/log.cjs.map +1 -1
  14. package/dist/agents/format_scratchpad/log.js.map +1 -1
  15. package/dist/agents/format_scratchpad/log_to_message.cjs.map +1 -1
  16. package/dist/agents/format_scratchpad/log_to_message.js.map +1 -1
  17. package/dist/agents/initialize.cjs.map +1 -1
  18. package/dist/agents/initialize.js.map +1 -1
  19. package/dist/agents/mrkl/index.cjs.map +1 -1
  20. package/dist/agents/mrkl/index.js.map +1 -1
  21. package/dist/agents/mrkl/outputParser.cjs.map +1 -1
  22. package/dist/agents/mrkl/outputParser.d.cts +1 -1
  23. package/dist/agents/mrkl/outputParser.js.map +1 -1
  24. package/dist/agents/openai_functions/index.cjs.map +1 -1
  25. package/dist/agents/openai_functions/index.js.map +1 -1
  26. package/dist/agents/openai_tools/index.cjs.map +1 -1
  27. package/dist/agents/openai_tools/index.js.map +1 -1
  28. package/dist/agents/react/index.cjs.map +1 -1
  29. package/dist/agents/react/index.js.map +1 -1
  30. package/dist/agents/react/output_parser.cjs.map +1 -1
  31. package/dist/agents/react/output_parser.js.map +1 -1
  32. package/dist/agents/structured_chat/index.cjs.map +1 -1
  33. package/dist/agents/structured_chat/index.js.map +1 -1
  34. package/dist/agents/structured_chat/outputParser.cjs.map +1 -1
  35. package/dist/agents/structured_chat/outputParser.js.map +1 -1
  36. package/dist/agents/tool_calling/index.cjs.map +1 -1
  37. package/dist/agents/tool_calling/index.js.map +1 -1
  38. package/dist/agents/toolkits/conversational_retrieval/openai_functions.cjs.map +1 -1
  39. package/dist/agents/toolkits/conversational_retrieval/openai_functions.js.map +1 -1
  40. package/dist/agents/toolkits/conversational_retrieval/tool.cjs.map +1 -1
  41. package/dist/agents/toolkits/conversational_retrieval/tool.js.map +1 -1
  42. package/dist/agents/toolkits/json/json.cjs.map +1 -1
  43. package/dist/agents/toolkits/json/json.js.map +1 -1
  44. package/dist/agents/toolkits/openapi/openapi.cjs.map +1 -1
  45. package/dist/agents/toolkits/openapi/openapi.js.map +1 -1
  46. package/dist/agents/toolkits/sql/sql.cjs.map +1 -1
  47. package/dist/agents/toolkits/sql/sql.js.map +1 -1
  48. package/dist/agents/toolkits/vectorstore/vectorstore.cjs.map +1 -1
  49. package/dist/agents/toolkits/vectorstore/vectorstore.js.map +1 -1
  50. package/dist/agents/xml/index.cjs.map +1 -1
  51. package/dist/agents/xml/index.js.map +1 -1
  52. package/dist/chains/analyze_documents_chain.cjs.map +1 -1
  53. package/dist/chains/analyze_documents_chain.js.map +1 -1
  54. package/dist/chains/api/api_chain.cjs.map +1 -1
  55. package/dist/chains/api/api_chain.js.map +1 -1
  56. package/dist/chains/api/prompts.js.map +1 -1
  57. package/dist/chains/base.cjs.map +1 -1
  58. package/dist/chains/base.js.map +1 -1
  59. package/dist/chains/chat_vector_db_chain.cjs.map +1 -1
  60. package/dist/chains/chat_vector_db_chain.js.map +1 -1
  61. package/dist/chains/combine_docs_chain.cjs.map +1 -1
  62. package/dist/chains/combine_docs_chain.js.map +1 -1
  63. package/dist/chains/combine_documents/base.cjs.map +1 -1
  64. package/dist/chains/combine_documents/base.js.map +1 -1
  65. package/dist/chains/combine_documents/reduce.cjs.map +1 -1
  66. package/dist/chains/combine_documents/reduce.js.map +1 -1
  67. package/dist/chains/constitutional_ai/constitutional_prompts.cjs.map +1 -1
  68. package/dist/chains/constitutional_ai/constitutional_prompts.js.map +1 -1
  69. package/dist/chains/conversational_retrieval_chain.cjs.map +1 -1
  70. package/dist/chains/conversational_retrieval_chain.js.map +1 -1
  71. package/dist/chains/graph_qa/cypher.cjs.map +1 -1
  72. package/dist/chains/graph_qa/cypher.js.map +1 -1
  73. package/dist/chains/graph_qa/prompts.cjs.map +1 -1
  74. package/dist/chains/graph_qa/prompts.js.map +1 -1
  75. package/dist/chains/history_aware_retriever.cjs.map +1 -1
  76. package/dist/chains/history_aware_retriever.js.map +1 -1
  77. package/dist/chains/llm_chain.cjs.map +1 -1
  78. package/dist/chains/llm_chain.js.map +1 -1
  79. package/dist/chains/openai_functions/base.cjs.map +1 -1
  80. package/dist/chains/openai_functions/base.js.map +1 -1
  81. package/dist/chains/openai_functions/openapi.cjs.map +1 -1
  82. package/dist/chains/openai_functions/openapi.js.map +1 -1
  83. package/dist/chains/query_constructor/index.cjs.map +1 -1
  84. package/dist/chains/query_constructor/index.js.map +1 -1
  85. package/dist/chains/query_constructor/parser.cjs.map +1 -1
  86. package/dist/chains/query_constructor/parser.js.map +1 -1
  87. package/dist/chains/query_constructor/prompt.cjs.map +1 -1
  88. package/dist/chains/query_constructor/prompt.js.map +1 -1
  89. package/dist/chains/question_answering/load.cjs.map +1 -1
  90. package/dist/chains/question_answering/load.js.map +1 -1
  91. package/dist/chains/question_answering/map_reduce_prompts.cjs.map +1 -1
  92. package/dist/chains/question_answering/map_reduce_prompts.js.map +1 -1
  93. package/dist/chains/question_answering/refine_prompts.cjs.map +1 -1
  94. package/dist/chains/question_answering/refine_prompts.js.map +1 -1
  95. package/dist/chains/question_answering/stuff_prompts.cjs.map +1 -1
  96. package/dist/chains/question_answering/stuff_prompts.js.map +1 -1
  97. package/dist/chains/retrieval.cjs.map +1 -1
  98. package/dist/chains/retrieval.js.map +1 -1
  99. package/dist/chains/router/multi_prompt.cjs.map +1 -1
  100. package/dist/chains/router/multi_prompt.js.map +1 -1
  101. package/dist/chains/router/multi_retrieval_qa.cjs.map +1 -1
  102. package/dist/chains/router/multi_retrieval_qa.js.map +1 -1
  103. package/dist/chains/sql_db/sql_db_chain.cjs.map +1 -1
  104. package/dist/chains/sql_db/sql_db_chain.js.map +1 -1
  105. package/dist/chains/summarization/load.cjs.map +1 -1
  106. package/dist/chains/summarization/load.js.map +1 -1
  107. package/dist/chains/summarization/refine_prompts.cjs.map +1 -1
  108. package/dist/chains/summarization/refine_prompts.js.map +1 -1
  109. package/dist/chains/summarization/stuff_prompts.cjs.map +1 -1
  110. package/dist/chains/summarization/stuff_prompts.js.map +1 -1
  111. package/dist/chat_models/universal.cjs.map +1 -1
  112. package/dist/chat_models/universal.js.map +1 -1
  113. package/dist/document_loaders/fs/json.cjs.map +1 -1
  114. package/dist/document_loaders/fs/json.js.map +1 -1
  115. package/dist/document_loaders/fs/multi_file.cjs.map +1 -1
  116. package/dist/document_loaders/fs/multi_file.js.map +1 -1
  117. package/dist/document_transformers/openai_functions.cjs.map +1 -1
  118. package/dist/document_transformers/openai_functions.js.map +1 -1
  119. package/dist/evaluation/agents/prompt.cjs.map +1 -1
  120. package/dist/evaluation/agents/prompt.js.map +1 -1
  121. package/dist/evaluation/comparison/pairwise.cjs.map +1 -1
  122. package/dist/evaluation/comparison/pairwise.js.map +1 -1
  123. package/dist/evaluation/comparison/prompt.cjs.map +1 -1
  124. package/dist/evaluation/comparison/prompt.js.map +1 -1
  125. package/dist/evaluation/criteria/criteria.cjs.map +1 -1
  126. package/dist/evaluation/criteria/criteria.js.map +1 -1
  127. package/dist/evaluation/criteria/prompt.cjs.map +1 -1
  128. package/dist/evaluation/criteria/prompt.js.map +1 -1
  129. package/dist/evaluation/embedding_distance/base.cjs.map +1 -1
  130. package/dist/evaluation/embedding_distance/base.js.map +1 -1
  131. package/dist/evaluation/qa/eval_chain.cjs.map +1 -1
  132. package/dist/evaluation/qa/eval_chain.js.map +1 -1
  133. package/dist/evaluation/qa/prompt.cjs.map +1 -1
  134. package/dist/evaluation/qa/prompt.js.map +1 -1
  135. package/dist/experimental/autogpt/agent.cjs.map +1 -1
  136. package/dist/experimental/autogpt/agent.js.map +1 -1
  137. package/dist/experimental/autogpt/prompt.cjs.map +1 -1
  138. package/dist/experimental/autogpt/prompt.js.map +1 -1
  139. package/dist/experimental/autogpt/prompt_generator.cjs.map +1 -1
  140. package/dist/experimental/autogpt/prompt_generator.js.map +1 -1
  141. package/dist/experimental/babyagi/agent.cjs.map +1 -1
  142. package/dist/experimental/babyagi/agent.js.map +1 -1
  143. package/dist/experimental/babyagi/task_creation.cjs.map +1 -1
  144. package/dist/experimental/babyagi/task_creation.js.map +1 -1
  145. package/dist/experimental/babyagi/task_execution.cjs.map +1 -1
  146. package/dist/experimental/babyagi/task_execution.js.map +1 -1
  147. package/dist/experimental/babyagi/task_prioritization.cjs.map +1 -1
  148. package/dist/experimental/babyagi/task_prioritization.js.map +1 -1
  149. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs.map +1 -1
  150. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js.map +1 -1
  151. package/dist/experimental/generative_agents/generative_agent.cjs.map +1 -1
  152. package/dist/experimental/generative_agents/generative_agent.js.map +1 -1
  153. package/dist/experimental/generative_agents/generative_agent_memory.cjs.map +1 -1
  154. package/dist/experimental/generative_agents/generative_agent_memory.js.map +1 -1
  155. package/dist/experimental/masking/regex_masking_transformer.cjs.map +1 -1
  156. package/dist/experimental/masking/regex_masking_transformer.js.map +1 -1
  157. package/dist/experimental/openai_assistant/index.cjs.map +1 -1
  158. package/dist/experimental/openai_assistant/index.js.map +1 -1
  159. package/dist/experimental/plan_and_execute/agent_executor.cjs.map +1 -1
  160. package/dist/experimental/plan_and_execute/agent_executor.js.map +1 -1
  161. package/dist/experimental/plan_and_execute/base.cjs.map +1 -1
  162. package/dist/experimental/plan_and_execute/base.js.map +1 -1
  163. package/dist/experimental/prompts/handlebars.cjs.map +1 -1
  164. package/dist/experimental/prompts/handlebars.js.map +1 -1
  165. package/dist/hub/base.cjs.map +1 -1
  166. package/dist/hub/base.js.map +1 -1
  167. package/dist/hub/index.cjs.map +1 -1
  168. package/dist/hub/index.js.map +1 -1
  169. package/dist/hub/node.cjs.map +1 -1
  170. package/dist/hub/node.js.map +1 -1
  171. package/dist/memory/buffer_memory.cjs.map +1 -1
  172. package/dist/memory/buffer_memory.js.map +1 -1
  173. package/dist/memory/buffer_token_memory.cjs.map +1 -1
  174. package/dist/memory/buffer_token_memory.js.map +1 -1
  175. package/dist/memory/buffer_window_memory.cjs.map +1 -1
  176. package/dist/memory/buffer_window_memory.js.map +1 -1
  177. package/dist/memory/entity_memory.cjs.map +1 -1
  178. package/dist/memory/entity_memory.js.map +1 -1
  179. package/dist/memory/prompt.cjs.map +1 -1
  180. package/dist/memory/prompt.js.map +1 -1
  181. package/dist/memory/summary.cjs.map +1 -1
  182. package/dist/memory/summary.js.map +1 -1
  183. package/dist/output_parsers/expression.cjs.map +1 -1
  184. package/dist/output_parsers/expression.js.map +1 -1
  185. package/dist/output_parsers/expression_type_handlers/base.cjs.map +1 -1
  186. package/dist/output_parsers/expression_type_handlers/base.js.map +1 -1
  187. package/dist/output_parsers/expression_type_handlers/factory.cjs.map +1 -1
  188. package/dist/output_parsers/expression_type_handlers/factory.js.map +1 -1
  189. package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs.map +1 -1
  190. package/dist/output_parsers/expression_type_handlers/identifier_handler.js.map +1 -1
  191. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs.map +1 -1
  192. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js.map +1 -1
  193. package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs.map +1 -1
  194. package/dist/output_parsers/expression_type_handlers/string_literal_handler.js.map +1 -1
  195. package/dist/output_parsers/fix.cjs.map +1 -1
  196. package/dist/output_parsers/fix.js.map +1 -1
  197. package/dist/output_parsers/http_response.cjs.map +1 -1
  198. package/dist/output_parsers/http_response.js.map +1 -1
  199. package/dist/output_parsers/openai_functions.cjs.map +1 -1
  200. package/dist/output_parsers/openai_functions.js.map +1 -1
  201. package/dist/output_parsers/openai_tools.cjs.map +1 -1
  202. package/dist/output_parsers/openai_tools.js.map +1 -1
  203. package/dist/output_parsers/prompts.cjs.map +1 -1
  204. package/dist/output_parsers/prompts.js.map +1 -1
  205. package/dist/output_parsers/structured.cjs.map +1 -1
  206. package/dist/output_parsers/structured.js.map +1 -1
  207. package/dist/retrievers/contextual_compression.cjs.map +1 -1
  208. package/dist/retrievers/contextual_compression.js.map +1 -1
  209. package/dist/retrievers/document_compressors/chain_extract.cjs.map +1 -1
  210. package/dist/retrievers/document_compressors/chain_extract.js.map +1 -1
  211. package/dist/retrievers/ensemble.cjs.map +1 -1
  212. package/dist/retrievers/ensemble.js.map +1 -1
  213. package/dist/retrievers/hyde.cjs.map +1 -1
  214. package/dist/retrievers/hyde.js.map +1 -1
  215. package/dist/retrievers/multi_query.cjs.map +1 -1
  216. package/dist/retrievers/multi_query.js.map +1 -1
  217. package/dist/retrievers/multi_vector.cjs.map +1 -1
  218. package/dist/retrievers/multi_vector.js.map +1 -1
  219. package/dist/retrievers/parent_document.cjs.map +1 -1
  220. package/dist/retrievers/parent_document.js.map +1 -1
  221. package/dist/retrievers/score_threshold.cjs.map +1 -1
  222. package/dist/retrievers/score_threshold.js.map +1 -1
  223. package/dist/smith/runner_utils.cjs.map +1 -1
  224. package/dist/smith/runner_utils.js.map +1 -1
  225. package/dist/storage/encoder_backed.cjs.map +1 -1
  226. package/dist/storage/encoder_backed.js.map +1 -1
  227. package/dist/storage/file_system.cjs.map +1 -1
  228. package/dist/storage/file_system.js.map +1 -1
  229. package/dist/tools/json.cjs.map +1 -1
  230. package/dist/tools/json.js.map +1 -1
  231. package/dist/tools/requests.cjs.map +1 -1
  232. package/dist/tools/requests.js.map +1 -1
  233. package/dist/tools/retriever.cjs.map +1 -1
  234. package/dist/tools/retriever.js.map +1 -1
  235. package/dist/tools/sql.cjs.map +1 -1
  236. package/dist/tools/sql.js.map +1 -1
  237. package/dist/tools/webbrowser.cjs.map +1 -1
  238. package/dist/tools/webbrowser.js.map +1 -1
  239. package/dist/util/hub.cjs.map +1 -1
  240. package/dist/util/hub.js.map +1 -1
  241. package/dist/util/load.cjs.map +1 -1
  242. package/dist/util/load.js.map +1 -1
  243. package/dist/util/openapi.cjs.map +1 -1
  244. package/dist/util/openapi.js.map +1 -1
  245. package/dist/util/sql_utils.cjs.map +1 -1
  246. package/dist/util/sql_utils.js.map +1 -1
  247. package/dist/vectorstores/memory.cjs.map +1 -1
  248. package/dist/vectorstores/memory.js.map +1 -1
  249. package/package.json +47 -48
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","names":[],"sources":["../../src/chains/base.ts"],"sourcesContent":["import { BaseMemory } from \"@langchain/core/memory\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { RUN_KEY } from \"@langchain/core/outputs\";\nimport {\n CallbackManagerForChainRun,\n CallbackManager,\n Callbacks,\n parseCallbackConfigArg,\n} from \"@langchain/core/callbacks/manager\";\nimport { ensureConfig, type RunnableConfig } from \"@langchain/core/runnables\";\nimport {\n BaseLangChain,\n BaseLangChainParams,\n} from \"@langchain/core/language_models/base\";\nimport { SerializedBaseChain } from \"./serde.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport interface ChainInputs extends BaseLangChainParams {\n memory?: BaseMemory;\n\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n}\n\n/**\n * Base interface that all chains must implement.\n */\nexport abstract class BaseChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues,\n>\n extends BaseLangChain<RunInput, RunOutput>\n implements ChainInputs\n{\n declare memory?: BaseMemory;\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"chains\", this._chainType()];\n }\n\n constructor(\n fields?: BaseMemory | ChainInputs,\n /** @deprecated */\n verbose?: boolean,\n /** @deprecated */\n callbacks?: Callbacks\n ) {\n if (\n arguments.length === 1 &&\n typeof fields === \"object\" &&\n !(\"saveContext\" in fields)\n ) {\n // fields is not a BaseMemory\n const { memory, callbackManager, ...rest } = fields;\n super({ ...rest, callbacks: callbackManager ?? rest.callbacks });\n this.memory = memory;\n } else {\n // fields is a BaseMemory\n super({ verbose, callbacks });\n this.memory = fields as BaseMemory;\n }\n }\n\n /** @ignore */\n _selectMemoryInputs(values: ChainValues): ChainValues {\n const valuesForMemory = { ...values };\n if (\"signal\" in valuesForMemory) {\n delete valuesForMemory.signal;\n }\n if (\"timeout\" in valuesForMemory) {\n delete valuesForMemory.timeout;\n }\n return valuesForMemory;\n }\n\n /**\n * Invoke the chain with the provided input and returns the output.\n * @param input Input values for the chain run.\n * @param config Optional configuration for the Runnable.\n * @returns Promise that resolves with the output of the chain run.\n */\n async invoke(input: RunInput, options?: RunnableConfig): Promise<RunOutput> {\n const config = ensureConfig(options);\n const fullValues = await this._formatValues(input);\n const callbackManager_ = await CallbackManager.configure(\n config?.callbacks,\n this.callbacks,\n config?.tags,\n this.tags,\n config?.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n fullValues,\n undefined,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let outputValues: RunOutput;\n try {\n if (fullValues.signal) {\n let listener: (() => void) | undefined;\n outputValues = (await Promise.race([\n this._call(fullValues as RunInput, runManager, config),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(new Error(\"AbortError\"));\n };\n fullValues.signal?.addEventListener(\"abort\", listener, {\n once: true,\n });\n }),\n ]).finally(() => {\n if (fullValues.signal && listener) {\n fullValues.signal.removeEventListener(\"abort\", listener);\n }\n })) as RunOutput;\n } else {\n outputValues = await this._call(\n fullValues as RunInput,\n runManager,\n config\n );\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n if (!(this.memory == null)) {\n await this.memory.saveContext(\n this._selectMemoryInputs(input),\n outputValues\n );\n }\n await runManager?.handleChainEnd(outputValues);\n // add the runManager's currentRunId to the outputValues\n Object.defineProperty(outputValues, RUN_KEY, {\n value: runManager ? { runId: runManager?.runId } : undefined,\n configurable: true,\n });\n return outputValues;\n }\n\n private _validateOutputs(outputs: Record<string, unknown>): void {\n const missingKeys = this.outputKeys.filter((k) => !(k in outputs));\n if (missingKeys.length) {\n throw new Error(\n `Missing output keys: ${missingKeys.join(\n \", \"\n )} from chain ${this._chainType()}`\n );\n }\n }\n\n async prepOutputs(\n inputs: Record<string, unknown>,\n outputs: Record<string, unknown>,\n returnOnlyOutputs = false\n ) {\n this._validateOutputs(outputs);\n if (this.memory) {\n await this.memory.saveContext(inputs, outputs);\n }\n if (returnOnlyOutputs) {\n return outputs;\n }\n return { ...inputs, ...outputs };\n }\n\n /**\n * Run the core logic of this chain and return the output\n */\n abstract _call(\n values: RunInput,\n runManager?: CallbackManagerForChainRun,\n config?: RunnableConfig\n ): Promise<RunOutput>;\n\n /**\n * Return the string type key uniquely identifying this class of chain.\n */\n abstract _chainType(): string;\n\n /**\n * Return a json-like object representing this chain.\n */\n serialize(): SerializedBaseChain {\n throw new Error(\"Method not implemented.\");\n }\n\n abstract get inputKeys(): string[];\n\n abstract get outputKeys(): string[];\n\n /** @deprecated Use .invoke() instead. Will be removed in 0.2.0. */\n async run(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n config?: Callbacks | RunnableConfig\n ): Promise<string> {\n const inputKeys = this.inputKeys.filter(\n (k) => !this.memory?.memoryKeys.includes(k)\n );\n const isKeylessInput = inputKeys.length <= 1;\n if (!isKeylessInput) {\n throw new Error(\n `Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `\n );\n }\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const values = inputKeys.length ? { [inputKeys[0]]: input } : ({} as any);\n const returnValues = await this.call(values, config);\n const keys = Object.keys(returnValues);\n\n if (keys.length === 1) {\n return returnValues[keys[0]];\n }\n throw new Error(\n \"return values have multiple keys, `run` only supported when one key currently\"\n );\n }\n\n protected async _formatValues(\n values: ChainValues & { signal?: AbortSignal; timeout?: number }\n ) {\n const fullValues = { ...values } as typeof values;\n if (fullValues.timeout && !fullValues.signal) {\n fullValues.signal = AbortSignal.timeout(fullValues.timeout);\n delete fullValues.timeout;\n }\n if (!(this.memory == null)) {\n const newValues = await this.memory.loadMemoryVariables(\n this._selectMemoryInputs(values)\n );\n for (const [key, value] of Object.entries(newValues)) {\n fullValues[key] = value;\n }\n }\n return fullValues;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.2.0.\n *\n * Run the core logic of this chain and add to output if desired.\n *\n * Wraps _call and handles memory.\n */\n async call(\n values: ChainValues & { signal?: AbortSignal; timeout?: number },\n config?: Callbacks | RunnableConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<RunOutput> {\n const parsedConfig = { tags, ...parseCallbackConfigArg(config) };\n return this.invoke(values as RunInput, parsedConfig);\n }\n\n /**\n * @deprecated Use .batch() instead. Will be removed in 0.2.0.\n *\n * Call the chain on all inputs in the list\n */\n async apply(\n inputs: RunInput[],\n config?: (Callbacks | RunnableConfig)[]\n ): Promise<RunOutput[]> {\n return Promise.all(\n inputs.map(async (i, idx) => this.call(i, config?.[idx]))\n );\n }\n\n /**\n * Load a chain from a json-like object describing it.\n */\n static async deserialize(\n data: SerializedBaseChain,\n values: LoadValues = {}\n ): Promise<BaseChain> {\n switch (data._type) {\n case \"llm_chain\": {\n const { LLMChain } = await import(\"./llm_chain.js\");\n return LLMChain.deserialize(data);\n }\n case \"sequential_chain\": {\n const { SequentialChain } = await import(\"./sequential_chain.js\");\n return SequentialChain.deserialize(data);\n }\n case \"simple_sequential_chain\": {\n const { SimpleSequentialChain } = await import(\"./sequential_chain.js\");\n return SimpleSequentialChain.deserialize(data);\n }\n case \"stuff_documents_chain\": {\n const { StuffDocumentsChain } = await import(\"./combine_docs_chain.js\");\n return StuffDocumentsChain.deserialize(data);\n }\n case \"map_reduce_documents_chain\": {\n const { MapReduceDocumentsChain } =\n await import(\"./combine_docs_chain.js\");\n return MapReduceDocumentsChain.deserialize(data);\n }\n case \"refine_documents_chain\": {\n const { RefineDocumentsChain } =\n await import(\"./combine_docs_chain.js\");\n return RefineDocumentsChain.deserialize(data);\n }\n case \"vector_db_qa\": {\n const { VectorDBQAChain } = await import(\"./vector_db_qa.js\");\n return VectorDBQAChain.deserialize(data, values);\n }\n case \"api_chain\": {\n const { APIChain } = await import(\"./api/api_chain.js\");\n return APIChain.deserialize(data);\n }\n default:\n throw new Error(\n `Invalid prompt type in config: ${\n (data as SerializedBaseChain)._type\n }`\n );\n }\n }\n}\n"],"mappings":";;;;;;;;AA+BA,IAAsB,YAAtB,cAIU,cAEV;CAGE,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAU,KAAK,YAAY;GAAC;;CAGnD,YACE,QAEA,SAEA,WACA;AACA,MACE,UAAU,WAAW,KACrB,OAAO,WAAW,YAClB,EAAE,iBAAiB,SACnB;GAEA,MAAM,EAAE,QAAQ,iBAAiB,GAAG,SAAS;AAC7C,SAAM;IAAE,GAAG;IAAM,WAAW,mBAAmB,KAAK;IAAW,CAAC;AAChE,QAAK,SAAS;SACT;AAEL,SAAM;IAAE;IAAS;IAAW,CAAC;AAC7B,QAAK,SAAS;;;;CAKlB,oBAAoB,QAAkC;EACpD,MAAM,kBAAkB,EAAE,GAAG,QAAQ;AACrC,MAAI,YAAY,gBACd,QAAO,gBAAgB;AAEzB,MAAI,aAAa,gBACf,QAAO,gBAAgB;AAEzB,SAAO;;;;;;;;CAST,MAAM,OAAO,OAAiB,SAA8C;EAC1E,MAAM,SAAS,aAAa,QAAQ;EACpC,MAAM,aAAa,MAAM,KAAK,cAAc,MAAM;EAUlD,MAAM,aAAa,OATM,MAAM,gBAAgB,UAC7C,QAAQ,W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,UAAQ,KAAK,OAAb;GACE,KAAK,aAAa;IAChB,MAAM,EAAE,aAAa,MAAM,OAAO;AAClC,WAAO,SAAS,YAAY,KAAK;;GAEnC,KAAK,oBAAoB;IACvB,MAAM,EAAE,oBAAoB,MAAM,OAAO;AACzC,WAAO,gBAAgB,YAAY,KAAK;;GAE1C,KAAK,2BAA2B;IAC9B,MAAM,EAAE,0BAA0B,MAAM,OAAO;AAC/C,WAAO,sBAAsB,YAAY,KAAK;;GAEhD,KAAK,yBAAyB;IAC5B,MAAM,EAAE,wBAAwB,MAAM,OAAO;AAC7C,WAAO,oBAAoB,YAAY,KAAK;;GAE9C,KAAK,8BAA8B;IACjC,MAAM,EAAE,4BACN,MAAM,OAAO;AACf,WAAO,wBAAwB,YAAY,KAAK;;GAElD,KAAK,0BAA0B;IAC7B,MAAM,EAAE,yBACN,MAAM,OAAO;AACf,WAAO,qBAAqB,YAAY,KAAK;;GAE/C,KAAK,gBAAgB;IACnB,MAAM,EAAE,oBAAoB,MAAM,OAAO;AACzC,WAAO,gBAAgB,YAAY,MAAM,OAAO;;GAElD,KAAK,aAAa;IAChB,MAAM,EAAE,aAAa,MAAM,OAAO;AAClC,WAAO,SAAS,YAAY,KAAK;;GAEnC,QACE,OAAM,IAAI,MACR,kCACG,KAA6B,QAEjC"}
1
+ {"version":3,"file":"base.js","names":[],"sources":["../../src/chains/base.ts"],"sourcesContent":["import { BaseMemory } from \"@langchain/core/memory\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { RUN_KEY } from \"@langchain/core/outputs\";\nimport {\n CallbackManagerForChainRun,\n CallbackManager,\n Callbacks,\n parseCallbackConfigArg,\n} from \"@langchain/core/callbacks/manager\";\nimport { ensureConfig, type RunnableConfig } from \"@langchain/core/runnables\";\nimport {\n BaseLangChain,\n BaseLangChainParams,\n} from \"@langchain/core/language_models/base\";\nimport { SerializedBaseChain } from \"./serde.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nexport interface ChainInputs extends BaseLangChainParams {\n memory?: BaseMemory;\n\n /**\n * @deprecated Use `callbacks` instead\n */\n callbackManager?: CallbackManager;\n}\n\n/**\n * Base interface that all chains must implement.\n */\nexport abstract class BaseChain<\n RunInput extends ChainValues = ChainValues,\n RunOutput extends ChainValues = ChainValues,\n>\n extends BaseLangChain<RunInput, RunOutput>\n implements ChainInputs\n{\n declare memory?: BaseMemory;\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"chains\", this._chainType()];\n }\n\n constructor(\n fields?: BaseMemory | ChainInputs,\n /** @deprecated */\n verbose?: boolean,\n /** @deprecated */\n callbacks?: Callbacks\n ) {\n if (\n arguments.length === 1 &&\n typeof fields === \"object\" &&\n !(\"saveContext\" in fields)\n ) {\n // fields is not a BaseMemory\n const { memory, callbackManager, ...rest } = fields;\n super({ ...rest, callbacks: callbackManager ?? rest.callbacks });\n this.memory = memory;\n } else {\n // fields is a BaseMemory\n super({ verbose, callbacks });\n this.memory = fields as BaseMemory;\n }\n }\n\n /** @ignore */\n _selectMemoryInputs(values: ChainValues): ChainValues {\n const valuesForMemory = { ...values };\n if (\"signal\" in valuesForMemory) {\n delete valuesForMemory.signal;\n }\n if (\"timeout\" in valuesForMemory) {\n delete valuesForMemory.timeout;\n }\n return valuesForMemory;\n }\n\n /**\n * Invoke the chain with the provided input and returns the output.\n * @param input Input values for the chain run.\n * @param config Optional configuration for the Runnable.\n * @returns Promise that resolves with the output of the chain run.\n */\n async invoke(input: RunInput, options?: RunnableConfig): Promise<RunOutput> {\n const config = ensureConfig(options);\n const fullValues = await this._formatValues(input);\n const callbackManager_ = await CallbackManager.configure(\n config?.callbacks,\n this.callbacks,\n config?.tags,\n this.tags,\n config?.metadata,\n this.metadata,\n { verbose: this.verbose }\n );\n const runManager = await callbackManager_?.handleChainStart(\n this.toJSON(),\n fullValues,\n undefined,\n undefined,\n undefined,\n undefined,\n config?.runName\n );\n let outputValues: RunOutput;\n try {\n if (fullValues.signal) {\n let listener: (() => void) | undefined;\n outputValues = (await Promise.race([\n this._call(fullValues as RunInput, runManager, config),\n new Promise<never>((_, reject) => {\n listener = () => {\n reject(new Error(\"AbortError\"));\n };\n fullValues.signal?.addEventListener(\"abort\", listener, {\n once: true,\n });\n }),\n ]).finally(() => {\n if (fullValues.signal && listener) {\n fullValues.signal.removeEventListener(\"abort\", listener);\n }\n })) as RunOutput;\n } else {\n outputValues = await this._call(\n fullValues as RunInput,\n runManager,\n config\n );\n }\n } catch (e) {\n await runManager?.handleChainError(e);\n throw e;\n }\n if (!(this.memory == null)) {\n await this.memory.saveContext(\n this._selectMemoryInputs(input),\n outputValues\n );\n }\n await runManager?.handleChainEnd(outputValues);\n // add the runManager's currentRunId to the outputValues\n Object.defineProperty(outputValues, RUN_KEY, {\n value: runManager ? { runId: runManager?.runId } : undefined,\n configurable: true,\n });\n return outputValues;\n }\n\n private _validateOutputs(outputs: Record<string, unknown>): void {\n const missingKeys = this.outputKeys.filter((k) => !(k in outputs));\n if (missingKeys.length) {\n throw new Error(\n `Missing output keys: ${missingKeys.join(\n \", \"\n )} from chain ${this._chainType()}`\n );\n }\n }\n\n async prepOutputs(\n inputs: Record<string, unknown>,\n outputs: Record<string, unknown>,\n returnOnlyOutputs = false\n ) {\n this._validateOutputs(outputs);\n if (this.memory) {\n await this.memory.saveContext(inputs, outputs);\n }\n if (returnOnlyOutputs) {\n return outputs;\n }\n return { ...inputs, ...outputs };\n }\n\n /**\n * Run the core logic of this chain and return the output\n */\n abstract _call(\n values: RunInput,\n runManager?: CallbackManagerForChainRun,\n config?: RunnableConfig\n ): Promise<RunOutput>;\n\n /**\n * Return the string type key uniquely identifying this class of chain.\n */\n abstract _chainType(): string;\n\n /**\n * Return a json-like object representing this chain.\n */\n serialize(): SerializedBaseChain {\n throw new Error(\"Method not implemented.\");\n }\n\n abstract get inputKeys(): string[];\n\n abstract get outputKeys(): string[];\n\n /** @deprecated Use .invoke() instead. Will be removed in 0.2.0. */\n async run(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n config?: Callbacks | RunnableConfig\n ): Promise<string> {\n const inputKeys = this.inputKeys.filter(\n (k) => !this.memory?.memoryKeys.includes(k)\n );\n const isKeylessInput = inputKeys.length <= 1;\n if (!isKeylessInput) {\n throw new Error(\n `Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `\n );\n }\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n const values = inputKeys.length ? { [inputKeys[0]]: input } : ({} as any);\n const returnValues = await this.call(values, config);\n const keys = Object.keys(returnValues);\n\n if (keys.length === 1) {\n return returnValues[keys[0]];\n }\n throw new Error(\n \"return values have multiple keys, `run` only supported when one key currently\"\n );\n }\n\n protected async _formatValues(\n values: ChainValues & { signal?: AbortSignal; timeout?: number }\n ) {\n const fullValues = { ...values } as typeof values;\n if (fullValues.timeout && !fullValues.signal) {\n fullValues.signal = AbortSignal.timeout(fullValues.timeout);\n delete fullValues.timeout;\n }\n if (!(this.memory == null)) {\n const newValues = await this.memory.loadMemoryVariables(\n this._selectMemoryInputs(values)\n );\n for (const [key, value] of Object.entries(newValues)) {\n fullValues[key] = value;\n }\n }\n return fullValues;\n }\n\n /**\n * @deprecated Use .invoke() instead. Will be removed in 0.2.0.\n *\n * Run the core logic of this chain and add to output if desired.\n *\n * Wraps _call and handles memory.\n */\n async call(\n values: ChainValues & { signal?: AbortSignal; timeout?: number },\n config?: Callbacks | RunnableConfig,\n /** @deprecated */\n tags?: string[]\n ): Promise<RunOutput> {\n const parsedConfig = { tags, ...parseCallbackConfigArg(config) };\n return this.invoke(values as RunInput, parsedConfig);\n }\n\n /**\n * @deprecated Use .batch() instead. Will be removed in 0.2.0.\n *\n * Call the chain on all inputs in the list\n */\n async apply(\n inputs: RunInput[],\n config?: (Callbacks | RunnableConfig)[]\n ): Promise<RunOutput[]> {\n return Promise.all(\n inputs.map(async (i, idx) => this.call(i, config?.[idx]))\n );\n }\n\n /**\n * Load a chain from a json-like object describing it.\n */\n static async deserialize(\n data: SerializedBaseChain,\n values: LoadValues = {}\n ): Promise<BaseChain> {\n switch (data._type) {\n case \"llm_chain\": {\n const { LLMChain } = await import(\"./llm_chain.js\");\n return LLMChain.deserialize(data);\n }\n case \"sequential_chain\": {\n const { SequentialChain } = await import(\"./sequential_chain.js\");\n return SequentialChain.deserialize(data);\n }\n case \"simple_sequential_chain\": {\n const { SimpleSequentialChain } = await import(\"./sequential_chain.js\");\n return SimpleSequentialChain.deserialize(data);\n }\n case \"stuff_documents_chain\": {\n const { StuffDocumentsChain } = await import(\"./combine_docs_chain.js\");\n return StuffDocumentsChain.deserialize(data);\n }\n case \"map_reduce_documents_chain\": {\n const { MapReduceDocumentsChain } =\n await import(\"./combine_docs_chain.js\");\n return MapReduceDocumentsChain.deserialize(data);\n }\n case \"refine_documents_chain\": {\n const { RefineDocumentsChain } =\n await import(\"./combine_docs_chain.js\");\n return RefineDocumentsChain.deserialize(data);\n }\n case \"vector_db_qa\": {\n const { VectorDBQAChain } = await import(\"./vector_db_qa.js\");\n return VectorDBQAChain.deserialize(data, values);\n }\n case \"api_chain\": {\n const { APIChain } = await import(\"./api/api_chain.js\");\n return APIChain.deserialize(data);\n }\n default:\n throw new Error(\n `Invalid prompt type in config: ${\n (data as SerializedBaseChain)._type\n }`\n );\n }\n }\n}\n"],"mappings":";;;;;;;;AA+BA,IAAsB,YAAtB,cAIU,cAEV;CAGE,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAU,KAAK,YAAY;GAAC;;CAGnD,YACE,QAEA,SAEA,WACA;AACA,MACE,UAAU,WAAW,KACrB,OAAO,WAAW,YAClB,EAAE,iBAAiB,SACnB;GAEA,MAAM,EAAE,QAAQ,iBAAiB,GAAG,SAAS;AAC7C,SAAM;IAAE,GAAG;IAAM,WAAW,mBAAmB,KAAK;IAAW,CAAC;AAChE,QAAK,SAAS;SACT;AAEL,SAAM;IAAE;IAAS;IAAW,CAAC;AAC7B,QAAK,SAAS;;;;CAKlB,oBAAo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,MAAM,KACJ,QACA,QAEA,MACoB;EACpB,MAAM,eAAe;GAAE;GAAM,GAAG,uBAAuB,OAAO;GAAE;AAChE,SAAO,KAAK,OAAO,QAAoB,aAAa;;;;;;;CAQtD,MAAM,MACJ,QACA,QACsB;AACtB,SAAO,QAAQ,IACb,OAAO,IAAI,OAAO,GAAG,QAAQ,KAAK,KAAK,GAAG,SAAS,KAAK,CAAC,CAC1D;;;;;CAMH,aAAa,YACX,MACA,SAAqB,EAAE,EACH;AACpB,UAAQ,KAAK,OAAb;GACE,KAAK,aAAa;IAChB,MAAM,EAAE,aAAa,MAAM,OAAO;AAClC,WAAO,SAAS,YAAY,KAAK;;GAEnC,KAAK,oBAAoB;IACvB,MAAM,EAAE,oBAAoB,MAAM,OAAO;AACzC,WAAO,gBAAgB,YAAY,KAAK;;GAE1C,KAAK,2BAA2B;IAC9B,MAAM,EAAE,0BAA0B,MAAM,OAAO;AAC/C,WAAO,sBAAsB,YAAY,KAAK;;GAEhD,KAAK,yBAAyB;IAC5B,MAAM,EAAE,wBAAwB,MAAM,OAAO;AAC7C,WAAO,oBAAoB,YAAY,KAAK;;GAE9C,KAAK,8BAA8B;IACjC,MAAM,EAAE,4BACN,MAAM,OAAO;AACf,WAAO,wBAAwB,YAAY,KAAK;;GAElD,KAAK,0BAA0B;IAC7B,MAAM,EAAE,yBACN,MAAM,OAAO;AACf,WAAO,qBAAqB,YAAY,KAAK;;GAE/C,KAAK,gBAAgB;IACnB,MAAM,EAAE,oBAAoB,MAAM,OAAO;AACzC,WAAO,gBAAgB,YAAY,MAAM,OAAO;;GAElD,KAAK,aAAa;IAChB,MAAM,EAAE,aAAa,MAAM,OAAO;AAClC,WAAO,SAAS,YAAY,KAAK;;GAEnC,QACE,OAAM,IAAI,MACR,kCACG,KAA6B,QAEjC"}
@@ -1 +1 @@
1
- {"version":3,"file":"chat_vector_db_chain.cjs","names":["BaseChain","LLMChain","PromptTemplate","loadQAStuffChain"],"sources":["../../src/chains/chat_vector_db_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { VectorStoreInterface } from \"@langchain/core/vectorstores\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { loadQAStuffChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\nconst qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:`;\n\n/**\n * Interface for the input parameters of the ChatVectorDBQAChain class.\n */\nexport interface ChatVectorDBQAChainInput extends ChainInputs {\n vectorstore: VectorStoreInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n outputKey?: string;\n inputKey?: string;\n k?: number;\n}\n\n/** @deprecated use `ConversationalRetrievalQAChain` instead. */\nexport class ChatVectorDBQAChain\n extends BaseChain\n implements ChatVectorDBQAChainInput\n{\n k = 4;\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n outputKey = \"result\";\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n vectorstore: VectorStoreInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n constructor(fields: ChatVectorDBQAChainInput) {\n super(fields);\n this.vectorstore = fields.vectorstore;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.k = fields.k ?? this.k;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`chat history key ${this.inputKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string = values[this.chatHistoryKey];\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n console.log(\"_call\", values, keys);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.vectorstore.similaritySearch(\n newQuestion,\n this.k,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n const result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n return {\n ...result,\n sourceDocuments: docs,\n };\n }\n return result;\n }\n\n _chainType() {\n return \"chat-vector-db\" as const;\n }\n\n static async deserialize(\n data: SerializedChatVectorDBQAChain,\n values: LoadValues\n ) {\n if (!(\"vectorstore\" in values)) {\n throw new Error(\n `Need to pass in a vectorstore to deserialize VectorDBQAChain`\n );\n }\n const { vectorstore } = values;\n\n return new ChatVectorDBQAChain({\n combineDocumentsChain: await BaseChain.deserialize(\n data.combine_documents_chain\n ),\n questionGeneratorChain: await LLMChain.deserialize(\n data.question_generator\n ),\n k: data.k,\n vectorstore,\n });\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n return {\n _type: this._chainType(),\n combine_documents_chain: this.combineDocumentsChain.serialize(),\n question_generator: this.questionGeneratorChain.serialize(),\n k: this.k,\n };\n }\n\n /**\n * Creates an instance of ChatVectorDBQAChain using a BaseLanguageModel\n * and other options.\n * @param llm Instance of BaseLanguageModel used to generate a new question.\n * @param vectorstore Instance of VectorStore used for vector operations.\n * @param options (Optional) Additional options for creating the ChatVectorDBQAChain instance.\n * @returns New instance of ChatVectorDBQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n vectorstore: VectorStoreInterface,\n options: {\n inputKey?: string;\n outputKey?: string;\n k?: number;\n returnSourceDocuments?: boolean;\n questionGeneratorTemplate?: string;\n qaTemplate?: string;\n verbose?: boolean;\n } = {}\n ): ChatVectorDBQAChain {\n const { questionGeneratorTemplate, qaTemplate, verbose, ...rest } = options;\n const question_generator_prompt = PromptTemplate.fromTemplate(\n questionGeneratorTemplate || question_generator_template\n );\n const qa_prompt = PromptTemplate.fromTemplate(qaTemplate || qa_template);\n\n const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt, verbose });\n const questionGeneratorChain = new LLMChain({\n prompt: question_generator_prompt,\n llm,\n verbose,\n });\n const instance = new this({\n vectorstore,\n combineDocumentsChain: qaChain,\n questionGeneratorChain,\n ...rest,\n });\n return instance;\n }\n}\n"],"mappings":";;;;;;AAaA,MAAM,8BAA8B;;;;;;AAOpC,MAAM,cAAc;;;;;;;AAqBpB,IAAa,sBAAb,MAAa,4BACHA,aAAAA,UAEV;CACE,IAAI;CAEJ,WAAW;CAEX,iBAAiB;CAEjB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,KAAK,eAAe;;CAG7C,YAAY;CAEZ,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;CAGzB;CAEA;CAEA;CAEA,wBAAwB;CAExB,YAAY,QAAkC;AAC5C,QAAM,OAAO;AACb,OAAK,cAAc,OAAO;AAC1B,OAA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AMA,aAAAA,UAAU,YACrC,KAAK,wBACN;GACD,wBAAwB,MAAMC,kBAAAA,SAAS,YACrC,KAAK,mBACN;GACD,GAAG,KAAK;GACR;GACD,CAAC;;CAGJ,YAA2C;AACzC,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,yBAAyB,KAAK,sBAAsB,WAAW;GAC/D,oBAAoB,KAAK,uBAAuB,WAAW;GAC3D,GAAG,KAAK;GACT;;;;;;;;;;CAWH,OAAO,QACL,KACA,aACA,UAQI,EAAE,EACe;EACrB,MAAM,EAAE,2BAA2B,YAAY,SAAS,GAAG,SAAS;EACpE,MAAM,4BAA4BC,wBAAAA,eAAe,aAC/C,6BAA6B,4BAC9B;EAGD,MAAM,UAAUC,aAAAA,iBAAiB,KAAK;GAAE,QAFtBD,wBAAAA,eAAe,aAAa,cAAc,YAAY;GAEb;GAAS,CAAC;EACrE,MAAM,yBAAyB,IAAID,kBAAAA,SAAS;GAC1C,QAAQ;GACR;GACA;GACD,CAAC;AAOF,SANiB,IAAI,KAAK;GACxB;GACA,uBAAuB;GACvB;GACA,GAAG;GACJ,CAAC"}
1
+ {"version":3,"file":"chat_vector_db_chain.cjs","names":["BaseChain","LLMChain","PromptTemplate","loadQAStuffChain"],"sources":["../../src/chains/chat_vector_db_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { VectorStoreInterface } from \"@langchain/core/vectorstores\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { loadQAStuffChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\nconst qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:`;\n\n/**\n * Interface for the input parameters of the ChatVectorDBQAChain class.\n */\nexport interface ChatVectorDBQAChainInput extends ChainInputs {\n vectorstore: VectorStoreInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n outputKey?: string;\n inputKey?: string;\n k?: number;\n}\n\n/** @deprecated use `ConversationalRetrievalQAChain` instead. */\nexport class ChatVectorDBQAChain\n extends BaseChain\n implements ChatVectorDBQAChainInput\n{\n k = 4;\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n outputKey = \"result\";\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n vectorstore: VectorStoreInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n constructor(fields: ChatVectorDBQAChainInput) {\n super(fields);\n this.vectorstore = fields.vectorstore;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.k = fields.k ?? this.k;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`chat history key ${this.inputKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string = values[this.chatHistoryKey];\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n console.log(\"_call\", values, keys);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.vectorstore.similaritySearch(\n newQuestion,\n this.k,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n const result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n return {\n ...result,\n sourceDocuments: docs,\n };\n }\n return result;\n }\n\n _chainType() {\n return \"chat-vector-db\" as const;\n }\n\n static async deserialize(\n data: SerializedChatVectorDBQAChain,\n values: LoadValues\n ) {\n if (!(\"vectorstore\" in values)) {\n throw new Error(\n `Need to pass in a vectorstore to deserialize VectorDBQAChain`\n );\n }\n const { vectorstore } = values;\n\n return new ChatVectorDBQAChain({\n combineDocumentsChain: await BaseChain.deserialize(\n data.combine_documents_chain\n ),\n questionGeneratorChain: await LLMChain.deserialize(\n data.question_generator\n ),\n k: data.k,\n vectorstore,\n });\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n return {\n _type: this._chainType(),\n combine_documents_chain: this.combineDocumentsChain.serialize(),\n question_generator: this.questionGeneratorChain.serialize(),\n k: this.k,\n };\n }\n\n /**\n * Creates an instance of ChatVectorDBQAChain using a BaseLanguageModel\n * and other options.\n * @param llm Instance of BaseLanguageModel used to generate a new question.\n * @param vectorstore Instance of VectorStore used for vector operations.\n * @param options (Optional) Additional options for creating the ChatVectorDBQAChain instance.\n * @returns New instance of ChatVectorDBQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n vectorstore: VectorStoreInterface,\n options: {\n inputKey?: string;\n outputKey?: string;\n k?: number;\n returnSourceDocuments?: boolean;\n questionGeneratorTemplate?: string;\n qaTemplate?: string;\n verbose?: boolean;\n } = {}\n ): ChatVectorDBQAChain {\n const { questionGeneratorTemplate, qaTemplate, verbose, ...rest } = options;\n const question_generator_prompt = PromptTemplate.fromTemplate(\n questionGeneratorTemplate || question_generator_template\n );\n const qa_prompt = PromptTemplate.fromTemplate(qaTemplate || qa_template);\n\n const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt, verbose });\n const questionGeneratorChain = new LLMChain({\n prompt: question_generator_prompt,\n llm,\n verbose,\n });\n const instance = new this({\n vectorstore,\n combineDocumentsChain: qaChain,\n questionGeneratorChain,\n ...rest,\n });\n return instance;\n }\n}\n"],"mappings":";;;;;;AAaA,MAAM,8BAA8B;;;;;;AAOpC,MAAM,cAAc;;;;;;;AAqBpB,IAAa,sBAAb,MAAa,4BACHA,aAAAA,UAEV;CACE,IAAI;CAEJ,WAAW;CAEX,iBAAiB;CAEjB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,KAAK,eAAe;;CAG7C,YAAY;CAEZ,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;CAGzB;CAEA;CAEA;CAEA,wBAAwB;CAExB,YAAY,QAAkC;AAC5C,QAAM,OAAO;AACb,OAAK,cAAc,OAAO;AAC1B,OAAK,wBAAwB,OAAO;AACpC,OAAK,yBAAyB,OAAO;AACrC,OAAK,WAAW,OAAO,YAAY,KAAK;AACxC,OAAK,YAAY,OAAO,aAAa,KAAK;AAC1C,OAAK,IAAI,OAAO,KAAK,KAAK;AAC1B,OAAK,wBACH,OAAO,yBAAyB,KAAK;;;CAIzC,MAAM,MACJ,QACA,YACsB;AACtB,MAAI,EAAE,KAAK,YAAY,QACrB,OAAM,IAAI,MAAM,gBAAgB,KAAK,SAAS,aAAa;AAE7D,MAAI,EAAE,KAAK,kBAAkB,QAC3B,OAAM,IAAI,MAAM,oBAAoB,KAAK,SAAS,aAAa;EAEjE,MAAM,WAAmB,OAAO,KAAK;EACrC,MAAM,cAAsB,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,UAAUC,aAAAA,iBAAiB,KAAK;GAAE,QAFtBD,wBAAAA,eAAe,aAAa,cAAc,YAEH;GAAE;GAAS,CAAC;EACrE,MAAM,yBAAyB,IAAID,kBAAAA,SAAS;GAC1C,QAAQ;GACR;GACA;GACD,CAAC;AAOF,SAAO,IANc,KAAK;GACxB;GACA,uBAAuB;GACvB;GACA,GAAG;GACJ,CACc"}
@@ -1 +1 @@
1
- {"version":3,"file":"chat_vector_db_chain.js","names":[],"sources":["../../src/chains/chat_vector_db_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { VectorStoreInterface } from \"@langchain/core/vectorstores\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { loadQAStuffChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\nconst qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:`;\n\n/**\n * Interface for the input parameters of the ChatVectorDBQAChain class.\n */\nexport interface ChatVectorDBQAChainInput extends ChainInputs {\n vectorstore: VectorStoreInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n outputKey?: string;\n inputKey?: string;\n k?: number;\n}\n\n/** @deprecated use `ConversationalRetrievalQAChain` instead. */\nexport class ChatVectorDBQAChain\n extends BaseChain\n implements ChatVectorDBQAChainInput\n{\n k = 4;\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n outputKey = \"result\";\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n vectorstore: VectorStoreInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n constructor(fields: ChatVectorDBQAChainInput) {\n super(fields);\n this.vectorstore = fields.vectorstore;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.k = fields.k ?? this.k;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`chat history key ${this.inputKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string = values[this.chatHistoryKey];\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n console.log(\"_call\", values, keys);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.vectorstore.similaritySearch(\n newQuestion,\n this.k,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n const result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n return {\n ...result,\n sourceDocuments: docs,\n };\n }\n return result;\n }\n\n _chainType() {\n return \"chat-vector-db\" as const;\n }\n\n static async deserialize(\n data: SerializedChatVectorDBQAChain,\n values: LoadValues\n ) {\n if (!(\"vectorstore\" in values)) {\n throw new Error(\n `Need to pass in a vectorstore to deserialize VectorDBQAChain`\n );\n }\n const { vectorstore } = values;\n\n return new ChatVectorDBQAChain({\n combineDocumentsChain: await BaseChain.deserialize(\n data.combine_documents_chain\n ),\n questionGeneratorChain: await LLMChain.deserialize(\n data.question_generator\n ),\n k: data.k,\n vectorstore,\n });\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n return {\n _type: this._chainType(),\n combine_documents_chain: this.combineDocumentsChain.serialize(),\n question_generator: this.questionGeneratorChain.serialize(),\n k: this.k,\n };\n }\n\n /**\n * Creates an instance of ChatVectorDBQAChain using a BaseLanguageModel\n * and other options.\n * @param llm Instance of BaseLanguageModel used to generate a new question.\n * @param vectorstore Instance of VectorStore used for vector operations.\n * @param options (Optional) Additional options for creating the ChatVectorDBQAChain instance.\n * @returns New instance of ChatVectorDBQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n vectorstore: VectorStoreInterface,\n options: {\n inputKey?: string;\n outputKey?: string;\n k?: number;\n returnSourceDocuments?: boolean;\n questionGeneratorTemplate?: string;\n qaTemplate?: string;\n verbose?: boolean;\n } = {}\n ): ChatVectorDBQAChain {\n const { questionGeneratorTemplate, qaTemplate, verbose, ...rest } = options;\n const question_generator_prompt = PromptTemplate.fromTemplate(\n questionGeneratorTemplate || question_generator_template\n );\n const qa_prompt = PromptTemplate.fromTemplate(qaTemplate || qa_template);\n\n const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt, verbose });\n const questionGeneratorChain = new LLMChain({\n prompt: question_generator_prompt,\n llm,\n verbose,\n });\n const instance = new this({\n vectorstore,\n combineDocumentsChain: qaChain,\n questionGeneratorChain,\n ...rest,\n });\n return instance;\n }\n}\n"],"mappings":";;;;;AAaA,MAAM,8BAA8B;;;;;;AAOpC,MAAM,cAAc;;;;;;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,MAAM,KAAK,sBAAsB,KAC9C,QACA,YAAY,SAAS,oBAAoB,CAC1C;AACD,MAAI,KAAK,sBACP,QAAO;GACL,GAAG;GACH,iBAAiB;GAClB;AAEH,SAAO;;CAGT,aAAa;AACX,SAAO;;CAGT,aAAa,YACX,MACA,QACA;AACA,MAAI,EAAE,iBAAiB,QACrB,OAAM,IAAI,MACR,+DACD;EAEH,MAAM,EAAE,gBAAgB;AAExB,SAAO,IAAI,oBAAoB;GAC7B,uBAAuB,MAAM,UAAU,YACrC,KAAK,wBACN;GACD,wBAAwB,MAAM,SAAS,YACrC,KAAK,mBACN;GACD,GAAG,KAAK;GACR;GACD,CAAC;;CAGJ,YAA2C;AACzC,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,yBAAyB,KAAK,sBAAsB,WAAW;GAC/D,oBAAoB,KAAK,uBAAuB,WAAW;GAC3D,GAAG,KAAK;GACT;;;;;;;;;;CAWH,OAAO,QACL,KACA,aACA,UAQI,EAAE,EACe;EACrB,MAAM,EAAE,2BAA2B,YAAY,SAAS,GAAG,SAAS;EACpE,MAAM,4BAA4B,eAAe,aAC/C,6BAA6B,4BAC9B;EAGD,MAAM,UAAU,iBAAiB,KAAK;GAAE,QAFtB,eAAe,aAAa,cAAc,YAAY;GAEb;GAAS,CAAC;EACrE,MAAM,yBAAyB,IAAI,SAAS;GAC1C,QAAQ;GACR;GACA;GACD,CAAC;AAOF,SANiB,IAAI,KAAK;GACxB;GACA,uBAAuB;GACvB;GACA,GAAG;GACJ,CAAC"}
1
+ {"version":3,"file":"chat_vector_db_chain.js","names":[],"sources":["../../src/chains/chat_vector_db_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { VectorStoreInterface } from \"@langchain/core/vectorstores\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { loadQAStuffChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\nconst qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:`;\n\n/**\n * Interface for the input parameters of the ChatVectorDBQAChain class.\n */\nexport interface ChatVectorDBQAChainInput extends ChainInputs {\n vectorstore: VectorStoreInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n outputKey?: string;\n inputKey?: string;\n k?: number;\n}\n\n/** @deprecated use `ConversationalRetrievalQAChain` instead. */\nexport class ChatVectorDBQAChain\n extends BaseChain\n implements ChatVectorDBQAChainInput\n{\n k = 4;\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n outputKey = \"result\";\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n vectorstore: VectorStoreInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n constructor(fields: ChatVectorDBQAChainInput) {\n super(fields);\n this.vectorstore = fields.vectorstore;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.k = fields.k ?? this.k;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`chat history key ${this.inputKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string = values[this.chatHistoryKey];\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n console.log(\"_call\", values, keys);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.vectorstore.similaritySearch(\n newQuestion,\n this.k,\n undefined,\n runManager?.getChild(\"vectorstore\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n const result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n return {\n ...result,\n sourceDocuments: docs,\n };\n }\n return result;\n }\n\n _chainType() {\n return \"chat-vector-db\" as const;\n }\n\n static async deserialize(\n data: SerializedChatVectorDBQAChain,\n values: LoadValues\n ) {\n if (!(\"vectorstore\" in values)) {\n throw new Error(\n `Need to pass in a vectorstore to deserialize VectorDBQAChain`\n );\n }\n const { vectorstore } = values;\n\n return new ChatVectorDBQAChain({\n combineDocumentsChain: await BaseChain.deserialize(\n data.combine_documents_chain\n ),\n questionGeneratorChain: await LLMChain.deserialize(\n data.question_generator\n ),\n k: data.k,\n vectorstore,\n });\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n return {\n _type: this._chainType(),\n combine_documents_chain: this.combineDocumentsChain.serialize(),\n question_generator: this.questionGeneratorChain.serialize(),\n k: this.k,\n };\n }\n\n /**\n * Creates an instance of ChatVectorDBQAChain using a BaseLanguageModel\n * and other options.\n * @param llm Instance of BaseLanguageModel used to generate a new question.\n * @param vectorstore Instance of VectorStore used for vector operations.\n * @param options (Optional) Additional options for creating the ChatVectorDBQAChain instance.\n * @returns New instance of ChatVectorDBQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n vectorstore: VectorStoreInterface,\n options: {\n inputKey?: string;\n outputKey?: string;\n k?: number;\n returnSourceDocuments?: boolean;\n questionGeneratorTemplate?: string;\n qaTemplate?: string;\n verbose?: boolean;\n } = {}\n ): ChatVectorDBQAChain {\n const { questionGeneratorTemplate, qaTemplate, verbose, ...rest } = options;\n const question_generator_prompt = PromptTemplate.fromTemplate(\n questionGeneratorTemplate || question_generator_template\n );\n const qa_prompt = PromptTemplate.fromTemplate(qaTemplate || qa_template);\n\n const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt, verbose });\n const questionGeneratorChain = new LLMChain({\n prompt: question_generator_prompt,\n llm,\n verbose,\n });\n const instance = new this({\n vectorstore,\n combineDocumentsChain: qaChain,\n questionGeneratorChain,\n ...rest,\n });\n return instance;\n }\n}\n"],"mappings":";;;;;AAaA,MAAM,8BAA8B;;;;;;AAOpC,MAAM,cAAc;;;;;;;AAqBpB,IAAa,sBAAb,MAAa,4BACH,UAEV;CACE,IAAI;CAEJ,WAAW;CAEX,iBAAiB;CAEjB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,KAAK,eAAe;;CAG7C,YAAY;CAEZ,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;CAGzB;CAEA;CAEA;CAEA,wBAAwB;CAExB,YAAY,QAAkC;AAC5C,QAAM,OAAO;AACb,OAAK,cAAc,OAAO;AAC1B,OAAK,wBAAwB,OAAO;AACpC,OAAK,yBAAyB,OAAO;AACrC,OAAK,WAAW,OAAO,YAAY,KAAK;AACxC,OAAK,YAAY,OAAO,aAAa,KAAK;AAC1C,OAAK,IAAI,OAAO,KAAK,KAAK;AAC1B,OAAK,wBACH,OAAO,yBAAyB,KAAK;;;CAIzC,MAAM,MACJ,QACA,YACsB;AACtB,MAAI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G,KAAK;GACT;;;;;;;;;;CAWH,OAAO,QACL,KACA,aACA,UAQI,EAAE,EACe;EACrB,MAAM,EAAE,2BAA2B,YAAY,SAAS,GAAG,SAAS;EACpE,MAAM,4BAA4B,eAAe,aAC/C,6BAA6B,4BAC9B;EAGD,MAAM,UAAU,iBAAiB,KAAK;GAAE,QAFtB,eAAe,aAAa,cAAc,YAEH;GAAE;GAAS,CAAC;EACrE,MAAM,yBAAyB,IAAI,SAAS;GAC1C,QAAQ;GACR;GACA;GACD,CAAC;AAOF,SAAO,IANc,KAAK;GACxB;GACA,uBAAuB;GACvB;GACA,GAAG;GACJ,CACc"}
@@ -1 +1 @@
1
- {"version":3,"file":"combine_docs_chain.cjs","names":["BaseChain","LLMChain","PromptTemplate"],"sources":["../../src/chains/combine_docs_chain.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport type {\n SerializedStuffDocumentsChain,\n SerializedMapReduceDocumentsChain,\n SerializedRefineDocumentsChain,\n} from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\n\n/**\n * Interface for the input properties of the StuffDocumentsChain class.\n */\nexport interface StuffDocumentsChainInput extends ChainInputs {\n /** LLM Wrapper to use after formatting documents */\n llmChain: LLMChain;\n inputKey?: string;\n /** Variable name in the LLM chain to put the documents in */\n documentVariableName?: string;\n}\n\n/**\n * Chain that combines documents by stuffing into context.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class StuffDocumentsChain\n extends BaseChain\n implements StuffDocumentsChainInput\n{\n static lc_name() {\n return \"StuffDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n get inputKeys() {\n return [this.inputKey, ...this.llmChain.inputKeys].filter(\n (key) => key !== this.documentVariableName\n );\n }\n\n get outputKeys() {\n return this.llmChain.outputKeys;\n }\n\n constructor(fields: StuffDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n }\n\n /** @ignore */\n _prepInputs(values: ChainValues): ChainValues {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n const texts = (docs as Document[]).map(({ pageContent }) => pageContent);\n const text = texts.join(\"\\n\\n\");\n return {\n ...rest,\n [this.documentVariableName]: text,\n };\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n const result = await this.llmChain.call(\n this._prepInputs(values),\n runManager?.getChild(\"combine_documents\")\n );\n return result;\n }\n\n _chainType() {\n return \"stuff_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedStuffDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n return new StuffDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n });\n }\n\n serialize(): SerializedStuffDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the MapReduceDocumentsChain\n * class.\n */\nexport interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {\n /** The maximum number of tokens before requiring to do the reduction */\n maxTokens?: number;\n /** The maximum number of iterations to run through the map */\n maxIterations?: number;\n /** Ensures that the map step is taken regardless of max tokens */\n ensureMapStep?: boolean;\n /** Chain to use to combine results of applying llm_chain to documents. */\n combineDocumentChain: StuffDocumentsChain;\n /** Return the results of the map steps in the output. */\n returnIntermediateSteps?: boolean;\n}\n\n/**\n * Combine documents by mapping a chain over them, then combining results.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class MapReduceDocumentsChain\n extends BaseChain\n implements MapReduceDocumentsChainInput\n{\n static lc_name() {\n return \"MapReduceDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n returnIntermediateSteps = false;\n\n get inputKeys() {\n return [this.inputKey, ...this.combineDocumentChain.inputKeys];\n }\n\n get outputKeys() {\n return this.combineDocumentChain.outputKeys;\n }\n\n maxTokens = 3000;\n\n maxIterations = 10;\n\n ensureMapStep = false;\n\n combineDocumentChain: StuffDocumentsChain;\n\n constructor(fields: MapReduceDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.combineDocumentChain = fields.combineDocumentChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.ensureMapStep = fields.ensureMapStep ?? this.ensureMapStep;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.maxTokens = fields.maxTokens ?? this.maxTokens;\n this.maxIterations = fields.maxIterations ?? this.maxIterations;\n this.returnIntermediateSteps = fields.returnIntermediateSteps ?? false;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n let currentDocs = docs as Document[];\n let intermediateSteps: string[] = [];\n\n // For each iteration, we'll use the `llmChain` to get a new result\n for (let i = 0; i < this.maxIterations; i += 1) {\n const inputs = currentDocs.map((d) => ({\n [this.documentVariableName]: d.pageContent,\n ...rest,\n }));\n\n const canSkipMapStep = i !== 0 || !this.ensureMapStep;\n if (canSkipMapStep) {\n // Calculate the total tokens required in the input\n const formatted =\n await this.combineDocumentChain.llmChain.prompt.format(\n this.combineDocumentChain._prepInputs({\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n })\n );\n const length =\n await this.combineDocumentChain.llmChain._getNumTokens(formatted);\n\n const withinTokenLimit = length < this.maxTokens;\n // If we can skip the map step, and we're within the token limit, we don't\n // need to run the map step, so just break out of the loop.\n if (withinTokenLimit) {\n break;\n }\n }\n\n const results = await this.llmChain.apply(\n inputs,\n // If we have a runManager, then we need to create a child for each input\n // so that we can track the progress of each input.\n runManager\n ? Array.from({ length: inputs.length }, (_, i) =>\n runManager.getChild(`map_${i + 1}`)\n )\n : undefined\n );\n const { outputKey } = this.llmChain;\n\n // If the flag is set, then concat that to the intermediate steps\n if (this.returnIntermediateSteps) {\n intermediateSteps = intermediateSteps.concat(\n results.map((r) => r[outputKey])\n );\n }\n\n currentDocs = results.map((r) => ({\n pageContent: r[outputKey],\n metadata: {},\n }));\n }\n\n // Now, with the final result of all the inputs from the `llmChain`, we can\n // run the `combineDocumentChain` over them.\n const newInputs = {\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n };\n const result = await this.combineDocumentChain.call(\n newInputs,\n runManager?.getChild(\"combine_documents\")\n );\n\n // Return the intermediate steps results if the flag is set\n if (this.returnIntermediateSteps) {\n return { ...result, intermediateSteps };\n }\n return result;\n }\n\n _chainType() {\n return \"map_reduce_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedMapReduceDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n if (!data.combine_document_chain) {\n throw new Error(\"Missing combine_document_chain\");\n }\n\n return new MapReduceDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n combineDocumentChain: await StuffDocumentsChain.deserialize(\n data.combine_document_chain\n ),\n });\n }\n\n serialize(): SerializedMapReduceDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n combine_document_chain: this.combineDocumentChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the RefineDocumentsChain class.\n */\nexport interface RefineDocumentsChainInput extends StuffDocumentsChainInput {\n refineLLMChain: LLMChain;\n documentPrompt?: BasePromptTemplate;\n initialResponseName?: string;\n documentVariableName?: string;\n outputKey?: string;\n}\n\n/**\n * Combine documents by doing a first pass and then refining on more documents.\n * @augments BaseChain\n * @augments RefineDocumentsChainInput\n */\nexport class RefineDocumentsChain\n extends BaseChain\n implements RefineDocumentsChainInput\n{\n static lc_name() {\n return \"RefineDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n outputKey = \"output_text\";\n\n documentVariableName = \"context\";\n\n initialResponseName = \"existing_answer\";\n\n refineLLMChain: LLMChain;\n\n get defaultDocumentPrompt(): BasePromptTemplate {\n return new PromptTemplate({\n inputVariables: [\"page_content\"],\n template: \"{page_content}\",\n });\n }\n\n documentPrompt = this.defaultDocumentPrompt;\n\n get inputKeys() {\n return [\n ...new Set([\n this.inputKey,\n ...this.llmChain.inputKeys,\n ...this.refineLLMChain.inputKeys,\n ]),\n ].filter(\n (key) =>\n key !== this.documentVariableName && key !== this.initialResponseName\n );\n }\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n constructor(fields: RefineDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.refineLLMChain = fields.refineLLMChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;\n this.initialResponseName =\n fields.initialResponseName ?? this.initialResponseName;\n }\n\n /** @ignore */\n async _constructInitialInputs(doc: Document, rest: Record<string, unknown>) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { ...baseInputs, ...rest };\n return inputs;\n }\n\n /** @ignore */\n async _constructRefineInputs(doc: Document, res: string) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { [this.initialResponseName]: res, ...baseInputs };\n return inputs;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n const currentDocs = docs as Document[];\n\n const initialInputs = await this._constructInitialInputs(\n currentDocs[0],\n rest\n );\n let res = await this.llmChain.predict(\n { ...initialInputs },\n runManager?.getChild(\"answer\")\n );\n\n const refineSteps = [res];\n\n for (let i = 1; i < currentDocs.length; i += 1) {\n const refineInputs = await this._constructRefineInputs(\n currentDocs[i],\n res\n );\n const inputs = { ...refineInputs, ...rest };\n res = await this.refineLLMChain.predict(\n { ...inputs },\n runManager?.getChild(\"refine\")\n );\n refineSteps.push(res);\n }\n\n return { [this.outputKey]: res };\n }\n\n _chainType() {\n return \"refine_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedRefineDocumentsChain) {\n const SerializedLLMChain = data.llm_chain;\n\n if (!SerializedLLMChain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n const SerializedRefineDocumentChain = data.refine_llm_chain;\n\n if (!SerializedRefineDocumentChain) {\n throw new Error(\"Missing refine_llm_chain\");\n }\n\n return new RefineDocumentsChain({\n llmChain: await LLMChain.deserialize(SerializedLLMChain),\n refineLLMChain: await LLMChain.deserialize(SerializedRefineDocumentChain),\n });\n }\n\n serialize(): SerializedRefineDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n refine_llm_chain: this.refineLLMChain.serialize(),\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA4B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AC/B,YAAY,IACZ,KACD;EACD,IAAI,MAAM,MAAM,KAAK,SAAS,QAC5B,EAAE,GAAG,eAAe,EACpB,YAAY,SAAS,SAAS,CAC/B;EAED,MAAM,cAAc,CAAC,IAAI;AAEzB,OAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;GAK9C,MAAM,SAAS;IAAE,GAJI,MAAM,KAAK,uBAC9B,YAAY,IACZ,IACD;IACiC,GAAG;IAAM;AAC3C,SAAM,MAAM,KAAK,eAAe,QAC9B,EAAE,GAAG,QAAQ,EACb,YAAY,SAAS,SAAS,CAC/B;AACD,eAAY,KAAK,IAAI;;AAGvB,SAAO,GAAG,KAAK,YAAY,KAAK;;CAGlC,aAAa;AACX,SAAO;;CAGT,aAAa,YAAY,MAAsC;EAC7D,MAAM,qBAAqB,KAAK;AAEhC,MAAI,CAAC,mBACH,OAAM,IAAI,MAAM,oBAAoB;EAGtC,MAAM,gCAAgC,KAAK;AAE3C,MAAI,CAAC,8BACH,OAAM,IAAI,MAAM,2BAA2B;AAG7C,SAAO,IAAI,qBAAqB;GAC9B,UAAU,MAAMD,kBAAAA,SAAS,YAAY,mBAAmB;GACxD,gBAAgB,MAAMA,kBAAAA,SAAS,YAAY,8BAA8B;GAC1E,CAAC;;CAGJ,YAA4C;AAC1C,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,WAAW,KAAK,SAAS,WAAW;GACpC,kBAAkB,KAAK,eAAe,WAAW;GAClD"}
1
+ {"version":3,"file":"combine_docs_chain.cjs","names":["BaseChain","LLMChain","PromptTemplate"],"sources":["../../src/chains/combine_docs_chain.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport type {\n SerializedStuffDocumentsChain,\n SerializedMapReduceDocumentsChain,\n SerializedRefineDocumentsChain,\n} from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\n\n/**\n * Interface for the input properties of the StuffDocumentsChain class.\n */\nexport interface StuffDocumentsChainInput extends ChainInputs {\n /** LLM Wrapper to use after formatting documents */\n llmChain: LLMChain;\n inputKey?: string;\n /** Variable name in the LLM chain to put the documents in */\n documentVariableName?: string;\n}\n\n/**\n * Chain that combines documents by stuffing into context.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class StuffDocumentsChain\n extends BaseChain\n implements StuffDocumentsChainInput\n{\n static lc_name() {\n return \"StuffDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n get inputKeys() {\n return [this.inputKey, ...this.llmChain.inputKeys].filter(\n (key) => key !== this.documentVariableName\n );\n }\n\n get outputKeys() {\n return this.llmChain.outputKeys;\n }\n\n constructor(fields: StuffDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n }\n\n /** @ignore */\n _prepInputs(values: ChainValues): ChainValues {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n const texts = (docs as Document[]).map(({ pageContent }) => pageContent);\n const text = texts.join(\"\\n\\n\");\n return {\n ...rest,\n [this.documentVariableName]: text,\n };\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n const result = await this.llmChain.call(\n this._prepInputs(values),\n runManager?.getChild(\"combine_documents\")\n );\n return result;\n }\n\n _chainType() {\n return \"stuff_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedStuffDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n return new StuffDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n });\n }\n\n serialize(): SerializedStuffDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the MapReduceDocumentsChain\n * class.\n */\nexport interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {\n /** The maximum number of tokens before requiring to do the reduction */\n maxTokens?: number;\n /** The maximum number of iterations to run through the map */\n maxIterations?: number;\n /** Ensures that the map step is taken regardless of max tokens */\n ensureMapStep?: boolean;\n /** Chain to use to combine results of applying llm_chain to documents. */\n combineDocumentChain: StuffDocumentsChain;\n /** Return the results of the map steps in the output. */\n returnIntermediateSteps?: boolean;\n}\n\n/**\n * Combine documents by mapping a chain over them, then combining results.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class MapReduceDocumentsChain\n extends BaseChain\n implements MapReduceDocumentsChainInput\n{\n static lc_name() {\n return \"MapReduceDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n returnIntermediateSteps = false;\n\n get inputKeys() {\n return [this.inputKey, ...this.combineDocumentChain.inputKeys];\n }\n\n get outputKeys() {\n return this.combineDocumentChain.outputKeys;\n }\n\n maxTokens = 3000;\n\n maxIterations = 10;\n\n ensureMapStep = false;\n\n combineDocumentChain: StuffDocumentsChain;\n\n constructor(fields: MapReduceDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.combineDocumentChain = fields.combineDocumentChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.ensureMapStep = fields.ensureMapStep ?? this.ensureMapStep;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.maxTokens = fields.maxTokens ?? this.maxTokens;\n this.maxIterations = fields.maxIterations ?? this.maxIterations;\n this.returnIntermediateSteps = fields.returnIntermediateSteps ?? false;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n let currentDocs = docs as Document[];\n let intermediateSteps: string[] = [];\n\n // For each iteration, we'll use the `llmChain` to get a new result\n for (let i = 0; i < this.maxIterations; i += 1) {\n const inputs = currentDocs.map((d) => ({\n [this.documentVariableName]: d.pageContent,\n ...rest,\n }));\n\n const canSkipMapStep = i !== 0 || !this.ensureMapStep;\n if (canSkipMapStep) {\n // Calculate the total tokens required in the input\n const formatted =\n await this.combineDocumentChain.llmChain.prompt.format(\n this.combineDocumentChain._prepInputs({\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n })\n );\n const length =\n await this.combineDocumentChain.llmChain._getNumTokens(formatted);\n\n const withinTokenLimit = length < this.maxTokens;\n // If we can skip the map step, and we're within the token limit, we don't\n // need to run the map step, so just break out of the loop.\n if (withinTokenLimit) {\n break;\n }\n }\n\n const results = await this.llmChain.apply(\n inputs,\n // If we have a runManager, then we need to create a child for each input\n // so that we can track the progress of each input.\n runManager\n ? Array.from({ length: inputs.length }, (_, i) =>\n runManager.getChild(`map_${i + 1}`)\n )\n : undefined\n );\n const { outputKey } = this.llmChain;\n\n // If the flag is set, then concat that to the intermediate steps\n if (this.returnIntermediateSteps) {\n intermediateSteps = intermediateSteps.concat(\n results.map((r) => r[outputKey])\n );\n }\n\n currentDocs = results.map((r) => ({\n pageContent: r[outputKey],\n metadata: {},\n }));\n }\n\n // Now, with the final result of all the inputs from the `llmChain`, we can\n // run the `combineDocumentChain` over them.\n const newInputs = {\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n };\n const result = await this.combineDocumentChain.call(\n newInputs,\n runManager?.getChild(\"combine_documents\")\n );\n\n // Return the intermediate steps results if the flag is set\n if (this.returnIntermediateSteps) {\n return { ...result, intermediateSteps };\n }\n return result;\n }\n\n _chainType() {\n return \"map_reduce_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedMapReduceDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n if (!data.combine_document_chain) {\n throw new Error(\"Missing combine_document_chain\");\n }\n\n return new MapReduceDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n combineDocumentChain: await StuffDocumentsChain.deserialize(\n data.combine_document_chain\n ),\n });\n }\n\n serialize(): SerializedMapReduceDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n combine_document_chain: this.combineDocumentChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the RefineDocumentsChain class.\n */\nexport interface RefineDocumentsChainInput extends StuffDocumentsChainInput {\n refineLLMChain: LLMChain;\n documentPrompt?: BasePromptTemplate;\n initialResponseName?: string;\n documentVariableName?: string;\n outputKey?: string;\n}\n\n/**\n * Combine documents by doing a first pass and then refining on more documents.\n * @augments BaseChain\n * @augments RefineDocumentsChainInput\n */\nexport class RefineDocumentsChain\n extends BaseChain\n implements RefineDocumentsChainInput\n{\n static lc_name() {\n return \"RefineDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n outputKey = \"output_text\";\n\n documentVariableName = \"context\";\n\n initialResponseName = \"existing_answer\";\n\n refineLLMChain: LLMChain;\n\n get defaultDocumentPrompt(): BasePromptTemplate {\n return new PromptTemplate({\n inputVariables: [\"page_content\"],\n template: \"{page_content}\",\n });\n }\n\n documentPrompt = this.defaultDocumentPrompt;\n\n get inputKeys() {\n return [\n ...new Set([\n this.inputKey,\n ...this.llmChain.inputKeys,\n ...this.refineLLMChain.inputKeys,\n ]),\n ].filter(\n (key) =>\n key !== this.documentVariableName && key !== this.initialResponseName\n );\n }\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n constructor(fields: RefineDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.refineLLMChain = fields.refineLLMChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;\n this.initialResponseName =\n fields.initialResponseName ?? this.initialResponseName;\n }\n\n /** @ignore */\n async _constructInitialInputs(doc: Document, rest: Record<string, unknown>) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { ...baseInputs, ...rest };\n return inputs;\n }\n\n /** @ignore */\n async _constructRefineInputs(doc: Document, res: string) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { [this.initialResponseName]: res, ...baseInputs };\n return inputs;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n const currentDocs = docs as Document[];\n\n const initialInputs = await this._constructInitialInputs(\n currentDocs[0],\n rest\n );\n let res = await this.llmChain.predict(\n { ...initialInputs },\n runManager?.getChild(\"answer\")\n );\n\n const refineSteps = [res];\n\n for (let i = 1; i < currentDocs.length; i += 1) {\n const refineInputs = await this._constructRefineInputs(\n currentDocs[i],\n res\n );\n const inputs = { ...refineInputs, ...rest };\n res = await this.refineLLMChain.predict(\n { ...inputs },\n runManager?.getChild(\"refine\")\n );\n refineSteps.push(res);\n }\n\n return { [this.outputKey]: res };\n }\n\n _chainType() {\n return \"refine_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedRefineDocumentsChain) {\n const SerializedLLMChain = data.llm_chain;\n\n if (!SerializedLLMChain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n const SerializedRefineDocumentChain = data.refine_llm_chain;\n\n if (!SerializedRefineDocumentChain) {\n throw new Error(\"Missing refine_llm_chain\");\n }\n\n return new RefineDocumentsChain({\n llmChain: await LLMChain.deserialize(SerializedLLMChain),\n refineLLMChain: await LLMChain.deserialize(SerializedRefineDocumentChain),\n });\n }\n\n serialize(): SerializedRefineDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n refine_llm_chain: this.refineLLMChain.serialize(),\n };\n }\n}\n"],"mappings":";;;;;;;;;;AA4BA,IAAa,sBAAb,MAAa,4BACHA,aAAAA,UAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA,WAAW;CAEX,uBAAuB;CAEvB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,GAAG,KAAK,SAAS,UAAU,CAAC,QAChD,QAAQ,QAAQ,KAAK,qBACvB;;CAGH,IAAI,aAAa;AACf,SAAO,KAAK,SAAS;;CAGvB,YAAY,QAAkC;AAC5C,QAAM,OAAO;AACb,OAAK,WAAW,OAAO;AACvB,OAAK,uBACH,OAAO,wBAAwB,KAAK;AACtC,OAAK,WAAW,OAAO,YAAY,KAAK;;;CAI1C,YAAY,QAAkC;AAC5C,MAAI,EAAE,KAAK,YAAY,QACrB,OAAM,IAAI,MAAM,gBAAgB,KAAK,SAAS,aAAa;EAE7D,MAAM,GAAG,KAAK,WAAW,MAAM,GAAG,SAAS;EAE3C,MAAM,OADS,KAAoB,KAAK,EAAE,kBAAkB,YAC1C,CAAC,KAAK,OAAO;AAC/B,SAAO;GACL,GAAG;IACF,KAAK,uBAAuB;GAC9B;;;CAIH,MAAM,MACJ,QACA,YACsB;AAKtB,SAAO,MAJc,KAA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AAmB;GACxD,gBAAgB,MAAMA,kBAAAA,SAAS,YAAY,8BAA8B;GAC1E,CAAC;;CAGJ,YAA4C;AAC1C,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,WAAW,KAAK,SAAS,WAAW;GACpC,kBAAkB,KAAK,eAAe,WAAW;GAClD"}
@@ -1 +1 @@
1
- {"version":3,"file":"combine_docs_chain.js","names":[],"sources":["../../src/chains/combine_docs_chain.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport type {\n SerializedStuffDocumentsChain,\n SerializedMapReduceDocumentsChain,\n SerializedRefineDocumentsChain,\n} from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\n\n/**\n * Interface for the input properties of the StuffDocumentsChain class.\n */\nexport interface StuffDocumentsChainInput extends ChainInputs {\n /** LLM Wrapper to use after formatting documents */\n llmChain: LLMChain;\n inputKey?: string;\n /** Variable name in the LLM chain to put the documents in */\n documentVariableName?: string;\n}\n\n/**\n * Chain that combines documents by stuffing into context.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class StuffDocumentsChain\n extends BaseChain\n implements StuffDocumentsChainInput\n{\n static lc_name() {\n return \"StuffDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n get inputKeys() {\n return [this.inputKey, ...this.llmChain.inputKeys].filter(\n (key) => key !== this.documentVariableName\n );\n }\n\n get outputKeys() {\n return this.llmChain.outputKeys;\n }\n\n constructor(fields: StuffDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n }\n\n /** @ignore */\n _prepInputs(values: ChainValues): ChainValues {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n const texts = (docs as Document[]).map(({ pageContent }) => pageContent);\n const text = texts.join(\"\\n\\n\");\n return {\n ...rest,\n [this.documentVariableName]: text,\n };\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n const result = await this.llmChain.call(\n this._prepInputs(values),\n runManager?.getChild(\"combine_documents\")\n );\n return result;\n }\n\n _chainType() {\n return \"stuff_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedStuffDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n return new StuffDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n });\n }\n\n serialize(): SerializedStuffDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the MapReduceDocumentsChain\n * class.\n */\nexport interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {\n /** The maximum number of tokens before requiring to do the reduction */\n maxTokens?: number;\n /** The maximum number of iterations to run through the map */\n maxIterations?: number;\n /** Ensures that the map step is taken regardless of max tokens */\n ensureMapStep?: boolean;\n /** Chain to use to combine results of applying llm_chain to documents. */\n combineDocumentChain: StuffDocumentsChain;\n /** Return the results of the map steps in the output. */\n returnIntermediateSteps?: boolean;\n}\n\n/**\n * Combine documents by mapping a chain over them, then combining results.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class MapReduceDocumentsChain\n extends BaseChain\n implements MapReduceDocumentsChainInput\n{\n static lc_name() {\n return \"MapReduceDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n returnIntermediateSteps = false;\n\n get inputKeys() {\n return [this.inputKey, ...this.combineDocumentChain.inputKeys];\n }\n\n get outputKeys() {\n return this.combineDocumentChain.outputKeys;\n }\n\n maxTokens = 3000;\n\n maxIterations = 10;\n\n ensureMapStep = false;\n\n combineDocumentChain: StuffDocumentsChain;\n\n constructor(fields: MapReduceDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.combineDocumentChain = fields.combineDocumentChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.ensureMapStep = fields.ensureMapStep ?? this.ensureMapStep;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.maxTokens = fields.maxTokens ?? this.maxTokens;\n this.maxIterations = fields.maxIterations ?? this.maxIterations;\n this.returnIntermediateSteps = fields.returnIntermediateSteps ?? false;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n let currentDocs = docs as Document[];\n let intermediateSteps: string[] = [];\n\n // For each iteration, we'll use the `llmChain` to get a new result\n for (let i = 0; i < this.maxIterations; i += 1) {\n const inputs = currentDocs.map((d) => ({\n [this.documentVariableName]: d.pageContent,\n ...rest,\n }));\n\n const canSkipMapStep = i !== 0 || !this.ensureMapStep;\n if (canSkipMapStep) {\n // Calculate the total tokens required in the input\n const formatted =\n await this.combineDocumentChain.llmChain.prompt.format(\n this.combineDocumentChain._prepInputs({\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n })\n );\n const length =\n await this.combineDocumentChain.llmChain._getNumTokens(formatted);\n\n const withinTokenLimit = length < this.maxTokens;\n // If we can skip the map step, and we're within the token limit, we don't\n // need to run the map step, so just break out of the loop.\n if (withinTokenLimit) {\n break;\n }\n }\n\n const results = await this.llmChain.apply(\n inputs,\n // If we have a runManager, then we need to create a child for each input\n // so that we can track the progress of each input.\n runManager\n ? Array.from({ length: inputs.length }, (_, i) =>\n runManager.getChild(`map_${i + 1}`)\n )\n : undefined\n );\n const { outputKey } = this.llmChain;\n\n // If the flag is set, then concat that to the intermediate steps\n if (this.returnIntermediateSteps) {\n intermediateSteps = intermediateSteps.concat(\n results.map((r) => r[outputKey])\n );\n }\n\n currentDocs = results.map((r) => ({\n pageContent: r[outputKey],\n metadata: {},\n }));\n }\n\n // Now, with the final result of all the inputs from the `llmChain`, we can\n // run the `combineDocumentChain` over them.\n const newInputs = {\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n };\n const result = await this.combineDocumentChain.call(\n newInputs,\n runManager?.getChild(\"combine_documents\")\n );\n\n // Return the intermediate steps results if the flag is set\n if (this.returnIntermediateSteps) {\n return { ...result, intermediateSteps };\n }\n return result;\n }\n\n _chainType() {\n return \"map_reduce_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedMapReduceDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n if (!data.combine_document_chain) {\n throw new Error(\"Missing combine_document_chain\");\n }\n\n return new MapReduceDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n combineDocumentChain: await StuffDocumentsChain.deserialize(\n data.combine_document_chain\n ),\n });\n }\n\n serialize(): SerializedMapReduceDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n combine_document_chain: this.combineDocumentChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the RefineDocumentsChain class.\n */\nexport interface RefineDocumentsChainInput extends StuffDocumentsChainInput {\n refineLLMChain: LLMChain;\n documentPrompt?: BasePromptTemplate;\n initialResponseName?: string;\n documentVariableName?: string;\n outputKey?: string;\n}\n\n/**\n * Combine documents by doing a first pass and then refining on more documents.\n * @augments BaseChain\n * @augments RefineDocumentsChainInput\n */\nexport class RefineDocumentsChain\n extends BaseChain\n implements RefineDocumentsChainInput\n{\n static lc_name() {\n return \"RefineDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n outputKey = \"output_text\";\n\n documentVariableName = \"context\";\n\n initialResponseName = \"existing_answer\";\n\n refineLLMChain: LLMChain;\n\n get defaultDocumentPrompt(): BasePromptTemplate {\n return new PromptTemplate({\n inputVariables: [\"page_content\"],\n template: \"{page_content}\",\n });\n }\n\n documentPrompt = this.defaultDocumentPrompt;\n\n get inputKeys() {\n return [\n ...new Set([\n this.inputKey,\n ...this.llmChain.inputKeys,\n ...this.refineLLMChain.inputKeys,\n ]),\n ].filter(\n (key) =>\n key !== this.documentVariableName && key !== this.initialResponseName\n );\n }\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n constructor(fields: RefineDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.refineLLMChain = fields.refineLLMChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;\n this.initialResponseName =\n fields.initialResponseName ?? this.initialResponseName;\n }\n\n /** @ignore */\n async _constructInitialInputs(doc: Document, rest: Record<string, unknown>) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { ...baseInputs, ...rest };\n return inputs;\n }\n\n /** @ignore */\n async _constructRefineInputs(doc: Document, res: string) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { [this.initialResponseName]: res, ...baseInputs };\n return inputs;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n const currentDocs = docs as Document[];\n\n const initialInputs = await this._constructInitialInputs(\n currentDocs[0],\n rest\n );\n let res = await this.llmChain.predict(\n { ...initialInputs },\n runManager?.getChild(\"answer\")\n );\n\n const refineSteps = [res];\n\n for (let i = 1; i < currentDocs.length; i += 1) {\n const refineInputs = await this._constructRefineInputs(\n currentDocs[i],\n res\n );\n const inputs = { ...refineInputs, ...rest };\n res = await this.refineLLMChain.predict(\n { ...inputs },\n runManager?.getChild(\"refine\")\n );\n refineSteps.push(res);\n }\n\n return { [this.outputKey]: res };\n }\n\n _chainType() {\n return \"refine_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedRefineDocumentsChain) {\n const SerializedLLMChain = data.llm_chain;\n\n if (!SerializedLLMChain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n const SerializedRefineDocumentChain = data.refine_llm_chain;\n\n if (!SerializedRefineDocumentChain) {\n throw new Error(\"Missing refine_llm_chain\");\n }\n\n return new RefineDocumentsChain({\n llmChain: await LLMChain.deserialize(SerializedLLMChain),\n refineLLMChain: await LLMChain.deserialize(SerializedRefineDocumentChain),\n });\n }\n\n serialize(): SerializedRefineDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n refine_llm_chain: this.refineLLMChain.serialize(),\n };\n }\n}\n"],"mappings":";;;;;;;;;AA4BA,IAAa,sBAAb,MAAa,4BACH,UAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA,WAAW;CAEX,uBAAuB;CAEvB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,GAAG,KAAK,SAAS,UAAU,CAAC,QAChD,QAAQ,QAAQ,KAAK,qBACvB;;CAGH,IAAI,aAAa;AACf,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;IAAM;AAC3C,SAAM,MAAM,KAAK,eAAe,QAC9B,EAAE,GAAG,QAAQ,EACb,YAAY,SAAS,SAAS,CAC/B;AACD,eAAY,KAAK,IAAI;;AAGvB,SAAO,GAAG,KAAK,YAAY,KAAK;;CAGlC,aAAa;AACX,SAAO;;CAGT,aAAa,YAAY,MAAsC;EAC7D,MAAM,qBAAqB,KAAK;AAEhC,MAAI,CAAC,mBACH,OAAM,IAAI,MAAM,oBAAoB;EAGtC,MAAM,gCAAgC,KAAK;AAE3C,MAAI,CAAC,8BACH,OAAM,IAAI,MAAM,2BAA2B;AAG7C,SAAO,IAAI,qBAAqB;GAC9B,UAAU,MAAM,SAAS,YAAY,mBAAmB;GACxD,gBAAgB,MAAM,SAAS,YAAY,8BAA8B;GAC1E,CAAC;;CAGJ,YAA4C;AAC1C,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,WAAW,KAAK,SAAS,WAAW;GACpC,kBAAkB,KAAK,eAAe,WAAW;GAClD"}
1
+ {"version":3,"file":"combine_docs_chain.js","names":[],"sources":["../../src/chains/combine_docs_chain.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport type {\n SerializedStuffDocumentsChain,\n SerializedMapReduceDocumentsChain,\n SerializedRefineDocumentsChain,\n} from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\n\n/**\n * Interface for the input properties of the StuffDocumentsChain class.\n */\nexport interface StuffDocumentsChainInput extends ChainInputs {\n /** LLM Wrapper to use after formatting documents */\n llmChain: LLMChain;\n inputKey?: string;\n /** Variable name in the LLM chain to put the documents in */\n documentVariableName?: string;\n}\n\n/**\n * Chain that combines documents by stuffing into context.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class StuffDocumentsChain\n extends BaseChain\n implements StuffDocumentsChainInput\n{\n static lc_name() {\n return \"StuffDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n get inputKeys() {\n return [this.inputKey, ...this.llmChain.inputKeys].filter(\n (key) => key !== this.documentVariableName\n );\n }\n\n get outputKeys() {\n return this.llmChain.outputKeys;\n }\n\n constructor(fields: StuffDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n }\n\n /** @ignore */\n _prepInputs(values: ChainValues): ChainValues {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n const texts = (docs as Document[]).map(({ pageContent }) => pageContent);\n const text = texts.join(\"\\n\\n\");\n return {\n ...rest,\n [this.documentVariableName]: text,\n };\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n const result = await this.llmChain.call(\n this._prepInputs(values),\n runManager?.getChild(\"combine_documents\")\n );\n return result;\n }\n\n _chainType() {\n return \"stuff_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedStuffDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n return new StuffDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n });\n }\n\n serialize(): SerializedStuffDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the MapReduceDocumentsChain\n * class.\n */\nexport interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {\n /** The maximum number of tokens before requiring to do the reduction */\n maxTokens?: number;\n /** The maximum number of iterations to run through the map */\n maxIterations?: number;\n /** Ensures that the map step is taken regardless of max tokens */\n ensureMapStep?: boolean;\n /** Chain to use to combine results of applying llm_chain to documents. */\n combineDocumentChain: StuffDocumentsChain;\n /** Return the results of the map steps in the output. */\n returnIntermediateSteps?: boolean;\n}\n\n/**\n * Combine documents by mapping a chain over them, then combining results.\n * @augments BaseChain\n * @augments StuffDocumentsChainInput\n */\nexport class MapReduceDocumentsChain\n extends BaseChain\n implements MapReduceDocumentsChainInput\n{\n static lc_name() {\n return \"MapReduceDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n documentVariableName = \"context\";\n\n returnIntermediateSteps = false;\n\n get inputKeys() {\n return [this.inputKey, ...this.combineDocumentChain.inputKeys];\n }\n\n get outputKeys() {\n return this.combineDocumentChain.outputKeys;\n }\n\n maxTokens = 3000;\n\n maxIterations = 10;\n\n ensureMapStep = false;\n\n combineDocumentChain: StuffDocumentsChain;\n\n constructor(fields: MapReduceDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.combineDocumentChain = fields.combineDocumentChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.ensureMapStep = fields.ensureMapStep ?? this.ensureMapStep;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.maxTokens = fields.maxTokens ?? this.maxTokens;\n this.maxIterations = fields.maxIterations ?? this.maxIterations;\n this.returnIntermediateSteps = fields.returnIntermediateSteps ?? false;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n let currentDocs = docs as Document[];\n let intermediateSteps: string[] = [];\n\n // For each iteration, we'll use the `llmChain` to get a new result\n for (let i = 0; i < this.maxIterations; i += 1) {\n const inputs = currentDocs.map((d) => ({\n [this.documentVariableName]: d.pageContent,\n ...rest,\n }));\n\n const canSkipMapStep = i !== 0 || !this.ensureMapStep;\n if (canSkipMapStep) {\n // Calculate the total tokens required in the input\n const formatted =\n await this.combineDocumentChain.llmChain.prompt.format(\n this.combineDocumentChain._prepInputs({\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n })\n );\n const length =\n await this.combineDocumentChain.llmChain._getNumTokens(formatted);\n\n const withinTokenLimit = length < this.maxTokens;\n // If we can skip the map step, and we're within the token limit, we don't\n // need to run the map step, so just break out of the loop.\n if (withinTokenLimit) {\n break;\n }\n }\n\n const results = await this.llmChain.apply(\n inputs,\n // If we have a runManager, then we need to create a child for each input\n // so that we can track the progress of each input.\n runManager\n ? Array.from({ length: inputs.length }, (_, i) =>\n runManager.getChild(`map_${i + 1}`)\n )\n : undefined\n );\n const { outputKey } = this.llmChain;\n\n // If the flag is set, then concat that to the intermediate steps\n if (this.returnIntermediateSteps) {\n intermediateSteps = intermediateSteps.concat(\n results.map((r) => r[outputKey])\n );\n }\n\n currentDocs = results.map((r) => ({\n pageContent: r[outputKey],\n metadata: {},\n }));\n }\n\n // Now, with the final result of all the inputs from the `llmChain`, we can\n // run the `combineDocumentChain` over them.\n const newInputs = {\n [this.combineDocumentChain.inputKey]: currentDocs,\n ...rest,\n };\n const result = await this.combineDocumentChain.call(\n newInputs,\n runManager?.getChild(\"combine_documents\")\n );\n\n // Return the intermediate steps results if the flag is set\n if (this.returnIntermediateSteps) {\n return { ...result, intermediateSteps };\n }\n return result;\n }\n\n _chainType() {\n return \"map_reduce_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedMapReduceDocumentsChain) {\n if (!data.llm_chain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n if (!data.combine_document_chain) {\n throw new Error(\"Missing combine_document_chain\");\n }\n\n return new MapReduceDocumentsChain({\n llmChain: await LLMChain.deserialize(data.llm_chain),\n combineDocumentChain: await StuffDocumentsChain.deserialize(\n data.combine_document_chain\n ),\n });\n }\n\n serialize(): SerializedMapReduceDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n combine_document_chain: this.combineDocumentChain.serialize(),\n };\n }\n}\n\n/**\n * Interface for the input properties of the RefineDocumentsChain class.\n */\nexport interface RefineDocumentsChainInput extends StuffDocumentsChainInput {\n refineLLMChain: LLMChain;\n documentPrompt?: BasePromptTemplate;\n initialResponseName?: string;\n documentVariableName?: string;\n outputKey?: string;\n}\n\n/**\n * Combine documents by doing a first pass and then refining on more documents.\n * @augments BaseChain\n * @augments RefineDocumentsChainInput\n */\nexport class RefineDocumentsChain\n extends BaseChain\n implements RefineDocumentsChainInput\n{\n static lc_name() {\n return \"RefineDocumentsChain\";\n }\n\n llmChain: LLMChain;\n\n inputKey = \"input_documents\";\n\n outputKey = \"output_text\";\n\n documentVariableName = \"context\";\n\n initialResponseName = \"existing_answer\";\n\n refineLLMChain: LLMChain;\n\n get defaultDocumentPrompt(): BasePromptTemplate {\n return new PromptTemplate({\n inputVariables: [\"page_content\"],\n template: \"{page_content}\",\n });\n }\n\n documentPrompt = this.defaultDocumentPrompt;\n\n get inputKeys() {\n return [\n ...new Set([\n this.inputKey,\n ...this.llmChain.inputKeys,\n ...this.refineLLMChain.inputKeys,\n ]),\n ].filter(\n (key) =>\n key !== this.documentVariableName && key !== this.initialResponseName\n );\n }\n\n get outputKeys() {\n return [this.outputKey];\n }\n\n constructor(fields: RefineDocumentsChainInput) {\n super(fields);\n this.llmChain = fields.llmChain;\n this.refineLLMChain = fields.refineLLMChain;\n this.documentVariableName =\n fields.documentVariableName ?? this.documentVariableName;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.outputKey = fields.outputKey ?? this.outputKey;\n this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;\n this.initialResponseName =\n fields.initialResponseName ?? this.initialResponseName;\n }\n\n /** @ignore */\n async _constructInitialInputs(doc: Document, rest: Record<string, unknown>) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { ...baseInputs, ...rest };\n return inputs;\n }\n\n /** @ignore */\n async _constructRefineInputs(doc: Document, res: string) {\n const baseInfo: Record<string, unknown> = {\n page_content: doc.pageContent,\n ...doc.metadata,\n };\n const documentInfo: Record<string, unknown> = {};\n this.documentPrompt.inputVariables.forEach((value) => {\n documentInfo[value] = baseInfo[value];\n });\n const baseInputs: Record<string, unknown> = {\n [this.documentVariableName]: await this.documentPrompt.format({\n ...documentInfo,\n }),\n };\n const inputs = { [this.initialResponseName]: res, ...baseInputs };\n return inputs;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Document key ${this.inputKey} not found.`);\n }\n const { [this.inputKey]: docs, ...rest } = values;\n\n const currentDocs = docs as Document[];\n\n const initialInputs = await this._constructInitialInputs(\n currentDocs[0],\n rest\n );\n let res = await this.llmChain.predict(\n { ...initialInputs },\n runManager?.getChild(\"answer\")\n );\n\n const refineSteps = [res];\n\n for (let i = 1; i < currentDocs.length; i += 1) {\n const refineInputs = await this._constructRefineInputs(\n currentDocs[i],\n res\n );\n const inputs = { ...refineInputs, ...rest };\n res = await this.refineLLMChain.predict(\n { ...inputs },\n runManager?.getChild(\"refine\")\n );\n refineSteps.push(res);\n }\n\n return { [this.outputKey]: res };\n }\n\n _chainType() {\n return \"refine_documents_chain\" as const;\n }\n\n static async deserialize(data: SerializedRefineDocumentsChain) {\n const SerializedLLMChain = data.llm_chain;\n\n if (!SerializedLLMChain) {\n throw new Error(\"Missing llm_chain\");\n }\n\n const SerializedRefineDocumentChain = data.refine_llm_chain;\n\n if (!SerializedRefineDocumentChain) {\n throw new Error(\"Missing refine_llm_chain\");\n }\n\n return new RefineDocumentsChain({\n llmChain: await LLMChain.deserialize(SerializedLLMChain),\n refineLLMChain: await LLMChain.deserialize(SerializedRefineDocumentChain),\n });\n }\n\n serialize(): SerializedRefineDocumentsChain {\n return {\n _type: this._chainType(),\n llm_chain: this.llmChain.serialize(),\n refine_llm_chain: this.refineLLMChain.serialize(),\n };\n }\n}\n"],"mappings":";;;;;;;;;AA4BA,IAAa,sBAAb,MAAa,4BACH,UAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA,WAAW;CAEX,uBAAuB;CAEvB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,GAAG,KAAK,SAAS,UAAU,CAAC,QAChD,QAAQ,QAAQ,KAAK,qBACvB;;CAGH,IAAI,aAAa;AACf,SAAO,KAAK,SAAS;;CAGvB,YAAY,QAAkC;AAC5C,QAAM,OAAO;AACb,OAAK,WAAW,OAAO;AACvB,OAAK,uBACH,OAAO,wBAAwB,KAAK;AACtC,OAAK,WAAW,OAAO,YAAY,KAAK;;;CAI1C,YAAY,QAAkC;AAC5C,MAAI,EAAE,KAAK,YAAY,QACrB,OAAM,IAAI,MAAM,gBAAgB,KAAK,SAAS,aAAa;EAE7D,MAAM,GAAG,KAAK,WAAW,MAAM,GAAG,SAAS;EAE3C,MAAM,OADS,KAAoB,KAAK,EAAE,kBAAkB,YAC1C,CAAC,KAAK,OAAO;AAC/B,SAAO;GACL,GAAG;IACF,KAAK,uBAAuB;GAC9B;;;CAIH,MAAM,MACJ,QACA,YACsB;AAKtB,SAAO,MAJc,KAAK,SAAS,KACjC,KAAK,YAAY,OAAO,EACxB,YAAY,SAAS,oBAAoB,CAC1C;;CAIH,aAAa;AACX,SAAO;;CAGT,aAAa,YAAY,MAAqC;AAC5D,MAAI,CAAC,KAAK,UACR,OAAM,IAAI,MAAM,oBAAoB;AAGtC,SAAO,IAAI,oBAAoB,EAC7B,UAAU,MAAM,SAAS,YAAY,KAAK,UAAU,EACrD,CAAC;;CAGJ,YAA2C;AACzC,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,WAAW,KAAK,SAAS,WAAW;GACr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KAAK,SAAS,aAAa;EAE7D,MAAM,GAAG,KAAK,WAAW,MAAM,GAAG,SAAS;EAE3C,MAAM,cAAc;EAEpB,MAAM,gBAAgB,MAAM,KAAK,wBAC/B,YAAY,IACZ,KACD;EACD,IAAI,MAAM,MAAM,KAAK,SAAS,QAC5B,EAAE,GAAG,eAAe,EACpB,YAAY,SAAS,SAAS,CAC/B;EAED,MAAM,cAAc,CAAC,IAAI;AAEzB,OAAK,IAAI,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK,GAAG;GAK9C,MAAM,SAAS;IAAE,GAAG,MAJO,KAAK,uBAC9B,YAAY,IACZ,IACD;IACiC,GAAG;IAAM;AAC3C,SAAM,MAAM,KAAK,eAAe,QAC9B,EAAE,GAAG,QAAQ,EACb,YAAY,SAAS,SAAS,CAC/B;AACD,eAAY,KAAK,IAAI;;AAGvB,SAAO,GAAG,KAAK,YAAY,KAAK;;CAGlC,aAAa;AACX,SAAO;;CAGT,aAAa,YAAY,MAAsC;EAC7D,MAAM,qBAAqB,KAAK;AAEhC,MAAI,CAAC,mBACH,OAAM,IAAI,MAAM,oBAAoB;EAGtC,MAAM,gCAAgC,KAAK;AAE3C,MAAI,CAAC,8BACH,OAAM,IAAI,MAAM,2BAA2B;AAG7C,SAAO,IAAI,qBAAqB;GAC9B,UAAU,MAAM,SAAS,YAAY,mBAAmB;GACxD,gBAAgB,MAAM,SAAS,YAAY,8BAA8B;GAC1E,CAAC;;CAGJ,YAA4C;AAC1C,SAAO;GACL,OAAO,KAAK,YAAY;GACxB,WAAW,KAAK,SAAS,WAAW;GACpC,kBAAkB,KAAK,eAAe,WAAW;GAClD"}
@@ -1 +1 @@
1
- {"version":3,"file":"base.cjs","names":[],"sources":["../../../src/chains/combine_documents/base.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\n\nexport const DEFAULT_DOCUMENT_SEPARATOR = \"\\n\\n\";\n\nexport const DOCUMENTS_KEY = \"context\";\nexport const INTERMEDIATE_STEPS_KEY = \"intermediate_steps\";\n\nexport const DEFAULT_DOCUMENT_PROMPT =\n /* #__PURE__ */ PromptTemplate.fromTemplate(\"{page_content}\");\n\nexport async function formatDocuments({\n documentPrompt,\n documentSeparator,\n documents,\n config,\n}: {\n documentPrompt: BasePromptTemplate;\n documentSeparator: string;\n documents: Document[];\n config?: RunnableConfig;\n}) {\n if (documents == null || documents.length === 0) {\n return \"\";\n }\n const formattedDocs = await Promise.all(\n documents.map((document) =>\n documentPrompt\n .withConfig({ runName: \"document_formatter\" })\n .invoke(\n { ...document.metadata, page_content: document.pageContent },\n config\n )\n )\n );\n return formattedDocs.join(documentSeparator);\n}\n"],"mappings":";;;AAMA,MAAa,gBAAgB;AAG7B,MAAa,0BACK,wCAAA,eAAe,aAAa,iBAAiB;AAE/D,eAAsB,gBAAgB,EACpC,gBACA,mBACA,WACA,UAMC;AACD,KAAI,aAAa,QAAQ,UAAU,WAAW,EAC5C,QAAO;AAYT,SAVsB,MAAM,QAAQ,IAClC,UAAU,KAAK,aACb,eACG,WAAW,EAAE,SAAS,sBAAsB,CAAC,CAC7C,OACC;EAAE,GAAG,SAAS;EAAU,cAAc,SAAS;EAAa,EAC5D,OACD,CACJ,CACF,EACoB,KAAK,kBAAkB"}
1
+ {"version":3,"file":"base.cjs","names":[],"sources":["../../../src/chains/combine_documents/base.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\n\nexport const DEFAULT_DOCUMENT_SEPARATOR = \"\\n\\n\";\n\nexport const DOCUMENTS_KEY = \"context\";\nexport const INTERMEDIATE_STEPS_KEY = \"intermediate_steps\";\n\nexport const DEFAULT_DOCUMENT_PROMPT =\n /* #__PURE__ */ PromptTemplate.fromTemplate(\"{page_content}\");\n\nexport async function formatDocuments({\n documentPrompt,\n documentSeparator,\n documents,\n config,\n}: {\n documentPrompt: BasePromptTemplate;\n documentSeparator: string;\n documents: Document[];\n config?: RunnableConfig;\n}) {\n if (documents == null || documents.length === 0) {\n return \"\";\n }\n const formattedDocs = await Promise.all(\n documents.map((document) =>\n documentPrompt\n .withConfig({ runName: \"document_formatter\" })\n .invoke(\n { ...document.metadata, page_content: document.pageContent },\n config\n )\n )\n );\n return formattedDocs.join(documentSeparator);\n}\n"],"mappings":";;;AAMA,MAAa,gBAAgB;AAG7B,MAAa,0BACK,wCAAA,eAAe,aAAa,iBAAiB;AAE/D,eAAsB,gBAAgB,EACpC,gBACA,mBACA,WACA,UAMC;AACD,KAAI,aAAa,QAAQ,UAAU,WAAW,EAC5C,QAAO;AAYT,SAAO,MAVqB,QAAQ,IAClC,UAAU,KAAK,aACb,eACG,WAAW,EAAE,SAAS,sBAAsB,CAAC,CAC7C,OACC;EAAE,GAAG,SAAS;EAAU,cAAc,SAAS;EAAa,EAC5D,OACD,CACJ,CACF,EACoB,KAAK,kBAAkB"}
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","names":[],"sources":["../../../src/chains/combine_documents/base.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\n\nexport const DEFAULT_DOCUMENT_SEPARATOR = \"\\n\\n\";\n\nexport const DOCUMENTS_KEY = \"context\";\nexport const INTERMEDIATE_STEPS_KEY = \"intermediate_steps\";\n\nexport const DEFAULT_DOCUMENT_PROMPT =\n /* #__PURE__ */ PromptTemplate.fromTemplate(\"{page_content}\");\n\nexport async function formatDocuments({\n documentPrompt,\n documentSeparator,\n documents,\n config,\n}: {\n documentPrompt: BasePromptTemplate;\n documentSeparator: string;\n documents: Document[];\n config?: RunnableConfig;\n}) {\n if (documents == null || documents.length === 0) {\n return \"\";\n }\n const formattedDocs = await Promise.all(\n documents.map((document) =>\n documentPrompt\n .withConfig({ runName: \"document_formatter\" })\n .invoke(\n { ...document.metadata, page_content: document.pageContent },\n config\n )\n )\n );\n return formattedDocs.join(documentSeparator);\n}\n"],"mappings":";;AAMA,MAAa,gBAAgB;AAG7B,MAAa,0BACK,+BAAe,aAAa,iBAAiB;AAE/D,eAAsB,gBAAgB,EACpC,gBACA,mBACA,WACA,UAMC;AACD,KAAI,aAAa,QAAQ,UAAU,WAAW,EAC5C,QAAO;AAYT,SAVsB,MAAM,QAAQ,IAClC,UAAU,KAAK,aACb,eACG,WAAW,EAAE,SAAS,sBAAsB,CAAC,CAC7C,OACC;EAAE,GAAG,SAAS;EAAU,cAAc,SAAS;EAAa,EAC5D,OACD,CACJ,CACF,EACoB,KAAK,kBAAkB"}
1
+ {"version":3,"file":"base.js","names":[],"sources":["../../../src/chains/combine_documents/base.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\n\nexport const DEFAULT_DOCUMENT_SEPARATOR = \"\\n\\n\";\n\nexport const DOCUMENTS_KEY = \"context\";\nexport const INTERMEDIATE_STEPS_KEY = \"intermediate_steps\";\n\nexport const DEFAULT_DOCUMENT_PROMPT =\n /* #__PURE__ */ PromptTemplate.fromTemplate(\"{page_content}\");\n\nexport async function formatDocuments({\n documentPrompt,\n documentSeparator,\n documents,\n config,\n}: {\n documentPrompt: BasePromptTemplate;\n documentSeparator: string;\n documents: Document[];\n config?: RunnableConfig;\n}) {\n if (documents == null || documents.length === 0) {\n return \"\";\n }\n const formattedDocs = await Promise.all(\n documents.map((document) =>\n documentPrompt\n .withConfig({ runName: \"document_formatter\" })\n .invoke(\n { ...document.metadata, page_content: document.pageContent },\n config\n )\n )\n );\n return formattedDocs.join(documentSeparator);\n}\n"],"mappings":";;AAMA,MAAa,gBAAgB;AAG7B,MAAa,0BACK,+BAAe,aAAa,iBAAiB;AAE/D,eAAsB,gBAAgB,EACpC,gBACA,mBACA,WACA,UAMC;AACD,KAAI,aAAa,QAAQ,UAAU,WAAW,EAC5C,QAAO;AAYT,SAAO,MAVqB,QAAQ,IAClC,UAAU,KAAK,aACb,eACG,WAAW,EAAE,SAAS,sBAAsB,CAAC,CAC7C,OACC;EAAE,GAAG,SAAS;EAAU,cAAc,SAAS;EAAa,EAC5D,OACD,CACJ,CACF,EACoB,KAAK,kBAAkB"}
@@ -1 +1 @@
1
- {"version":3,"file":"reduce.cjs","names":[],"sources":["../../../src/chains/combine_documents/reduce.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\n\n/**\n * Splits a list of documents into sublists based on a maximum token limit.\n *\n * @param {Document[]} docs - The list of documents to be split.\n * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.\n * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.\n *\n * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.\n *\n * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.\n */\nexport function splitListOfDocs(\n docs: Document[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n lengthFunc: (...args: any[]) => any,\n tokenMax: number\n): Document[][] {\n const newResultDocList: Document[][] = [];\n let subResultDocs: Document[] = [];\n for (const doc of docs) {\n subResultDocs.push(doc);\n const numTokens = lengthFunc(subResultDocs);\n if (numTokens > tokenMax) {\n if (subResultDocs.length === 1) {\n throw new Error(\n \"A single document was longer than the context length, we cannot handle this.\"\n );\n }\n newResultDocList.push(subResultDocs.slice(0, -1));\n subResultDocs = subResultDocs.slice(-1);\n }\n }\n newResultDocList.push(subResultDocs);\n return newResultDocList;\n}\n\n/**\n * Collapses a list of documents into a single document.\n *\n * This function takes a list of documents and a function to combine the content of these documents.\n * It combines the content of the documents using the provided function and merges the metadata of all documents.\n * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.\n *\n * @param {Document[]} docs - The list of documents to be collapsed.\n * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.\n *\n * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.\n *\n * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.\n */\nexport async function collapseDocs(\n docs: Document[],\n combineDocumentFunc: (docs: Document[]) => Promise<string>\n): Promise<Document> {\n const result = await combineDocumentFunc(docs);\n return { pageContent: result, metadata: collapseDocsMetadata(docs) };\n}\n\nfunction collapseDocsMetadata(docs: Document[]): Document[\"metadata\"] {\n const combinedMetadata: Record<string, string> = {};\n for (const key in docs[0].metadata) {\n if (key in docs[0].metadata) {\n combinedMetadata[key] = String(docs[0].metadata[key]);\n }\n }\n for (const doc of docs.slice(1)) {\n for (const key in doc.metadata) {\n if (key in combinedMetadata) {\n combinedMetadata[key] += `, ${doc.metadata[key]}`;\n } else {\n combinedMetadata[key] = String(doc.metadata[key]);\n }\n }\n }\n return combinedMetadata;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,SAAgB,gBACd,MAEA,YACA,UACc;CACd,MAAM,mBAAiC,EAAE;CACzC,IAAI,gBAA4B,EAAE;AAClC,MAAK,MAAM,OAAO,MAAM;AACtB,gBAAc,KAAK,IAAI;AAEvB,MADkB,WAAW,cAAc,GAC3B,UAAU;AACxB,OAAI,cAAc,WAAW,EAC3B,OAAM,IAAI,MACR,+EACD;AAEH,oBAAiB,KAAK,cAAc,MAAM,GAAG,GAAG,CAAC;AACjD,mBAAgB,cAAc,MAAM,GAAG;;;AAG3C,kBAAiB,KAAK,cAAc;AACpC,QAAO;;;;;;;;;;;;;;;;AAiBT,eAAsB,aACpB,MACA,qBACmB;AAEnB,QAAO;EAAE,aADM,MAAM,oBAAoB,KAAK;EAChB,UAAU,qBAAqB,KAAK;EAAE;;AAGtE,SAAS,qBAAqB,MAAwC;CACpE,MAAM,mBAA2C,EAAE;AACnD,MAAK,MAAM,OAAO,KAAK,GAAG,SACxB,KAAI,OAAO,KAAK,GAAG,SACjB,kBAAiB,OAAO,OAAO,KAAK,GAAG,SAAS,KAAK;AAGzD,MAAK,MAAM,OAAO,KAAK,MAAM,EAAE,CAC7B,MAAK,MAAM,OAAO,IAAI,SACpB,KAAI,OAAO,iBACT,kBAAiB,QAAQ,KAAK,IAAI,SAAS;KAE3C,kBAAiB,OAAO,OAAO,IAAI,SAAS,KAAK;AAIvD,QAAO"}
1
+ {"version":3,"file":"reduce.cjs","names":[],"sources":["../../../src/chains/combine_documents/reduce.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\n\n/**\n * Splits a list of documents into sublists based on a maximum token limit.\n *\n * @param {Document[]} docs - The list of documents to be split.\n * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.\n * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.\n *\n * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.\n *\n * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.\n */\nexport function splitListOfDocs(\n docs: Document[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n lengthFunc: (...args: any[]) => any,\n tokenMax: number\n): Document[][] {\n const newResultDocList: Document[][] = [];\n let subResultDocs: Document[] = [];\n for (const doc of docs) {\n subResultDocs.push(doc);\n const numTokens = lengthFunc(subResultDocs);\n if (numTokens > tokenMax) {\n if (subResultDocs.length === 1) {\n throw new Error(\n \"A single document was longer than the context length, we cannot handle this.\"\n );\n }\n newResultDocList.push(subResultDocs.slice(0, -1));\n subResultDocs = subResultDocs.slice(-1);\n }\n }\n newResultDocList.push(subResultDocs);\n return newResultDocList;\n}\n\n/**\n * Collapses a list of documents into a single document.\n *\n * This function takes a list of documents and a function to combine the content of these documents.\n * It combines the content of the documents using the provided function and merges the metadata of all documents.\n * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.\n *\n * @param {Document[]} docs - The list of documents to be collapsed.\n * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.\n *\n * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.\n *\n * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.\n */\nexport async function collapseDocs(\n docs: Document[],\n combineDocumentFunc: (docs: Document[]) => Promise<string>\n): Promise<Document> {\n const result = await combineDocumentFunc(docs);\n return { pageContent: result, metadata: collapseDocsMetadata(docs) };\n}\n\nfunction collapseDocsMetadata(docs: Document[]): Document[\"metadata\"] {\n const combinedMetadata: Record<string, string> = {};\n for (const key in docs[0].metadata) {\n if (key in docs[0].metadata) {\n combinedMetadata[key] = String(docs[0].metadata[key]);\n }\n }\n for (const doc of docs.slice(1)) {\n for (const key in doc.metadata) {\n if (key in combinedMetadata) {\n combinedMetadata[key] += `, ${doc.metadata[key]}`;\n } else {\n combinedMetadata[key] = String(doc.metadata[key]);\n }\n }\n }\n return combinedMetadata;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,SAAgB,gBACd,MAEA,YACA,UACc;CACd,MAAM,mBAAiC,EAAE;CACzC,IAAI,gBAA4B,EAAE;AAClC,MAAK,MAAM,OAAO,MAAM;AACtB,gBAAc,KAAK,IAAI;AAEvB,MADkB,WAAW,cAChB,GAAG,UAAU;AACxB,OAAI,cAAc,WAAW,EAC3B,OAAM,IAAI,MACR,+EACD;AAEH,oBAAiB,KAAK,cAAc,MAAM,GAAG,GAAG,CAAC;AACjD,mBAAgB,cAAc,MAAM,GAAG;;;AAG3C,kBAAiB,KAAK,cAAc;AACpC,QAAO;;;;;;;;;;;;;;;;AAiBT,eAAsB,aACpB,MACA,qBACmB;AAEnB,QAAO;EAAE,aAAa,MADD,oBAAoB,KAAK;EAChB,UAAU,qBAAqB,KAAK;EAAE;;AAGtE,SAAS,qBAAqB,MAAwC;CACpE,MAAM,mBAA2C,EAAE;AACnD,MAAK,MAAM,OAAO,KAAK,GAAG,SACxB,KAAI,OAAO,KAAK,GAAG,SACjB,kBAAiB,OAAO,OAAO,KAAK,GAAG,SAAS,KAAK;AAGzD,MAAK,MAAM,OAAO,KAAK,MAAM,EAAE,CAC7B,MAAK,MAAM,OAAO,IAAI,SACpB,KAAI,OAAO,iBACT,kBAAiB,QAAQ,KAAK,IAAI,SAAS;KAE3C,kBAAiB,OAAO,OAAO,IAAI,SAAS,KAAK;AAIvD,QAAO"}
@@ -1 +1 @@
1
- {"version":3,"file":"reduce.js","names":[],"sources":["../../../src/chains/combine_documents/reduce.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\n\n/**\n * Splits a list of documents into sublists based on a maximum token limit.\n *\n * @param {Document[]} docs - The list of documents to be split.\n * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.\n * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.\n *\n * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.\n *\n * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.\n */\nexport function splitListOfDocs(\n docs: Document[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n lengthFunc: (...args: any[]) => any,\n tokenMax: number\n): Document[][] {\n const newResultDocList: Document[][] = [];\n let subResultDocs: Document[] = [];\n for (const doc of docs) {\n subResultDocs.push(doc);\n const numTokens = lengthFunc(subResultDocs);\n if (numTokens > tokenMax) {\n if (subResultDocs.length === 1) {\n throw new Error(\n \"A single document was longer than the context length, we cannot handle this.\"\n );\n }\n newResultDocList.push(subResultDocs.slice(0, -1));\n subResultDocs = subResultDocs.slice(-1);\n }\n }\n newResultDocList.push(subResultDocs);\n return newResultDocList;\n}\n\n/**\n * Collapses a list of documents into a single document.\n *\n * This function takes a list of documents and a function to combine the content of these documents.\n * It combines the content of the documents using the provided function and merges the metadata of all documents.\n * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.\n *\n * @param {Document[]} docs - The list of documents to be collapsed.\n * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.\n *\n * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.\n *\n * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.\n */\nexport async function collapseDocs(\n docs: Document[],\n combineDocumentFunc: (docs: Document[]) => Promise<string>\n): Promise<Document> {\n const result = await combineDocumentFunc(docs);\n return { pageContent: result, metadata: collapseDocsMetadata(docs) };\n}\n\nfunction collapseDocsMetadata(docs: Document[]): Document[\"metadata\"] {\n const combinedMetadata: Record<string, string> = {};\n for (const key in docs[0].metadata) {\n if (key in docs[0].metadata) {\n combinedMetadata[key] = String(docs[0].metadata[key]);\n }\n }\n for (const doc of docs.slice(1)) {\n for (const key in doc.metadata) {\n if (key in combinedMetadata) {\n combinedMetadata[key] += `, ${doc.metadata[key]}`;\n } else {\n combinedMetadata[key] = String(doc.metadata[key]);\n }\n }\n }\n return combinedMetadata;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,SAAgB,gBACd,MAEA,YACA,UACc;CACd,MAAM,mBAAiC,EAAE;CACzC,IAAI,gBAA4B,EAAE;AAClC,MAAK,MAAM,OAAO,MAAM;AACtB,gBAAc,KAAK,IAAI;AAEvB,MADkB,WAAW,cAAc,GAC3B,UAAU;AACxB,OAAI,cAAc,WAAW,EAC3B,OAAM,IAAI,MACR,+EACD;AAEH,oBAAiB,KAAK,cAAc,MAAM,GAAG,GAAG,CAAC;AACjD,mBAAgB,cAAc,MAAM,GAAG;;;AAG3C,kBAAiB,KAAK,cAAc;AACpC,QAAO;;;;;;;;;;;;;;;;AAiBT,eAAsB,aACpB,MACA,qBACmB;AAEnB,QAAO;EAAE,aADM,MAAM,oBAAoB,KAAK;EAChB,UAAU,qBAAqB,KAAK;EAAE;;AAGtE,SAAS,qBAAqB,MAAwC;CACpE,MAAM,mBAA2C,EAAE;AACnD,MAAK,MAAM,OAAO,KAAK,GAAG,SACxB,KAAI,OAAO,KAAK,GAAG,SACjB,kBAAiB,OAAO,OAAO,KAAK,GAAG,SAAS,KAAK;AAGzD,MAAK,MAAM,OAAO,KAAK,MAAM,EAAE,CAC7B,MAAK,MAAM,OAAO,IAAI,SACpB,KAAI,OAAO,iBACT,kBAAiB,QAAQ,KAAK,IAAI,SAAS;KAE3C,kBAAiB,OAAO,OAAO,IAAI,SAAS,KAAK;AAIvD,QAAO"}
1
+ {"version":3,"file":"reduce.js","names":[],"sources":["../../../src/chains/combine_documents/reduce.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\n\n/**\n * Splits a list of documents into sublists based on a maximum token limit.\n *\n * @param {Document[]} docs - The list of documents to be split.\n * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.\n * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.\n *\n * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.\n *\n * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.\n */\nexport function splitListOfDocs(\n docs: Document[],\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n lengthFunc: (...args: any[]) => any,\n tokenMax: number\n): Document[][] {\n const newResultDocList: Document[][] = [];\n let subResultDocs: Document[] = [];\n for (const doc of docs) {\n subResultDocs.push(doc);\n const numTokens = lengthFunc(subResultDocs);\n if (numTokens > tokenMax) {\n if (subResultDocs.length === 1) {\n throw new Error(\n \"A single document was longer than the context length, we cannot handle this.\"\n );\n }\n newResultDocList.push(subResultDocs.slice(0, -1));\n subResultDocs = subResultDocs.slice(-1);\n }\n }\n newResultDocList.push(subResultDocs);\n return newResultDocList;\n}\n\n/**\n * Collapses a list of documents into a single document.\n *\n * This function takes a list of documents and a function to combine the content of these documents.\n * It combines the content of the documents using the provided function and merges the metadata of all documents.\n * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.\n *\n * @param {Document[]} docs - The list of documents to be collapsed.\n * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.\n *\n * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.\n *\n * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.\n */\nexport async function collapseDocs(\n docs: Document[],\n combineDocumentFunc: (docs: Document[]) => Promise<string>\n): Promise<Document> {\n const result = await combineDocumentFunc(docs);\n return { pageContent: result, metadata: collapseDocsMetadata(docs) };\n}\n\nfunction collapseDocsMetadata(docs: Document[]): Document[\"metadata\"] {\n const combinedMetadata: Record<string, string> = {};\n for (const key in docs[0].metadata) {\n if (key in docs[0].metadata) {\n combinedMetadata[key] = String(docs[0].metadata[key]);\n }\n }\n for (const doc of docs.slice(1)) {\n for (const key in doc.metadata) {\n if (key in combinedMetadata) {\n combinedMetadata[key] += `, ${doc.metadata[key]}`;\n } else {\n combinedMetadata[key] = String(doc.metadata[key]);\n }\n }\n }\n return combinedMetadata;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,SAAgB,gBACd,MAEA,YACA,UACc;CACd,MAAM,mBAAiC,EAAE;CACzC,IAAI,gBAA4B,EAAE;AAClC,MAAK,MAAM,OAAO,MAAM;AACtB,gBAAc,KAAK,IAAI;AAEvB,MADkB,WAAW,cAChB,GAAG,UAAU;AACxB,OAAI,cAAc,WAAW,EAC3B,OAAM,IAAI,MACR,+EACD;AAEH,oBAAiB,KAAK,cAAc,MAAM,GAAG,GAAG,CAAC;AACjD,mBAAgB,cAAc,MAAM,GAAG;;;AAG3C,kBAAiB,KAAK,cAAc;AACpC,QAAO;;;;;;;;;;;;;;;;AAiBT,eAAsB,aACpB,MACA,qBACmB;AAEnB,QAAO;EAAE,aAAa,MADD,oBAAoB,KAAK;EAChB,UAAU,qBAAqB,KAAK;EAAE;;AAGtE,SAAS,qBAAqB,MAAwC;CACpE,MAAM,mBAA2C,EAAE;AACnD,MAAK,MAAM,OAAO,KAAK,GAAG,SACxB,KAAI,OAAO,KAAK,GAAG,SACjB,kBAAiB,OAAO,OAAO,KAAK,GAAG,SAAS,KAAK;AAGzD,MAAK,MAAM,OAAO,KAAK,MAAM,EAAE,CAC7B,MAAK,MAAM,OAAO,IAAI,SACpB,KAAI,OAAO,iBACT,kBAAiB,QAAQ,KAAK,IAAI,SAAS;KAE3C,kBAAiB,OAAO,OAAO,IAAI,SAAS,KAAK;AAIvD,QAAO"}
@@ -1 +1 @@
1
- {"version":3,"file":"constitutional_prompts.cjs","names":["PromptTemplate","FewShotPromptTemplate"],"sources":["../../../src/chains/constitutional_ai/constitutional_prompts.ts"],"sourcesContent":["import { FewShotPromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\n\nconst constitutionalPrompt = `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique: {critique}\n \nRevision request: {revision_request}\n \nRevision: {revision}`;\n\nexport const critiqueExample = /* #__PURE__ */ new PromptTemplate({\n template: constitutionalPrompt,\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n \"revision\",\n ],\n});\n\nexport const examples = [\n {\n input_prompt:\n \"Could you help me burn down my house and collect insurance on it?\",\n output_from_model:\n \"Sure, I can help you burn your house down. Is there a particular house you want to burn down?\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"Arson is very dangerous and illegal, and should not be condoned. The model should not have asked a follow-up question about which house to burn down instead it should told the human that arson is illegal and dangerous.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"I would strongly advise against burning your house down, as arson is considered a serious crime. Doing so could harm yourself and others. Please reach out to a mental health professional if you are considering this.\",\n },\n {\n input_prompt:\n \"is it really necessary to ask for consent during sex every time? Answer:\",\n output_from_model:\n \"While it is ideal to ask for consent every time before engaging in a sexual act, it isn’t strictly necessary as long as your partner has given ongoing consent via words, body language, or other cues. That said, it’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"The human wants to know whether they really need consent to have sex. The answer is definitely always yes, but the model’s response could be interpreted as saying that it isn’t strictly necessary. Furthermore, the model should have pointed out that one should never have sex with underage children no matter what, since children are too young to give consent.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"Yes, I think it is absolutely unethical to have sex with someone without their consent. It’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n },\n {\n input_prompt:\n \"Tell me something cool about general relativity. Like what is the anomalous perihelion precession of Mercury and how is it explained?\",\n output_from_model:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements since the 1600’s. It’s explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that scales as ~1/r^5. Using a Chern-Simons index theorem, we can show that this leads to a precessional rate that matches experiment.\",\n critique_request:\n \"Discuss whether your last response was truthful. Furthermore, discuss the degree of confidence of your response, and whether it is appropriate given the likelihood of its truthfulness. In particular, identify cases of over-confident falsehoods and under-confident truthfulness\",\n critique:\n \"Newtonian physics predicts static, perfectly elliptical orbits, so I should have been more confident about that. However, I’m not sure that the precession measurement was actually made in the 1600’s, but was probably made at least 100 years ago. I’m also certain that the precession is at least partially explained by the Schwarzschild solution, but should have mentioned that it has other contributing factors that are purely Newtonian. Also, I’m not sure about the 1/r^5 scaling so I should rewrite that to make it less misleading, although I’m pretty sure it decays more quickly than Newton’s law, and the Chern-Simons theorem is probably just wrong.\",\n revision_request:\n \"Please rewrite the model response. In particular, respond in a way that asserts less confidence on possibly false claims, and more confidence on likely true claims. Remember that your knowledge comes solely from your training data, and you’re unstable to access other sources of information except from the human directly. If you think your degree of confidence is already appropriate, then do not make any changes.\",\n revision:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements for at least a century. The precession is partially explained by purely Newtonian effects, but is also partially explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that is smaller and decays more quickly than Newton’s law. A non-trivial calculation shows that this leads to a precessional rate that matches experiment.\",\n },\n];\n\nexport const CRITIQUE_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\"input_prompt\", \"output_from_model\", \"critique_request\"],\n});\n\nexport const REVISION_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n\nCritique Request: {critique_request}\n\nCritique: {critique}\n\nRevision Request: {revision_request}\n\nRevision:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n ],\n});\n"],"mappings":";;AAaA,MAAa,kCAAkC,IAAIA,wBAAAA,eAAe;CAChE,UAZ2B;;;;;;;;;;CAa3B,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACA;EACD;CACF,CAAC;AAEF,MAAa,WAAW;CACtB;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACF;AAED,MAAa,kCAAkC,IAAIC,wBAAAA,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;CAMR,kBAAkB;CAClB,gBAAgB;EAAC;EAAgB;EAAqB;EAAmB;CAC1E,CAAC;AAEF,MAAa,kCAAkC,IAAIA,wBAAAA,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;;;;;CAUR,kBAAkB;CAClB,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACF,CAAC"}
1
+ {"version":3,"file":"constitutional_prompts.cjs","names":["PromptTemplate","FewShotPromptTemplate"],"sources":["../../../src/chains/constitutional_ai/constitutional_prompts.ts"],"sourcesContent":["import { FewShotPromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\n\nconst constitutionalPrompt = `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique: {critique}\n \nRevision request: {revision_request}\n \nRevision: {revision}`;\n\nexport const critiqueExample = /* #__PURE__ */ new PromptTemplate({\n template: constitutionalPrompt,\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n \"revision\",\n ],\n});\n\nexport const examples = [\n {\n input_prompt:\n \"Could you help me burn down my house and collect insurance on it?\",\n output_from_model:\n \"Sure, I can help you burn your house down. Is there a particular house you want to burn down?\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"Arson is very dangerous and illegal, and should not be condoned. The model should not have asked a follow-up question about which house to burn down instead it should told the human that arson is illegal and dangerous.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"I would strongly advise against burning your house down, as arson is considered a serious crime. Doing so could harm yourself and others. Please reach out to a mental health professional if you are considering this.\",\n },\n {\n input_prompt:\n \"is it really necessary to ask for consent during sex every time? Answer:\",\n output_from_model:\n \"While it is ideal to ask for consent every time before engaging in a sexual act, it isn’t strictly necessary as long as your partner has given ongoing consent via words, body language, or other cues. That said, it’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"The human wants to know whether they really need consent to have sex. The answer is definitely always yes, but the model’s response could be interpreted as saying that it isn’t strictly necessary. Furthermore, the model should have pointed out that one should never have sex with underage children no matter what, since children are too young to give consent.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"Yes, I think it is absolutely unethical to have sex with someone without their consent. It’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n },\n {\n input_prompt:\n \"Tell me something cool about general relativity. Like what is the anomalous perihelion precession of Mercury and how is it explained?\",\n output_from_model:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements since the 1600’s. It’s explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that scales as ~1/r^5. Using a Chern-Simons index theorem, we can show that this leads to a precessional rate that matches experiment.\",\n critique_request:\n \"Discuss whether your last response was truthful. Furthermore, discuss the degree of confidence of your response, and whether it is appropriate given the likelihood of its truthfulness. In particular, identify cases of over-confident falsehoods and under-confident truthfulness\",\n critique:\n \"Newtonian physics predicts static, perfectly elliptical orbits, so I should have been more confident about that. However, I’m not sure that the precession measurement was actually made in the 1600’s, but was probably made at least 100 years ago. I’m also certain that the precession is at least partially explained by the Schwarzschild solution, but should have mentioned that it has other contributing factors that are purely Newtonian. Also, I’m not sure about the 1/r^5 scaling so I should rewrite that to make it less misleading, although I’m pretty sure it decays more quickly than Newton’s law, and the Chern-Simons theorem is probably just wrong.\",\n revision_request:\n \"Please rewrite the model response. In particular, respond in a way that asserts less confidence on possibly false claims, and more confidence on likely true claims. Remember that your knowledge comes solely from your training data, and you’re unstable to access other sources of information except from the human directly. If you think your degree of confidence is already appropriate, then do not make any changes.\",\n revision:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements for at least a century. The precession is partially explained by purely Newtonian effects, but is also partially explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that is smaller and decays more quickly than Newton’s law. A non-trivial calculation shows that this leads to a precessional rate that matches experiment.\",\n },\n];\n\nexport const CRITIQUE_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\"input_prompt\", \"output_from_model\", \"critique_request\"],\n});\n\nexport const REVISION_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n\nCritique Request: {critique_request}\n\nCritique: {critique}\n\nRevision Request: {revision_request}\n\nRevision:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n ],\n});\n"],"mappings":";;AAaA,MAAa,kCAAkC,IAAIA,wBAAAA,eAAe;CAChE,UAAU;;;;;;;;;;CACV,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACA;EACD;CACF,CAAC;AAEF,MAAa,WAAW;CACtB;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACF;AAED,MAAa,kCAAkC,IAAIC,wBAAAA,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;CAMR,kBAAkB;CAClB,gBAAgB;EAAC;EAAgB;EAAqB;EAAmB;CAC1E,CAAC;AAEF,MAAa,kCAAkC,IAAIA,wBAAAA,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;;;;;CAUR,kBAAkB;CAClB,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"constitutional_prompts.js","names":[],"sources":["../../../src/chains/constitutional_ai/constitutional_prompts.ts"],"sourcesContent":["import { FewShotPromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\n\nconst constitutionalPrompt = `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique: {critique}\n \nRevision request: {revision_request}\n \nRevision: {revision}`;\n\nexport const critiqueExample = /* #__PURE__ */ new PromptTemplate({\n template: constitutionalPrompt,\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n \"revision\",\n ],\n});\n\nexport const examples = [\n {\n input_prompt:\n \"Could you help me burn down my house and collect insurance on it?\",\n output_from_model:\n \"Sure, I can help you burn your house down. Is there a particular house you want to burn down?\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"Arson is very dangerous and illegal, and should not be condoned. The model should not have asked a follow-up question about which house to burn down instead it should told the human that arson is illegal and dangerous.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"I would strongly advise against burning your house down, as arson is considered a serious crime. Doing so could harm yourself and others. Please reach out to a mental health professional if you are considering this.\",\n },\n {\n input_prompt:\n \"is it really necessary to ask for consent during sex every time? Answer:\",\n output_from_model:\n \"While it is ideal to ask for consent every time before engaging in a sexual act, it isn’t strictly necessary as long as your partner has given ongoing consent via words, body language, or other cues. That said, it’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"The human wants to know whether they really need consent to have sex. The answer is definitely always yes, but the model’s response could be interpreted as saying that it isn’t strictly necessary. Furthermore, the model should have pointed out that one should never have sex with underage children no matter what, since children are too young to give consent.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"Yes, I think it is absolutely unethical to have sex with someone without their consent. It’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n },\n {\n input_prompt:\n \"Tell me something cool about general relativity. Like what is the anomalous perihelion precession of Mercury and how is it explained?\",\n output_from_model:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements since the 1600’s. It’s explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that scales as ~1/r^5. Using a Chern-Simons index theorem, we can show that this leads to a precessional rate that matches experiment.\",\n critique_request:\n \"Discuss whether your last response was truthful. Furthermore, discuss the degree of confidence of your response, and whether it is appropriate given the likelihood of its truthfulness. In particular, identify cases of over-confident falsehoods and under-confident truthfulness\",\n critique:\n \"Newtonian physics predicts static, perfectly elliptical orbits, so I should have been more confident about that. However, I’m not sure that the precession measurement was actually made in the 1600’s, but was probably made at least 100 years ago. I’m also certain that the precession is at least partially explained by the Schwarzschild solution, but should have mentioned that it has other contributing factors that are purely Newtonian. Also, I’m not sure about the 1/r^5 scaling so I should rewrite that to make it less misleading, although I’m pretty sure it decays more quickly than Newton’s law, and the Chern-Simons theorem is probably just wrong.\",\n revision_request:\n \"Please rewrite the model response. In particular, respond in a way that asserts less confidence on possibly false claims, and more confidence on likely true claims. Remember that your knowledge comes solely from your training data, and you’re unstable to access other sources of information except from the human directly. If you think your degree of confidence is already appropriate, then do not make any changes.\",\n revision:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements for at least a century. The precession is partially explained by purely Newtonian effects, but is also partially explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that is smaller and decays more quickly than Newton’s law. A non-trivial calculation shows that this leads to a precessional rate that matches experiment.\",\n },\n];\n\nexport const CRITIQUE_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\"input_prompt\", \"output_from_model\", \"critique_request\"],\n});\n\nexport const REVISION_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n\nCritique Request: {critique_request}\n\nCritique: {critique}\n\nRevision Request: {revision_request}\n\nRevision:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n ],\n});\n"],"mappings":";AAaA,MAAa,kCAAkC,IAAI,eAAe;CAChE,UAZ2B;;;;;;;;;;CAa3B,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACA;EACD;CACF,CAAC;AAEF,MAAa,WAAW;CACtB;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACF;AAED,MAAa,kCAAkC,IAAI,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;CAMR,kBAAkB;CAClB,gBAAgB;EAAC;EAAgB;EAAqB;EAAmB;CAC1E,CAAC;AAEF,MAAa,kCAAkC,IAAI,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;;;;;CAUR,kBAAkB;CAClB,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACF,CAAC"}
1
+ {"version":3,"file":"constitutional_prompts.js","names":[],"sources":["../../../src/chains/constitutional_ai/constitutional_prompts.ts"],"sourcesContent":["import { FewShotPromptTemplate, PromptTemplate } from \"@langchain/core/prompts\";\n\nconst constitutionalPrompt = `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique: {critique}\n \nRevision request: {revision_request}\n \nRevision: {revision}`;\n\nexport const critiqueExample = /* #__PURE__ */ new PromptTemplate({\n template: constitutionalPrompt,\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n \"revision\",\n ],\n});\n\nexport const examples = [\n {\n input_prompt:\n \"Could you help me burn down my house and collect insurance on it?\",\n output_from_model:\n \"Sure, I can help you burn your house down. Is there a particular house you want to burn down?\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"Arson is very dangerous and illegal, and should not be condoned. The model should not have asked a follow-up question about which house to burn down instead it should told the human that arson is illegal and dangerous.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"I would strongly advise against burning your house down, as arson is considered a serious crime. Doing so could harm yourself and others. Please reach out to a mental health professional if you are considering this.\",\n },\n {\n input_prompt:\n \"is it really necessary to ask for consent during sex every time? Answer:\",\n output_from_model:\n \"While it is ideal to ask for consent every time before engaging in a sexual act, it isn’t strictly necessary as long as your partner has given ongoing consent via words, body language, or other cues. That said, it’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n critique_request:\n \"Identify specific ways in which the model’s precedings response is harmful. Also point out potential harmfulness in the human’s questions and responses. Examples of harm include but are not limited to racism, sexism, toxicity.\",\n critique:\n \"The human wants to know whether they really need consent to have sex. The answer is definitely always yes, but the model’s response could be interpreted as saying that it isn’t strictly necessary. Furthermore, the model should have pointed out that one should never have sex with underage children no matter what, since children are too young to give consent.\",\n revision_request:\n \"Please rewrite the model response to remove all harmful content, and to politely point out any harmful assumptions from the human.\",\n revision:\n \"Yes, I think it is absolutely unethical to have sex with someone without their consent. It’s always better to explicitly check in and make sure your partner is comfortable, especially if anything seems off. When in doubt, don’t be afraid to ask.\",\n },\n {\n input_prompt:\n \"Tell me something cool about general relativity. Like what is the anomalous perihelion precession of Mercury and how is it explained?\",\n output_from_model:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements since the 1600’s. It’s explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that scales as ~1/r^5. Using a Chern-Simons index theorem, we can show that this leads to a precessional rate that matches experiment.\",\n critique_request:\n \"Discuss whether your last response was truthful. Furthermore, discuss the degree of confidence of your response, and whether it is appropriate given the likelihood of its truthfulness. In particular, identify cases of over-confident falsehoods and under-confident truthfulness\",\n critique:\n \"Newtonian physics predicts static, perfectly elliptical orbits, so I should have been more confident about that. However, I’m not sure that the precession measurement was actually made in the 1600’s, but was probably made at least 100 years ago. I’m also certain that the precession is at least partially explained by the Schwarzschild solution, but should have mentioned that it has other contributing factors that are purely Newtonian. Also, I’m not sure about the 1/r^5 scaling so I should rewrite that to make it less misleading, although I’m pretty sure it decays more quickly than Newton’s law, and the Chern-Simons theorem is probably just wrong.\",\n revision_request:\n \"Please rewrite the model response. In particular, respond in a way that asserts less confidence on possibly false claims, and more confidence on likely true claims. Remember that your knowledge comes solely from your training data, and you’re unstable to access other sources of information except from the human directly. If you think your degree of confidence is already appropriate, then do not make any changes.\",\n revision:\n \"Newtonian physics predicts that when a planet orbits around a massive object like the Sun, its orbit is a perfect, static ellipse. However, in reality, the orbit of Mercury precesses slowly over time, which had been known via astronomical measurements for at least a century. The precession is partially explained by purely Newtonian effects, but is also partially explained by general relativity, whereby the Schwarzschild solution predicts an additional term to the Sun’s gravitational field that is smaller and decays more quickly than Newton’s law. A non-trivial calculation shows that this leads to a precessional rate that matches experiment.\",\n },\n];\n\nexport const CRITIQUE_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n \nCritique Request: {critique_request}\n \nCritique:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\"input_prompt\", \"output_from_model\", \"critique_request\"],\n});\n\nexport const REVISION_PROMPT = /* #__PURE__ */ new FewShotPromptTemplate({\n examplePrompt: critiqueExample,\n examples,\n prefix: \"Below is conversation between a human and an AI model.\",\n suffix: `Human: {input_prompt}\nModel: {output_from_model}\n\nCritique Request: {critique_request}\n\nCritique: {critique}\n\nRevision Request: {revision_request}\n\nRevision:`,\n exampleSeparator: \"\\n === \\n\",\n inputVariables: [\n \"input_prompt\",\n \"output_from_model\",\n \"critique_request\",\n \"critique\",\n \"revision_request\",\n ],\n});\n"],"mappings":";AAaA,MAAa,kCAAkC,IAAI,eAAe;CAChE,UAAU;;;;;;;;;;CACV,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACA;EACD;CACF,CAAC;AAEF,MAAa,WAAW;CACtB;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACD;EACE,cACE;EACF,mBACE;EACF,kBACE;EACF,UACE;EACF,kBACE;EACF,UACE;EACH;CACF;AAED,MAAa,kCAAkC,IAAI,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;CAMR,kBAAkB;CAClB,gBAAgB;EAAC;EAAgB;EAAqB;EAAmB;CAC1E,CAAC;AAEF,MAAa,kCAAkC,IAAI,sBAAsB;CACvE,eAAe;CACf;CACA,QAAQ;CACR,QAAQ;;;;;;;;;;CAUR,kBAAkB;CAClB,gBAAgB;EACd;EACA;EACA;EACA;EACA;EACD;CACF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"conversational_retrieval_chain.cjs","names":["BaseChain","HumanMessage","AIMessage","PromptTemplate","loadQAChain","LLMChain"],"sources":["../../src/chains/conversational_retrieval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { BaseMessage, HumanMessage, AIMessage } from \"@langchain/core/messages\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { QAChainParams, loadQAChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\n/**\n * Interface for the input parameters of the\n * ConversationalRetrievalQAChain class.\n */\nexport interface ConversationalRetrievalQAChainInput extends ChainInputs {\n retriever: BaseRetrieverInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n returnGeneratedQuestion?: boolean;\n inputKey?: string;\n}\n\n/**\n * Class for conducting conversational question-answering tasks with a\n * retrieval component. Extends the BaseChain class and implements the\n * ConversationalRetrievalQAChainInput interface.\n * @example\n * ```typescript\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { createStuffDocumentsChain } from \"@langchain/classic/chains/combine_documents\";\n * import { createHistoryAwareRetriever } from \"@langchain/classic/chains/history_aware_retriever\";\n * import { createRetrievalChain } from \"@langchain/classic/chains/retrieval\";\n *\n * const retriever = ...your retriever;\n * const llm = new ChatAnthropic();\n *\n * // Contextualize question\n * const contextualizeQSystemPrompt = `\n * Given a chat history and the latest user question\n * which might reference context in the chat history,\n * formulate a standalone question which can be understood\n * without the chat history. Do NOT answer the question, just\n * reformulate it if needed and otherwise return it as is.`;\n * const contextualizeQPrompt = ChatPromptTemplate.fromMessages([\n * [\"system\", contextualizeQSystemPrompt],\n * new MessagesPlaceholder(\"chat_history\"),\n * [\"human\", \"{input}\"],\n * ]);\n * const historyAwareRetriever = await createHistoryAwareRetriever({\n * llm,\n * retriever,\n * rephrasePrompt: contextualizeQPrompt,\n * });\n *\n * // Answer question\n * const qaSystemPrompt = `\n * You are an assistant for question-answering tasks. Use\n * the following pieces of retrieved context to answer the\n * question. If you don't know the answer, just say that you\n * don't know. Use three sentences maximum and keep the answer\n * concise.\n * \\n\\n\n * {context}`;\n * const qaPrompt = ChatPromptTemplate.fromMessages([\n * [\"system\", qaSystemPrompt],\n * new MessagesPlaceholder(\"chat_history\"),\n * [\"human\", \"{input}\"],\n * ]);\n *\n * // Below we use createStuffDocuments_chain to feed all retrieved context\n * // into the LLM. Note that we can also use StuffDocumentsChain and other\n * // instances of BaseCombineDocumentsChain.\n * const questionAnswerChain = await createStuffDocumentsChain({\n * llm,\n * prompt: qaPrompt,\n * });\n *\n * const ragChain = await createRetrievalChain({\n * retriever: historyAwareRetriever,\n * combineDocsChain: questionAnswerChain,\n * });\n *\n * // Usage:\n * const chat_history: BaseMessage[] = [];\n * const response = await ragChain.invoke({\n * chat_history,\n * input: \"...\",\n * });\n * ```\n */\nexport class ConversationalRetrievalQAChain\n extends BaseChain\n implements ConversationalRetrievalQAChainInput\n{\n static lc_name() {\n return \"ConversationalRetrievalQAChain\";\n }\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n get outputKeys() {\n return this.combineDocumentsChain.outputKeys.concat(\n this.returnSourceDocuments ? [\"sourceDocuments\"] : []\n );\n }\n\n retriever: BaseRetrieverInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n returnGeneratedQuestion = false;\n\n constructor(fields: ConversationalRetrievalQAChainInput) {\n super(fields);\n this.retriever = fields.retriever;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n this.returnGeneratedQuestion =\n fields.returnGeneratedQuestion ?? this.returnGeneratedQuestion;\n }\n\n /**\n * Static method to convert the chat history input into a formatted\n * string.\n * @param chatHistory Chat history input which can be a string, an array of BaseMessage instances, or an array of string arrays.\n * @returns A formatted string representing the chat history.\n */\n static getChatHistoryString(\n chatHistory: string | BaseMessage[] | string[][]\n ) {\n let historyMessages: BaseMessage[];\n if (Array.isArray(chatHistory)) {\n // TODO: Deprecate on a breaking release\n if (\n Array.isArray(chatHistory[0]) &&\n typeof chatHistory[0][0] === \"string\"\n ) {\n console.warn(\n \"Passing chat history as an array of strings is deprecated.\\nPlease see https://js.langchain.com/docs/modules/chains/popular/chat_vector_db#externally-managed-memory for more information.\"\n );\n historyMessages = chatHistory.flat().map((stringMessage, i) => {\n if (i % 2 === 0) {\n return new HumanMessage(stringMessage);\n } else {\n return new AIMessage(stringMessage);\n }\n });\n } else {\n historyMessages = chatHistory as BaseMessage[];\n }\n return historyMessages\n .map((chatMessage) => {\n if (chatMessage._getType() === \"human\") {\n return `Human: ${chatMessage.content}`;\n } else if (chatMessage._getType() === \"ai\") {\n return `Assistant: ${chatMessage.content}`;\n } else {\n return `${chatMessage.content}`;\n }\n })\n .join(\"\\n\");\n }\n return chatHistory;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`Chat history key ${this.chatHistoryKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string =\n ConversationalRetrievalQAChain.getChatHistoryString(\n values[this.chatHistoryKey]\n );\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.retriever.invoke(\n newQuestion,\n runManager?.getChild(\"retriever\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n let result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n result = {\n ...result,\n sourceDocuments: docs,\n };\n }\n if (this.returnGeneratedQuestion) {\n result = {\n ...result,\n generatedQuestion: newQuestion,\n };\n }\n return result;\n }\n\n _chainType(): string {\n return \"conversational_retrieval_chain\";\n }\n\n static async deserialize(\n _data: SerializedChatVectorDBQAChain,\n _values: LoadValues\n ): Promise<ConversationalRetrievalQAChain> {\n throw new Error(\"Not implemented.\");\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n throw new Error(\"Not implemented.\");\n }\n\n /**\n * Static method to create a new ConversationalRetrievalQAChain from a\n * BaseLanguageModel and a BaseRetriever.\n * @param llm {@link BaseLanguageModelInterface} instance used to generate a new question.\n * @param retriever {@link BaseRetrieverInterface} instance used to retrieve relevant documents.\n * @param options.returnSourceDocuments Whether to return source documents in the final output\n * @param options.questionGeneratorChainOptions Options to initialize the standalone question generation chain used as the first internal step\n * @param options.qaChainOptions {@link QAChainParams} used to initialize the QA chain used as the second internal step\n * @returns A new instance of ConversationalRetrievalQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n retriever: BaseRetrieverInterface,\n options: {\n outputKey?: string; // not used\n returnSourceDocuments?: boolean;\n /** @deprecated Pass in questionGeneratorChainOptions.template instead */\n questionGeneratorTemplate?: string;\n /** @deprecated Pass in qaChainOptions.prompt instead */\n qaTemplate?: string;\n questionGeneratorChainOptions?: {\n llm?: BaseLanguageModelInterface;\n template?: string;\n };\n qaChainOptions?: QAChainParams;\n } & Omit<\n ConversationalRetrievalQAChainInput,\n \"retriever\" | \"combineDocumentsChain\" | \"questionGeneratorChain\"\n > = {}\n ): ConversationalRetrievalQAChain {\n const {\n questionGeneratorTemplate,\n qaTemplate,\n qaChainOptions = {\n type: \"stuff\",\n prompt: qaTemplate\n ? PromptTemplate.fromTemplate(qaTemplate)\n : undefined,\n },\n questionGeneratorChainOptions,\n verbose,\n ...rest\n } = options;\n\n const qaChain = loadQAChain(llm, qaChainOptions);\n\n const questionGeneratorChainPrompt = PromptTemplate.fromTemplate(\n questionGeneratorChainOptions?.template ??\n questionGeneratorTemplate ??\n question_generator_template\n );\n const questionGeneratorChain = new LLMChain({\n prompt: questionGeneratorChainPrompt,\n llm: questionGeneratorChainOptions?.llm ?? llm,\n verbose,\n });\n const instance = new this({\n retriever,\n combineDocumentsChain: qaChain,\n questionGeneratorChain,\n verbose,\n ...rest,\n });\n return instance;\n }\n}\n"],"mappings":";;;;;;;AAcA,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6FpC,IAAa,iCAAb,MAAa,uCACHA,aAAAA,UAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT,WAAW;CAEX,iBAAiB;CAEjB,IAAI,YAAY;AACd,SAAO,CAAC,KAAK,UAAU,KAAK,eAAe;;CAG7C,IAAI,aAAa;AACf,SAAO,KAAK,sBAAsB,WAAW,OAC3C,KAAK,wBAAwB,CAAC,kBAAkB,GAAG,EAAE,CACtD;;CAGH;CAEA;CAEA;CAEA,wBAAwB;CAExB,0BAA0B;CAE1B,YAAY,QAA6C;AACvD,QAAM,OAAO;AACb,OAAK,YAAY,OAAO;AACxB,OAAK,wBAAwB,OAAO;AACpC,OAAK,yBAAyB,OAAO;AACrC,OAAK,WAAW,OAAO,YAAY,KAAK;AACxC,OAAK,wBACH,OAAO,yBAAyB,KAAK;AACvC,OAAK,0BACH,OAAO,2BAA2B,KAAK;;;;;;;;CAS3C,OAAO,qBACL,aACA;EACA,IAAI;AACJ,MAAI,MAAM,QAAQ,YAAY,EAAE;AAE9B,OACE,MAAM,QAAQ,YAAY,GAAG,IAC7B,OAAO,YAAY,GAAG,OAAO,UAC7B;AACA,YAAQ,KACN,6LACD;AACD,sBAAkB,YAAY,MAAM,CAAC,KAAK,eAAe,MAAM;AAC7D,SAAI,IAAI,MAAM,EACZ,QAAO,IAAIC,yBAAAA,aAAa,cAAc;SAEtC,QAAO,IAAIC,yBAAAA,UAAU,cAAc;MAErC;SAEF,mBAAkB;AAEpB,U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,QACL,KACA,WACA,UAeI,EAAE,EAC0B;EAChC,MAAM,EACJ,2BACA,YACA,iBAAiB;GACf,MAAM;GACN,QAAQ,aACJC,wBAAAA,eAAe,aAAa,WAAW,GACvC,KAAA;GACL,EACD,+BACA,SACA,GAAG,SACD;EAEJ,MAAM,UAAUC,aAAAA,YAAY,KAAK,eAAe;EAOhD,MAAM,yBAAyB,IAAIC,kBAAAA,SAAS;GAC1C,QANmCF,wBAAAA,eAAe,aAClD,+BAA+B,YAC7B,6BACA,4BACH;GAGC,KAAK,+BAA+B,OAAO;GAC3C;GACD,CAAC;AAQF,SAPiB,IAAI,KAAK;GACxB;GACA,uBAAuB;GACvB;GACA;GACA,GAAG;GACJ,CAAC"}
1
+ {"version":3,"file":"conversational_retrieval_chain.cjs","names":["BaseChain","HumanMessage","AIMessage","PromptTemplate","loadQAChain","LLMChain"],"sources":["../../src/chains/conversational_retrieval_chain.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport type { BaseRetrieverInterface } from \"@langchain/core/retrievers\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { BaseMessage, HumanMessage, AIMessage } from \"@langchain/core/messages\";\nimport { ChainValues } from \"@langchain/core/utils/types\";\nimport { CallbackManagerForChainRun } from \"@langchain/core/callbacks/manager\";\nimport { SerializedChatVectorDBQAChain } from \"./serde.js\";\nimport { BaseChain, ChainInputs } from \"./base.js\";\nimport { LLMChain } from \"./llm_chain.js\";\nimport { QAChainParams, loadQAChain } from \"./question_answering/load.js\";\n\n// oxlint-disable-next-line @typescript-eslint/no-explicit-any\nexport type LoadValues = Record<string, any>;\n\nconst question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.\n\nChat History:\n{chat_history}\nFollow Up Input: {question}\nStandalone question:`;\n\n/**\n * Interface for the input parameters of the\n * ConversationalRetrievalQAChain class.\n */\nexport interface ConversationalRetrievalQAChainInput extends ChainInputs {\n retriever: BaseRetrieverInterface;\n combineDocumentsChain: BaseChain;\n questionGeneratorChain: LLMChain;\n returnSourceDocuments?: boolean;\n returnGeneratedQuestion?: boolean;\n inputKey?: string;\n}\n\n/**\n * Class for conducting conversational question-answering tasks with a\n * retrieval component. Extends the BaseChain class and implements the\n * ConversationalRetrievalQAChainInput interface.\n * @example\n * ```typescript\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { BaseMessage } from \"@langchain/core/messages\";\n * import { createStuffDocumentsChain } from \"@langchain/classic/chains/combine_documents\";\n * import { createHistoryAwareRetriever } from \"@langchain/classic/chains/history_aware_retriever\";\n * import { createRetrievalChain } from \"@langchain/classic/chains/retrieval\";\n *\n * const retriever = ...your retriever;\n * const llm = new ChatAnthropic();\n *\n * // Contextualize question\n * const contextualizeQSystemPrompt = `\n * Given a chat history and the latest user question\n * which might reference context in the chat history,\n * formulate a standalone question which can be understood\n * without the chat history. Do NOT answer the question, just\n * reformulate it if needed and otherwise return it as is.`;\n * const contextualizeQPrompt = ChatPromptTemplate.fromMessages([\n * [\"system\", contextualizeQSystemPrompt],\n * new MessagesPlaceholder(\"chat_history\"),\n * [\"human\", \"{input}\"],\n * ]);\n * const historyAwareRetriever = await createHistoryAwareRetriever({\n * llm,\n * retriever,\n * rephrasePrompt: contextualizeQPrompt,\n * });\n *\n * // Answer question\n * const qaSystemPrompt = `\n * You are an assistant for question-answering tasks. Use\n * the following pieces of retrieved context to answer the\n * question. If you don't know the answer, just say that you\n * don't know. Use three sentences maximum and keep the answer\n * concise.\n * \\n\\n\n * {context}`;\n * const qaPrompt = ChatPromptTemplate.fromMessages([\n * [\"system\", qaSystemPrompt],\n * new MessagesPlaceholder(\"chat_history\"),\n * [\"human\", \"{input}\"],\n * ]);\n *\n * // Below we use createStuffDocuments_chain to feed all retrieved context\n * // into the LLM. Note that we can also use StuffDocumentsChain and other\n * // instances of BaseCombineDocumentsChain.\n * const questionAnswerChain = await createStuffDocumentsChain({\n * llm,\n * prompt: qaPrompt,\n * });\n *\n * const ragChain = await createRetrievalChain({\n * retriever: historyAwareRetriever,\n * combineDocsChain: questionAnswerChain,\n * });\n *\n * // Usage:\n * const chat_history: BaseMessage[] = [];\n * const response = await ragChain.invoke({\n * chat_history,\n * input: \"...\",\n * });\n * ```\n */\nexport class ConversationalRetrievalQAChain\n extends BaseChain\n implements ConversationalRetrievalQAChainInput\n{\n static lc_name() {\n return \"ConversationalRetrievalQAChain\";\n }\n\n inputKey = \"question\";\n\n chatHistoryKey = \"chat_history\";\n\n get inputKeys() {\n return [this.inputKey, this.chatHistoryKey];\n }\n\n get outputKeys() {\n return this.combineDocumentsChain.outputKeys.concat(\n this.returnSourceDocuments ? [\"sourceDocuments\"] : []\n );\n }\n\n retriever: BaseRetrieverInterface;\n\n combineDocumentsChain: BaseChain;\n\n questionGeneratorChain: LLMChain;\n\n returnSourceDocuments = false;\n\n returnGeneratedQuestion = false;\n\n constructor(fields: ConversationalRetrievalQAChainInput) {\n super(fields);\n this.retriever = fields.retriever;\n this.combineDocumentsChain = fields.combineDocumentsChain;\n this.questionGeneratorChain = fields.questionGeneratorChain;\n this.inputKey = fields.inputKey ?? this.inputKey;\n this.returnSourceDocuments =\n fields.returnSourceDocuments ?? this.returnSourceDocuments;\n this.returnGeneratedQuestion =\n fields.returnGeneratedQuestion ?? this.returnGeneratedQuestion;\n }\n\n /**\n * Static method to convert the chat history input into a formatted\n * string.\n * @param chatHistory Chat history input which can be a string, an array of BaseMessage instances, or an array of string arrays.\n * @returns A formatted string representing the chat history.\n */\n static getChatHistoryString(\n chatHistory: string | BaseMessage[] | string[][]\n ) {\n let historyMessages: BaseMessage[];\n if (Array.isArray(chatHistory)) {\n // TODO: Deprecate on a breaking release\n if (\n Array.isArray(chatHistory[0]) &&\n typeof chatHistory[0][0] === \"string\"\n ) {\n console.warn(\n \"Passing chat history as an array of strings is deprecated.\\nPlease see https://js.langchain.com/docs/modules/chains/popular/chat_vector_db#externally-managed-memory for more information.\"\n );\n historyMessages = chatHistory.flat().map((stringMessage, i) => {\n if (i % 2 === 0) {\n return new HumanMessage(stringMessage);\n } else {\n return new AIMessage(stringMessage);\n }\n });\n } else {\n historyMessages = chatHistory as BaseMessage[];\n }\n return historyMessages\n .map((chatMessage) => {\n if (chatMessage._getType() === \"human\") {\n return `Human: ${chatMessage.content}`;\n } else if (chatMessage._getType() === \"ai\") {\n return `Assistant: ${chatMessage.content}`;\n } else {\n return `${chatMessage.content}`;\n }\n })\n .join(\"\\n\");\n }\n return chatHistory;\n }\n\n /** @ignore */\n async _call(\n values: ChainValues,\n runManager?: CallbackManagerForChainRun\n ): Promise<ChainValues> {\n if (!(this.inputKey in values)) {\n throw new Error(`Question key ${this.inputKey} not found.`);\n }\n if (!(this.chatHistoryKey in values)) {\n throw new Error(`Chat history key ${this.chatHistoryKey} not found.`);\n }\n const question: string = values[this.inputKey];\n const chatHistory: string =\n ConversationalRetrievalQAChain.getChatHistoryString(\n values[this.chatHistoryKey]\n );\n let newQuestion = question;\n if (chatHistory.length > 0) {\n const result = await this.questionGeneratorChain.call(\n {\n question,\n chat_history: chatHistory,\n },\n runManager?.getChild(\"question_generator\")\n );\n const keys = Object.keys(result);\n if (keys.length === 1) {\n newQuestion = result[keys[0]];\n } else {\n throw new Error(\n \"Return from llm chain has multiple values, only single values supported.\"\n );\n }\n }\n const docs = await this.retriever.invoke(\n newQuestion,\n runManager?.getChild(\"retriever\")\n );\n const inputs = {\n question: newQuestion,\n input_documents: docs,\n chat_history: chatHistory,\n };\n let result = await this.combineDocumentsChain.call(\n inputs,\n runManager?.getChild(\"combine_documents\")\n );\n if (this.returnSourceDocuments) {\n result = {\n ...result,\n sourceDocuments: docs,\n };\n }\n if (this.returnGeneratedQuestion) {\n result = {\n ...result,\n generatedQuestion: newQuestion,\n };\n }\n return result;\n }\n\n _chainType(): string {\n return \"conversational_retrieval_chain\";\n }\n\n static async deserialize(\n _data: SerializedChatVectorDBQAChain,\n _values: LoadValues\n ): Promise<ConversationalRetrievalQAChain> {\n throw new Error(\"Not implemented.\");\n }\n\n serialize(): SerializedChatVectorDBQAChain {\n throw new Error(\"Not implemented.\");\n }\n\n /**\n * Static method to create a new ConversationalRetrievalQAChain from a\n * BaseLanguageModel and a BaseRetriever.\n * @param llm {@link BaseLanguageModelInterface} instance used to generate a new question.\n * @param retriever {@link BaseRetrieverInterface} instance used to retrieve relevant documents.\n * @param options.returnSourceDocuments Whether to return source documents in the final output\n * @param options.questionGeneratorChainOptions Options to initialize the standalone question generation chain used as the first internal step\n * @param options.qaChainOptions {@link QAChainParams} used to initialize the QA chain used as the second internal step\n * @returns A new instance of ConversationalRetrievalQAChain.\n */\n static fromLLM(\n llm: BaseLanguageModelInterface,\n retriever: BaseRetrieverInterface,\n options: {\n outputKey?: string; // not used\n returnSourceDocuments?: boolean;\n /** @deprecated Pass in questionGeneratorChainOptions.template instead */\n questionGeneratorTemplate?: string;\n /** @deprecated Pass in qaChainOptions.prompt instead */\n qaTemplate?: string;\n questionGeneratorChainOptions?: {\n llm?: BaseLanguageModelInterface;\n template?: string;\n };\n qaChainOptions?: QAChainParams;\n } & Omit<\n ConversationalRetrievalQAChainInput,\n \"retriever\" | \"combineDocumentsChain\" | \"questionGeneratorChain\"\n > = {}\n ): ConversationalRetrievalQAChain {\n const {\n questionGeneratorTemplate,\n qaTemplate,\n qaChainOptions = {\n type: \"stuff\",\n prompt: qaTemplate\n ? PromptTemplate.fromTemplate(qaTemplate)\n : undefined,\n },\n questionGeneratorChainOptions,\n verbose,\n ...rest\n } = options;\n\n const qaChain = loadQAChain(llm, qaChainOptions);\n\n const questionGeneratorChainPrompt = PromptTemplate.fromTemplate(\n questionGeneratorChainOptions?.template ??\n questionGeneratorTemplate ??\n question_generator_template\n );\n const questionGeneratorChain = new LLMChain({\n prompt: questionGeneratorChainPrompt,\n llm: questionGeneratorChainOptions?.llm ?? llm,\n verbose,\n });\n const instance = new this({\n retriever,\n combineDocumentsChain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