@langchain/classic 1.0.8 → 1.0.10-dev-1768440391024

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 (493) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/agents/agent.cjs.map +1 -1
  3. package/dist/agents/agent.js.map +1 -1
  4. package/dist/agents/helpers.cjs.map +1 -1
  5. package/dist/agents/helpers.js.map +1 -1
  6. package/dist/agents/openai_functions/index.cjs.map +1 -1
  7. package/dist/agents/openai_functions/index.js.map +1 -1
  8. package/dist/agents/openai_tools/index.d.cts +2 -2
  9. package/dist/agents/openai_tools/index.d.cts.map +1 -1
  10. package/dist/agents/openai_tools/index.d.ts +2 -2
  11. package/dist/agents/openai_tools/index.d.ts.map +1 -1
  12. package/dist/agents/react/index.d.ts +2 -2
  13. package/dist/agents/react/index.d.ts.map +1 -1
  14. package/dist/agents/structured_chat/index.d.cts +2 -2
  15. package/dist/agents/structured_chat/index.d.cts.map +1 -1
  16. package/dist/agents/structured_chat/index.d.ts +2 -2
  17. package/dist/agents/structured_chat/index.d.ts.map +1 -1
  18. package/dist/agents/tool_calling/index.d.ts +2 -2
  19. package/dist/agents/tool_calling/index.d.ts.map +1 -1
  20. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts +2 -2
  21. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.cts.map +1 -1
  22. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  23. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts.map +1 -1
  24. package/dist/agents/toolkits/conversational_retrieval/tool.d.cts +1 -1
  25. package/dist/agents/toolkits/conversational_retrieval/tool.d.cts.map +1 -1
  26. package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -1
  27. package/dist/agents/toolkits/conversational_retrieval/tool.d.ts.map +1 -1
  28. package/dist/agents/types.cjs.map +1 -1
  29. package/dist/agents/types.js.map +1 -1
  30. package/dist/chains/base.cjs.map +1 -1
  31. package/dist/chains/base.js.map +1 -1
  32. package/dist/chains/combine_docs_chain.cjs.map +1 -1
  33. package/dist/chains/combine_docs_chain.js.map +1 -1
  34. package/dist/chains/llm_chain.cjs.map +1 -1
  35. package/dist/chains/llm_chain.js.map +1 -1
  36. package/dist/chains/openai_functions/base.cjs.map +1 -1
  37. package/dist/chains/openai_functions/base.js.map +1 -1
  38. package/dist/chains/openai_functions/openapi.cjs.map +1 -1
  39. package/dist/chains/openai_functions/openapi.js.map +1 -1
  40. package/dist/chains/query_constructor/index.cjs.map +1 -1
  41. package/dist/chains/query_constructor/index.js.map +1 -1
  42. package/dist/chains/query_constructor/parser.cjs.map +1 -1
  43. package/dist/chains/query_constructor/parser.js.map +1 -1
  44. package/dist/chains/router/multi_prompt.cjs.map +1 -1
  45. package/dist/chains/router/multi_prompt.js.map +1 -1
  46. package/dist/chains/transform.cjs.map +1 -1
  47. package/dist/chains/transform.js.map +1 -1
  48. package/dist/chat_models/universal.cjs.map +1 -1
  49. package/dist/chat_models/universal.js.map +1 -1
  50. package/dist/document_loaders/fs/json.cjs.map +1 -1
  51. package/dist/document_loaders/fs/json.js.map +1 -1
  52. package/dist/embeddings/cache_backed.cjs.map +1 -1
  53. package/dist/embeddings/cache_backed.js.map +1 -1
  54. package/dist/evaluation/base.cjs.map +1 -1
  55. package/dist/evaluation/base.js.map +1 -1
  56. package/dist/evaluation/comparison/pairwise.d.ts +3 -3
  57. package/dist/evaluation/comparison/pairwise.d.ts.map +1 -1
  58. package/dist/evaluation/criteria/criteria.d.ts +3 -3
  59. package/dist/evaluation/criteria/criteria.d.ts.map +1 -1
  60. package/dist/experimental/autogpt/prompt.d.cts +2 -2
  61. package/dist/experimental/autogpt/prompt.d.cts.map +1 -1
  62. package/dist/experimental/autogpt/prompt.d.ts +2 -2
  63. package/dist/experimental/autogpt/prompt.d.ts.map +1 -1
  64. package/dist/experimental/masking/regex_masking_transformer.cjs.map +1 -1
  65. package/dist/experimental/masking/regex_masking_transformer.js.map +1 -1
  66. package/dist/experimental/openai_assistant/index.cjs.map +1 -1
  67. package/dist/experimental/openai_assistant/index.js.map +1 -1
  68. package/dist/experimental/plan_and_execute/agent_executor.cjs.map +1 -1
  69. package/dist/experimental/plan_and_execute/agent_executor.js.map +1 -1
  70. package/dist/experimental/prompts/custom_format.cjs.map +1 -1
  71. package/dist/experimental/prompts/custom_format.d.cts.map +1 -1
  72. package/dist/experimental/prompts/custom_format.d.ts.map +1 -1
  73. package/dist/experimental/prompts/custom_format.js.map +1 -1
  74. package/dist/experimental/prompts/handlebars.cjs.map +1 -1
  75. package/dist/experimental/prompts/handlebars.d.cts.map +1 -1
  76. package/dist/experimental/prompts/handlebars.js.map +1 -1
  77. package/dist/load/import_constants.cjs +24 -24
  78. package/dist/load/import_constants.cjs.map +1 -1
  79. package/dist/load/import_constants.js +24 -24
  80. package/dist/load/import_constants.js.map +1 -1
  81. package/dist/load/import_map.cjs +44 -44
  82. package/dist/load/import_map.cjs.map +1 -1
  83. package/dist/load/import_map.js +52 -52
  84. package/dist/load/import_map.js.map +1 -1
  85. package/dist/output_parsers/openai_functions.cjs.map +1 -1
  86. package/dist/output_parsers/openai_functions.js.map +1 -1
  87. package/dist/output_parsers/regex.cjs.map +1 -1
  88. package/dist/output_parsers/regex.js.map +1 -1
  89. package/dist/output_parsers/router.cjs.map +1 -1
  90. package/dist/output_parsers/router.js.map +1 -1
  91. package/dist/output_parsers/structured.cjs.map +1 -1
  92. package/dist/output_parsers/structured.js.map +1 -1
  93. package/dist/retrievers/document_compressors/index.cjs.map +1 -1
  94. package/dist/retrievers/document_compressors/index.js.map +1 -1
  95. package/dist/retrievers/hyde.cjs +1 -1
  96. package/dist/retrievers/hyde.cjs.map +1 -1
  97. package/dist/retrievers/hyde.d.cts.map +1 -1
  98. package/dist/retrievers/hyde.js +1 -1
  99. package/dist/retrievers/hyde.js.map +1 -1
  100. package/dist/retrievers/matryoshka_retriever.cjs +1 -1
  101. package/dist/retrievers/matryoshka_retriever.cjs.map +1 -1
  102. package/dist/retrievers/matryoshka_retriever.js +1 -1
  103. package/dist/retrievers/matryoshka_retriever.js.map +1 -1
  104. package/dist/retrievers/score_threshold.cjs.map +1 -1
  105. package/dist/retrievers/score_threshold.js.map +1 -1
  106. package/dist/retrievers/self_query/index.cjs +1 -1
  107. package/dist/retrievers/self_query/index.d.ts +1 -1
  108. package/dist/retrievers/self_query/index.js +1 -1
  109. package/dist/smith/config.cjs.map +1 -1
  110. package/dist/smith/config.js.map +1 -1
  111. package/dist/smith/runner_utils.cjs.map +1 -1
  112. package/dist/smith/runner_utils.js.map +1 -1
  113. package/dist/tools/retriever.d.cts +1 -1
  114. package/dist/tools/retriever.d.cts.map +1 -1
  115. package/dist/tools/retriever.d.ts +1 -1
  116. package/dist/tools/retriever.d.ts.map +1 -1
  117. package/dist/util/sql_utils.cjs.map +1 -1
  118. package/dist/util/sql_utils.js.map +1 -1
  119. package/dist/vectorstores/memory.cjs +1 -1
  120. package/dist/vectorstores/memory.js +1 -1
  121. package/package.json +19 -20
  122. package/agents/format_scratchpad/log.cjs +0 -1
  123. package/agents/format_scratchpad/log.d.cts +0 -1
  124. package/agents/format_scratchpad/log.d.ts +0 -1
  125. package/agents/format_scratchpad/log.js +0 -1
  126. package/agents/format_scratchpad/log_to_message.cjs +0 -1
  127. package/agents/format_scratchpad/log_to_message.d.cts +0 -1
  128. package/agents/format_scratchpad/log_to_message.d.ts +0 -1
  129. package/agents/format_scratchpad/log_to_message.js +0 -1
  130. package/agents/format_scratchpad/openai_functions.cjs +0 -1
  131. package/agents/format_scratchpad/openai_functions.d.cts +0 -1
  132. package/agents/format_scratchpad/openai_functions.d.ts +0 -1
  133. package/agents/format_scratchpad/openai_functions.js +0 -1
  134. package/agents/format_scratchpad/openai_tools.cjs +0 -1
  135. package/agents/format_scratchpad/openai_tools.d.cts +0 -1
  136. package/agents/format_scratchpad/openai_tools.d.ts +0 -1
  137. package/agents/format_scratchpad/openai_tools.js +0 -1
  138. package/agents/format_scratchpad/xml.cjs +0 -1
  139. package/agents/format_scratchpad/xml.d.cts +0 -1
  140. package/agents/format_scratchpad/xml.d.ts +0 -1
  141. package/agents/format_scratchpad/xml.js +0 -1
  142. package/agents/load.cjs +0 -1
  143. package/agents/load.d.cts +0 -1
  144. package/agents/load.d.ts +0 -1
  145. package/agents/load.js +0 -1
  146. package/agents/openai/output_parser.cjs +0 -1
  147. package/agents/openai/output_parser.d.cts +0 -1
  148. package/agents/openai/output_parser.d.ts +0 -1
  149. package/agents/openai/output_parser.js +0 -1
  150. package/agents/react/output_parser.cjs +0 -1
  151. package/agents/react/output_parser.d.cts +0 -1
  152. package/agents/react/output_parser.d.ts +0 -1
  153. package/agents/react/output_parser.js +0 -1
  154. package/agents/toolkits/sql.cjs +0 -1
  155. package/agents/toolkits/sql.d.cts +0 -1
  156. package/agents/toolkits/sql.d.ts +0 -1
  157. package/agents/toolkits/sql.js +0 -1
  158. package/agents/toolkits.cjs +0 -1
  159. package/agents/toolkits.d.cts +0 -1
  160. package/agents/toolkits.d.ts +0 -1
  161. package/agents/toolkits.js +0 -1
  162. package/agents/xml/output_parser.cjs +0 -1
  163. package/agents/xml/output_parser.d.cts +0 -1
  164. package/agents/xml/output_parser.d.ts +0 -1
  165. package/agents/xml/output_parser.js +0 -1
  166. package/agents.cjs +0 -1
  167. package/agents.d.cts +0 -1
  168. package/agents.d.ts +0 -1
  169. package/agents.js +0 -1
  170. package/cache/file_system.cjs +0 -1
  171. package/cache/file_system.d.cts +0 -1
  172. package/cache/file_system.d.ts +0 -1
  173. package/cache/file_system.js +0 -1
  174. package/callbacks.cjs +0 -1
  175. package/callbacks.d.cts +0 -1
  176. package/callbacks.d.ts +0 -1
  177. package/callbacks.js +0 -1
  178. package/chains/combine_documents/reduce.cjs +0 -1
  179. package/chains/combine_documents/reduce.d.cts +0 -1
  180. package/chains/combine_documents/reduce.d.ts +0 -1
  181. package/chains/combine_documents/reduce.js +0 -1
  182. package/chains/combine_documents.cjs +0 -1
  183. package/chains/combine_documents.d.cts +0 -1
  184. package/chains/combine_documents.d.ts +0 -1
  185. package/chains/combine_documents.js +0 -1
  186. package/chains/graph_qa/cypher.cjs +0 -1
  187. package/chains/graph_qa/cypher.d.cts +0 -1
  188. package/chains/graph_qa/cypher.d.ts +0 -1
  189. package/chains/graph_qa/cypher.js +0 -1
  190. package/chains/history_aware_retriever.cjs +0 -1
  191. package/chains/history_aware_retriever.d.cts +0 -1
  192. package/chains/history_aware_retriever.d.ts +0 -1
  193. package/chains/history_aware_retriever.js +0 -1
  194. package/chains/load.cjs +0 -1
  195. package/chains/load.d.cts +0 -1
  196. package/chains/load.d.ts +0 -1
  197. package/chains/load.js +0 -1
  198. package/chains/openai_functions.cjs +0 -1
  199. package/chains/openai_functions.d.cts +0 -1
  200. package/chains/openai_functions.d.ts +0 -1
  201. package/chains/openai_functions.js +0 -1
  202. package/chains/query_constructor/ir.cjs +0 -1
  203. package/chains/query_constructor/ir.d.cts +0 -1
  204. package/chains/query_constructor/ir.d.ts +0 -1
  205. package/chains/query_constructor/ir.js +0 -1
  206. package/chains/query_constructor.cjs +0 -1
  207. package/chains/query_constructor.d.cts +0 -1
  208. package/chains/query_constructor.d.ts +0 -1
  209. package/chains/query_constructor.js +0 -1
  210. package/chains/retrieval.cjs +0 -1
  211. package/chains/retrieval.d.cts +0 -1
  212. package/chains/retrieval.d.ts +0 -1
  213. package/chains/retrieval.js +0 -1
  214. package/chains/sql_db.cjs +0 -1
  215. package/chains/sql_db.d.cts +0 -1
  216. package/chains/sql_db.d.ts +0 -1
  217. package/chains/sql_db.js +0 -1
  218. package/chains.cjs +0 -1
  219. package/chains.d.cts +0 -1
  220. package/chains.d.ts +0 -1
  221. package/chains.js +0 -1
  222. package/chat_models/universal.cjs +0 -1
  223. package/chat_models/universal.d.cts +0 -1
  224. package/chat_models/universal.d.ts +0 -1
  225. package/chat_models/universal.js +0 -1
  226. package/document.cjs +0 -1
  227. package/document.d.cts +0 -1
  228. package/document.d.ts +0 -1
  229. package/document.js +0 -1
  230. package/document_loaders/base.cjs +0 -1
  231. package/document_loaders/base.d.cts +0 -1
  232. package/document_loaders/base.d.ts +0 -1
  233. package/document_loaders/base.js +0 -1
  234. package/document_loaders/fs/buffer.cjs +0 -1
  235. package/document_loaders/fs/buffer.d.cts +0 -1
  236. package/document_loaders/fs/buffer.d.ts +0 -1
  237. package/document_loaders/fs/buffer.js +0 -1
  238. package/document_loaders/fs/directory.cjs +0 -1
  239. package/document_loaders/fs/directory.d.cts +0 -1
  240. package/document_loaders/fs/directory.d.ts +0 -1
  241. package/document_loaders/fs/directory.js +0 -1
  242. package/document_loaders/fs/json.cjs +0 -1
  243. package/document_loaders/fs/json.d.cts +0 -1
  244. package/document_loaders/fs/json.d.ts +0 -1
  245. package/document_loaders/fs/json.js +0 -1
  246. package/document_loaders/fs/multi_file.cjs +0 -1
  247. package/document_loaders/fs/multi_file.d.cts +0 -1
  248. package/document_loaders/fs/multi_file.d.ts +0 -1
  249. package/document_loaders/fs/multi_file.js +0 -1
  250. package/document_loaders/fs/text.cjs +0 -1
  251. package/document_loaders/fs/text.d.cts +0 -1
  252. package/document_loaders/fs/text.d.ts +0 -1
  253. package/document_loaders/fs/text.js +0 -1
  254. package/document_transformers/openai_functions.cjs +0 -1
  255. package/document_transformers/openai_functions.d.cts +0 -1
  256. package/document_transformers/openai_functions.d.ts +0 -1
  257. package/document_transformers/openai_functions.js +0 -1
  258. package/embeddings/cache_backed.cjs +0 -1
  259. package/embeddings/cache_backed.d.cts +0 -1
  260. package/embeddings/cache_backed.d.ts +0 -1
  261. package/embeddings/cache_backed.js +0 -1
  262. package/embeddings/fake.cjs +0 -1
  263. package/embeddings/fake.d.cts +0 -1
  264. package/embeddings/fake.d.ts +0 -1
  265. package/embeddings/fake.js +0 -1
  266. package/evaluation.cjs +0 -1
  267. package/evaluation.d.cts +0 -1
  268. package/evaluation.d.ts +0 -1
  269. package/evaluation.js +0 -1
  270. package/experimental/autogpt.cjs +0 -1
  271. package/experimental/autogpt.d.cts +0 -1
  272. package/experimental/autogpt.d.ts +0 -1
  273. package/experimental/autogpt.js +0 -1
  274. package/experimental/babyagi.cjs +0 -1
  275. package/experimental/babyagi.d.cts +0 -1
  276. package/experimental/babyagi.d.ts +0 -1
  277. package/experimental/babyagi.js +0 -1
  278. package/experimental/chains/violation_of_expectations.cjs +0 -1
  279. package/experimental/chains/violation_of_expectations.d.cts +0 -1
  280. package/experimental/chains/violation_of_expectations.d.ts +0 -1
  281. package/experimental/chains/violation_of_expectations.js +0 -1
  282. package/experimental/generative_agents.cjs +0 -1
  283. package/experimental/generative_agents.d.cts +0 -1
  284. package/experimental/generative_agents.d.ts +0 -1
  285. package/experimental/generative_agents.js +0 -1
  286. package/experimental/masking.cjs +0 -1
  287. package/experimental/masking.d.cts +0 -1
  288. package/experimental/masking.d.ts +0 -1
  289. package/experimental/masking.js +0 -1
  290. package/experimental/openai_assistant.cjs +0 -1
  291. package/experimental/openai_assistant.d.cts +0 -1
  292. package/experimental/openai_assistant.d.ts +0 -1
  293. package/experimental/openai_assistant.js +0 -1
  294. package/experimental/openai_files.cjs +0 -1
  295. package/experimental/openai_files.d.cts +0 -1
  296. package/experimental/openai_files.d.ts +0 -1
  297. package/experimental/openai_files.js +0 -1
  298. package/experimental/plan_and_execute.cjs +0 -1
  299. package/experimental/plan_and_execute.d.cts +0 -1
  300. package/experimental/plan_and_execute.d.ts +0 -1
  301. package/experimental/plan_and_execute.js +0 -1
  302. package/experimental/prompts/custom_format.cjs +0 -1
  303. package/experimental/prompts/custom_format.d.cts +0 -1
  304. package/experimental/prompts/custom_format.d.ts +0 -1
  305. package/experimental/prompts/custom_format.js +0 -1
  306. package/experimental/prompts/handlebars.cjs +0 -1
  307. package/experimental/prompts/handlebars.d.cts +0 -1
  308. package/experimental/prompts/handlebars.d.ts +0 -1
  309. package/experimental/prompts/handlebars.js +0 -1
  310. package/hub/node.cjs +0 -1
  311. package/hub/node.d.cts +0 -1
  312. package/hub/node.d.ts +0 -1
  313. package/hub/node.js +0 -1
  314. package/hub.cjs +0 -1
  315. package/hub.d.cts +0 -1
  316. package/hub.d.ts +0 -1
  317. package/hub.js +0 -1
  318. package/indexes.cjs +0 -1
  319. package/indexes.d.cts +0 -1
  320. package/indexes.d.ts +0 -1
  321. package/indexes.js +0 -1
  322. package/load/serializable.cjs +0 -1
  323. package/load/serializable.d.cts +0 -1
  324. package/load/serializable.d.ts +0 -1
  325. package/load/serializable.js +0 -1
  326. package/load.cjs +0 -1
  327. package/load.d.cts +0 -1
  328. package/load.d.ts +0 -1
  329. package/load.js +0 -1
  330. package/memory/chat_memory.cjs +0 -1
  331. package/memory/chat_memory.d.cts +0 -1
  332. package/memory/chat_memory.d.ts +0 -1
  333. package/memory/chat_memory.js +0 -1
  334. package/memory.cjs +0 -1
  335. package/memory.d.cts +0 -1
  336. package/memory.d.ts +0 -1
  337. package/memory.js +0 -1
  338. package/output_parsers/expression.cjs +0 -1
  339. package/output_parsers/expression.d.cts +0 -1
  340. package/output_parsers/expression.d.ts +0 -1
  341. package/output_parsers/expression.js +0 -1
  342. package/output_parsers.cjs +0 -1
  343. package/output_parsers.d.cts +0 -1
  344. package/output_parsers.d.ts +0 -1
  345. package/output_parsers.js +0 -1
  346. package/retrievers/contextual_compression.cjs +0 -1
  347. package/retrievers/contextual_compression.d.cts +0 -1
  348. package/retrievers/contextual_compression.d.ts +0 -1
  349. package/retrievers/contextual_compression.js +0 -1
  350. package/retrievers/document_compressors/chain_extract.cjs +0 -1
  351. package/retrievers/document_compressors/chain_extract.d.cts +0 -1
  352. package/retrievers/document_compressors/chain_extract.d.ts +0 -1
  353. package/retrievers/document_compressors/chain_extract.js +0 -1
  354. package/retrievers/document_compressors/embeddings_filter.cjs +0 -1
  355. package/retrievers/document_compressors/embeddings_filter.d.cts +0 -1
  356. package/retrievers/document_compressors/embeddings_filter.d.ts +0 -1
  357. package/retrievers/document_compressors/embeddings_filter.js +0 -1
  358. package/retrievers/document_compressors.cjs +0 -1
  359. package/retrievers/document_compressors.d.cts +0 -1
  360. package/retrievers/document_compressors.d.ts +0 -1
  361. package/retrievers/document_compressors.js +0 -1
  362. package/retrievers/ensemble.cjs +0 -1
  363. package/retrievers/ensemble.d.cts +0 -1
  364. package/retrievers/ensemble.d.ts +0 -1
  365. package/retrievers/ensemble.js +0 -1
  366. package/retrievers/hyde.cjs +0 -1
  367. package/retrievers/hyde.d.cts +0 -1
  368. package/retrievers/hyde.d.ts +0 -1
  369. package/retrievers/hyde.js +0 -1
  370. package/retrievers/matryoshka_retriever.cjs +0 -1
  371. package/retrievers/matryoshka_retriever.d.cts +0 -1
  372. package/retrievers/matryoshka_retriever.d.ts +0 -1
  373. package/retrievers/matryoshka_retriever.js +0 -1
  374. package/retrievers/multi_query.cjs +0 -1
  375. package/retrievers/multi_query.d.cts +0 -1
  376. package/retrievers/multi_query.d.ts +0 -1
  377. package/retrievers/multi_query.js +0 -1
  378. package/retrievers/multi_vector.cjs +0 -1
  379. package/retrievers/multi_vector.d.cts +0 -1
  380. package/retrievers/multi_vector.d.ts +0 -1
  381. package/retrievers/multi_vector.js +0 -1
  382. package/retrievers/parent_document.cjs +0 -1
  383. package/retrievers/parent_document.d.cts +0 -1
  384. package/retrievers/parent_document.d.ts +0 -1
  385. package/retrievers/parent_document.js +0 -1
  386. package/retrievers/score_threshold.cjs +0 -1
  387. package/retrievers/score_threshold.d.cts +0 -1
  388. package/retrievers/score_threshold.d.ts +0 -1
  389. package/retrievers/score_threshold.js +0 -1
  390. package/retrievers/self_query/functional.cjs +0 -1
  391. package/retrievers/self_query/functional.d.cts +0 -1
  392. package/retrievers/self_query/functional.d.ts +0 -1
  393. package/retrievers/self_query/functional.js +0 -1
  394. package/retrievers/self_query.cjs +0 -1
  395. package/retrievers/self_query.d.cts +0 -1
  396. package/retrievers/self_query.d.ts +0 -1
  397. package/retrievers/self_query.js +0 -1
  398. package/retrievers/time_weighted.cjs +0 -1
  399. package/retrievers/time_weighted.d.cts +0 -1
  400. package/retrievers/time_weighted.d.ts +0 -1
  401. package/retrievers/time_weighted.js +0 -1
  402. package/schema/prompt_template.cjs +0 -1
  403. package/schema/prompt_template.d.cts +0 -1
  404. package/schema/prompt_template.d.ts +0 -1
  405. package/schema/prompt_template.js +0 -1
  406. package/schema/query_constructor.cjs +0 -1
  407. package/schema/query_constructor.d.cts +0 -1
  408. package/schema/query_constructor.d.ts +0 -1
  409. package/schema/query_constructor.js +0 -1
  410. package/smith.cjs +0 -1
  411. package/smith.d.cts +0 -1
  412. package/smith.d.ts +0 -1
  413. package/smith.js +0 -1
  414. package/sql_db.cjs +0 -1
  415. package/sql_db.d.cts +0 -1
  416. package/sql_db.d.ts +0 -1
  417. package/sql_db.js +0 -1
  418. package/storage/encoder_backed.cjs +0 -1
  419. package/storage/encoder_backed.d.cts +0 -1
  420. package/storage/encoder_backed.d.ts +0 -1
  421. package/storage/encoder_backed.js +0 -1
  422. package/storage/file_system.cjs +0 -1
  423. package/storage/file_system.d.cts +0 -1
  424. package/storage/file_system.d.ts +0 -1
  425. package/storage/file_system.js +0 -1
  426. package/storage/in_memory.cjs +0 -1
  427. package/storage/in_memory.d.cts +0 -1
  428. package/storage/in_memory.d.ts +0 -1
  429. package/storage/in_memory.js +0 -1
  430. package/stores/doc/base.cjs +0 -1
  431. package/stores/doc/base.d.cts +0 -1
  432. package/stores/doc/base.d.ts +0 -1
  433. package/stores/doc/base.js +0 -1
  434. package/stores/doc/in_memory.cjs +0 -1
  435. package/stores/doc/in_memory.d.cts +0 -1
  436. package/stores/doc/in_memory.d.ts +0 -1
  437. package/stores/doc/in_memory.js +0 -1
  438. package/stores/file/in_memory.cjs +0 -1
  439. package/stores/file/in_memory.d.cts +0 -1
  440. package/stores/file/in_memory.d.ts +0 -1
  441. package/stores/file/in_memory.js +0 -1
  442. package/stores/file/node.cjs +0 -1
  443. package/stores/file/node.d.cts +0 -1
  444. package/stores/file/node.d.ts +0 -1
  445. package/stores/file/node.js +0 -1
  446. package/stores/message/in_memory.cjs +0 -1
  447. package/stores/message/in_memory.d.cts +0 -1
  448. package/stores/message/in_memory.d.ts +0 -1
  449. package/stores/message/in_memory.js +0 -1
  450. package/text_splitter.cjs +0 -1
  451. package/text_splitter.d.cts +0 -1
  452. package/text_splitter.d.ts +0 -1
  453. package/text_splitter.js +0 -1
  454. package/tools/chain.cjs +0 -1
  455. package/tools/chain.d.cts +0 -1
  456. package/tools/chain.d.ts +0 -1
  457. package/tools/chain.js +0 -1
  458. package/tools/render.cjs +0 -1
  459. package/tools/render.d.cts +0 -1
  460. package/tools/render.d.ts +0 -1
  461. package/tools/render.js +0 -1
  462. package/tools/retriever.cjs +0 -1
  463. package/tools/retriever.d.cts +0 -1
  464. package/tools/retriever.d.ts +0 -1
  465. package/tools/retriever.js +0 -1
  466. package/tools/sql.cjs +0 -1
  467. package/tools/sql.d.cts +0 -1
  468. package/tools/sql.d.ts +0 -1
  469. package/tools/sql.js +0 -1
  470. package/tools/webbrowser.cjs +0 -1
  471. package/tools/webbrowser.d.cts +0 -1
  472. package/tools/webbrowser.d.ts +0 -1
  473. package/tools/webbrowser.js +0 -1
  474. package/tools.cjs +0 -1
  475. package/tools.d.cts +0 -1
  476. package/tools.d.ts +0 -1
  477. package/tools.js +0 -1
  478. package/util/document.cjs +0 -1
  479. package/util/document.d.cts +0 -1
  480. package/util/document.d.ts +0 -1
  481. package/util/document.js +0 -1
  482. package/util/math.cjs +0 -1
  483. package/util/math.d.cts +0 -1
  484. package/util/math.d.ts +0 -1
  485. package/util/math.js +0 -1
  486. package/util/time.cjs +0 -1
  487. package/util/time.d.cts +0 -1
  488. package/util/time.d.ts +0 -1
  489. package/util/time.js +0 -1
  490. package/vectorstores/memory.cjs +0 -1
  491. package/vectorstores/memory.d.cts +0 -1
  492. package/vectorstores/memory.d.ts +0 -1
  493. package/vectorstores/memory.js +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"structured.cjs","names":["BaseOutputParser","schema: T","schemas: S","z","text: string","OutputParserException","options?: JsonMarkdownFormatInstructionsOptions","schemaInput: JsonSchema7Type","type: string","type","description"],"sources":["../../src/output_parsers/structured.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport {\n toJsonSchema,\n type JsonSchema7Type,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n BaseOutputParser,\n FormatInstructionsOptions,\n OutputParserException,\n} from \"@langchain/core/output_parsers\";\nimport {\n interopParseAsync,\n type InferInteropZodOutput,\n type InteropZodType,\n} from \"@langchain/core/utils/types\";\n\nexport type JsonMarkdownStructuredOutputParserInput = {\n interpolationDepth?: number;\n};\n\nexport interface JsonMarkdownFormatInstructionsOptions\n extends FormatInstructionsOptions {\n interpolationDepth?: number;\n}\n\nexport class StructuredOutputParser<\n T extends InteropZodType\n> extends BaseOutputParser<InferInteropZodOutput<T>> {\n static lc_name() {\n return \"StructuredOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"output_parsers\", \"structured\"];\n\n toJSON() {\n return this.toJSONNotImplemented();\n }\n\n constructor(public schema: T) {\n super(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a Zod schema.\n * @param schema The Zod schema which the output should match\n * @returns A new instance of StructuredOutputParser.\n */\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a set of names and\n * descriptions.\n * @param schemas An object where each key is a name and each value is a description\n * @returns A new instance of StructuredOutputParser.\n */\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this(zodSchema);\n }\n\n /**\n * Returns a markdown code snippet with a JSON object formatted according\n * to the schema.\n * @param options Optional. The options for formatting the instructions\n * @returns A markdown code snippet with a JSON object formatted according to the schema.\n */\n getFormatInstructions(): string {\n return `You must format your output as a JSON value that adheres to a given \"JSON Schema\" instance.\n\n\"JSON Schema\" is a declarative language that allows you to annotate and validate JSON documents.\n\nFor example, the example \"JSON Schema\" instance {{\"properties\": {{\"foo\": {{\"description\": \"a list of test words\", \"type\": \"array\", \"items\": {{\"type\": \"string\"}}}}}}, \"required\": [\"foo\"]}}}}\nwould match an object with one required property, \"foo\". The \"type\" property specifies \"foo\" must be an \"array\", and the \"description\" property semantically describes it as \"a list of test words\". The items within \"foo\" must be strings.\nThus, the object {{\"foo\": [\"bar\", \"baz\"]}} is a well-formatted instance of this example \"JSON Schema\". The object {{\"properties\": {{\"foo\": [\"bar\", \"baz\"]}}}} is not well-formatted.\n\nYour output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!\n\nHere is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:\n\\`\\`\\`json\n${JSON.stringify(toJsonSchema(this.schema))}\n\\`\\`\\`\n`;\n }\n\n /**\n * Parses the given text according to the schema.\n * @param text The text to parse\n * @returns The parsed output.\n */\n async parse(text: string): Promise<InferInteropZodOutput<T>> {\n try {\n const json = text.includes(\"```\")\n ? text.trim().split(/```(?:json)?/)[1]\n : text.trim();\n return await interopParseAsync(this.schema, JSON.parse(json));\n } catch {\n try {\n return await interopParseAsync(this.schema, JSON.parse(text.trim()));\n } catch (e2) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e2}`,\n text\n );\n }\n }\n }\n}\n\n/**\n * A specific type of `StructuredOutputParser` that parses JSON data\n * formatted as a markdown code snippet.\n */\nexport class JsonMarkdownStructuredOutputParser<\n T extends InteropZodType\n> extends StructuredOutputParser<T> {\n static lc_name() {\n return \"JsonMarkdownStructuredOutputParser\";\n }\n\n getFormatInstructions(\n options?: JsonMarkdownFormatInstructionsOptions\n ): string {\n const interpolationDepth = options?.interpolationDepth ?? 1;\n if (interpolationDepth < 1) {\n throw new Error(\"f string interpolation depth must be at least 1\");\n }\n\n return `Return a markdown code snippet with a JSON object formatted to look like:\\n\\`\\`\\`json\\n${this._schemaToInstruction(\n toJsonSchema(this.schema)\n )\n .replaceAll(\"{\", \"{\".repeat(interpolationDepth))\n .replaceAll(\"}\", \"}\".repeat(interpolationDepth))}\\n\\`\\`\\``;\n }\n\n private _schemaToInstruction(\n schemaInput: JsonSchema7Type,\n indent = 2\n ): string {\n const schema = schemaInput as Extract<\n JsonSchema7Type,\n | JsonSchema7ObjectType\n | JsonSchema7ArrayType\n | JsonSchema7StringType\n | JsonSchema7NumberType\n | JsonSchema7NullableType\n >;\n\n if (\"type\" in schema) {\n let nullable = false;\n let type: string;\n if (Array.isArray(schema.type)) {\n const nullIdx = schema.type.findIndex((type) => type === \"null\");\n if (nullIdx !== -1) {\n nullable = true;\n schema.type.splice(nullIdx, 1);\n }\n type = schema.type.join(\" | \") as string;\n } else {\n type = schema.type;\n }\n\n if (schema.type === \"object\" && schema.properties) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n const properties = Object.entries(schema.properties)\n .map(([key, value]) => {\n const isOptional = schema.required?.includes(key)\n ? \"\"\n : \" (optional)\";\n return `${\" \".repeat(indent)}\"${key}\": ${this._schemaToInstruction(\n value,\n indent + 2\n )}${isOptional}`;\n })\n .join(\"\\n\");\n return `{\\n${properties}\\n${\" \".repeat(indent - 2)}}${description}`;\n }\n if (schema.type === \"array\" && schema.items) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n return `array[\\n${\" \".repeat(indent)}${this._schemaToInstruction(\n schema.items,\n indent + 2\n )}\\n${\" \".repeat(indent - 2)}] ${description}`;\n }\n const isNullable = nullable ? \" (nullable)\" : \"\";\n const description = schema.description ? ` // ${schema.description}` : \"\";\n return `${type}${description}${isNullable}`;\n }\n\n if (\"anyOf\" in schema) {\n return schema.anyOf\n .map((s) => this._schemaToInstruction(s, indent))\n .join(`\\n${\" \".repeat(indent - 2)}`);\n }\n\n throw new Error(\"unsupported schema type\");\n }\n\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this<T>(schema);\n }\n\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this<typeof zodSchema>(zodSchema);\n }\n}\n\nexport interface AsymmetricStructuredOutputParserFields<\n T extends InteropZodType\n> {\n inputSchema: T;\n}\n\n/**\n * A type of `StructuredOutputParser` that handles asymmetric input and\n * output schemas.\n */\nexport abstract class AsymmetricStructuredOutputParser<\n T extends InteropZodType,\n Y = unknown\n> extends BaseOutputParser<Y> {\n private structuredInputParser: JsonMarkdownStructuredOutputParser<T>;\n\n constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>) {\n super(...arguments);\n this.structuredInputParser = new JsonMarkdownStructuredOutputParser(\n inputSchema\n );\n }\n\n /**\n * Processes the parsed input into the desired output format. Must be\n * implemented by subclasses.\n * @param input The parsed input\n * @returns The processed output.\n */\n abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;\n\n async parse(text: string): Promise<Y> {\n let parsedInput;\n try {\n parsedInput = await this.structuredInputParser.parse(text);\n } catch (e) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e}`,\n text\n );\n }\n\n return this.outputProcessor(parsedInput);\n }\n\n getFormatInstructions(): string {\n return this.structuredInputParser.getFormatInstructions();\n }\n}\n"],"mappings":";;;;;;;AA8BA,IAAa,yBAAb,cAEUA,iDAA2C;CACnD,OAAO,UAAU;AACf,SAAO;CACR;CAED,eAAe;EAAC;EAAa;EAAkB;CAAa;CAE5D,SAAS;AACP,SAAO,KAAK,sBAAsB;CACnC;CAED,YAAmBC,QAAW;EAC5B,MAAM,OAAO;EADI;CAElB;;;;;;CAOD,OAAO,cAAwCA,QAAW;AACxD,SAAO,IAAI,KAAK;CACjB;;;;;;;CAQD,OAAO,y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,EAAE,EAAE,IAAI,OAAO,SAAS,EAAE,EAAE,CAAC;AAGxC,QAAM,IAAI,MAAM;CACjB;CAED,OAAO,cAAwCT,QAAW;AACxD,SAAO,IAAI,KAAQ;CACpB;CAED,OAAO,yBACLC,SACA;EACA,MAAM,YAAYC,SAAE,OAClB,OAAO,YACL,OAAO,QAAQ,QAAQ,CAAC,IACtB,CAAC,CAAC,MAAM,YAAY,KAClB,CAAC,MAAMA,SAAE,QAAQ,CAAC,SAAS,YAAY,AAAC,EAC3C,CACF,CACF;AAED,SAAO,IAAI,KAAuB;CACnC;AACF;;;;;AAYD,IAAsB,mCAAtB,cAGUH,iDAAoB;CAC5B,AAAQ;CAER,YAAY,EAAE,aAAwD,EAAE;EACtE,MAAM,GAAG,UAAU;EACnB,KAAK,wBAAwB,IAAI,mCAC/B;CAEH;CAUD,MAAM,MAAMI,MAA0B;EACpC,IAAI;AACJ,MAAI;GACF,cAAc,MAAM,KAAK,sBAAsB,MAAM,KAAK;EAC3D,SAAQ,GAAG;AACV,SAAM,IAAIC,sDACR,CAAC,wBAAwB,EAAE,KAAK,UAAU,EAAE,GAAG,EAC/C;EAEH;AAED,SAAO,KAAK,gBAAgB,YAAY;CACzC;CAED,wBAAgC;AAC9B,SAAO,KAAK,sBAAsB,uBAAuB;CAC1D;AACF"}
1
+ {"version":3,"file":"structured.cjs","names":["BaseOutputParser","schema: T","schemas: S","z","text: string","OutputParserException","options?: JsonMarkdownFormatInstructionsOptions","schemaInput: JsonSchema7Type","type: string","type","description"],"sources":["../../src/output_parsers/structured.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport {\n toJsonSchema,\n type JsonSchema7Type,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n BaseOutputParser,\n FormatInstructionsOptions,\n OutputParserException,\n} from \"@langchain/core/output_parsers\";\nimport {\n interopParseAsync,\n type InferInteropZodOutput,\n type InteropZodType,\n} from \"@langchain/core/utils/types\";\n\nexport type JsonMarkdownStructuredOutputParserInput = {\n interpolationDepth?: number;\n};\n\nexport interface JsonMarkdownFormatInstructionsOptions\n extends FormatInstructionsOptions {\n interpolationDepth?: number;\n}\n\nexport class StructuredOutputParser<\n T extends InteropZodType,\n> extends BaseOutputParser<InferInteropZodOutput<T>> {\n static lc_name() {\n return \"StructuredOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"output_parsers\", \"structured\"];\n\n toJSON() {\n return this.toJSONNotImplemented();\n }\n\n constructor(public schema: T) {\n super(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a Zod schema.\n * @param schema The Zod schema which the output should match\n * @returns A new instance of StructuredOutputParser.\n */\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a set of names and\n * descriptions.\n * @param schemas An object where each key is a name and each value is a description\n * @returns A new instance of StructuredOutputParser.\n */\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this(zodSchema);\n }\n\n /**\n * Returns a markdown code snippet with a JSON object formatted according\n * to the schema.\n * @param options Optional. The options for formatting the instructions\n * @returns A markdown code snippet with a JSON object formatted according to the schema.\n */\n getFormatInstructions(): string {\n return `You must format your output as a JSON value that adheres to a given \"JSON Schema\" instance.\n\n\"JSON Schema\" is a declarative language that allows you to annotate and validate JSON documents.\n\nFor example, the example \"JSON Schema\" instance {{\"properties\": {{\"foo\": {{\"description\": \"a list of test words\", \"type\": \"array\", \"items\": {{\"type\": \"string\"}}}}}}, \"required\": [\"foo\"]}}}}\nwould match an object with one required property, \"foo\". The \"type\" property specifies \"foo\" must be an \"array\", and the \"description\" property semantically describes it as \"a list of test words\". The items within \"foo\" must be strings.\nThus, the object {{\"foo\": [\"bar\", \"baz\"]}} is a well-formatted instance of this example \"JSON Schema\". The object {{\"properties\": {{\"foo\": [\"bar\", \"baz\"]}}}} is not well-formatted.\n\nYour output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!\n\nHere is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:\n\\`\\`\\`json\n${JSON.stringify(toJsonSchema(this.schema))}\n\\`\\`\\`\n`;\n }\n\n /**\n * Parses the given text according to the schema.\n * @param text The text to parse\n * @returns The parsed output.\n */\n async parse(text: string): Promise<InferInteropZodOutput<T>> {\n try {\n const json = text.includes(\"```\")\n ? text.trim().split(/```(?:json)?/)[1]\n : text.trim();\n return await interopParseAsync(this.schema, JSON.parse(json));\n } catch {\n try {\n return await interopParseAsync(this.schema, JSON.parse(text.trim()));\n } catch (e2) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e2}`,\n text\n );\n }\n }\n }\n}\n\n/**\n * A specific type of `StructuredOutputParser` that parses JSON data\n * formatted as a markdown code snippet.\n */\nexport class JsonMarkdownStructuredOutputParser<\n T extends InteropZodType,\n> extends StructuredOutputParser<T> {\n static lc_name() {\n return \"JsonMarkdownStructuredOutputParser\";\n }\n\n getFormatInstructions(\n options?: JsonMarkdownFormatInstructionsOptions\n ): string {\n const interpolationDepth = options?.interpolationDepth ?? 1;\n if (interpolationDepth < 1) {\n throw new Error(\"f string interpolation depth must be at least 1\");\n }\n\n return `Return a markdown code snippet with a JSON object formatted to look like:\\n\\`\\`\\`json\\n${this._schemaToInstruction(\n toJsonSchema(this.schema)\n )\n .replaceAll(\"{\", \"{\".repeat(interpolationDepth))\n .replaceAll(\"}\", \"}\".repeat(interpolationDepth))}\\n\\`\\`\\``;\n }\n\n private _schemaToInstruction(\n schemaInput: JsonSchema7Type,\n indent = 2\n ): string {\n const schema = schemaInput as Extract<\n JsonSchema7Type,\n | JsonSchema7ObjectType\n | JsonSchema7ArrayType\n | JsonSchema7StringType\n | JsonSchema7NumberType\n | JsonSchema7NullableType\n >;\n\n if (\"type\" in schema) {\n let nullable = false;\n let type: string;\n if (Array.isArray(schema.type)) {\n const nullIdx = schema.type.findIndex((type) => type === \"null\");\n if (nullIdx !== -1) {\n nullable = true;\n schema.type.splice(nullIdx, 1);\n }\n type = schema.type.join(\" | \") as string;\n } else {\n type = schema.type;\n }\n\n if (schema.type === \"object\" && schema.properties) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n const properties = Object.entries(schema.properties)\n .map(([key, value]) => {\n const isOptional = schema.required?.includes(key)\n ? \"\"\n : \" (optional)\";\n return `${\" \".repeat(indent)}\"${key}\": ${this._schemaToInstruction(\n value,\n indent + 2\n )}${isOptional}`;\n })\n .join(\"\\n\");\n return `{\\n${properties}\\n${\" \".repeat(indent - 2)}}${description}`;\n }\n if (schema.type === \"array\" && schema.items) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n return `array[\\n${\" \".repeat(indent)}${this._schemaToInstruction(\n schema.items,\n indent + 2\n )}\\n${\" \".repeat(indent - 2)}] ${description}`;\n }\n const isNullable = nullable ? \" (nullable)\" : \"\";\n const description = schema.description ? ` // ${schema.description}` : \"\";\n return `${type}${description}${isNullable}`;\n }\n\n if (\"anyOf\" in schema) {\n return schema.anyOf\n .map((s) => this._schemaToInstruction(s, indent))\n .join(`\\n${\" \".repeat(indent - 2)}`);\n }\n\n throw new Error(\"unsupported schema type\");\n }\n\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this<T>(schema);\n }\n\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this<typeof zodSchema>(zodSchema);\n }\n}\n\nexport interface AsymmetricStructuredOutputParserFields<\n T extends InteropZodType,\n> {\n inputSchema: T;\n}\n\n/**\n * A type of `StructuredOutputParser` that handles asymmetric input and\n * output schemas.\n */\nexport abstract class AsymmetricStructuredOutputParser<\n T extends InteropZodType,\n Y = unknown,\n> extends BaseOutputParser<Y> {\n private structuredInputParser: JsonMarkdownStructuredOutputParser<T>;\n\n constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>) {\n super(...arguments);\n this.structuredInputParser = new JsonMarkdownStructuredOutputParser(\n inputSchema\n );\n }\n\n /**\n * Processes the parsed input into the desired output format. Must be\n * implemented by subclasses.\n * @param input The parsed input\n * @returns The processed output.\n */\n abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;\n\n async parse(text: string): Promise<Y> {\n let parsedInput;\n try {\n parsedInput = await this.structuredInputParser.parse(text);\n } catch (e) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e}`,\n text\n );\n }\n\n return this.outputProcessor(parsedInput);\n }\n\n getFormatInstructions(): string {\n return this.structuredInputParser.getFormatInstructions();\n }\n}\n"],"mappings":";;;;;;;AA8BA,IAAa,yBAAb,cAEUA,iDAA2C;CACnD,OAAO,UAAU;AACf,SAAO;CACR;CAED,eAAe;EAAC;EAAa;EAAkB;CAAa;CAE5D,SAAS;AACP,SAAO,KAAK,sBAAsB;CACnC;CAED,YAAmBC,QAAW;EAC5B,MAAM,OAAO;EADI;CAElB;;;;;;CAOD,OAAO,cAAwCA,QAAW;AACxD,SAAO,IAAI,KAAK;CACjB;;;;;;;CAQD,OAAO,yBACLC,SACA;EACA,MAAM,YAAYC,SAAE,OAClB,OAAO,YACL,OAAO,QAAQ,QAAQ,CAAC,IACtB,CAAC,CAAC,MAAM,YAAY,KAClB,CAAC,MAAMA,SAAE,QAAQ,CAAC,SAAS,YAAY,AAAC,EAC3C,CACF,CACF;AAED,SAAO,IAAI,KAAK;CACjB;;;;;;;CAQD,wBAAgC;AAC9B,SAAO,CAAC;;;;;;;;;;;;AAYZ,EAAE,KAAK,+DAAuB,KAAK,OAAO,CAAC,CAAC;;AAE5C,CAAC;CACE;;;;;;CAOD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,KAAuB;CACnC;AACF;;;;;AAYD,IAAsB,mCAAtB,cAGUH,iDAAoB;CAC5B,AAAQ;CAER,YAAY,EAAE,aAAwD,EAAE;EACtE,MAAM,GAAG,UAAU;EACnB,KAAK,wBAAwB,IAAI,mCAC/B;CAEH;CAUD,MAAM,MAAMI,MAA0B;EACpC,IAAI;AACJ,MAAI;GACF,cAAc,MAAM,KAAK,sBAAsB,MAAM,KAAK;EAC3D,SAAQ,GAAG;AACV,SAAM,IAAIC,sDACR,CAAC,wBAAwB,EAAE,KAAK,UAAU,EAAE,GAAG,EAC/C;EAEH;AAED,SAAO,KAAK,gBAAgB,YAAY;CACzC;CAED,wBAAgC;AAC9B,SAAO,KAAK,sBAAsB,uBAAuB;CAC1D;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"structured.js","names":["schema: T","schemas: S","text: string","options?: JsonMarkdownFormatInstructionsOptions","schemaInput: JsonSchema7Type","type: string","type","description"],"sources":["../../src/output_parsers/structured.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport {\n toJsonSchema,\n type JsonSchema7Type,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n BaseOutputParser,\n FormatInstructionsOptions,\n OutputParserException,\n} from \"@langchain/core/output_parsers\";\nimport {\n interopParseAsync,\n type InferInteropZodOutput,\n type InteropZodType,\n} from \"@langchain/core/utils/types\";\n\nexport type JsonMarkdownStructuredOutputParserInput = {\n interpolationDepth?: number;\n};\n\nexport interface JsonMarkdownFormatInstructionsOptions\n extends FormatInstructionsOptions {\n interpolationDepth?: number;\n}\n\nexport class StructuredOutputParser<\n T extends InteropZodType\n> extends BaseOutputParser<InferInteropZodOutput<T>> {\n static lc_name() {\n return \"StructuredOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"output_parsers\", \"structured\"];\n\n toJSON() {\n return this.toJSONNotImplemented();\n }\n\n constructor(public schema: T) {\n super(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a Zod schema.\n * @param schema The Zod schema which the output should match\n * @returns A new instance of StructuredOutputParser.\n */\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a set of names and\n * descriptions.\n * @param schemas An object where each key is a name and each value is a description\n * @returns A new instance of StructuredOutputParser.\n */\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this(zodSchema);\n }\n\n /**\n * Returns a markdown code snippet with a JSON object formatted according\n * to the schema.\n * @param options Optional. The options for formatting the instructions\n * @returns A markdown code snippet with a JSON object formatted according to the schema.\n */\n getFormatInstructions(): string {\n return `You must format your output as a JSON value that adheres to a given \"JSON Schema\" instance.\n\n\"JSON Schema\" is a declarative language that allows you to annotate and validate JSON documents.\n\nFor example, the example \"JSON Schema\" instance {{\"properties\": {{\"foo\": {{\"description\": \"a list of test words\", \"type\": \"array\", \"items\": {{\"type\": \"string\"}}}}}}, \"required\": [\"foo\"]}}}}\nwould match an object with one required property, \"foo\". The \"type\" property specifies \"foo\" must be an \"array\", and the \"description\" property semantically describes it as \"a list of test words\". The items within \"foo\" must be strings.\nThus, the object {{\"foo\": [\"bar\", \"baz\"]}} is a well-formatted instance of this example \"JSON Schema\". The object {{\"properties\": {{\"foo\": [\"bar\", \"baz\"]}}}} is not well-formatted.\n\nYour output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!\n\nHere is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:\n\\`\\`\\`json\n${JSON.stringify(toJsonSchema(this.schema))}\n\\`\\`\\`\n`;\n }\n\n /**\n * Parses the given text according to the schema.\n * @param text The text to parse\n * @returns The parsed output.\n */\n async parse(text: string): Promise<InferInteropZodOutput<T>> {\n try {\n const json = text.includes(\"```\")\n ? text.trim().split(/```(?:json)?/)[1]\n : text.trim();\n return await interopParseAsync(this.schema, JSON.parse(json));\n } catch {\n try {\n return await interopParseAsync(this.schema, JSON.parse(text.trim()));\n } catch (e2) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e2}`,\n text\n );\n }\n }\n }\n}\n\n/**\n * A specific type of `StructuredOutputParser` that parses JSON data\n * formatted as a markdown code snippet.\n */\nexport class JsonMarkdownStructuredOutputParser<\n T extends InteropZodType\n> extends StructuredOutputParser<T> {\n static lc_name() {\n return \"JsonMarkdownStructuredOutputParser\";\n }\n\n getFormatInstructions(\n options?: JsonMarkdownFormatInstructionsOptions\n ): string {\n const interpolationDepth = options?.interpolationDepth ?? 1;\n if (interpolationDepth < 1) {\n throw new Error(\"f string interpolation depth must be at least 1\");\n }\n\n return `Return a markdown code snippet with a JSON object formatted to look like:\\n\\`\\`\\`json\\n${this._schemaToInstruction(\n toJsonSchema(this.schema)\n )\n .replaceAll(\"{\", \"{\".repeat(interpolationDepth))\n .replaceAll(\"}\", \"}\".repeat(interpolationDepth))}\\n\\`\\`\\``;\n }\n\n private _schemaToInstruction(\n schemaInput: JsonSchema7Type,\n indent = 2\n ): string {\n const schema = schemaInput as Extract<\n JsonSchema7Type,\n | JsonSchema7ObjectType\n | JsonSchema7ArrayType\n | JsonSchema7StringType\n | JsonSchema7NumberType\n | JsonSchema7NullableType\n >;\n\n if (\"type\" in schema) {\n let nullable = false;\n let type: string;\n if (Array.isArray(schema.type)) {\n const nullIdx = schema.type.findIndex((type) => type === \"null\");\n if (nullIdx !== -1) {\n nullable = true;\n schema.type.splice(nullIdx, 1);\n }\n type = schema.type.join(\" | \") as string;\n } else {\n type = schema.type;\n }\n\n if (schema.type === \"object\" && schema.properties) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n const properties = Object.entries(schema.properties)\n .map(([key, value]) => {\n const isOptional = schema.required?.includes(key)\n ? \"\"\n : \" (optional)\";\n return `${\" \".repeat(indent)}\"${key}\": ${this._schemaToInstruction(\n value,\n indent + 2\n )}${isOptional}`;\n })\n .join(\"\\n\");\n return `{\\n${properties}\\n${\" \".repeat(indent - 2)}}${description}`;\n }\n if (schema.type === \"array\" && schema.items) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n return `array[\\n${\" \".repeat(indent)}${this._schemaToInstruction(\n schema.items,\n indent + 2\n )}\\n${\" \".repeat(indent - 2)}] ${description}`;\n }\n const isNullable = nullable ? \" (nullable)\" : \"\";\n const description = schema.description ? ` // ${schema.description}` : \"\";\n return `${type}${description}${isNullable}`;\n }\n\n if (\"anyOf\" in schema) {\n return schema.anyOf\n .map((s) => this._schemaToInstruction(s, indent))\n .join(`\\n${\" \".repeat(indent - 2)}`);\n }\n\n throw new Error(\"unsupported schema type\");\n }\n\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this<T>(schema);\n }\n\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this<typeof zodSchema>(zodSchema);\n }\n}\n\nexport interface AsymmetricStructuredOutputParserFields<\n T extends InteropZodType\n> {\n inputSchema: T;\n}\n\n/**\n * A type of `StructuredOutputParser` that handles asymmetric input and\n * output schemas.\n */\nexport abstract class AsymmetricStructuredOutputParser<\n T extends InteropZodType,\n Y = unknown\n> extends BaseOutputParser<Y> {\n private structuredInputParser: JsonMarkdownStructuredOutputParser<T>;\n\n constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>) {\n super(...arguments);\n this.structuredInputParser = new JsonMarkdownStructuredOutputParser(\n inputSchema\n );\n }\n\n /**\n * Processes the parsed input into the desired output format. Must be\n * implemented by subclasses.\n * @param input The parsed input\n * @returns The processed output.\n */\n abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;\n\n async parse(text: string): Promise<Y> {\n let parsedInput;\n try {\n parsedInput = await this.structuredInputParser.parse(text);\n } catch (e) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e}`,\n text\n );\n }\n\n return this.outputProcessor(parsedInput);\n }\n\n getFormatInstructions(): string {\n return this.structuredInputParser.getFormatInstructions();\n }\n}\n"],"mappings":";;;;;;AA8BA,IAAa,yBAAb,cAEU,iBAA2C;CACnD,OAAO,UAAU;AACf,SAAO;CACR;CAED,eAAe;EAAC;EAAa;EAAkB;CAAa;CAE5D,SAAS;AACP,SAAO,KAAK,sBAAsB;CACnC;CAED,YAAmBA,QAAW;EAC5B,MAAM,OAAO;EADI;CAElB;;;;;;CAOD,OAAO,cAAwCA,QAAW;AACxD,SAAO,IAAI,KAAK;CACjB;;;;;;;CAQD,OAAO,yBACLC,SACA;EACA,MAAM,YAAY,EAAE,OAClB,OAAO,YACL,OAAO,QAAQ,QAAQ,CAAC,IACtB,CAAC,CAAC,MAAM,YAAY,KAClB,CAAC,MAAM,EAAE,QAAQ,CAAC,SAAS,YAAY,AAAC,EAC3C,CACF,CACF;AAED,SAAO,IAAI,KAAK;CACjB;;;;;;;CAQD,wBAAgC;AAC9B,SAAO,CAAC;;;;;;;;;;;;AAYZ,EAAE,KAAK,UAAU,aAAa,KAAK,OAAO,CAAC,CAAC;;AAE5C,CAAC;CACE;;;;;;CAOD,MAAM,MAAMC,MAAiD;AAC3D,MAAI;GACF,MAAM,OAAO,KAAK,SAAS,MAAM,GAC7B,KAAK,MAAM,CAAC,MAAM,eAAe,CAAC,KAClC,KAAK,MAAM;AACf,UAAO,MAAM,kBAAkB,KAAK,QAAQ,KAAK,MAAM,KAAK,CAAC;EAC9D,QAAO;AACN,OAAI;AACF,WAAO,MAAM,kBAAkB,KAAK,QAAQ,KAAK,MAAM,KAAK,MAAM,CAAC,CAAC;GACrE,SAAQ,IAAI;AACX,UAAM,IAAI,sBACR,CAAC,wBAAwB,EAAE,KAAK,UA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G,EAC/C;EAEH;AAED,SAAO,KAAK,gBAAgB,YAAY;CACzC;CAED,wBAAgC;AAC9B,SAAO,KAAK,sBAAsB,uBAAuB;CAC1D;AACF"}
1
+ {"version":3,"file":"structured.js","names":["schema: T","schemas: S","text: string","options?: JsonMarkdownFormatInstructionsOptions","schemaInput: JsonSchema7Type","type: string","type","description"],"sources":["../../src/output_parsers/structured.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport {\n toJsonSchema,\n type JsonSchema7Type,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"@langchain/core/utils/json_schema\";\nimport {\n BaseOutputParser,\n FormatInstructionsOptions,\n OutputParserException,\n} from \"@langchain/core/output_parsers\";\nimport {\n interopParseAsync,\n type InferInteropZodOutput,\n type InteropZodType,\n} from \"@langchain/core/utils/types\";\n\nexport type JsonMarkdownStructuredOutputParserInput = {\n interpolationDepth?: number;\n};\n\nexport interface JsonMarkdownFormatInstructionsOptions\n extends FormatInstructionsOptions {\n interpolationDepth?: number;\n}\n\nexport class StructuredOutputParser<\n T extends InteropZodType,\n> extends BaseOutputParser<InferInteropZodOutput<T>> {\n static lc_name() {\n return \"StructuredOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"output_parsers\", \"structured\"];\n\n toJSON() {\n return this.toJSONNotImplemented();\n }\n\n constructor(public schema: T) {\n super(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a Zod schema.\n * @param schema The Zod schema which the output should match\n * @returns A new instance of StructuredOutputParser.\n */\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this(schema);\n }\n\n /**\n * Creates a new StructuredOutputParser from a set of names and\n * descriptions.\n * @param schemas An object where each key is a name and each value is a description\n * @returns A new instance of StructuredOutputParser.\n */\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this(zodSchema);\n }\n\n /**\n * Returns a markdown code snippet with a JSON object formatted according\n * to the schema.\n * @param options Optional. The options for formatting the instructions\n * @returns A markdown code snippet with a JSON object formatted according to the schema.\n */\n getFormatInstructions(): string {\n return `You must format your output as a JSON value that adheres to a given \"JSON Schema\" instance.\n\n\"JSON Schema\" is a declarative language that allows you to annotate and validate JSON documents.\n\nFor example, the example \"JSON Schema\" instance {{\"properties\": {{\"foo\": {{\"description\": \"a list of test words\", \"type\": \"array\", \"items\": {{\"type\": \"string\"}}}}}}, \"required\": [\"foo\"]}}}}\nwould match an object with one required property, \"foo\". The \"type\" property specifies \"foo\" must be an \"array\", and the \"description\" property semantically describes it as \"a list of test words\". The items within \"foo\" must be strings.\nThus, the object {{\"foo\": [\"bar\", \"baz\"]}} is a well-formatted instance of this example \"JSON Schema\". The object {{\"properties\": {{\"foo\": [\"bar\", \"baz\"]}}}} is not well-formatted.\n\nYour output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!\n\nHere is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:\n\\`\\`\\`json\n${JSON.stringify(toJsonSchema(this.schema))}\n\\`\\`\\`\n`;\n }\n\n /**\n * Parses the given text according to the schema.\n * @param text The text to parse\n * @returns The parsed output.\n */\n async parse(text: string): Promise<InferInteropZodOutput<T>> {\n try {\n const json = text.includes(\"```\")\n ? text.trim().split(/```(?:json)?/)[1]\n : text.trim();\n return await interopParseAsync(this.schema, JSON.parse(json));\n } catch {\n try {\n return await interopParseAsync(this.schema, JSON.parse(text.trim()));\n } catch (e2) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e2}`,\n text\n );\n }\n }\n }\n}\n\n/**\n * A specific type of `StructuredOutputParser` that parses JSON data\n * formatted as a markdown code snippet.\n */\nexport class JsonMarkdownStructuredOutputParser<\n T extends InteropZodType,\n> extends StructuredOutputParser<T> {\n static lc_name() {\n return \"JsonMarkdownStructuredOutputParser\";\n }\n\n getFormatInstructions(\n options?: JsonMarkdownFormatInstructionsOptions\n ): string {\n const interpolationDepth = options?.interpolationDepth ?? 1;\n if (interpolationDepth < 1) {\n throw new Error(\"f string interpolation depth must be at least 1\");\n }\n\n return `Return a markdown code snippet with a JSON object formatted to look like:\\n\\`\\`\\`json\\n${this._schemaToInstruction(\n toJsonSchema(this.schema)\n )\n .replaceAll(\"{\", \"{\".repeat(interpolationDepth))\n .replaceAll(\"}\", \"}\".repeat(interpolationDepth))}\\n\\`\\`\\``;\n }\n\n private _schemaToInstruction(\n schemaInput: JsonSchema7Type,\n indent = 2\n ): string {\n const schema = schemaInput as Extract<\n JsonSchema7Type,\n | JsonSchema7ObjectType\n | JsonSchema7ArrayType\n | JsonSchema7StringType\n | JsonSchema7NumberType\n | JsonSchema7NullableType\n >;\n\n if (\"type\" in schema) {\n let nullable = false;\n let type: string;\n if (Array.isArray(schema.type)) {\n const nullIdx = schema.type.findIndex((type) => type === \"null\");\n if (nullIdx !== -1) {\n nullable = true;\n schema.type.splice(nullIdx, 1);\n }\n type = schema.type.join(\" | \") as string;\n } else {\n type = schema.type;\n }\n\n if (schema.type === \"object\" && schema.properties) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n const properties = Object.entries(schema.properties)\n .map(([key, value]) => {\n const isOptional = schema.required?.includes(key)\n ? \"\"\n : \" (optional)\";\n return `${\" \".repeat(indent)}\"${key}\": ${this._schemaToInstruction(\n value,\n indent + 2\n )}${isOptional}`;\n })\n .join(\"\\n\");\n return `{\\n${properties}\\n${\" \".repeat(indent - 2)}}${description}`;\n }\n if (schema.type === \"array\" && schema.items) {\n const description = schema.description\n ? ` // ${schema.description}`\n : \"\";\n return `array[\\n${\" \".repeat(indent)}${this._schemaToInstruction(\n schema.items,\n indent + 2\n )}\\n${\" \".repeat(indent - 2)}] ${description}`;\n }\n const isNullable = nullable ? \" (nullable)\" : \"\";\n const description = schema.description ? ` // ${schema.description}` : \"\";\n return `${type}${description}${isNullable}`;\n }\n\n if (\"anyOf\" in schema) {\n return schema.anyOf\n .map((s) => this._schemaToInstruction(s, indent))\n .join(`\\n${\" \".repeat(indent - 2)}`);\n }\n\n throw new Error(\"unsupported schema type\");\n }\n\n static fromZodSchema<T extends InteropZodType>(schema: T) {\n return new this<T>(schema);\n }\n\n static fromNamesAndDescriptions<S extends { [key: string]: string }>(\n schemas: S\n ) {\n const zodSchema = z.object(\n Object.fromEntries(\n Object.entries(schemas).map(\n ([name, description]) =>\n [name, z.string().describe(description)] as const\n )\n )\n );\n\n return new this<typeof zodSchema>(zodSchema);\n }\n}\n\nexport interface AsymmetricStructuredOutputParserFields<\n T extends InteropZodType,\n> {\n inputSchema: T;\n}\n\n/**\n * A type of `StructuredOutputParser` that handles asymmetric input and\n * output schemas.\n */\nexport abstract class AsymmetricStructuredOutputParser<\n T extends InteropZodType,\n Y = unknown,\n> extends BaseOutputParser<Y> {\n private structuredInputParser: JsonMarkdownStructuredOutputParser<T>;\n\n constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>) {\n super(...arguments);\n this.structuredInputParser = new JsonMarkdownStructuredOutputParser(\n inputSchema\n );\n }\n\n /**\n * Processes the parsed input into the desired output format. Must be\n * implemented by subclasses.\n * @param input The parsed input\n * @returns The processed output.\n */\n abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;\n\n async parse(text: string): Promise<Y> {\n let parsedInput;\n try {\n parsedInput = await this.structuredInputParser.parse(text);\n } catch (e) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e}`,\n text\n );\n }\n\n return this.outputProcessor(parsedInput);\n }\n\n getFormatInstructions(): string {\n return this.structuredInputParser.getFormatInstructions();\n }\n}\n"],"mappings":";;;;;;AA8BA,IAAa,yBAAb,cAEU,iBAA2C;CACnD,OAAO,UAAU;AACf,SAAO;CACR;CAED,eAAe;EAAC;EAAa;EAAkB;CAAa;CAE5D,SAAS;AAC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IAAI,EAAE,OAAO,aAAa,GAAG;AACvE,UAAO,GAAG,OAAO,cAAc,YAAY;EAC5C;AAED,MAAI,WAAW,OACb,QAAO,OAAO,MACX,IAAI,CAAC,MAAM,KAAK,qBAAqB,GAAG,OAAO,CAAC,CAChD,KAAK,CAAC,EAAE,EAAE,IAAI,OAAO,SAAS,EAAE,EAAE,CAAC;AAGxC,QAAM,IAAI,MAAM;CACjB;CAED,OAAO,cAAwCP,QAAW;AACxD,SAAO,IAAI,KAAQ;CACpB;CAED,OAAO,yBACLC,SACA;EACA,MAAM,YAAY,EAAE,OAClB,OAAO,YACL,OAAO,QAAQ,QAAQ,CAAC,IACtB,CAAC,CAAC,MAAM,YAAY,KAClB,CAAC,MAAM,EAAE,QAAQ,CAAC,SAAS,YAAY,AAAC,EAC3C,CACF,CACF;AAED,SAAO,IAAI,KAAuB;CACnC;AACF;;;;;AAYD,IAAsB,mCAAtB,cAGU,iBAAoB;CAC5B,AAAQ;CAER,YAAY,EAAE,aAAwD,EAAE;EACtE,MAAM,GAAG,UAAU;EACnB,KAAK,wBAAwB,IAAI,mCAC/B;CAEH;CAUD,MAAM,MAAMC,MAA0B;EACpC,IAAI;AACJ,MAAI;GACF,cAAc,MAAM,KAAK,sBAAsB,MAAM,KAAK;EAC3D,SAAQ,GAAG;AACV,SAAM,IAAI,sBACR,CAAC,wBAAwB,EAAE,KAAK,UAAU,EAAE,GAAG,EAC/C;EAEH;AAED,SAAO,KAAK,gBAAgB,YAAY;CACzC;CAED,wBAAgC;AAC9B,SAAO,KAAK,sBAAsB,uBAAuB;CAC1D;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["x: any","fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }","documents: DocumentInterface[]","query: string","callbacks?: Callbacks"],"sources":["../../../src/retrievers/document_compressors/index.ts"],"sourcesContent":["import type { DocumentInterface } from \"@langchain/core/documents\";\nimport { BaseDocumentTransformer } from \"@langchain/core/documents\";\nimport { Callbacks } from \"@langchain/core/callbacks/manager\";\n\n/**\n * Base Document Compression class. All compressors should extend this class.\n */\nexport abstract class BaseDocumentCompressor {\n /**\n * Abstract method that must be implemented by any class that extends\n * `BaseDocumentCompressor`. This method takes an array of `Document`\n * objects and a query string as parameters and returns a Promise that\n * resolves with an array of compressed `Document` objects.\n * @param documents An array of `Document` objects to be compressed.\n * @param query A query string.\n * @returns A Promise that resolves with an array of compressed `Document` objects.\n */\n abstract compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n static isBaseDocumentCompressor(x: any): x is BaseDocumentCompressor {\n return x?.compressDocuments !== undefined;\n }\n}\n\n/**\n * Document compressor that uses a pipeline of Transformers.\n * @example\n * ```typescript\n * const compressorPipeline = new DocumentCompressorPipeline({\n * transformers: [\n * new RecursiveCharacterTextSplitter({\n * chunkSize: 200,\n * chunkOverlap: 0,\n * }),\n * new EmbeddingsFilter({\n * embeddings: new OpenAIEmbeddings(),\n * similarityThreshold: 0.8,\n * k: 5,\n * }),\n * ],\n * });\n * const retriever = new ContextualCompressionRetriever({\n * baseCompressor: compressorPipeline,\n * baseRetriever: new TavilySearchAPIRetriever({\n * includeRawContent: true,\n * }),\n * });\n * const retrievedDocs = await retriever.invoke(\n * \"What did the speaker say about Justice Breyer in the 2022 State of the Union?\",\n * );\n * console.log({ retrievedDocs });\n * ```\n */\nexport class DocumentCompressorPipeline extends BaseDocumentCompressor {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n\n constructor(fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }) {\n super();\n this.transformers = fields.transformers;\n }\n\n async compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]> {\n let transformedDocuments = documents;\n for (const transformer of this.transformers) {\n if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {\n transformedDocuments = await transformer.compressDocuments(\n transformedDocuments,\n query,\n callbacks\n );\n } else {\n transformedDocuments = await transformer.transformDocuments(\n transformedDocuments\n );\n }\n }\n return transformedDocuments;\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,IAAsB,yBAAtB,MAA6C;CAiB3C,OAAO,yBAAyBA,GAAqC;AACnE,SAAO,GAAG,sBAAsB;CACjC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BD,IAAa,6BAAb,cAAgD,uBAAuB;CACrE;CAEA,YAAYC,QAET;EACD,OAAO;EACP,KAAK,eAAe,OAAO;CAC5B;CAED,MAAM,kBACJC,WACAC,OACAC,WAC8B;EAC9B,IAAI,uBAAuB;AAC3B,OAAK,MAAM,eAAe,KAAK,aAC7B,KAAI,uBAAuB,yBAAyB,YAAY,EAC9D,uBAAuB,MAAM,YAAY,kBACvC,sBACA,OACA,UACD;OAED,uBAAuB,MAAM,YAAY,mBACvC,qBACD;AAGL,SAAO;CACR;AACF"}
1
+ {"version":3,"file":"index.cjs","names":["x: any","fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }","documents: DocumentInterface[]","query: string","callbacks?: Callbacks"],"sources":["../../../src/retrievers/document_compressors/index.ts"],"sourcesContent":["import type { DocumentInterface } from \"@langchain/core/documents\";\nimport { BaseDocumentTransformer } from \"@langchain/core/documents\";\nimport { Callbacks } from \"@langchain/core/callbacks/manager\";\n\n/**\n * Base Document Compression class. All compressors should extend this class.\n */\nexport abstract class BaseDocumentCompressor {\n /**\n * Abstract method that must be implemented by any class that extends\n * `BaseDocumentCompressor`. This method takes an array of `Document`\n * objects and a query string as parameters and returns a Promise that\n * resolves with an array of compressed `Document` objects.\n * @param documents An array of `Document` objects to be compressed.\n * @param query A query string.\n * @returns A Promise that resolves with an array of compressed `Document` objects.\n */\n abstract compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n static isBaseDocumentCompressor(x: any): x is BaseDocumentCompressor {\n return x?.compressDocuments !== undefined;\n }\n}\n\n/**\n * Document compressor that uses a pipeline of Transformers.\n * @example\n * ```typescript\n * const compressorPipeline = new DocumentCompressorPipeline({\n * transformers: [\n * new RecursiveCharacterTextSplitter({\n * chunkSize: 200,\n * chunkOverlap: 0,\n * }),\n * new EmbeddingsFilter({\n * embeddings: new OpenAIEmbeddings(),\n * similarityThreshold: 0.8,\n * k: 5,\n * }),\n * ],\n * });\n * const retriever = new ContextualCompressionRetriever({\n * baseCompressor: compressorPipeline,\n * baseRetriever: new TavilySearchAPIRetriever({\n * includeRawContent: true,\n * }),\n * });\n * const retrievedDocs = await retriever.invoke(\n * \"What did the speaker say about Justice Breyer in the 2022 State of the Union?\",\n * );\n * console.log({ retrievedDocs });\n * ```\n */\nexport class DocumentCompressorPipeline extends BaseDocumentCompressor {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n\n constructor(fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }) {\n super();\n this.transformers = fields.transformers;\n }\n\n async compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]> {\n let transformedDocuments = documents;\n for (const transformer of this.transformers) {\n if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {\n transformedDocuments = await transformer.compressDocuments(\n transformedDocuments,\n query,\n callbacks\n );\n } else {\n transformedDocuments =\n await transformer.transformDocuments(transformedDocuments);\n }\n }\n return transformedDocuments;\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,IAAsB,yBAAtB,MAA6C;CAiB3C,OAAO,yBAAyBA,GAAqC;AACnE,SAAO,GAAG,sBAAsB;CACjC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BD,IAAa,6BAAb,cAAgD,uBAAuB;CACrE;CAEA,YAAYC,QAET;EACD,OAAO;EACP,KAAK,eAAe,OAAO;CAC5B;CAED,MAAM,kBACJC,WACAC,OACAC,WAC8B;EAC9B,IAAI,uBAAuB;AAC3B,OAAK,MAAM,eAAe,KAAK,aAC7B,KAAI,uBAAuB,yBAAyB,YAAY,EAC9D,uBAAuB,MAAM,YAAY,kBACvC,sBACA,OACA,UACD;OAED,uBACE,MAAM,YAAY,mBAAmB,qBAAqB;AAGhE,SAAO;CACR;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["x: any","fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }","documents: DocumentInterface[]","query: string","callbacks?: Callbacks"],"sources":["../../../src/retrievers/document_compressors/index.ts"],"sourcesContent":["import type { DocumentInterface } from \"@langchain/core/documents\";\nimport { BaseDocumentTransformer } from \"@langchain/core/documents\";\nimport { Callbacks } from \"@langchain/core/callbacks/manager\";\n\n/**\n * Base Document Compression class. All compressors should extend this class.\n */\nexport abstract class BaseDocumentCompressor {\n /**\n * Abstract method that must be implemented by any class that extends\n * `BaseDocumentCompressor`. This method takes an array of `Document`\n * objects and a query string as parameters and returns a Promise that\n * resolves with an array of compressed `Document` objects.\n * @param documents An array of `Document` objects to be compressed.\n * @param query A query string.\n * @returns A Promise that resolves with an array of compressed `Document` objects.\n */\n abstract compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n static isBaseDocumentCompressor(x: any): x is BaseDocumentCompressor {\n return x?.compressDocuments !== undefined;\n }\n}\n\n/**\n * Document compressor that uses a pipeline of Transformers.\n * @example\n * ```typescript\n * const compressorPipeline = new DocumentCompressorPipeline({\n * transformers: [\n * new RecursiveCharacterTextSplitter({\n * chunkSize: 200,\n * chunkOverlap: 0,\n * }),\n * new EmbeddingsFilter({\n * embeddings: new OpenAIEmbeddings(),\n * similarityThreshold: 0.8,\n * k: 5,\n * }),\n * ],\n * });\n * const retriever = new ContextualCompressionRetriever({\n * baseCompressor: compressorPipeline,\n * baseRetriever: new TavilySearchAPIRetriever({\n * includeRawContent: true,\n * }),\n * });\n * const retrievedDocs = await retriever.invoke(\n * \"What did the speaker say about Justice Breyer in the 2022 State of the Union?\",\n * );\n * console.log({ retrievedDocs });\n * ```\n */\nexport class DocumentCompressorPipeline extends BaseDocumentCompressor {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n\n constructor(fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }) {\n super();\n this.transformers = fields.transformers;\n }\n\n async compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]> {\n let transformedDocuments = documents;\n for (const transformer of this.transformers) {\n if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {\n transformedDocuments = await transformer.compressDocuments(\n transformedDocuments,\n query,\n callbacks\n );\n } else {\n transformedDocuments = await transformer.transformDocuments(\n transformedDocuments\n );\n }\n }\n return transformedDocuments;\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,IAAsB,yBAAtB,MAA6C;CAiB3C,OAAO,yBAAyBA,GAAqC;AACnE,SAAO,GAAG,sBAAsB;CACjC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BD,IAAa,6BAAb,cAAgD,uBAAuB;CACrE;CAEA,YAAYC,QAET;EACD,OAAO;EACP,KAAK,eAAe,OAAO;CAC5B;CAED,MAAM,kBACJC,WACAC,OACAC,WAC8B;EAC9B,IAAI,uBAAuB;AAC3B,OAAK,MAAM,eAAe,KAAK,aAC7B,KAAI,uBAAuB,yBAAyB,YAAY,EAC9D,uBAAuB,MAAM,YAAY,kBACvC,sBACA,OACA,UACD;OAED,uBAAuB,MAAM,YAAY,mBACvC,qBACD;AAGL,SAAO;CACR;AACF"}
1
+ {"version":3,"file":"index.js","names":["x: any","fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }","documents: DocumentInterface[]","query: string","callbacks?: Callbacks"],"sources":["../../../src/retrievers/document_compressors/index.ts"],"sourcesContent":["import type { DocumentInterface } from \"@langchain/core/documents\";\nimport { BaseDocumentTransformer } from \"@langchain/core/documents\";\nimport { Callbacks } from \"@langchain/core/callbacks/manager\";\n\n/**\n * Base Document Compression class. All compressors should extend this class.\n */\nexport abstract class BaseDocumentCompressor {\n /**\n * Abstract method that must be implemented by any class that extends\n * `BaseDocumentCompressor`. This method takes an array of `Document`\n * objects and a query string as parameters and returns a Promise that\n * resolves with an array of compressed `Document` objects.\n * @param documents An array of `Document` objects to be compressed.\n * @param query A query string.\n * @returns A Promise that resolves with an array of compressed `Document` objects.\n */\n abstract compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n static isBaseDocumentCompressor(x: any): x is BaseDocumentCompressor {\n return x?.compressDocuments !== undefined;\n }\n}\n\n/**\n * Document compressor that uses a pipeline of Transformers.\n * @example\n * ```typescript\n * const compressorPipeline = new DocumentCompressorPipeline({\n * transformers: [\n * new RecursiveCharacterTextSplitter({\n * chunkSize: 200,\n * chunkOverlap: 0,\n * }),\n * new EmbeddingsFilter({\n * embeddings: new OpenAIEmbeddings(),\n * similarityThreshold: 0.8,\n * k: 5,\n * }),\n * ],\n * });\n * const retriever = new ContextualCompressionRetriever({\n * baseCompressor: compressorPipeline,\n * baseRetriever: new TavilySearchAPIRetriever({\n * includeRawContent: true,\n * }),\n * });\n * const retrievedDocs = await retriever.invoke(\n * \"What did the speaker say about Justice Breyer in the 2022 State of the Union?\",\n * );\n * console.log({ retrievedDocs });\n * ```\n */\nexport class DocumentCompressorPipeline extends BaseDocumentCompressor {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n\n constructor(fields: {\n transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];\n }) {\n super();\n this.transformers = fields.transformers;\n }\n\n async compressDocuments(\n documents: DocumentInterface[],\n query: string,\n callbacks?: Callbacks\n ): Promise<DocumentInterface[]> {\n let transformedDocuments = documents;\n for (const transformer of this.transformers) {\n if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {\n transformedDocuments = await transformer.compressDocuments(\n transformedDocuments,\n query,\n callbacks\n );\n } else {\n transformedDocuments =\n await transformer.transformDocuments(transformedDocuments);\n }\n }\n return transformedDocuments;\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,IAAsB,yBAAtB,MAA6C;CAiB3C,OAAO,yBAAyBA,GAAqC;AACnE,SAAO,GAAG,sBAAsB;CACjC;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BD,IAAa,6BAAb,cAAgD,uBAAuB;CACrE;CAEA,YAAYC,QAET;EACD,OAAO;EACP,KAAK,eAAe,OAAO;CAC5B;CAED,MAAM,kBACJC,WACAC,OACAC,WAC8B;EAC9B,IAAI,uBAAuB;AAC3B,OAAK,MAAM,eAAe,KAAK,aAC7B,KAAI,uBAAuB,yBAAyB,YAAY,EAC9D,uBAAuB,MAAM,YAAY,kBACvC,sBACA,OACA,UACD;OAED,uBACE,MAAM,YAAY,mBAAmB,qBAAqB;AAGhE,SAAO;CACR;AACF"}
@@ -1,7 +1,7 @@
1
1
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
2
  const __langchain_core_prompts = require_rolldown_runtime.__toESM(require("@langchain/core/prompts"));
3
- const __langchain_core_vectorstores = require_rolldown_runtime.__toESM(require("@langchain/core/vectorstores"));
4
3
  const __langchain_core_prompt_values = require_rolldown_runtime.__toESM(require("@langchain/core/prompt_values"));
4
+ const __langchain_core_vectorstores = require_rolldown_runtime.__toESM(require("@langchain/core/vectorstores"));
5
5
 
6
6
  //#region src/retrievers/hyde.ts
7
7
  var hyde_exports = {};
@@ -1 +1 @@
1
- {"version":3,"file":"hyde.cjs","names":["VectorStoreRetriever","fields: HydeRetrieverOptions<V>","query: string","runManager?: CallbackManagerForRetrieverRun","value: BasePromptValue","StringPromptValue","key: PromptKey","template: string","PromptTemplate"],"sources":["../../src/retrievers/hyde.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { PromptTemplate, BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n StringPromptValue,\n BasePromptValue,\n} from \"@langchain/core/prompt_values\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey =\n | \"websearch\"\n | \"scifact\"\n | \"arguana\"\n | \"trec-covid\"\n | \"fiqa\"\n | \"dbpedia-entity\"\n | \"trec-news\"\n | \"mr-tydi\";\n\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> =\n VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n };\n\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport class HydeRetriever<\n V extends VectorStore = VectorStore\n> extends VectorStoreRetriever<V> {\n static lc_name() {\n return \"HydeRetriever\";\n }\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"retrievers\", \"hyde\"];\n }\n\n llm: BaseLanguageModelInterface;\n\n promptTemplate?: BasePromptTemplate;\n\n constructor(fields: HydeRetrieverOptions<V>) {\n super(fields);\n this.llm = fields.llm;\n this.promptTemplate =\n typeof fields.promptTemplate === \"string\"\n ? getPromptTemplateFromKey(fields.promptTemplate)\n : fields.promptTemplate;\n if (this.promptTemplate) {\n const { inputVariables } = this.promptTemplate;\n if (inputVariables.length !== 1 && inputVariables[0] !== \"question\") {\n throw new Error(\n `Prompt template must accept a single input variable 'question'. Invalid input variables for prompt template: ${inputVariables}`\n );\n }\n }\n }\n\n async _getRelevantDocuments(\n query: string,\n runManager?: CallbackManagerForRetrieverRun\n ): Promise<Document[]> {\n let value: BasePromptValue = new StringPromptValue(query);\n\n // Use a custom template if provided\n if (this.promptTemplate) {\n value = await this.promptTemplate.formatPromptValue({ question: query });\n }\n\n // Get a hypothetical answer from the LLM\n const res = await this.llm.generatePrompt([value]);\n const answer = res.generations[0][0].text;\n\n // Retrieve relevant documents based on the hypothetical answer\n const results = await this.vectorStore.similaritySearch(\n answer,\n this.k,\n this.filter,\n runManager?.getChild(\"vectorstore\")\n );\n\n return results;\n }\n}\n\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate {\n let template: string;\n\n switch (key) {\n case \"websearch\":\n template = `Please write a passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"scifact\":\n template = `Please write a scientific paper passage to support/refute the claim\nClaim: {question}\nPassage:`;\n break;\n case \"arguana\":\n template = `Please write a counter argument for the passage\nPassage: {question}\nCounter Argument:`;\n break;\n case \"trec-covid\":\n template = `Please write a scientific paper passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"fiqa\":\n template = `Please write a financial article passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"dbpedia-entity\":\n template = `Please write a passage to answer the question.\nQuestion: {question}\nPassage:`;\n break;\n case \"trec-news\":\n template = `Please write a news passage about the topic.\nTopic: {question}\nPassage:`;\n break;\n case \"mr-tydi\":\n template = `Please write a passage in Swahili/Korean/Japanese/Bengali to answer the question in detail.\nQuestion: {question}\nPassage:`;\n break;\n default:\n throw new Error(`Invalid prompt key: ${key}`);\n }\n\n return PromptTemplate.fromTemplate(template);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAa,gBAAb,cAEUA,mDAAwB;CAChC,OAAO,UAAU;AACf,SAAO;CACR;CAED,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAc;EAAO;CAC3C;CAED;CAEA;CAEA,YAAYC,QAAiC;EAC3C,MAAM,OAAO;EACb,KAAK,MAAM,OAAO;EAClB,KAAK,iBACH,OAAO,OAAO,mBAAmB,WAC7B,yBAAyB,OAAO,eAAe,GAC/C,OAAO;AACb,MAAI,KAAK,gBAAgB;GACvB,MAAM,EAAE,gBAAgB,GAAG,KAAK;AAChC,OAAI,eAAe,WAAW,KAAK,eAAe,OAAO,WACvD,OAAM,IAAI,MACR,CAAC,6GAA6G,EAAE,gBAAgB;EAGrI;CACF;CAED,MAAM,sBACJC,OACAC,YACqB;EACrB,IAAIC,QAAyB,IAAIC,iDAAkB;AAGnD,MAAI,KAAK,gBACP,QAAQ,MAAM,KAAK,eAAe,kBAAkB,EAAE,UAAU,MAAO,EAAC;EAI1E,MAAM,MAAM,MAAM,KAAK,IAAI,eAAe,CAAC,KAAM,EAAC;EAClD,MAAM,SAAS,IAAI,YAAY,GAAG,GAAG;EAGrC,MAAM,UAAU,MAAM,KAAK,YAAY,iBACrC,QACA,KAAK,GACL,KAAK,QACL,YAAY,SAAS,cAAc,CACpC;AAED,SAAO;CACR;AACF;;;;AAKD,SAAgB,yBAAyBC,KAAoC;CAC3E,IAAIC;AAEJ,SAAQ,KAAR;EACE,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;iBAED,CAAC;AACZ;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,QACE,OAAM,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK;CAC/C;AAED,QAAOC,wCAAe,aAAa,SAAS;AAC7C"}
1
+ {"version":3,"file":"hyde.cjs","names":["VectorStoreRetriever","fields: HydeRetrieverOptions<V>","query: string","runManager?: CallbackManagerForRetrieverRun","value: BasePromptValue","StringPromptValue","key: PromptKey","template: string","PromptTemplate"],"sources":["../../src/retrievers/hyde.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { PromptTemplate, BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n StringPromptValue,\n BasePromptValue,\n} from \"@langchain/core/prompt_values\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey =\n | \"websearch\"\n | \"scifact\"\n | \"arguana\"\n | \"trec-covid\"\n | \"fiqa\"\n | \"dbpedia-entity\"\n | \"trec-news\"\n | \"mr-tydi\";\n\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> =\n VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n };\n\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport class HydeRetriever<\n V extends VectorStore = VectorStore,\n> extends VectorStoreRetriever<V> {\n static lc_name() {\n return \"HydeRetriever\";\n }\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"retrievers\", \"hyde\"];\n }\n\n llm: BaseLanguageModelInterface;\n\n promptTemplate?: BasePromptTemplate;\n\n constructor(fields: HydeRetrieverOptions<V>) {\n super(fields);\n this.llm = fields.llm;\n this.promptTemplate =\n typeof fields.promptTemplate === \"string\"\n ? getPromptTemplateFromKey(fields.promptTemplate)\n : fields.promptTemplate;\n if (this.promptTemplate) {\n const { inputVariables } = this.promptTemplate;\n if (inputVariables.length !== 1 && inputVariables[0] !== \"question\") {\n throw new Error(\n `Prompt template must accept a single input variable 'question'. Invalid input variables for prompt template: ${inputVariables}`\n );\n }\n }\n }\n\n async _getRelevantDocuments(\n query: string,\n runManager?: CallbackManagerForRetrieverRun\n ): Promise<Document[]> {\n let value: BasePromptValue = new StringPromptValue(query);\n\n // Use a custom template if provided\n if (this.promptTemplate) {\n value = await this.promptTemplate.formatPromptValue({ question: query });\n }\n\n // Get a hypothetical answer from the LLM\n const res = await this.llm.generatePrompt([value]);\n const answer = res.generations[0][0].text;\n\n // Retrieve relevant documents based on the hypothetical answer\n const results = await this.vectorStore.similaritySearch(\n answer,\n this.k,\n this.filter,\n runManager?.getChild(\"vectorstore\")\n );\n\n return results;\n }\n}\n\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate {\n let template: string;\n\n switch (key) {\n case \"websearch\":\n template = `Please write a passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"scifact\":\n template = `Please write a scientific paper passage to support/refute the claim\nClaim: {question}\nPassage:`;\n break;\n case \"arguana\":\n template = `Please write a counter argument for the passage\nPassage: {question}\nCounter Argument:`;\n break;\n case \"trec-covid\":\n template = `Please write a scientific paper passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"fiqa\":\n template = `Please write a financial article passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"dbpedia-entity\":\n template = `Please write a passage to answer the question.\nQuestion: {question}\nPassage:`;\n break;\n case \"trec-news\":\n template = `Please write a news passage about the topic.\nTopic: {question}\nPassage:`;\n break;\n case \"mr-tydi\":\n template = `Please write a passage in Swahili/Korean/Japanese/Bengali to answer the question in detail.\nQuestion: {question}\nPassage:`;\n break;\n default:\n throw new Error(`Invalid prompt key: ${key}`);\n }\n\n return PromptTemplate.fromTemplate(template);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAa,gBAAb,cAEUA,mDAAwB;CAChC,OAAO,UAAU;AACf,SAAO;CACR;CAED,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAc;EAAO;CAC3C;CAED;CAEA;CAEA,YAAYC,QAAiC;EAC3C,MAAM,OAAO;EACb,KAAK,MAAM,OAAO;EAClB,KAAK,iBACH,OAAO,OAAO,mBAAmB,WAC7B,yBAAyB,OAAO,eAAe,GAC/C,OAAO;AACb,MAAI,KAAK,gBAAgB;GACvB,MAAM,EAAE,gBAAgB,GAAG,KAAK;AAChC,OAAI,eAAe,WAAW,KAAK,eAAe,OAAO,WACvD,OAAM,IAAI,MACR,CAAC,6GAA6G,EAAE,gBAAgB;EAGrI;CACF;CAED,MAAM,sBACJC,OACAC,YACqB;EACrB,IAAIC,QAAyB,IAAIC,iDAAkB;AAGnD,MAAI,KAAK,gBACP,QAAQ,MAAM,KAAK,eAAe,kBAAkB,EAAE,UAAU,MAAO,EAAC;EAI1E,MAAM,MAAM,MAAM,KAAK,IAAI,eAAe,CAAC,KAAM,EAAC;EAClD,MAAM,SAAS,IAAI,YAAY,GAAG,GAAG;EAGrC,MAAM,UAAU,MAAM,KAAK,YAAY,iBACrC,QACA,KAAK,GACL,KAAK,QACL,YAAY,SAAS,cAAc,CACpC;AAED,SAAO;CACR;AACF;;;;AAKD,SAAgB,yBAAyBC,KAAoC;CAC3E,IAAIC;AAEJ,SAAQ,KAAR;EACE,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;iBAED,CAAC;AACZ;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,QACE,OAAM,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK;CAC/C;AAED,QAAOC,wCAAe,aAAa,SAAS;AAC7C"}
@@ -1 +1 @@
1
- {"version":3,"file":"hyde.d.cts","names":["BaseLanguageModelInterface","Document","BasePromptTemplate","VectorStore","VectorStoreRetriever","VectorStoreRetrieverInput","CallbackManagerForRetrieverRun","PromptKey","HydeRetrieverOptions","V","HydeRetriever","Promise","getPromptTemplateFromKey"],"sources":["../../src/retrievers/hyde.d.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport { VectorStore, VectorStoreRetriever, VectorStoreRetrieverInput } from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey = \"websearch\" | \"scifact\" | \"arguana\" | \"trec-covid\" | \"fiqa\" | \"dbpedia-entity\" | \"trec-news\" | \"mr-tydi\";\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> = VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n};\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport declare class HydeRetriever<V extends VectorStore = VectorStore> extends VectorStoreRetriever<V> {\n static lc_name(): string;\n get lc_namespace(): string[];\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate;\n constructor(fields: HydeRetrieverOptions<V>);\n _getRelevantDocuments(query: string, runManager?: CallbackManagerForRetrieverRun): Promise<Document[]>;\n}\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport declare function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate;\n//# sourceMappingURL=hyde.d.ts.map"],"mappings":";;;;;;;;;;AAQA;AAMYQ,KANAD,SAAAA,GAMAC,WAAoBC,GAAAA,SAAA,GAAA,SAAA,GAAA,YAAA,GAAA,MAAA,GAAA,gBAAA,GAAA,WAAA,GAAA,SAAA;;;;;;AAEUF,KAF9BC,oBAE8BD,CAAAA,UAFCJ,WAEDI,CAAAA,GAFgBF,yBAEhBE,CAF0CE,CAE1CF,CAAAA,GAAAA;EAAS,GAAA,EAD1CP,0BAC0C;EA2B9BU,cAAAA,CAAAA,EA3BAR,kBA2Ba,GA3BQK,SA2BR;CAAWJ;;;;;;;;;;;AAAuD;AAWpG;;;;;;;;;;;;;;cAXqBO,wBAAwBP,cAAcA,qBAAqBC,qBAAqBK;;;OAG5FT;mBACYE;sBACGM,qBAAqBC;oDACSH,iCAAiCK,QAAQV;;;;;iBAKvEW,wBAAAA,MAA8BL,YAAYL"}
1
+ {"version":3,"file":"hyde.d.cts","names":["BaseLanguageModelInterface","Document","BasePromptTemplate","VectorStore","VectorStoreRetriever","VectorStoreRetrieverInput","CallbackManagerForRetrieverRun","PromptKey","HydeRetrieverOptions","V","HydeRetriever","Promise","getPromptTemplateFromKey"],"sources":["../../src/retrievers/hyde.d.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport { VectorStore, VectorStoreRetriever, VectorStoreRetrieverInput } from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey = \"websearch\" | \"scifact\" | \"arguana\" | \"trec-covid\" | \"fiqa\" | \"dbpedia-entity\" | \"trec-news\" | \"mr-tydi\";\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> = VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n};\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport declare class HydeRetriever<V extends VectorStore = VectorStore> extends VectorStoreRetriever<V> {\n static lc_name(): string;\n get lc_namespace(): string[];\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate;\n constructor(fields: HydeRetrieverOptions<V>);\n _getRelevantDocuments(query: string, runManager?: CallbackManagerForRetrieverRun): Promise<Document[]>;\n}\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport declare function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate;\n//# sourceMappingURL=hyde.d.ts.map"],"mappings":";;;;;;;;;;AAQA;AAMYQ,KANAD,SAAAA,GAMAC,WAAoBC,GAAA,SAAA,GAAA,SAAA,GAAA,YAAA,GAAA,MAAA,GAAA,gBAAA,GAAA,WAAA,GAAA,SAAA;;;;;;AAEUF,KAF9BC,oBAE8BD,CAAAA,UAFCJ,WAEDI,CAAAA,GAFgBF,yBAEhBE,CAF0CE,CAE1CF,CAAAA,GAAAA;EAAS,GAAA,EAD1CP,0BAC0C;EA2B9BU,cAAAA,CAAAA,EA3BAR,kBA2Ba,GA3BQK,SA2BR;CAAWJ;;;;;;;;;;;AAAuD;AAWpG;;;;;;;;;;;;;;cAXqBO,wBAAwBP,cAAcA,qBAAqBC,qBAAqBK;;;OAG5FT;mBACYE;sBACGM,qBAAqBC;oDACSH,iCAAiCK,QAAQV;;;;;iBAKvEW,wBAAAA,MAA8BL,YAAYL"}
@@ -1,7 +1,7 @@
1
1
  import { __export } from "../_virtual/rolldown_runtime.js";
2
2
  import { PromptTemplate } from "@langchain/core/prompts";
3
- import { VectorStoreRetriever } from "@langchain/core/vectorstores";
4
3
  import { StringPromptValue } from "@langchain/core/prompt_values";
4
+ import { VectorStoreRetriever } from "@langchain/core/vectorstores";
5
5
 
6
6
  //#region src/retrievers/hyde.ts
7
7
  var hyde_exports = {};
@@ -1 +1 @@
1
- {"version":3,"file":"hyde.js","names":["fields: HydeRetrieverOptions<V>","query: string","runManager?: CallbackManagerForRetrieverRun","value: BasePromptValue","key: PromptKey","template: string"],"sources":["../../src/retrievers/hyde.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { PromptTemplate, BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n StringPromptValue,\n BasePromptValue,\n} from \"@langchain/core/prompt_values\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey =\n | \"websearch\"\n | \"scifact\"\n | \"arguana\"\n | \"trec-covid\"\n | \"fiqa\"\n | \"dbpedia-entity\"\n | \"trec-news\"\n | \"mr-tydi\";\n\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> =\n VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n };\n\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport class HydeRetriever<\n V extends VectorStore = VectorStore\n> extends VectorStoreRetriever<V> {\n static lc_name() {\n return \"HydeRetriever\";\n }\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"retrievers\", \"hyde\"];\n }\n\n llm: BaseLanguageModelInterface;\n\n promptTemplate?: BasePromptTemplate;\n\n constructor(fields: HydeRetrieverOptions<V>) {\n super(fields);\n this.llm = fields.llm;\n this.promptTemplate =\n typeof fields.promptTemplate === \"string\"\n ? getPromptTemplateFromKey(fields.promptTemplate)\n : fields.promptTemplate;\n if (this.promptTemplate) {\n const { inputVariables } = this.promptTemplate;\n if (inputVariables.length !== 1 && inputVariables[0] !== \"question\") {\n throw new Error(\n `Prompt template must accept a single input variable 'question'. Invalid input variables for prompt template: ${inputVariables}`\n );\n }\n }\n }\n\n async _getRelevantDocuments(\n query: string,\n runManager?: CallbackManagerForRetrieverRun\n ): Promise<Document[]> {\n let value: BasePromptValue = new StringPromptValue(query);\n\n // Use a custom template if provided\n if (this.promptTemplate) {\n value = await this.promptTemplate.formatPromptValue({ question: query });\n }\n\n // Get a hypothetical answer from the LLM\n const res = await this.llm.generatePrompt([value]);\n const answer = res.generations[0][0].text;\n\n // Retrieve relevant documents based on the hypothetical answer\n const results = await this.vectorStore.similaritySearch(\n answer,\n this.k,\n this.filter,\n runManager?.getChild(\"vectorstore\")\n );\n\n return results;\n }\n}\n\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate {\n let template: string;\n\n switch (key) {\n case \"websearch\":\n template = `Please write a passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"scifact\":\n template = `Please write a scientific paper passage to support/refute the claim\nClaim: {question}\nPassage:`;\n break;\n case \"arguana\":\n template = `Please write a counter argument for the passage\nPassage: {question}\nCounter Argument:`;\n break;\n case \"trec-covid\":\n template = `Please write a scientific paper passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"fiqa\":\n template = `Please write a financial article passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"dbpedia-entity\":\n template = `Please write a passage to answer the question.\nQuestion: {question}\nPassage:`;\n break;\n case \"trec-news\":\n template = `Please write a news passage about the topic.\nTopic: {question}\nPassage:`;\n break;\n case \"mr-tydi\":\n template = `Please write a passage in Swahili/Korean/Japanese/Bengali to answer the question in detail.\nQuestion: {question}\nPassage:`;\n break;\n default:\n throw new Error(`Invalid prompt key: ${key}`);\n }\n\n return PromptTemplate.fromTemplate(template);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAa,gBAAb,cAEU,qBAAwB;CAChC,OAAO,UAAU;AACf,SAAO;CACR;CAED,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAc;EAAO;CAC3C;CAED;CAEA;CAEA,YAAYA,QAAiC;EAC3C,MAAM,OAAO;EACb,KAAK,MAAM,OAAO;EAClB,KAAK,iBACH,OAAO,OAAO,mBAAmB,WAC7B,yBAAyB,OAAO,eAAe,GAC/C,OAAO;AACb,MAAI,KAAK,gBAAgB;GACvB,MAAM,EAAE,gBAAgB,GAAG,KAAK;AAChC,OAAI,eAAe,WAAW,KAAK,eAAe,OAAO,WACvD,OAAM,IAAI,MACR,CAAC,6GAA6G,EAAE,gBAAgB;EAGrI;CACF;CAED,MAAM,sBACJC,OACAC,YACqB;EACrB,IAAIC,QAAyB,IAAI,kBAAkB;AAGnD,MAAI,KAAK,gBACP,QAAQ,MAAM,KAAK,eAAe,kBAAkB,EAAE,UAAU,MAAO,EAAC;EAI1E,MAAM,MAAM,MAAM,KAAK,IAAI,eAAe,CAAC,KAAM,EAAC;EAClD,MAAM,SAAS,IAAI,YAAY,GAAG,GAAG;EAGrC,MAAM,UAAU,MAAM,KAAK,YAAY,iBACrC,QACA,KAAK,GACL,KAAK,QACL,YAAY,SAAS,cAAc,CACpC;AAED,SAAO;CACR;AACF;;;;AAKD,SAAgB,yBAAyBC,KAAoC;CAC3E,IAAIC;AAEJ,SAAQ,KAAR;EACE,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;iBAED,CAAC;AACZ;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,QACE,OAAM,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK;CAC/C;AAED,QAAO,eAAe,aAAa,SAAS;AAC7C"}
1
+ {"version":3,"file":"hyde.js","names":["fields: HydeRetrieverOptions<V>","query: string","runManager?: CallbackManagerForRetrieverRun","value: BasePromptValue","key: PromptKey","template: string"],"sources":["../../src/retrievers/hyde.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Document } from \"@langchain/core/documents\";\nimport { PromptTemplate, BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n StringPromptValue,\n BasePromptValue,\n} from \"@langchain/core/prompt_values\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\nimport { CallbackManagerForRetrieverRun } from \"@langchain/core/callbacks/manager\";\n\n/**\n * A string that corresponds to a specific prompt template.\n */\nexport type PromptKey =\n | \"websearch\"\n | \"scifact\"\n | \"arguana\"\n | \"trec-covid\"\n | \"fiqa\"\n | \"dbpedia-entity\"\n | \"trec-news\"\n | \"mr-tydi\";\n\n/**\n * Options for the HydeRetriever class, which includes a BaseLanguageModel\n * instance, a VectorStore instance, and an optional promptTemplate which\n * can either be a BasePromptTemplate instance or a PromptKey.\n */\nexport type HydeRetrieverOptions<V extends VectorStore> =\n VectorStoreRetrieverInput<V> & {\n llm: BaseLanguageModelInterface;\n promptTemplate?: BasePromptTemplate | PromptKey;\n };\n\n/**\n * A class for retrieving relevant documents based on a given query. It\n * extends the VectorStoreRetriever class and uses a BaseLanguageModel to\n * generate a hypothetical answer to the query, which is then used to\n * retrieve relevant documents.\n * @example\n * ```typescript\n * const retriever = new HydeRetriever({\n * vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),\n * llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n * k: 1,\n * });\n * await vectorStore.addDocuments(\n * [\n * \"My name is John.\",\n * \"My name is Bob.\",\n * \"My favourite food is pizza.\",\n * \"My favourite food is pasta.\",\n * ].map((pageContent) => new Document({ pageContent })),\n * );\n * const results = await retriever.invoke(\n * \"What is my favourite food?\",\n * );\n * ```\n */\nexport class HydeRetriever<\n V extends VectorStore = VectorStore,\n> extends VectorStoreRetriever<V> {\n static lc_name() {\n return \"HydeRetriever\";\n }\n\n get lc_namespace(): string[] {\n return [\"langchain\", \"retrievers\", \"hyde\"];\n }\n\n llm: BaseLanguageModelInterface;\n\n promptTemplate?: BasePromptTemplate;\n\n constructor(fields: HydeRetrieverOptions<V>) {\n super(fields);\n this.llm = fields.llm;\n this.promptTemplate =\n typeof fields.promptTemplate === \"string\"\n ? getPromptTemplateFromKey(fields.promptTemplate)\n : fields.promptTemplate;\n if (this.promptTemplate) {\n const { inputVariables } = this.promptTemplate;\n if (inputVariables.length !== 1 && inputVariables[0] !== \"question\") {\n throw new Error(\n `Prompt template must accept a single input variable 'question'. Invalid input variables for prompt template: ${inputVariables}`\n );\n }\n }\n }\n\n async _getRelevantDocuments(\n query: string,\n runManager?: CallbackManagerForRetrieverRun\n ): Promise<Document[]> {\n let value: BasePromptValue = new StringPromptValue(query);\n\n // Use a custom template if provided\n if (this.promptTemplate) {\n value = await this.promptTemplate.formatPromptValue({ question: query });\n }\n\n // Get a hypothetical answer from the LLM\n const res = await this.llm.generatePrompt([value]);\n const answer = res.generations[0][0].text;\n\n // Retrieve relevant documents based on the hypothetical answer\n const results = await this.vectorStore.similaritySearch(\n answer,\n this.k,\n this.filter,\n runManager?.getChild(\"vectorstore\")\n );\n\n return results;\n }\n}\n\n/**\n * Returns a BasePromptTemplate instance based on a given PromptKey.\n */\nexport function getPromptTemplateFromKey(key: PromptKey): BasePromptTemplate {\n let template: string;\n\n switch (key) {\n case \"websearch\":\n template = `Please write a passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"scifact\":\n template = `Please write a scientific paper passage to support/refute the claim\nClaim: {question}\nPassage:`;\n break;\n case \"arguana\":\n template = `Please write a counter argument for the passage\nPassage: {question}\nCounter Argument:`;\n break;\n case \"trec-covid\":\n template = `Please write a scientific paper passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"fiqa\":\n template = `Please write a financial article passage to answer the question\nQuestion: {question}\nPassage:`;\n break;\n case \"dbpedia-entity\":\n template = `Please write a passage to answer the question.\nQuestion: {question}\nPassage:`;\n break;\n case \"trec-news\":\n template = `Please write a news passage about the topic.\nTopic: {question}\nPassage:`;\n break;\n case \"mr-tydi\":\n template = `Please write a passage in Swahili/Korean/Japanese/Bengali to answer the question in detail.\nQuestion: {question}\nPassage:`;\n break;\n default:\n throw new Error(`Invalid prompt key: ${key}`);\n }\n\n return PromptTemplate.fromTemplate(template);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,IAAa,gBAAb,cAEU,qBAAwB;CAChC,OAAO,UAAU;AACf,SAAO;CACR;CAED,IAAI,eAAyB;AAC3B,SAAO;GAAC;GAAa;GAAc;EAAO;CAC3C;CAED;CAEA;CAEA,YAAYA,QAAiC;EAC3C,MAAM,OAAO;EACb,KAAK,MAAM,OAAO;EAClB,KAAK,iBACH,OAAO,OAAO,mBAAmB,WAC7B,yBAAyB,OAAO,eAAe,GAC/C,OAAO;AACb,MAAI,KAAK,gBAAgB;GACvB,MAAM,EAAE,gBAAgB,GAAG,KAAK;AAChC,OAAI,eAAe,WAAW,KAAK,eAAe,OAAO,WACvD,OAAM,IAAI,MACR,CAAC,6GAA6G,EAAE,gBAAgB;EAGrI;CACF;CAED,MAAM,sBACJC,OACAC,YACqB;EACrB,IAAIC,QAAyB,IAAI,kBAAkB;AAGnD,MAAI,KAAK,gBACP,QAAQ,MAAM,KAAK,eAAe,kBAAkB,EAAE,UAAU,MAAO,EAAC;EAI1E,MAAM,MAAM,MAAM,KAAK,IAAI,eAAe,CAAC,KAAM,EAAC;EAClD,MAAM,SAAS,IAAI,YAAY,GAAG,GAAG;EAGrC,MAAM,UAAU,MAAM,KAAK,YAAY,iBACrC,QACA,KAAK,GACL,KAAK,QACL,YAAY,SAAS,cAAc,CACpC;AAED,SAAO;CACR;AACF;;;;AAKD,SAAgB,yBAAyBC,KAAoC;CAC3E,IAAIC;AAEJ,SAAQ,KAAR;EACE,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;iBAED,CAAC;AACZ;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,KAAK;GACH,WAAW,CAAC;;QAEV,CAAC;AACH;EACF,QACE,OAAM,IAAI,MAAM,CAAC,oBAAoB,EAAE,KAAK;CAC/C;AAED,QAAO,eAAe,aAAa,SAAS;AAC7C"}
@@ -1,6 +1,6 @@
1
1
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
- const __langchain_core_vectorstores = require_rolldown_runtime.__toESM(require("@langchain/core/vectorstores"));
3
2
  const __langchain_core_utils_math = require_rolldown_runtime.__toESM(require("@langchain/core/utils/math"));
3
+ const __langchain_core_vectorstores = require_rolldown_runtime.__toESM(require("@langchain/core/vectorstores"));
4
4
 
5
5
  //#region src/retrievers/matryoshka_retriever.ts
6
6
  var matryoshka_retriever_exports = {};
@@ -1 +1 @@
1
- {"version":3,"file":"matryoshka_retriever.cjs","names":["VectorStoreRetriever","fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>","embeddedQuery: number[]","smallResults: DocumentInterface[]","largeEmbeddings: Array<number[]>","func: () => Array<number[]>","query: string","documents: DocumentInterface[]","options?: AddDocumentOptions","newDocuments: Array<DocumentInterface>"],"sources":["../../src/retrievers/matryoshka_retriever.ts"],"sourcesContent":["import { DocumentInterface } from \"@langchain/core/documents\";\nimport { Embeddings } from \"@langchain/core/embeddings\";\nimport {\n cosineSimilarity,\n euclideanDistance,\n innerProduct,\n} from \"@langchain/core/utils/math\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\n/**\n * Type for options when adding a document to the VectorStore.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AddDocumentOptions = Record<string, any>;\n\nexport interface MatryoshkaRetrieverFields {\n /**\n * The number of documents to retrieve from the small store.\n * @default 50\n */\n smallK?: number;\n /**\n * The number of documents to retrieve from the large store.\n * @default 8\n */\n largeK?: number;\n /**\n * The metadata key to store the larger embeddings.\n * @default \"lc_large_embedding\"\n */\n largeEmbeddingKey?: string;\n /**\n * The embedding model to use when generating the large\n * embeddings.\n */\n largeEmbeddingModel: Embeddings;\n /**\n * The type of search to perform using the large embeddings.\n * @default \"cosine\"\n */\n searchType?: \"cosine\" | \"innerProduct\" | \"euclidean\";\n}\n\n/**\n * A retriever that uses two sets of embeddings to perform adaptive retrieval. Based\n * off of the \"Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval\"\n * blog post {@link https://supabase.com/blog/matryoshka-embeddings}.\n *\n *\n * This class performs \"Adaptive Retrieval\" for searching text embeddings efficiently using the\n * Matryoshka Representation Learning (MRL) technique. It retrieves documents similar to a query\n * embedding in two steps:\n *\n * First-pass: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast,\n * but less accurate search.\n *\n * Second-pass: Re-ranks the top results from the first pass using the full, high-dimensional\n * embedding for higher accuracy.\n *\n *\n * This code implements MRL embeddings for efficient vector search by combining faster,\n * lower-dimensional initial search with accurate, high-dimensional re-ranking.\n */\nexport class MatryoshkaRetriever<\n Store extends VectorStore = VectorStore\n> extends VectorStoreRetriever<Store> {\n smallK = 50;\n\n largeK = 8;\n\n largeEmbeddingKey = \"lc_large_embedding\";\n\n largeEmbeddingModel: Embeddings;\n\n searchType: \"cosine\" | \"innerProduct\" | \"euclidean\" = \"cosine\";\n\n constructor(\n fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>\n ) {\n super(fields);\n this.smallK = fields.smallK ?? this.smallK;\n this.largeK = fields.largeK ?? this.largeK;\n this.largeEmbeddingKey = fields.largeEmbeddingKey ?? this.largeEmbeddingKey;\n this.largeEmbeddingModel = fields.largeEmbeddingModel;\n this.searchType = fields.searchType ?? this.searchType;\n }\n\n /**\n * Ranks documents based on their similarity to a query embedding using larger embeddings.\n *\n * This method takes a query embedding and a list of documents (smallResults) as input. Each document\n * in the smallResults array has previously been associated with a large embedding stored in its metadata.\n * Depending on the `searchType` (cosine, innerProduct, or euclidean), it calculates the similarity scores\n * between the query embedding and each document's large embedding. It then ranks the documents based on\n * these similarity scores, from the most similar to the least similar.\n *\n * The method returns a promise that resolves to an array of the top `largeK` documents, where `largeK`\n * is a class property defining the number of documents to return. This subset of documents is determined\n * by sorting the entire list of documents based on their similarity scores and then selecting the top\n * `largeK` documents.\n *\n * @param {number[]} embeddedQuery The embedding of the query, represented as an array of numbers.\n * @param {DocumentInterface[]} smallResults An array of documents, each with metadata that includes a large embedding for similarity comparison.\n * @returns {Promise<DocumentInterface[]>} A promise that resolves to an array of the top `largeK` ranked documents based on their similarity to the query embedding.\n */\n private _rankByLargeEmbeddings(\n embeddedQuery: number[],\n smallResults: DocumentInterface[]\n ): DocumentInterface[] {\n const largeEmbeddings: Array<number[]> = smallResults.map((doc) =>\n JSON.parse(doc.metadata[this.largeEmbeddingKey])\n );\n let func: () => Array<number[]>;\n\n switch (this.searchType) {\n case \"cosine\":\n func = () => cosineSimilarity([embeddedQuery], largeEmbeddings);\n break;\n case \"innerProduct\":\n func = () => innerProduct([embeddedQuery], largeEmbeddings);\n break;\n case \"euclidean\":\n func = () => euclideanDistance([embeddedQuery], largeEmbeddings);\n break;\n default:\n throw new Error(`Unknown search type: ${this.searchType}`);\n }\n\n // Calculate the similarity scores between the query embedding and the large embeddings\n const [similarityScores] = func();\n\n // Create an array of indices from 0 to N-1, where N is the number of documents\n let indices = Array.from(\n { length: smallResults.length },\n (_, index) => index\n );\n\n indices = indices\n .map((v, i) => [similarityScores[i], v])\n .sort(([a], [b]) => b - a)\n .slice(0, this.largeK)\n .map(([, i]) => i);\n\n return indices.map((i) => smallResults[i]);\n }\n\n async _getRelevantDocuments(query: string): Promise<DocumentInterface[]> {\n const [embeddedQuery, smallResults] = await Promise.all([\n this.largeEmbeddingModel.embedQuery(query),\n this.vectorStore.similaritySearch(query, this.smallK, this.filter),\n ]);\n\n return this._rankByLargeEmbeddings(embeddedQuery, smallResults);\n }\n\n /**\n * Override the default `addDocuments` method to embed the documents twice,\n * once using the larger embeddings model, and then again using the default\n * embedding model linked to the vector store.\n *\n * @param {DocumentInterface[]} documents - An array of documents to add to the vector store.\n * @param {AddDocumentOptions} options - An optional object containing additional options for adding documents.\n * @returns {Promise<string[] | void>} A promise that resolves to an array of the document IDs that were added to the vector store.\n */\n override addDocuments = async (\n documents: DocumentInterface[],\n options?: AddDocumentOptions\n ): Promise<string[] | void> => {\n // Insure documents metadata does not contain the large embedding key\n if (documents.some((doc) => this.largeEmbeddingKey in doc.metadata)) {\n throw new Error(\n `All documents must not contain the large embedding key: ${this.largeEmbeddingKey} in their metadata.`\n );\n }\n\n const allDocPageContent = documents.map((doc) => doc.pageContent);\n const allDocLargeEmbeddings = await this.largeEmbeddingModel.embedDocuments(\n allDocPageContent\n );\n\n const newDocuments: Array<DocumentInterface> = documents.map(\n (doc, idx) => ({\n ...doc,\n metadata: {\n ...doc.metadata,\n [this.largeEmbeddingKey]: JSON.stringify(allDocLargeEmbeddings[idx]),\n },\n })\n );\n\n return this.vectorStore.addDocuments(newDocuments, options);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,IAAa,sBAAb,cAEUA,mDAA4B;CACpC,SAAS;CAET,SAAS;CAET,oBAAoB;CAEpB;CAEA,aAAsD;CAEtD,YACEC,QACA;EACA,MAAM,OAAO;EACb,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,oBAAoB,OAAO,qBAAqB,KAAK;EAC1D,KAAK,sBAAsB,OAAO;EAClC,KAAK,aAAa,OAAO,cAAc,KAAK;CAC7C;;;;;;;;;;;;;;;;;;;CAoBD,AAAQ,uBACNC,eACAC,cACqB;EACrB,MAAMC,kBAAmC,aAAa,IAAI,CAAC,QACzD,KAAK,MAAM,IAAI,SAAS,KAAK,mBAAmB,CACjD;EACD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AyC,UAAU,IACvD,CAAC,KAAK,SAAS;GACb,GAAG;GACH,UAAU;IACR,GAAG,IAAI;KACN,KAAK,oBAAoB,KAAK,UAAU,sBAAsB,KAAK;GACrE;EACF,GACF;AAED,SAAO,KAAK,YAAY,aAAa,cAAc,QAAQ;CAC5D;AACF"}
1
+ {"version":3,"file":"matryoshka_retriever.cjs","names":["VectorStoreRetriever","fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>","embeddedQuery: number[]","smallResults: DocumentInterface[]","largeEmbeddings: Array<number[]>","func: () => Array<number[]>","query: string","documents: DocumentInterface[]","options?: AddDocumentOptions","newDocuments: Array<DocumentInterface>"],"sources":["../../src/retrievers/matryoshka_retriever.ts"],"sourcesContent":["import { DocumentInterface } from \"@langchain/core/documents\";\nimport { Embeddings } from \"@langchain/core/embeddings\";\nimport {\n cosineSimilarity,\n euclideanDistance,\n innerProduct,\n} from \"@langchain/core/utils/math\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\n/**\n * Type for options when adding a document to the VectorStore.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AddDocumentOptions = Record<string, any>;\n\nexport interface MatryoshkaRetrieverFields {\n /**\n * The number of documents to retrieve from the small store.\n * @default 50\n */\n smallK?: number;\n /**\n * The number of documents to retrieve from the large store.\n * @default 8\n */\n largeK?: number;\n /**\n * The metadata key to store the larger embeddings.\n * @default \"lc_large_embedding\"\n */\n largeEmbeddingKey?: string;\n /**\n * The embedding model to use when generating the large\n * embeddings.\n */\n largeEmbeddingModel: Embeddings;\n /**\n * The type of search to perform using the large embeddings.\n * @default \"cosine\"\n */\n searchType?: \"cosine\" | \"innerProduct\" | \"euclidean\";\n}\n\n/**\n * A retriever that uses two sets of embeddings to perform adaptive retrieval. Based\n * off of the \"Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval\"\n * blog post {@link https://supabase.com/blog/matryoshka-embeddings}.\n *\n *\n * This class performs \"Adaptive Retrieval\" for searching text embeddings efficiently using the\n * Matryoshka Representation Learning (MRL) technique. It retrieves documents similar to a query\n * embedding in two steps:\n *\n * First-pass: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast,\n * but less accurate search.\n *\n * Second-pass: Re-ranks the top results from the first pass using the full, high-dimensional\n * embedding for higher accuracy.\n *\n *\n * This code implements MRL embeddings for efficient vector search by combining faster,\n * lower-dimensional initial search with accurate, high-dimensional re-ranking.\n */\nexport class MatryoshkaRetriever<\n Store extends VectorStore = VectorStore,\n> extends VectorStoreRetriever<Store> {\n smallK = 50;\n\n largeK = 8;\n\n largeEmbeddingKey = \"lc_large_embedding\";\n\n largeEmbeddingModel: Embeddings;\n\n searchType: \"cosine\" | \"innerProduct\" | \"euclidean\" = \"cosine\";\n\n constructor(\n fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>\n ) {\n super(fields);\n this.smallK = fields.smallK ?? this.smallK;\n this.largeK = fields.largeK ?? this.largeK;\n this.largeEmbeddingKey = fields.largeEmbeddingKey ?? this.largeEmbeddingKey;\n this.largeEmbeddingModel = fields.largeEmbeddingModel;\n this.searchType = fields.searchType ?? this.searchType;\n }\n\n /**\n * Ranks documents based on their similarity to a query embedding using larger embeddings.\n *\n * This method takes a query embedding and a list of documents (smallResults) as input. Each document\n * in the smallResults array has previously been associated with a large embedding stored in its metadata.\n * Depending on the `searchType` (cosine, innerProduct, or euclidean), it calculates the similarity scores\n * between the query embedding and each document's large embedding. It then ranks the documents based on\n * these similarity scores, from the most similar to the least similar.\n *\n * The method returns a promise that resolves to an array of the top `largeK` documents, where `largeK`\n * is a class property defining the number of documents to return. This subset of documents is determined\n * by sorting the entire list of documents based on their similarity scores and then selecting the top\n * `largeK` documents.\n *\n * @param {number[]} embeddedQuery The embedding of the query, represented as an array of numbers.\n * @param {DocumentInterface[]} smallResults An array of documents, each with metadata that includes a large embedding for similarity comparison.\n * @returns {Promise<DocumentInterface[]>} A promise that resolves to an array of the top `largeK` ranked documents based on their similarity to the query embedding.\n */\n private _rankByLargeEmbeddings(\n embeddedQuery: number[],\n smallResults: DocumentInterface[]\n ): DocumentInterface[] {\n const largeEmbeddings: Array<number[]> = smallResults.map((doc) =>\n JSON.parse(doc.metadata[this.largeEmbeddingKey])\n );\n let func: () => Array<number[]>;\n\n switch (this.searchType) {\n case \"cosine\":\n func = () => cosineSimilarity([embeddedQuery], largeEmbeddings);\n break;\n case \"innerProduct\":\n func = () => innerProduct([embeddedQuery], largeEmbeddings);\n break;\n case \"euclidean\":\n func = () => euclideanDistance([embeddedQuery], largeEmbeddings);\n break;\n default:\n throw new Error(`Unknown search type: ${this.searchType}`);\n }\n\n // Calculate the similarity scores between the query embedding and the large embeddings\n const [similarityScores] = func();\n\n // Create an array of indices from 0 to N-1, where N is the number of documents\n let indices = Array.from(\n { length: smallResults.length },\n (_, index) => index\n );\n\n indices = indices\n .map((v, i) => [similarityScores[i], v])\n .sort(([a], [b]) => b - a)\n .slice(0, this.largeK)\n .map(([, i]) => i);\n\n return indices.map((i) => smallResults[i]);\n }\n\n async _getRelevantDocuments(query: string): Promise<DocumentInterface[]> {\n const [embeddedQuery, smallResults] = await Promise.all([\n this.largeEmbeddingModel.embedQuery(query),\n this.vectorStore.similaritySearch(query, this.smallK, this.filter),\n ]);\n\n return this._rankByLargeEmbeddings(embeddedQuery, smallResults);\n }\n\n /**\n * Override the default `addDocuments` method to embed the documents twice,\n * once using the larger embeddings model, and then again using the default\n * embedding model linked to the vector store.\n *\n * @param {DocumentInterface[]} documents - An array of documents to add to the vector store.\n * @param {AddDocumentOptions} options - An optional object containing additional options for adding documents.\n * @returns {Promise<string[] | void>} A promise that resolves to an array of the document IDs that were added to the vector store.\n */\n override addDocuments = async (\n documents: DocumentInterface[],\n options?: AddDocumentOptions\n ): Promise<string[] | void> => {\n // Insure documents metadata does not contain the large embedding key\n if (documents.some((doc) => this.largeEmbeddingKey in doc.metadata)) {\n throw new Error(\n `All documents must not contain the large embedding key: ${this.largeEmbeddingKey} in their metadata.`\n );\n }\n\n const allDocPageContent = documents.map((doc) => doc.pageContent);\n const allDocLargeEmbeddings =\n await this.largeEmbeddingModel.embedDocuments(allDocPageContent);\n\n const newDocuments: Array<DocumentInterface> = documents.map(\n (doc, idx) => ({\n ...doc,\n metadata: {\n ...doc.metadata,\n [this.largeEmbeddingKey]: JSON.stringify(allDocLargeEmbeddings[idx]),\n },\n })\n );\n\n return this.vectorStore.addDocuments(newDocuments, options);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,IAAa,sBAAb,cAEUA,mDAA4B;CACpC,SAAS;CAET,SAAS;CAET,oBAAoB;CAEpB;CAEA,aAAsD;CAEtD,YACEC,QACA;EACA,MAAM,OAAO;EACb,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,oBAAoB,OAAO,qBAAqB,KAAK;EAC1D,KAAK,sBAAsB,OAAO;EAClC,KAAK,aAAa,OAAO,cAAc,KAAK;CAC7C;;;;;;;;;;;;;;;;;;;CAoBD,AAAQ,uBACNC,eACAC,cACqB;EACrB,MAAMC,kBAAmC,aAAa,IAAI,CAAC,QACzD,KAAK,MAAM,IAAI,SAAS,KAAK,mBAAmB,CACjD;EACD,IAAIC;AAEJ,UAAQ,KAAK,YAAb;GACE,KAAK;IACH,OAAO,wDAAuB,CAAC,aAAc,GAAE,gBAAgB;AAC/D;GACF,KAAK;IACH,OAAO,oDAAmB,CAAC,aAAc,GAAE,gBAAgB;AAC3D;GACF,KAAK;IACH,OAAO,yDAAwB,CAAC,aAAc,GAAE,gBAAgB;AAChE;GACF,QACE,OAAM,IAAI,MAAM,CAAC,qBAAqB,EAAE,KAAK,YAAY;EAC5D;EAGD,MAAM,CAAC,iBAAiB,GAAG,MAAM;EAGjC,IAAI,UAAU,MAAM,KAClB,EAAE,QAAQ,aAAa,OAAQ,GAC/B,CAAC,GAAG,UAAU,MACf;EAED,UAAU,QACP,IAAI,CAAC,GAAG,MAAM,CAAC,iBAAiB,IAAI,CAAE,EAAC,CACvC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,IAAI,EAAE,CACzB,MAAM,GAAG,KAAK,OAAO,CACrB,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE;AAEpB,SAAO,QAAQ,IAAI,CAAC,MAAM,aAAa,GAAG;CAC3C;CAED,MAAM,sBAAsBC,OAA6C;EACvE,MAAM,CAAC,eAAe,aAAa,GAAG,MAAM,QAAQ,IAAI,CACtD,KAAK,oBAAoB,WAAW,MAAM,EAC1C,KAAK,YAAY,iBAAiB,OAAO,KAAK,QAAQ,KAAK,OAAO,AACnE,EAAC;AAEF,SAAO,KAAK,uBAAuB,eAAe,aAAa;CAChE;;;;;;;;;;CAWD,AAAS,eAAe,OACtBC,WACAC,YAC6B;AAE7B,MAAI,UAAU,KAAK,CAAC,QAAQ,KAAK,qBAAqB,IAAI,SAAS,CACjE,OAAM,IAAI,MACR,CAAC,wDAAwD,EAAE,KAAK,kBAAkB,mBAAmB,CAAC;EAI1G,MAAM,oBAAoB,UAAU,IAAI,CAAC,QAAQ,IAAI,YAAY;EACjE,MAAM,wBACJ,MAAM,KAAK,oBAAoB,eAAe,kBAAkB;EAElE,MAAMC,eAAyC,UAAU,IACvD,CAAC,KAAK,SAAS;GACb,GAAG;GACH,UAAU;IACR,GAAG,IAAI;KACN,KAAK,oBAAoB,KAAK,UAAU,sBAAsB,KAAK;GACrE;EACF,GACF;AAED,SAAO,KAAK,YAAY,aAAa,cAAc,QAAQ;CAC5D;AACF"}
@@ -1,6 +1,6 @@
1
1
  import { __export } from "../_virtual/rolldown_runtime.js";
2
- import { VectorStoreRetriever } from "@langchain/core/vectorstores";
3
2
  import { cosineSimilarity, euclideanDistance, innerProduct } from "@langchain/core/utils/math";
3
+ import { VectorStoreRetriever } from "@langchain/core/vectorstores";
4
4
 
5
5
  //#region src/retrievers/matryoshka_retriever.ts
6
6
  var matryoshka_retriever_exports = {};
@@ -1 +1 @@
1
- {"version":3,"file":"matryoshka_retriever.js","names":["fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>","embeddedQuery: number[]","smallResults: DocumentInterface[]","largeEmbeddings: Array<number[]>","func: () => Array<number[]>","query: string","documents: DocumentInterface[]","options?: AddDocumentOptions","newDocuments: Array<DocumentInterface>"],"sources":["../../src/retrievers/matryoshka_retriever.ts"],"sourcesContent":["import { DocumentInterface } from \"@langchain/core/documents\";\nimport { Embeddings } from \"@langchain/core/embeddings\";\nimport {\n cosineSimilarity,\n euclideanDistance,\n innerProduct,\n} from \"@langchain/core/utils/math\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\n/**\n * Type for options when adding a document to the VectorStore.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AddDocumentOptions = Record<string, any>;\n\nexport interface MatryoshkaRetrieverFields {\n /**\n * The number of documents to retrieve from the small store.\n * @default 50\n */\n smallK?: number;\n /**\n * The number of documents to retrieve from the large store.\n * @default 8\n */\n largeK?: number;\n /**\n * The metadata key to store the larger embeddings.\n * @default \"lc_large_embedding\"\n */\n largeEmbeddingKey?: string;\n /**\n * The embedding model to use when generating the large\n * embeddings.\n */\n largeEmbeddingModel: Embeddings;\n /**\n * The type of search to perform using the large embeddings.\n * @default \"cosine\"\n */\n searchType?: \"cosine\" | \"innerProduct\" | \"euclidean\";\n}\n\n/**\n * A retriever that uses two sets of embeddings to perform adaptive retrieval. Based\n * off of the \"Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval\"\n * blog post {@link https://supabase.com/blog/matryoshka-embeddings}.\n *\n *\n * This class performs \"Adaptive Retrieval\" for searching text embeddings efficiently using the\n * Matryoshka Representation Learning (MRL) technique. It retrieves documents similar to a query\n * embedding in two steps:\n *\n * First-pass: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast,\n * but less accurate search.\n *\n * Second-pass: Re-ranks the top results from the first pass using the full, high-dimensional\n * embedding for higher accuracy.\n *\n *\n * This code implements MRL embeddings for efficient vector search by combining faster,\n * lower-dimensional initial search with accurate, high-dimensional re-ranking.\n */\nexport class MatryoshkaRetriever<\n Store extends VectorStore = VectorStore\n> extends VectorStoreRetriever<Store> {\n smallK = 50;\n\n largeK = 8;\n\n largeEmbeddingKey = \"lc_large_embedding\";\n\n largeEmbeddingModel: Embeddings;\n\n searchType: \"cosine\" | \"innerProduct\" | \"euclidean\" = \"cosine\";\n\n constructor(\n fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>\n ) {\n super(fields);\n this.smallK = fields.smallK ?? this.smallK;\n this.largeK = fields.largeK ?? this.largeK;\n this.largeEmbeddingKey = fields.largeEmbeddingKey ?? this.largeEmbeddingKey;\n this.largeEmbeddingModel = fields.largeEmbeddingModel;\n this.searchType = fields.searchType ?? this.searchType;\n }\n\n /**\n * Ranks documents based on their similarity to a query embedding using larger embeddings.\n *\n * This method takes a query embedding and a list of documents (smallResults) as input. Each document\n * in the smallResults array has previously been associated with a large embedding stored in its metadata.\n * Depending on the `searchType` (cosine, innerProduct, or euclidean), it calculates the similarity scores\n * between the query embedding and each document's large embedding. It then ranks the documents based on\n * these similarity scores, from the most similar to the least similar.\n *\n * The method returns a promise that resolves to an array of the top `largeK` documents, where `largeK`\n * is a class property defining the number of documents to return. This subset of documents is determined\n * by sorting the entire list of documents based on their similarity scores and then selecting the top\n * `largeK` documents.\n *\n * @param {number[]} embeddedQuery The embedding of the query, represented as an array of numbers.\n * @param {DocumentInterface[]} smallResults An array of documents, each with metadata that includes a large embedding for similarity comparison.\n * @returns {Promise<DocumentInterface[]>} A promise that resolves to an array of the top `largeK` ranked documents based on their similarity to the query embedding.\n */\n private _rankByLargeEmbeddings(\n embeddedQuery: number[],\n smallResults: DocumentInterface[]\n ): DocumentInterface[] {\n const largeEmbeddings: Array<number[]> = smallResults.map((doc) =>\n JSON.parse(doc.metadata[this.largeEmbeddingKey])\n );\n let func: () => Array<number[]>;\n\n switch (this.searchType) {\n case \"cosine\":\n func = () => cosineSimilarity([embeddedQuery], largeEmbeddings);\n break;\n case \"innerProduct\":\n func = () => innerProduct([embeddedQuery], largeEmbeddings);\n break;\n case \"euclidean\":\n func = () => euclideanDistance([embeddedQuery], largeEmbeddings);\n break;\n default:\n throw new Error(`Unknown search type: ${this.searchType}`);\n }\n\n // Calculate the similarity scores between the query embedding and the large embeddings\n const [similarityScores] = func();\n\n // Create an array of indices from 0 to N-1, where N is the number of documents\n let indices = Array.from(\n { length: smallResults.length },\n (_, index) => index\n );\n\n indices = indices\n .map((v, i) => [similarityScores[i], v])\n .sort(([a], [b]) => b - a)\n .slice(0, this.largeK)\n .map(([, i]) => i);\n\n return indices.map((i) => smallResults[i]);\n }\n\n async _getRelevantDocuments(query: string): Promise<DocumentInterface[]> {\n const [embeddedQuery, smallResults] = await Promise.all([\n this.largeEmbeddingModel.embedQuery(query),\n this.vectorStore.similaritySearch(query, this.smallK, this.filter),\n ]);\n\n return this._rankByLargeEmbeddings(embeddedQuery, smallResults);\n }\n\n /**\n * Override the default `addDocuments` method to embed the documents twice,\n * once using the larger embeddings model, and then again using the default\n * embedding model linked to the vector store.\n *\n * @param {DocumentInterface[]} documents - An array of documents to add to the vector store.\n * @param {AddDocumentOptions} options - An optional object containing additional options for adding documents.\n * @returns {Promise<string[] | void>} A promise that resolves to an array of the document IDs that were added to the vector store.\n */\n override addDocuments = async (\n documents: DocumentInterface[],\n options?: AddDocumentOptions\n ): Promise<string[] | void> => {\n // Insure documents metadata does not contain the large embedding key\n if (documents.some((doc) => this.largeEmbeddingKey in doc.metadata)) {\n throw new Error(\n `All documents must not contain the large embedding key: ${this.largeEmbeddingKey} in their metadata.`\n );\n }\n\n const allDocPageContent = documents.map((doc) => doc.pageContent);\n const allDocLargeEmbeddings = await this.largeEmbeddingModel.embedDocuments(\n allDocPageContent\n );\n\n const newDocuments: Array<DocumentInterface> = documents.map(\n (doc, idx) => ({\n ...doc,\n metadata: {\n ...doc.metadata,\n [this.largeEmbeddingKey]: JSON.stringify(allDocLargeEmbeddings[idx]),\n },\n })\n );\n\n return this.vectorStore.addDocuments(newDocuments, options);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,IAAa,sBAAb,cAEU,qBAA4B;CACpC,SAAS;CAET,SAAS;CAET,oBAAoB;CAEpB;CAEA,aAAsD;CAEtD,YACEA,QACA;EACA,MAAM,OAAO;EACb,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,oBAAoB,OAAO,qBAAqB,KAAK;EAC1D,KAAK,sBAAsB,OAAO;EAClC,KAAK,aAAa,OAAO,cAAc,KAAK;CAC7C;;;;;;;;;;;;;;;;;;;CAoBD,AAAQ,uBACNC,eACAC,cACqB;EACrB,MAAMC,kBAAmC,aAAa,IAAI,CAAC,QACzD,KAAK,MAAM,IAAI,SAAS,KAAK,mBAAmB,CACjD;EACD,IAAIC;AAEJ,UAAQ,KAAK,YAAb;GACE,KAAK;IACH,OAAO,MAAM,iBAAiB,CAAC,aAAc,GAAE,gBAAgB;AAC/D;GACF,KAAK;IACH,OAAO,MAAM,aAAa,CAAC,aAAc,GAAE,gBAAgB;AAC3D;GACF,KAAK;IACH,OAAO,MAAM,kBAAkB,CAAC,aAAc,GAAE,gBAAgB;AAChE;GACF,QACE,OAAM,IAAI,MAAM,CAAC,qBAAqB,EAAE,KAAK,YAAY;EAC5D;EAGD,MAAM,CAAC,iBAAiB,GAAG,MAAM;EAGjC,IAAI,UAAU,MAAM,KAClB,EAAE,QAAQ,aAAa,OAAQ,GAC/B,CAAC,GAAG,UAAU,MACf;EAED,UAAU,QACP,IAAI,CAAC,GAAG,MAAM,CAAC,iBAAiB,IAAI,CAAE,EAAC,CACvC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,KAAK,IAAI,EAAE,CACzB,MAAM,GAAG,KAAK,OAAO,CACrB,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE;AAEpB,SAAO,QAAQ,IAAI,CAAC,MAAM,aAAa,GAAG;CAC3C;CAED,MAAM,sBAAsBC,OAA6C;EACvE,MAAM,CAAC,eAAe,aAAa,GAAG,MAAM,QAAQ,IAAI,CACtD,KAAK,oBAAoB,WAAW,MAAM,EAC1C,KAAK,YAAY,iBAAiB,OAAO,KAAK,QAAQ,KAAK,OAAO,AACnE,EAAC;AAEF,SAAO,KAAK,uBAAuB,eAAe,aAAa;CAChE;;;;;;;;;;CAWD,AAAS,eAAe,OACtBC,WACAC,YAC6B;AAE7B,MAAI,UAAU,KAAK,CAAC,QAAQ,KAAK,qBAAqB,IAAI,SAAS,CACjE,OAAM,IAAI,MACR,CAAC,wDAAwD,EAAE,KAAK,kBAAkB,mBAAmB,CAAC;EAI1G,MAAM,oBAAoB,UAAU,IAAI,CAAC,QAAQ,IAAI,YAAY;EACjE,MAAM,wBAAwB,MAAM,KAAK,oBAAoB,eAC3D,kBACD;EAED,MAAMC,eAAyC,UAAU,IACvD,CAAC,KAAK,SAAS;GACb,GAAG;GACH,UAAU;IACR,GAAG,IAAI;KACN,KAAK,oBAAoB,KAAK,UAAU,sBAAsB,KAAK;GACrE;EACF,GACF;AAED,SAAO,KAAK,YAAY,aAAa,cAAc,QAAQ;CAC5D;AACF"}
1
+ {"version":3,"file":"matryoshka_retriever.js","names":["fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>","embeddedQuery: number[]","smallResults: DocumentInterface[]","largeEmbeddings: Array<number[]>","func: () => Array<number[]>","query: string","documents: DocumentInterface[]","options?: AddDocumentOptions","newDocuments: Array<DocumentInterface>"],"sources":["../../src/retrievers/matryoshka_retriever.ts"],"sourcesContent":["import { DocumentInterface } from \"@langchain/core/documents\";\nimport { Embeddings } from \"@langchain/core/embeddings\";\nimport {\n cosineSimilarity,\n euclideanDistance,\n innerProduct,\n} from \"@langchain/core/utils/math\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\n/**\n * Type for options when adding a document to the VectorStore.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AddDocumentOptions = Record<string, any>;\n\nexport interface MatryoshkaRetrieverFields {\n /**\n * The number of documents to retrieve from the small store.\n * @default 50\n */\n smallK?: number;\n /**\n * The number of documents to retrieve from the large store.\n * @default 8\n */\n largeK?: number;\n /**\n * The metadata key to store the larger embeddings.\n * @default \"lc_large_embedding\"\n */\n largeEmbeddingKey?: string;\n /**\n * The embedding model to use when generating the large\n * embeddings.\n */\n largeEmbeddingModel: Embeddings;\n /**\n * The type of search to perform using the large embeddings.\n * @default \"cosine\"\n */\n searchType?: \"cosine\" | \"innerProduct\" | \"euclidean\";\n}\n\n/**\n * A retriever that uses two sets of embeddings to perform adaptive retrieval. Based\n * off of the \"Matryoshka embeddings: faster OpenAI vector search using Adaptive Retrieval\"\n * blog post {@link https://supabase.com/blog/matryoshka-embeddings}.\n *\n *\n * This class performs \"Adaptive Retrieval\" for searching text embeddings efficiently using the\n * Matryoshka Representation Learning (MRL) technique. It retrieves documents similar to a query\n * embedding in two steps:\n *\n * First-pass: Uses a lower dimensional sub-vector from the MRL embedding for an initial, fast,\n * but less accurate search.\n *\n * Second-pass: Re-ranks the top results from the first pass using the full, high-dimensional\n * embedding for higher accuracy.\n *\n *\n * This code implements MRL embeddings for efficient vector search by combining faster,\n * lower-dimensional initial search with accurate, high-dimensional re-ranking.\n */\nexport class MatryoshkaRetriever<\n Store extends VectorStore = VectorStore,\n> extends VectorStoreRetriever<Store> {\n smallK = 50;\n\n largeK = 8;\n\n largeEmbeddingKey = \"lc_large_embedding\";\n\n largeEmbeddingModel: Embeddings;\n\n searchType: \"cosine\" | \"innerProduct\" | \"euclidean\" = \"cosine\";\n\n constructor(\n fields: MatryoshkaRetrieverFields & VectorStoreRetrieverInput<Store>\n ) {\n super(fields);\n this.smallK = fields.smallK ?? this.smallK;\n this.largeK = fields.largeK ?? this.largeK;\n this.largeEmbeddingKey = fields.largeEmbeddingKey ?? this.largeEmbeddingKey;\n this.largeEmbeddingModel = fields.largeEmbeddingModel;\n this.searchType = fields.searchType ?? this.searchType;\n }\n\n /**\n * Ranks documents based on their similarity to a query embedding using larger embeddings.\n *\n * This method takes a query embedding and a list of documents (smallResults) as input. Each document\n * in the smallResults array has previously been associated with a large embedding stored in its metadata.\n * Depending on the `searchType` (cosine, innerProduct, or euclidean), it calculates the similarity scores\n * between the query embedding and each document's large embedding. It then ranks the documents based on\n * these similarity scores, from the most similar to the least similar.\n *\n * The method returns a promise that resolves to an array of the top `largeK` documents, where `largeK`\n * is a class property defining the number of documents to return. This subset of documents is determined\n * by sorting the entire list of documents based on their similarity scores and then selecting the top\n * `largeK` documents.\n *\n * @param {number[]} embeddedQuery The embedding of the query, represented as an array of numbers.\n * @param {DocumentInterface[]} smallResults An array of documents, each with metadata that includes a large embedding for similarity comparison.\n * @returns {Promise<DocumentInterface[]>} A promise that resolves to an array of the top `largeK` ranked documents based on their similarity to the query embedding.\n */\n private _rankByLargeEmbeddings(\n embeddedQuery: number[],\n smallResults: DocumentInterface[]\n ): DocumentInterface[] {\n const largeEmbeddings: Array<number[]> = smallResults.map((doc) =>\n JSON.parse(doc.metadata[this.largeEmbeddingKey])\n );\n let func: () => Array<number[]>;\n\n switch (this.searchType) {\n case \"cosine\":\n func = () => cosineSimilarity([embeddedQuery], largeEmbeddings);\n break;\n case \"innerProduct\":\n func = () => innerProduct([embeddedQuery], largeEmbeddings);\n break;\n case \"euclidean\":\n func = () => euclideanDistance([embeddedQuery], largeEmbeddings);\n break;\n default:\n throw new Error(`Unknown search type: ${this.searchType}`);\n }\n\n // Calculate the similarity scores between the query embedding and the large embeddings\n const [similarityScores] = func();\n\n // Create an array of indices from 0 to N-1, where N is the number of documents\n let indices = Array.from(\n { length: smallResults.length },\n (_, index) => index\n );\n\n indices = indices\n .map((v, i) => [similarityScores[i], v])\n .sort(([a], [b]) => b - a)\n .slice(0, this.largeK)\n .map(([, i]) => i);\n\n return indices.map((i) => smallResults[i]);\n }\n\n async _getRelevantDocuments(query: string): Promise<DocumentInterface[]> {\n const [embeddedQuery, smallResults] = await Promise.all([\n this.largeEmbeddingModel.embedQuery(query),\n this.vectorStore.similaritySearch(query, this.smallK, this.filter),\n ]);\n\n return this._rankByLargeEmbeddings(embeddedQuery, smallResults);\n }\n\n /**\n * Override the default `addDocuments` method to embed the documents twice,\n * once using the larger embeddings model, and then again using the default\n * embedding model linked to the vector store.\n *\n * @param {DocumentInterface[]} documents - An array of documents to add to the vector store.\n * @param {AddDocumentOptions} options - An optional object containing additional options for adding documents.\n * @returns {Promise<string[] | void>} A promise that resolves to an array of the document IDs that were added to the vector store.\n */\n override addDocuments = async (\n documents: DocumentInterface[],\n options?: AddDocumentOptions\n ): Promise<string[] | void> => {\n // Insure documents metadata does not contain the large embedding key\n if (documents.some((doc) => this.largeEmbeddingKey in doc.metadata)) {\n throw new Error(\n `All documents must not contain the large embedding key: ${this.largeEmbeddingKey} in their metadata.`\n );\n }\n\n const allDocPageContent = documents.map((doc) => doc.pageContent);\n const allDocLargeEmbeddings =\n await this.largeEmbeddingModel.embedDocuments(allDocPageContent);\n\n const newDocuments: Array<DocumentInterface> = documents.map(\n (doc, idx) => ({\n ...doc,\n metadata: {\n ...doc.metadata,\n [this.largeEmbeddingKey]: JSON.stringify(allDocLargeEmbeddings[idx]),\n },\n })\n );\n\n return this.vectorStore.addDocuments(newDocuments, options);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmEA,IAAa,sBAAb,cAEU,qBAA4B;CACpC,SAAS;CAET,SAAS;CAET,oBAAoB;CAEpB;CAEA,aAAsD;CAEtD,YACEA,QACA;EACA,MAAM,OAAO;EACb,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,SAAS,OAAO,UAAU,KAAK;EACpC,KAAK,oBAAoB,OAAO,qBAAq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AAK,CAAC,QAAQ,KAAK,qBAAqB,IAAI,SAAS,CACjE,OAAM,IAAI,MACR,CAAC,wDAAwD,EAAE,KAAK,kBAAkB,mBAAmB,CAAC;EAI1G,MAAM,oBAAoB,UAAU,IAAI,CAAC,QAAQ,IAAI,YAAY;EACjE,MAAM,wBACJ,MAAM,KAAK,oBAAoB,eAAe,kBAAkB;EAElE,MAAMC,eAAyC,UAAU,IACvD,CAAC,KAAK,SAAS;GACb,GAAG;GACH,UAAU;IACR,GAAG,IAAI;KACN,KAAK,oBAAoB,KAAK,UAAU,sBAAsB,KAAK;GACrE;EACF,GACF;AAED,SAAO,KAAK,YAAY,aAAa,cAAc,QAAQ;CAC5D;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"score_threshold.cjs","names":["VectorStoreRetriever","input: ScoreThresholdRetrieverInput<V>","query: string","filteredResults: [Document, number][]","vectorStore: V","options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">"],"sources":["../../src/retrievers/score_threshold.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\nexport type ScoreThresholdRetrieverInput<V extends VectorStore> = Omit<\n VectorStoreRetrieverInput<V>,\n \"k\"\n> & {\n maxK?: number;\n kIncrement?: number;\n minSimilarityScore: number;\n};\n\nexport class ScoreThresholdRetriever<\n V extends VectorStore\n> extends VectorStoreRetriever<V> {\n minSimilarityScore: number;\n\n kIncrement = 10;\n\n maxK = 100;\n\n constructor(input: ScoreThresholdRetrieverInput<V>) {\n super(input);\n this.maxK = input.maxK ?? this.maxK;\n this.minSimilarityScore =\n input.minSimilarityScore ?? this.minSimilarityScore;\n this.kIncrement = input.kIncrement ?? this.kIncrement;\n }\n\n async invoke(query: string): Promise<Document[]> {\n let currentK = 0;\n let filteredResults: [Document, number][] = [];\n do {\n currentK += this.kIncrement;\n const results = await this.vectorStore.similaritySearchWithScore(\n query,\n currentK,\n this.filter\n );\n filteredResults = results.filter(\n ([, score]) => score >= this.minSimilarityScore\n );\n } while (filteredResults.length >= currentK && currentK < this.maxK);\n return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);\n }\n\n static fromVectorStore<V extends VectorStore>(\n vectorStore: V,\n options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">\n ) {\n return new this<V>({ ...options, vectorStore });\n }\n}\n"],"mappings":";;;;;;AAgBA,IAAa,0BAAb,cAEUA,mDAAwB;CAChC;CAEA,aAAa;CAEb,OAAO;CAEP,YAAYC,OAAwC;EAClD,MAAM,MAAM;EACZ,KAAK,OAAO,MAAM,QAAQ,KAAK;EAC/B,KAAK,qBACH,MAAM,sBAAsB,KAAK;EACnC,KAAK,aAAa,MAAM,cAAc,KAAK;CAC5C;CAED,MAAM,OAAOC,OAAoC;EAC/C,IAAI,WAAW;EACf,IAAIC,kBAAwC,CAAE;AAC9C,KAAG;GACD,YAAY,KAAK;GACjB,MAAM,UAAU,MAAM,KAAK,YAAY,0BACrC,OACA,UACA,KAAK,OACN;GACD,kBAAkB,QAAQ,OACxB,CAAC,GAAG,MAAM,KAAK,SAAS,KAAK,mBAC9B;EACF,SAAQ,gBAAgB,UAAU,YAAY,WAAW,KAAK;AAC/D,SAAO,gBAAgB,IAAI,CAAC,cAAc,UAAU,GAAG,CAAC,MAAM,GAAG,KAAK,KAAK;CAC5E;CAED,OAAO,gBACLC,aACAC,SACA;AACA,SAAO,IAAI,KAAQ;GAAE,GAAG;GAAS;EAAa;CAC/C;AACF"}
1
+ {"version":3,"file":"score_threshold.cjs","names":["VectorStoreRetriever","input: ScoreThresholdRetrieverInput<V>","query: string","filteredResults: [Document, number][]","vectorStore: V","options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">"],"sources":["../../src/retrievers/score_threshold.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\nexport type ScoreThresholdRetrieverInput<V extends VectorStore> = Omit<\n VectorStoreRetrieverInput<V>,\n \"k\"\n> & {\n maxK?: number;\n kIncrement?: number;\n minSimilarityScore: number;\n};\n\nexport class ScoreThresholdRetriever<\n V extends VectorStore,\n> extends VectorStoreRetriever<V> {\n minSimilarityScore: number;\n\n kIncrement = 10;\n\n maxK = 100;\n\n constructor(input: ScoreThresholdRetrieverInput<V>) {\n super(input);\n this.maxK = input.maxK ?? this.maxK;\n this.minSimilarityScore =\n input.minSimilarityScore ?? this.minSimilarityScore;\n this.kIncrement = input.kIncrement ?? this.kIncrement;\n }\n\n async invoke(query: string): Promise<Document[]> {\n let currentK = 0;\n let filteredResults: [Document, number][] = [];\n do {\n currentK += this.kIncrement;\n const results = await this.vectorStore.similaritySearchWithScore(\n query,\n currentK,\n this.filter\n );\n filteredResults = results.filter(\n ([, score]) => score >= this.minSimilarityScore\n );\n } while (filteredResults.length >= currentK && currentK < this.maxK);\n return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);\n }\n\n static fromVectorStore<V extends VectorStore>(\n vectorStore: V,\n options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">\n ) {\n return new this<V>({ ...options, vectorStore });\n }\n}\n"],"mappings":";;;;;;AAgBA,IAAa,0BAAb,cAEUA,mDAAwB;CAChC;CAEA,aAAa;CAEb,OAAO;CAEP,YAAYC,OAAwC;EAClD,MAAM,MAAM;EACZ,KAAK,OAAO,MAAM,QAAQ,KAAK;EAC/B,KAAK,qBACH,MAAM,sBAAsB,KAAK;EACnC,KAAK,aAAa,MAAM,cAAc,KAAK;CAC5C;CAED,MAAM,OAAOC,OAAoC;EAC/C,IAAI,WAAW;EACf,IAAIC,kBAAwC,CAAE;AAC9C,KAAG;GACD,YAAY,KAAK;GACjB,MAAM,UAAU,MAAM,KAAK,YAAY,0BACrC,OACA,UACA,KAAK,OACN;GACD,kBAAkB,QAAQ,OACxB,CAAC,GAAG,MAAM,KAAK,SAAS,KAAK,mBAC9B;EACF,SAAQ,gBAAgB,UAAU,YAAY,WAAW,KAAK;AAC/D,SAAO,gBAAgB,IAAI,CAAC,cAAc,UAAU,GAAG,CAAC,MAAM,GAAG,KAAK,KAAK;CAC5E;CAED,OAAO,gBACLC,aACAC,SACA;AACA,SAAO,IAAI,KAAQ;GAAE,GAAG;GAAS;EAAa;CAC/C;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"score_threshold.js","names":["input: ScoreThresholdRetrieverInput<V>","query: string","filteredResults: [Document, number][]","vectorStore: V","options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">"],"sources":["../../src/retrievers/score_threshold.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\nexport type ScoreThresholdRetrieverInput<V extends VectorStore> = Omit<\n VectorStoreRetrieverInput<V>,\n \"k\"\n> & {\n maxK?: number;\n kIncrement?: number;\n minSimilarityScore: number;\n};\n\nexport class ScoreThresholdRetriever<\n V extends VectorStore\n> extends VectorStoreRetriever<V> {\n minSimilarityScore: number;\n\n kIncrement = 10;\n\n maxK = 100;\n\n constructor(input: ScoreThresholdRetrieverInput<V>) {\n super(input);\n this.maxK = input.maxK ?? this.maxK;\n this.minSimilarityScore =\n input.minSimilarityScore ?? this.minSimilarityScore;\n this.kIncrement = input.kIncrement ?? this.kIncrement;\n }\n\n async invoke(query: string): Promise<Document[]> {\n let currentK = 0;\n let filteredResults: [Document, number][] = [];\n do {\n currentK += this.kIncrement;\n const results = await this.vectorStore.similaritySearchWithScore(\n query,\n currentK,\n this.filter\n );\n filteredResults = results.filter(\n ([, score]) => score >= this.minSimilarityScore\n );\n } while (filteredResults.length >= currentK && currentK < this.maxK);\n return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);\n }\n\n static fromVectorStore<V extends VectorStore>(\n vectorStore: V,\n options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">\n ) {\n return new this<V>({ ...options, vectorStore });\n }\n}\n"],"mappings":";;;;;;AAgBA,IAAa,0BAAb,cAEU,qBAAwB;CAChC;CAEA,aAAa;CAEb,OAAO;CAEP,YAAYA,OAAwC;EAClD,MAAM,MAAM;EACZ,KAAK,OAAO,MAAM,QAAQ,KAAK;EAC/B,KAAK,qBACH,MAAM,sBAAsB,KAAK;EACnC,KAAK,aAAa,MAAM,cAAc,KAAK;CAC5C;CAED,MAAM,OAAOC,OAAoC;EAC/C,IAAI,WAAW;EACf,IAAIC,kBAAwC,CAAE;AAC9C,KAAG;GACD,YAAY,KAAK;GACjB,MAAM,UAAU,MAAM,KAAK,YAAY,0BACrC,OACA,UACA,KAAK,OACN;GACD,kBAAkB,QAAQ,OACxB,CAAC,GAAG,MAAM,KAAK,SAAS,KAAK,mBAC9B;EACF,SAAQ,gBAAgB,UAAU,YAAY,WAAW,KAAK;AAC/D,SAAO,gBAAgB,IAAI,CAAC,cAAc,UAAU,GAAG,CAAC,MAAM,GAAG,KAAK,KAAK;CAC5E;CAED,OAAO,gBACLC,aACAC,SACA;AACA,SAAO,IAAI,KAAQ;GAAE,GAAG;GAAS;EAAa;CAC/C;AACF"}
1
+ {"version":3,"file":"score_threshold.js","names":["input: ScoreThresholdRetrieverInput<V>","query: string","filteredResults: [Document, number][]","vectorStore: V","options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">"],"sources":["../../src/retrievers/score_threshold.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport {\n VectorStore,\n VectorStoreRetriever,\n VectorStoreRetrieverInput,\n} from \"@langchain/core/vectorstores\";\n\nexport type ScoreThresholdRetrieverInput<V extends VectorStore> = Omit<\n VectorStoreRetrieverInput<V>,\n \"k\"\n> & {\n maxK?: number;\n kIncrement?: number;\n minSimilarityScore: number;\n};\n\nexport class ScoreThresholdRetriever<\n V extends VectorStore,\n> extends VectorStoreRetriever<V> {\n minSimilarityScore: number;\n\n kIncrement = 10;\n\n maxK = 100;\n\n constructor(input: ScoreThresholdRetrieverInput<V>) {\n super(input);\n this.maxK = input.maxK ?? this.maxK;\n this.minSimilarityScore =\n input.minSimilarityScore ?? this.minSimilarityScore;\n this.kIncrement = input.kIncrement ?? this.kIncrement;\n }\n\n async invoke(query: string): Promise<Document[]> {\n let currentK = 0;\n let filteredResults: [Document, number][] = [];\n do {\n currentK += this.kIncrement;\n const results = await this.vectorStore.similaritySearchWithScore(\n query,\n currentK,\n this.filter\n );\n filteredResults = results.filter(\n ([, score]) => score >= this.minSimilarityScore\n );\n } while (filteredResults.length >= currentK && currentK < this.maxK);\n return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);\n }\n\n static fromVectorStore<V extends VectorStore>(\n vectorStore: V,\n options: Omit<ScoreThresholdRetrieverInput<V>, \"vectorStore\">\n ) {\n return new this<V>({ ...options, vectorStore });\n }\n}\n"],"mappings":";;;;;;AAgBA,IAAa,0BAAb,cAEU,qBAAwB;CAChC;CAEA,aAAa;CAEb,OAAO;CAEP,YAAYA,OAAwC;EAClD,MAAM,MAAM;EACZ,KAAK,OAAO,MAAM,QAAQ,KAAK;EAC/B,KAAK,qBACH,MAAM,sBAAsB,KAAK;EACnC,KAAK,aAAa,MAAM,cAAc,KAAK;CAC5C;CAED,MAAM,OAAOC,OAAoC;EAC/C,IAAI,WAAW;EACf,IAAIC,kBAAwC,CAAE;AAC9C,KAAG;GACD,YAAY,KAAK;GACjB,MAAM,UAAU,MAAM,KAAK,YAAY,0BACrC,OACA,UACA,KAAK,OACN;GACD,kBAAkB,QAAQ,OACxB,CAAC,GAAG,MAAM,KAAK,SAAS,KAAK,mBAC9B;EACF,SAAQ,gBAAgB,UAAU,YAAY,WAAW,KAAK;AAC/D,SAAO,gBAAgB,IAAI,CAAC,cAAc,UAAU,GAAG,CAAC,MAAM,GAAG,KAAK,KAAK;CAC5E;CAED,OAAO,gBACLC,aACAC,SACA;AACA,SAAO,IAAI,KAAQ;GAAE,GAAG;GAAS;EAAa;CAC/C;AACF"}
@@ -1,7 +1,7 @@
1
1
  const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
2
2
  const require_chains_query_constructor_index = require('../../chains/query_constructor/index.cjs');
3
- const __langchain_core_retrievers = require_rolldown_runtime.__toESM(require("@langchain/core/retrievers"));
4
3
  const __langchain_core_structured_query = require_rolldown_runtime.__toESM(require("@langchain/core/structured_query"));
4
+ const __langchain_core_retrievers = require_rolldown_runtime.__toESM(require("@langchain/core/retrievers"));
5
5
 
6
6
  //#region src/retrievers/self_query/index.ts
7
7
  var self_query_exports = {};
@@ -2,9 +2,9 @@ import { QueryConstructorRunnableOptions } from "../../chains/query_constructor/
2
2
  import { Document } from "@langchain/core/documents";
3
3
  import { RunnableInterface } from "@langchain/core/runnables";
4
4
  import { CallbackManagerForRetrieverRun } from "@langchain/core/callbacks/manager";
5
+ import { BaseTranslator, BaseTranslator as BaseTranslator$1, BasicTranslator, FunctionalTranslator, StructuredQuery } from "@langchain/core/structured_query";
5
6
  import { BaseRetriever, BaseRetrieverInput } from "@langchain/core/retrievers";
6
7
  import { VectorStore } from "@langchain/core/vectorstores";
7
- import { BaseTranslator, BaseTranslator as BaseTranslator$1, BasicTranslator, FunctionalTranslator, StructuredQuery } from "@langchain/core/structured_query";
8
8
 
9
9
  //#region src/retrievers/self_query/index.d.ts
10
10
 
@@ -1,7 +1,7 @@
1
1
  import { __export } from "../../_virtual/rolldown_runtime.js";
2
2
  import { loadQueryConstructorRunnable } from "../../chains/query_constructor/index.js";
3
- import { BaseRetriever } from "@langchain/core/retrievers";
4
3
  import { BaseTranslator, BasicTranslator, FunctionalTranslator } from "@langchain/core/structured_query";
4
+ import { BaseRetriever } from "@langchain/core/retrievers";
5
5
 
6
6
  //#region src/retrievers/self_query/index.ts
7
7
  var self_query_exports = {};
@@ -1 +1 @@
1
- {"version":3,"file":"config.cjs","names":["evaluator: T | EvalConfig | U","value: unknown","criteria: CriteriaType | Record<string, string>","config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >","distanceMetric: EmbeddingDistanceEvalChainInput[\"distanceMetric\"]","config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"embedding\" | \"feedbackKey\"\n >"],"sources":["../../src/smith/config.ts"],"sourcesContent":["import { BaseLanguageModel } from \"@langchain/core/language_models/base\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\nimport { Example, Run } from \"langsmith\";\nimport { EvaluationResult, RunEvaluator } from \"langsmith/evaluation\";\nimport {\n Criteria as CriteriaType,\n type EmbeddingDistanceEvalChainInput,\n} from \"../evaluation/index.js\";\nimport { LoadEvaluatorOptions } from \"../evaluation/loader.js\";\nimport { EvaluatorType } from \"../evaluation/types.js\";\n\nexport type EvaluatorInputs = {\n input?: string | unknown;\n prediction: string | unknown;\n reference?: string | unknown;\n};\n\nexport type EvaluatorInputFormatter = ({\n rawInput,\n rawPrediction,\n rawReferenceOutput,\n run,\n}: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawInput: any;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawPrediction: any;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawReferenceOutput?: any;\n run: Run;\n}) => EvaluatorInputs;\n\nexport type DynamicRunEvaluatorParams<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Input extends Record<string, any> = Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Prediction extends Record<string, any> = Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Reference extends Record<string, any> = Record<string, unknown>\n> = {\n input: Input;\n prediction?: Prediction;\n reference?: Reference;\n run: Run;\n example?: Example;\n};\n\n/**\n * Type of a function that can be coerced into a RunEvaluator function.\n * While we have the class-based RunEvaluator, it's often more convenient to directly\n * pass a function to the runner. This type allows us to do that.\n */\nexport type RunEvaluatorLike =\n | ((\n props: DynamicRunEvaluatorParams,\n options: RunnableConfig\n ) => Promise<EvaluationResult>)\n | ((\n props: DynamicRunEvaluatorParams,\n options: RunnableConfig\n ) => EvaluationResult);\n\nexport function isOffTheShelfEvaluator<\n T extends keyof EvaluatorType,\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike\n>(evaluator: T | EvalConfig | U): evaluator is T | EvalConfig {\n return typeof evaluator === \"string\" || \"evaluatorType\" in evaluator;\n}\n\nexport function isCustomEvaluator<\n T extends keyof EvaluatorType,\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike\n>(evaluator: T | EvalConfig | U): evaluator is U {\n return !isOffTheShelfEvaluator(evaluator);\n}\n\nexport type RunEvalType<\n T extends keyof EvaluatorType =\n | \"criteria\"\n | \"labeled_criteria\"\n | \"embedding_distance\",\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike\n> = T | EvalConfig | U;\n\n/**\n * Configuration class for running evaluations on datasets.\n *\n * @remarks\n * RunEvalConfig in LangSmith is a configuration class for running evaluations on datasets. Its primary purpose is to define the parameters and evaluators that will be applied during the evaluation of a dataset. This configuration can include various evaluators, custom evaluators, and different keys for inputs, predictions, and references.\n *\n * @typeparam T - The type of evaluators.\n * @typeparam U - The type of custom evaluators.\n */\nexport type RunEvalConfig<\n T extends keyof EvaluatorType =\n | \"criteria\"\n | \"labeled_criteria\"\n | \"embedding_distance\",\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike\n> = {\n /**\n * Evaluators to apply to a dataset run.\n * You can optionally specify these by name, or by\n * configuring them with an EvalConfig object.\n */\n evaluators?: RunEvalType<T, U>[];\n\n /**\n * Convert the evaluation data into formats that can be used by the evaluator.\n * This should most commonly be a string.\n * Parameters are the raw input from the run, the raw output, raw reference output, and the raw run.\n * @example\n * ```ts\n * // Chain input: { input: \"some string\" }\n * // Chain output: { output: \"some output\" }\n * // Reference example output format: { output: \"some reference output\" }\n * const formatEvaluatorInputs = ({\n * rawInput,\n * rawPrediction,\n * rawReferenceOutput,\n * }) => {\n * return {\n * input: rawInput.input,\n * prediction: rawPrediction.output,\n * reference: rawReferenceOutput.output,\n * };\n * };\n * ```\n * @returns The prepared data.\n */\n formatEvaluatorInputs?: EvaluatorInputFormatter;\n\n /**\n * Custom evaluators to apply to a dataset run.\n * Each evaluator is provided with a run trace containing the model\n * outputs, as well as an \"example\" object representing a record\n * in the dataset.\n *\n * @deprecated Use `evaluators` instead.\n */\n customEvaluators?: U[];\n};\n\nexport interface EvalConfig extends LoadEvaluatorOptions {\n /**\n * The name of the evaluator to use.\n * Example: labeled_criteria, criteria, etc.\n */\n evaluatorType: keyof EvaluatorType;\n\n /**\n * The feedback (or metric) name to use for the logged\n * evaluation results. If none provided, we default to\n * the evaluationName.\n */\n feedbackKey?: string;\n\n /**\n * Convert the evaluation data into formats that can be used by the evaluator.\n * This should most commonly be a string.\n * Parameters are the raw input from the run, the raw output, raw reference output, and the raw run.\n * @example\n * ```ts\n * // Chain input: { input: \"some string\" }\n * // Chain output: { output: \"some output\" }\n * // Reference example output format: { output: \"some reference output\" }\n * const formatEvaluatorInputs = ({\n * rawInput,\n * rawPrediction,\n * rawReferenceOutput,\n * }) => {\n * return {\n * input: rawInput.input,\n * prediction: rawPrediction.output,\n * reference: rawReferenceOutput.output,\n * };\n * };\n * ```\n * @returns The prepared data.\n */\n formatEvaluatorInputs: EvaluatorInputFormatter;\n}\n\nconst isStringifiableValue = (\n value: unknown\n): value is string | number | boolean | bigint =>\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\" ||\n typeof value === \"bigint\";\n\nconst getSingleStringifiedValue = (value: unknown) => {\n if (isStringifiableValue(value)) {\n return `${value}`;\n }\n\n if (typeof value === \"object\" && value != null && !Array.isArray(value)) {\n const entries = Object.entries(value);\n\n if (entries.length === 1 && isStringifiableValue(entries[0][1])) {\n return `${entries[0][1]}`;\n }\n }\n\n console.warn(\"Non-stringifiable value found when coercing\", value);\n return `${value}`;\n};\n\n/**\n * Configuration to load a \"CriteriaEvalChain\" evaluator,\n * which prompts an LLM to determine whether the model's\n * prediction complies with the provided criteria.\n * @param criteria - The criteria to use for the evaluator.\n * @param llm - The language model to use for the evaluator.\n * @returns The configuration for the evaluator.\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [Criteria(\"helpfulness\")],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [\n * Criteria({\n * \"isCompliant\": \"Does the submission comply with the requirements of XYZ\"\n * })\n * ],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"criteria\",\n * criteria: \"helpfulness\"\n * formatEvaluatorInputs: ...\n * }]\n * };\n * ```\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"criteria\",\n * criteria: { \"isCompliant\": \"Does the submission comply with the requirements of XYZ\" },\n * formatEvaluatorInputs: ...\n * }]\n * };\n */\nexport type Criteria = EvalConfig & {\n evaluatorType: \"criteria\";\n\n /**\n * The \"criteria\" to insert into the prompt template\n * used for evaluation. See the prompt at\n * https://smith.langchain.com/hub/langchain-ai/criteria-evaluator\n * for more information.\n */\n criteria?: CriteriaType | Record<string, string>;\n\n /**\n * The language model to use as the evaluator, defaults to GPT-4\n */\n llm?: BaseLanguageModel;\n};\n\n// for compatibility reasons\nexport type CriteriaEvalChainConfig = Criteria;\n\nexport function Criteria(\n criteria: CriteriaType | Record<string, string>,\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >\n): EvalConfig {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n input: getSingleStringifiedValue(payload.rawInput),\n }));\n\n if (typeof criteria !== \"string\" && Object.keys(criteria).length !== 1) {\n throw new Error(\n \"Only one criteria key is allowed when specifying custom criteria.\"\n );\n }\n\n const criteriaKey =\n typeof criteria === \"string\" ? criteria : Object.keys(criteria)[0];\n\n return {\n evaluatorType: \"criteria\",\n criteria,\n feedbackKey: config?.feedbackKey ?? criteriaKey,\n llm: config?.llm,\n formatEvaluatorInputs,\n };\n}\n\n/**\n * Configuration to load a \"LabeledCriteriaEvalChain\" evaluator,\n * which prompts an LLM to determine whether the model's\n * prediction complies with the provided criteria and also\n * provides a \"ground truth\" label for the evaluator to incorporate\n * in its evaluation.\n * @param criteria - The criteria to use for the evaluator.\n * @param llm - The language model to use for the evaluator.\n * @returns The configuration for the evaluator.\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [LabeledCriteria(\"correctness\")],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [\n * LabeledCriteria({\n * \"mentionsAllFacts\": \"Does the include all facts provided in the reference?\"\n * })\n * ],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"labeled_criteria\",\n * criteria: \"correctness\",\n * formatEvaluatorInputs: ...\n * }],\n * };\n * ```\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"labeled_criteria\",\n * criteria: { \"mentionsAllFacts\": \"Does the include all facts provided in the reference?\" },\n * formatEvaluatorInputs: ...\n * }],\n * };\n */\nexport type LabeledCriteria = EvalConfig & {\n evaluatorType: \"labeled_criteria\";\n\n /**\n * The \"criteria\" to insert into the prompt template\n * used for evaluation. See the prompt at\n * https://smith.langchain.com/hub/langchain-ai/labeled-criteria\n * for more information.\n */\n criteria?: CriteriaType | Record<string, string>;\n\n /**\n * The language model to use as the evaluator, defaults to GPT-4\n */\n llm?: BaseLanguageModel;\n};\n\nexport function LabeledCriteria(\n criteria: CriteriaType | Record<string, string>,\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >\n): LabeledCriteria {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n input: getSingleStringifiedValue(payload.rawInput),\n reference: getSingleStringifiedValue(payload.rawReferenceOutput),\n }));\n\n if (typeof criteria !== \"string\" && Object.keys(criteria).length !== 1) {\n throw new Error(\n \"Only one labeled criteria key is allowed when specifying custom criteria.\"\n );\n }\n\n const criteriaKey =\n typeof criteria === \"string\" ? criteria : Object.keys(criteria)[0];\n\n return {\n evaluatorType: \"labeled_criteria\",\n criteria,\n feedbackKey: config?.feedbackKey ?? criteriaKey,\n llm: config?.llm,\n formatEvaluatorInputs,\n };\n}\n\n/**\n * Configuration to load a \"EmbeddingDistanceEvalChain\" evaluator,\n * which embeds distances to score semantic difference between\n * a prediction and reference.\n */\nexport type EmbeddingDistance = EvalConfig &\n EmbeddingDistanceEvalChainInput & { evaluatorType: \"embedding_distance\" };\n\nexport function EmbeddingDistance(\n distanceMetric: EmbeddingDistanceEvalChainInput[\"distanceMetric\"],\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"embedding\" | \"feedbackKey\"\n >\n): EmbeddingDistance {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n reference: getSingleStringifiedValue(payload.rawReferenceOutput),\n }));\n\n return {\n evaluatorType: \"embedding_distance\",\n embedding: config?.embedding,\n distanceMetric,\n feedbackKey: config?.feedbackKey ?? \"embedding_distance\",\n formatEvaluatorInputs,\n };\n}\n"],"mappings":";;AA8DA,SAAgB,uBAGdA,WAA4D;AAC5D,QAAO,OAAO,cAAc,YAAY,mBAAmB;AAC5D;AAED,SAAgB,kBAGdA,WAA+C;AAC/C,QAAO,CAAC,uBAAuB,UAAU;AAC1C;AA6GD,MAAM,uBAAuB,CAC3BC,UAEA,OAAO,UAAU,YACjB,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,OAAO,UAAU;AAEnB,MAAM,4BAA4B,CAACA,UAAmB;AACpD,KAAI,qBAAqB,MAAM,C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,WAAW,0BAA0B,QAAQ,mBAAmB;CACjE;AAEH,QAAO;EACL,eAAe;EACf,WAAW,QAAQ;EACnB;EACA,aAAa,QAAQ,eAAe;EACpC;CACD;AACF"}
1
+ {"version":3,"file":"config.cjs","names":["evaluator: T | EvalConfig | U","value: unknown","criteria: CriteriaType | Record<string, string>","config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >","distanceMetric: EmbeddingDistanceEvalChainInput[\"distanceMetric\"]","config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"embedding\" | \"feedbackKey\"\n >"],"sources":["../../src/smith/config.ts"],"sourcesContent":["import { BaseLanguageModel } from \"@langchain/core/language_models/base\";\nimport { RunnableConfig } from \"@langchain/core/runnables\";\nimport { Example, Run } from \"langsmith\";\nimport { EvaluationResult, RunEvaluator } from \"langsmith/evaluation\";\nimport {\n Criteria as CriteriaType,\n type EmbeddingDistanceEvalChainInput,\n} from \"../evaluation/index.js\";\nimport { LoadEvaluatorOptions } from \"../evaluation/loader.js\";\nimport { EvaluatorType } from \"../evaluation/types.js\";\n\nexport type EvaluatorInputs = {\n input?: string | unknown;\n prediction: string | unknown;\n reference?: string | unknown;\n};\n\nexport type EvaluatorInputFormatter = ({\n rawInput,\n rawPrediction,\n rawReferenceOutput,\n run,\n}: {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawInput: any;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawPrediction: any;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawReferenceOutput?: any;\n run: Run;\n}) => EvaluatorInputs;\n\nexport type DynamicRunEvaluatorParams<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Input extends Record<string, any> = Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Prediction extends Record<string, any> = Record<string, unknown>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Reference extends Record<string, any> = Record<string, unknown>,\n> = {\n input: Input;\n prediction?: Prediction;\n reference?: Reference;\n run: Run;\n example?: Example;\n};\n\n/**\n * Type of a function that can be coerced into a RunEvaluator function.\n * While we have the class-based RunEvaluator, it's often more convenient to directly\n * pass a function to the runner. This type allows us to do that.\n */\nexport type RunEvaluatorLike =\n | ((\n props: DynamicRunEvaluatorParams,\n options: RunnableConfig\n ) => Promise<EvaluationResult>)\n | ((\n props: DynamicRunEvaluatorParams,\n options: RunnableConfig\n ) => EvaluationResult);\n\nexport function isOffTheShelfEvaluator<\n T extends keyof EvaluatorType,\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike,\n>(evaluator: T | EvalConfig | U): evaluator is T | EvalConfig {\n return typeof evaluator === \"string\" || \"evaluatorType\" in evaluator;\n}\n\nexport function isCustomEvaluator<\n T extends keyof EvaluatorType,\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike,\n>(evaluator: T | EvalConfig | U): evaluator is U {\n return !isOffTheShelfEvaluator(evaluator);\n}\n\nexport type RunEvalType<\n T extends keyof EvaluatorType =\n | \"criteria\"\n | \"labeled_criteria\"\n | \"embedding_distance\",\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike,\n> = T | EvalConfig | U;\n\n/**\n * Configuration class for running evaluations on datasets.\n *\n * @remarks\n * RunEvalConfig in LangSmith is a configuration class for running evaluations on datasets. Its primary purpose is to define the parameters and evaluators that will be applied during the evaluation of a dataset. This configuration can include various evaluators, custom evaluators, and different keys for inputs, predictions, and references.\n *\n * @typeparam T - The type of evaluators.\n * @typeparam U - The type of custom evaluators.\n */\nexport type RunEvalConfig<\n T extends keyof EvaluatorType =\n | \"criteria\"\n | \"labeled_criteria\"\n | \"embedding_distance\",\n U extends RunEvaluator | RunEvaluatorLike = RunEvaluator | RunEvaluatorLike,\n> = {\n /**\n * Evaluators to apply to a dataset run.\n * You can optionally specify these by name, or by\n * configuring them with an EvalConfig object.\n */\n evaluators?: RunEvalType<T, U>[];\n\n /**\n * Convert the evaluation data into formats that can be used by the evaluator.\n * This should most commonly be a string.\n * Parameters are the raw input from the run, the raw output, raw reference output, and the raw run.\n * @example\n * ```ts\n * // Chain input: { input: \"some string\" }\n * // Chain output: { output: \"some output\" }\n * // Reference example output format: { output: \"some reference output\" }\n * const formatEvaluatorInputs = ({\n * rawInput,\n * rawPrediction,\n * rawReferenceOutput,\n * }) => {\n * return {\n * input: rawInput.input,\n * prediction: rawPrediction.output,\n * reference: rawReferenceOutput.output,\n * };\n * };\n * ```\n * @returns The prepared data.\n */\n formatEvaluatorInputs?: EvaluatorInputFormatter;\n\n /**\n * Custom evaluators to apply to a dataset run.\n * Each evaluator is provided with a run trace containing the model\n * outputs, as well as an \"example\" object representing a record\n * in the dataset.\n *\n * @deprecated Use `evaluators` instead.\n */\n customEvaluators?: U[];\n};\n\nexport interface EvalConfig extends LoadEvaluatorOptions {\n /**\n * The name of the evaluator to use.\n * Example: labeled_criteria, criteria, etc.\n */\n evaluatorType: keyof EvaluatorType;\n\n /**\n * The feedback (or metric) name to use for the logged\n * evaluation results. If none provided, we default to\n * the evaluationName.\n */\n feedbackKey?: string;\n\n /**\n * Convert the evaluation data into formats that can be used by the evaluator.\n * This should most commonly be a string.\n * Parameters are the raw input from the run, the raw output, raw reference output, and the raw run.\n * @example\n * ```ts\n * // Chain input: { input: \"some string\" }\n * // Chain output: { output: \"some output\" }\n * // Reference example output format: { output: \"some reference output\" }\n * const formatEvaluatorInputs = ({\n * rawInput,\n * rawPrediction,\n * rawReferenceOutput,\n * }) => {\n * return {\n * input: rawInput.input,\n * prediction: rawPrediction.output,\n * reference: rawReferenceOutput.output,\n * };\n * };\n * ```\n * @returns The prepared data.\n */\n formatEvaluatorInputs: EvaluatorInputFormatter;\n}\n\nconst isStringifiableValue = (\n value: unknown\n): value is string | number | boolean | bigint =>\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\" ||\n typeof value === \"bigint\";\n\nconst getSingleStringifiedValue = (value: unknown) => {\n if (isStringifiableValue(value)) {\n return `${value}`;\n }\n\n if (typeof value === \"object\" && value != null && !Array.isArray(value)) {\n const entries = Object.entries(value);\n\n if (entries.length === 1 && isStringifiableValue(entries[0][1])) {\n return `${entries[0][1]}`;\n }\n }\n\n console.warn(\"Non-stringifiable value found when coercing\", value);\n return `${value}`;\n};\n\n/**\n * Configuration to load a \"CriteriaEvalChain\" evaluator,\n * which prompts an LLM to determine whether the model's\n * prediction complies with the provided criteria.\n * @param criteria - The criteria to use for the evaluator.\n * @param llm - The language model to use for the evaluator.\n * @returns The configuration for the evaluator.\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [Criteria(\"helpfulness\")],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [\n * Criteria({\n * \"isCompliant\": \"Does the submission comply with the requirements of XYZ\"\n * })\n * ],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"criteria\",\n * criteria: \"helpfulness\"\n * formatEvaluatorInputs: ...\n * }]\n * };\n * ```\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"criteria\",\n * criteria: { \"isCompliant\": \"Does the submission comply with the requirements of XYZ\" },\n * formatEvaluatorInputs: ...\n * }]\n * };\n */\nexport type Criteria = EvalConfig & {\n evaluatorType: \"criteria\";\n\n /**\n * The \"criteria\" to insert into the prompt template\n * used for evaluation. See the prompt at\n * https://smith.langchain.com/hub/langchain-ai/criteria-evaluator\n * for more information.\n */\n criteria?: CriteriaType | Record<string, string>;\n\n /**\n * The language model to use as the evaluator, defaults to GPT-4\n */\n llm?: BaseLanguageModel;\n};\n\n// for compatibility reasons\nexport type CriteriaEvalChainConfig = Criteria;\n\nexport function Criteria(\n criteria: CriteriaType | Record<string, string>,\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >\n): EvalConfig {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n input: getSingleStringifiedValue(payload.rawInput),\n }));\n\n if (typeof criteria !== \"string\" && Object.keys(criteria).length !== 1) {\n throw new Error(\n \"Only one criteria key is allowed when specifying custom criteria.\"\n );\n }\n\n const criteriaKey =\n typeof criteria === \"string\" ? criteria : Object.keys(criteria)[0];\n\n return {\n evaluatorType: \"criteria\",\n criteria,\n feedbackKey: config?.feedbackKey ?? criteriaKey,\n llm: config?.llm,\n formatEvaluatorInputs,\n };\n}\n\n/**\n * Configuration to load a \"LabeledCriteriaEvalChain\" evaluator,\n * which prompts an LLM to determine whether the model's\n * prediction complies with the provided criteria and also\n * provides a \"ground truth\" label for the evaluator to incorporate\n * in its evaluation.\n * @param criteria - The criteria to use for the evaluator.\n * @param llm - The language model to use for the evaluator.\n * @returns The configuration for the evaluator.\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [LabeledCriteria(\"correctness\")],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [\n * LabeledCriteria({\n * \"mentionsAllFacts\": \"Does the include all facts provided in the reference?\"\n * })\n * ],\n * };\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"labeled_criteria\",\n * criteria: \"correctness\",\n * formatEvaluatorInputs: ...\n * }],\n * };\n * ```\n * @example\n * ```ts\n * const evalConfig = {\n * evaluators: [{\n * evaluatorType: \"labeled_criteria\",\n * criteria: { \"mentionsAllFacts\": \"Does the include all facts provided in the reference?\" },\n * formatEvaluatorInputs: ...\n * }],\n * };\n */\nexport type LabeledCriteria = EvalConfig & {\n evaluatorType: \"labeled_criteria\";\n\n /**\n * The \"criteria\" to insert into the prompt template\n * used for evaluation. See the prompt at\n * https://smith.langchain.com/hub/langchain-ai/labeled-criteria\n * for more information.\n */\n criteria?: CriteriaType | Record<string, string>;\n\n /**\n * The language model to use as the evaluator, defaults to GPT-4\n */\n llm?: BaseLanguageModel;\n};\n\nexport function LabeledCriteria(\n criteria: CriteriaType | Record<string, string>,\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"llm\" | \"feedbackKey\"\n >\n): LabeledCriteria {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n input: getSingleStringifiedValue(payload.rawInput),\n reference: getSingleStringifiedValue(payload.rawReferenceOutput),\n }));\n\n if (typeof criteria !== \"string\" && Object.keys(criteria).length !== 1) {\n throw new Error(\n \"Only one labeled criteria key is allowed when specifying custom criteria.\"\n );\n }\n\n const criteriaKey =\n typeof criteria === \"string\" ? criteria : Object.keys(criteria)[0];\n\n return {\n evaluatorType: \"labeled_criteria\",\n criteria,\n feedbackKey: config?.feedbackKey ?? criteriaKey,\n llm: config?.llm,\n formatEvaluatorInputs,\n };\n}\n\n/**\n * Configuration to load a \"EmbeddingDistanceEvalChain\" evaluator,\n * which embeds distances to score semantic difference between\n * a prediction and reference.\n */\nexport type EmbeddingDistance = EvalConfig &\n EmbeddingDistanceEvalChainInput & { evaluatorType: \"embedding_distance\" };\n\nexport function EmbeddingDistance(\n distanceMetric: EmbeddingDistanceEvalChainInput[\"distanceMetric\"],\n config?: Pick<\n Partial<LabeledCriteria>,\n \"formatEvaluatorInputs\" | \"embedding\" | \"feedbackKey\"\n >\n): EmbeddingDistance {\n const formatEvaluatorInputs =\n config?.formatEvaluatorInputs ??\n ((payload) => ({\n prediction: getSingleStringifiedValue(payload.rawPrediction),\n reference: getSingleStringifiedValue(payload.rawReferenceOutput),\n }));\n\n return {\n evaluatorType: \"embedding_distance\",\n embedding: config?.embedding,\n distanceMetric,\n feedbackKey: config?.feedbackKey ?? \"embedding_distance\",\n formatEvaluatorInputs,\n };\n}\n"],"mappings":";;AA8DA,SAAgB,uBAGdA,WAA4D;AAC5D,QAAO,OAAO,cAAc,YAAY,mBAAmB;AAC5D;AAED,SAAgB,kBAGdA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