langchain 0.0.194 → 0.0.196

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 (241) hide show
  1. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  2. package/dist/base_language/count_tokens.cjs +5 -70
  3. package/dist/base_language/count_tokens.d.ts +1 -10
  4. package/dist/base_language/count_tokens.js +1 -65
  5. package/dist/base_language/index.cjs +6 -196
  6. package/dist/base_language/index.d.ts +1 -111
  7. package/dist/base_language/index.js +1 -191
  8. package/dist/cache/base.cjs +15 -37
  9. package/dist/cache/base.d.ts +1 -20
  10. package/dist/cache/base.js +1 -33
  11. package/dist/cache/index.cjs +2 -46
  12. package/dist/cache/index.d.ts +1 -29
  13. package/dist/cache/index.js +1 -45
  14. package/dist/callbacks/base.cjs +3 -139
  15. package/dist/callbacks/base.d.ts +1 -266
  16. package/dist/callbacks/base.js +1 -126
  17. package/dist/callbacks/handlers/console.cjs +14 -221
  18. package/dist/callbacks/handlers/console.d.ts +1 -117
  19. package/dist/callbacks/handlers/console.js +1 -217
  20. package/dist/callbacks/handlers/initialize.cjs +15 -30
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  22. package/dist/callbacks/handlers/initialize.js +1 -27
  23. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  25. package/dist/callbacks/handlers/log_stream.js +1 -289
  26. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  28. package/dist/callbacks/handlers/run_collector.js +1 -46
  29. package/dist/callbacks/handlers/tracer.cjs +15 -375
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  31. package/dist/callbacks/handlers/tracer.js +1 -373
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  38. package/dist/callbacks/manager.cjs +15 -676
  39. package/dist/callbacks/manager.d.ts +1 -180
  40. package/dist/callbacks/manager.js +1 -666
  41. package/dist/callbacks/promises.cjs +14 -42
  42. package/dist/callbacks/promises.d.ts +1 -11
  43. package/dist/callbacks/promises.js +1 -37
  44. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  45. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  46. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  47. package/dist/chains/openai_functions/structured_output.js +1 -1
  48. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  49. package/dist/chat_models/base.cjs +15 -296
  50. package/dist/chat_models/base.d.ts +1 -122
  51. package/dist/chat_models/base.js +1 -292
  52. package/dist/chat_models/bedrock/web.d.ts +1 -1
  53. package/dist/chat_models/fireworks.d.ts +1 -1
  54. package/dist/document.cjs +2 -24
  55. package/dist/document.d.ts +1 -12
  56. package/dist/document.js +1 -23
  57. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  58. package/dist/document_loaders/web/s3.d.ts +1 -1
  59. package/dist/embeddings/base.cjs +15 -22
  60. package/dist/embeddings/base.d.ts +1 -33
  61. package/dist/embeddings/base.js +1 -20
  62. package/dist/embeddings/cache_backed.cjs +2 -2
  63. package/dist/embeddings/cache_backed.js +1 -1
  64. package/dist/embeddings/hf.cjs +1 -2
  65. package/dist/embeddings/hf.js +1 -2
  66. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  67. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  68. package/dist/evaluation/qa/prompt.d.ts +2 -2
  69. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  70. package/dist/experimental/openai_assistant/index.cjs +2 -1
  71. package/dist/experimental/openai_assistant/index.d.ts +2 -1
  72. package/dist/experimental/openai_assistant/index.js +2 -1
  73. package/dist/experimental/openai_files/index.cjs +88 -0
  74. package/dist/experimental/openai_files/index.d.ts +79 -0
  75. package/dist/experimental/openai_files/index.js +84 -0
  76. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  77. package/dist/llms/base.cjs +15 -278
  78. package/dist/llms/base.d.ts +1 -115
  79. package/dist/llms/base.js +1 -275
  80. package/dist/llms/bedrock/web.d.ts +1 -1
  81. package/dist/llms/fireworks.d.ts +1 -1
  82. package/dist/load/import_constants.cjs +1 -0
  83. package/dist/load/import_constants.js +1 -0
  84. package/dist/load/import_map.cjs +4 -2
  85. package/dist/load/import_map.d.ts +2 -0
  86. package/dist/load/import_map.js +2 -0
  87. package/dist/load/index.cjs +7 -148
  88. package/dist/load/index.js +7 -148
  89. package/dist/load/map_keys.cjs +0 -24
  90. package/dist/load/map_keys.d.ts +0 -6
  91. package/dist/load/map_keys.js +1 -17
  92. package/dist/load/serializable.cjs +15 -178
  93. package/dist/load/serializable.d.ts +1 -66
  94. package/dist/load/serializable.js +1 -175
  95. package/dist/memory/base.cjs +17 -92
  96. package/dist/memory/base.d.ts +2 -68
  97. package/dist/memory/base.js +2 -87
  98. package/dist/output_parsers/openai_functions.cjs +2 -2
  99. package/dist/output_parsers/openai_functions.d.ts +1 -1
  100. package/dist/output_parsers/openai_functions.js +1 -1
  101. package/dist/output_parsers/regex.d.ts +1 -1
  102. package/dist/output_parsers/structured.d.ts +1 -1
  103. package/dist/prompts/base.cjs +8 -183
  104. package/dist/prompts/base.d.ts +3 -132
  105. package/dist/prompts/base.js +3 -178
  106. package/dist/prompts/chat.cjs +15 -477
  107. package/dist/prompts/chat.d.ts +1 -219
  108. package/dist/prompts/chat.js +1 -466
  109. package/dist/prompts/few_shot.cjs +15 -353
  110. package/dist/prompts/few_shot.d.ts +1 -192
  111. package/dist/prompts/few_shot.js +1 -350
  112. package/dist/prompts/index.cjs +3 -2
  113. package/dist/prompts/index.d.ts +2 -1
  114. package/dist/prompts/index.js +2 -1
  115. package/dist/prompts/pipeline.cjs +15 -142
  116. package/dist/prompts/pipeline.d.ts +1 -98
  117. package/dist/prompts/pipeline.js +1 -140
  118. package/dist/prompts/prompt.cjs +15 -146
  119. package/dist/prompts/prompt.d.ts +1 -92
  120. package/dist/prompts/prompt.js +1 -144
  121. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
  122. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  123. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  124. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  125. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  126. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  127. package/dist/prompts/selectors/conditional.cjs +15 -74
  128. package/dist/prompts/selectors/conditional.d.ts +1 -63
  129. package/dist/prompts/selectors/conditional.js +1 -69
  130. package/dist/prompts/serde.cjs +15 -0
  131. package/dist/prompts/serde.d.ts +1 -43
  132. package/dist/prompts/serde.js +1 -1
  133. package/dist/prompts/template.cjs +14 -88
  134. package/dist/prompts/template.d.ts +1 -36
  135. package/dist/prompts/template.js +1 -83
  136. package/dist/retrievers/chaindesk.cjs +9 -1
  137. package/dist/retrievers/chaindesk.d.ts +3 -1
  138. package/dist/retrievers/chaindesk.js +9 -1
  139. package/dist/retrievers/self_query/vectara.cjs +138 -0
  140. package/dist/retrievers/self_query/vectara.d.ts +41 -0
  141. package/dist/retrievers/self_query/vectara.js +134 -0
  142. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  143. package/dist/runnables/index.d.ts +1 -0
  144. package/dist/runnables/index.js +1 -0
  145. package/dist/schema/document.cjs +3 -34
  146. package/dist/schema/document.d.ts +2 -29
  147. package/dist/schema/document.js +2 -32
  148. package/dist/schema/index.cjs +37 -612
  149. package/dist/schema/index.d.ts +11 -311
  150. package/dist/schema/index.js +8 -583
  151. package/dist/schema/output_parser.cjs +15 -309
  152. package/dist/schema/output_parser.d.ts +1 -173
  153. package/dist/schema/output_parser.js +1 -301
  154. package/dist/schema/retriever.cjs +15 -77
  155. package/dist/schema/retriever.d.ts +1 -43
  156. package/dist/schema/retriever.js +1 -75
  157. package/dist/schema/runnable/base.cjs +10 -1072
  158. package/dist/schema/runnable/base.d.ts +1 -356
  159. package/dist/schema/runnable/base.js +1 -1060
  160. package/dist/schema/runnable/branch.cjs +2 -131
  161. package/dist/schema/runnable/branch.d.ts +1 -94
  162. package/dist/schema/runnable/branch.js +1 -130
  163. package/dist/schema/runnable/config.cjs +0 -6
  164. package/dist/schema/runnable/config.d.ts +1 -3
  165. package/dist/schema/runnable/config.js +1 -4
  166. package/dist/schema/runnable/index.cjs +15 -16
  167. package/dist/schema/runnable/index.d.ts +1 -5
  168. package/dist/schema/runnable/index.js +1 -4
  169. package/dist/schema/runnable/passthrough.cjs +3 -113
  170. package/dist/schema/runnable/passthrough.d.ts +1 -72
  171. package/dist/schema/runnable/passthrough.js +1 -111
  172. package/dist/schema/runnable/router.cjs +2 -71
  173. package/dist/schema/runnable/router.d.ts +1 -29
  174. package/dist/schema/runnable/router.js +1 -70
  175. package/dist/schema/storage.cjs +15 -8
  176. package/dist/schema/storage.d.ts +1 -57
  177. package/dist/schema/storage.js +1 -6
  178. package/dist/tools/bingserpapi.d.ts +1 -1
  179. package/dist/tools/searchapi.d.ts +1 -1
  180. package/dist/tools/serpapi.d.ts +1 -1
  181. package/dist/tools/serper.d.ts +1 -1
  182. package/dist/util/async_caller.cjs +14 -128
  183. package/dist/util/async_caller.d.ts +1 -45
  184. package/dist/util/async_caller.js +1 -124
  185. package/dist/vectorstores/vectara.cjs +77 -7
  186. package/dist/vectorstores/vectara.d.ts +9 -3
  187. package/dist/vectorstores/vectara.js +54 -7
  188. package/experimental/openai_files.cjs +1 -0
  189. package/experimental/openai_files.d.ts +1 -0
  190. package/experimental/openai_files.js +1 -0
  191. package/package.json +27 -5
  192. package/retrievers/self_query/vectara.cjs +1 -0
  193. package/retrievers/self_query/vectara.d.ts +1 -0
  194. package/retrievers/self_query/vectara.js +1 -0
  195. package/runnables.cjs +1 -0
  196. package/runnables.d.ts +1 -0
  197. package/runnables.js +1 -0
  198. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  199. package/dist/util/@cfworker/json-schema/index.js +0 -1
  200. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  201. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  202. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  203. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  204. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  205. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  206. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  207. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  208. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  209. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  210. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  211. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  212. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  213. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  214. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  215. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  216. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  217. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  218. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  219. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  220. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  221. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  222. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  223. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  224. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  225. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  226. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  227. package/dist/util/fast-json-patch/index.cjs +0 -49
  228. package/dist/util/fast-json-patch/index.d.ts +0 -22
  229. package/dist/util/fast-json-patch/index.js +0 -16
  230. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  231. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  232. package/dist/util/fast-json-patch/src/core.js +0 -459
  233. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  234. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  235. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  236. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  237. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  238. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  239. package/dist/util/js-sha1/hash.cjs +0 -358
  240. package/dist/util/js-sha1/hash.d.ts +0 -1
  241. package/dist/util/js-sha1/hash.js +0 -355
@@ -19,7 +19,7 @@ class ChaindeskRetriever extends retriever_js_1.BaseRetriever {
19
19
  static lc_name() {
20
20
  return "ChaindeskRetriever";
21
21
  }
22
- constructor({ datastoreId, apiKey, topK, ...rest }) {
22
+ constructor({ datastoreId, apiKey, topK, filter, ...rest }) {
23
23
  super();
24
24
  Object.defineProperty(this, "lc_namespace", {
25
25
  enumerable: true,
@@ -45,6 +45,12 @@ class ChaindeskRetriever extends retriever_js_1.BaseRetriever {
45
45
  writable: true,
46
46
  value: void 0
47
47
  });
48
+ Object.defineProperty(this, "filter", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
48
54
  Object.defineProperty(this, "apiKey", {
49
55
  enumerable: true,
50
56
  configurable: true,
@@ -55,6 +61,7 @@ class ChaindeskRetriever extends retriever_js_1.BaseRetriever {
55
61
  this.datastoreId = datastoreId;
56
62
  this.apiKey = apiKey;
57
63
  this.topK = topK;
64
+ this.filter = filter;
58
65
  }
59
66
  async getRelevantDocuments(query) {
60
67
  const r = await this.caller.call(fetch, `https://app.chaindesk.ai/api/datastores/${this.datastoreId}/query`, {
@@ -62,6 +69,7 @@ class ChaindeskRetriever extends retriever_js_1.BaseRetriever {
62
69
  body: JSON.stringify({
63
70
  query,
64
71
  ...(this.topK ? { topK: this.topK } : {}),
72
+ ...(this.filter ? { filters: this.filter } : {}),
65
73
  }),
66
74
  headers: {
67
75
  "Content-Type": "application/json",
@@ -4,6 +4,7 @@ import { AsyncCaller, type AsyncCallerParams } from "../util/async_caller.js";
4
4
  export interface ChaindeskRetrieverArgs extends AsyncCallerParams, BaseRetrieverInput {
5
5
  datastoreId: string;
6
6
  topK?: number;
7
+ filter?: Record<string, unknown>;
7
8
  apiKey?: string;
8
9
  }
9
10
  /**
@@ -23,7 +24,8 @@ export declare class ChaindeskRetriever extends BaseRetriever {
23
24
  caller: AsyncCaller;
24
25
  datastoreId: string;
25
26
  topK?: number;
27
+ filter?: Record<string, unknown>;
26
28
  apiKey?: string;
27
- constructor({ datastoreId, apiKey, topK, ...rest }: ChaindeskRetrieverArgs);
29
+ constructor({ datastoreId, apiKey, topK, filter, ...rest }: ChaindeskRetrieverArgs);
28
30
  getRelevantDocuments(query: string): Promise<Document[]>;
29
31
  }
@@ -16,7 +16,7 @@ export class ChaindeskRetriever extends BaseRetriever {
16
16
  static lc_name() {
17
17
  return "ChaindeskRetriever";
18
18
  }
19
- constructor({ datastoreId, apiKey, topK, ...rest }) {
19
+ constructor({ datastoreId, apiKey, topK, filter, ...rest }) {
20
20
  super();
21
21
  Object.defineProperty(this, "lc_namespace", {
22
22
  enumerable: true,
@@ -42,6 +42,12 @@ export class ChaindeskRetriever extends BaseRetriever {
42
42
  writable: true,
43
43
  value: void 0
44
44
  });
45
+ Object.defineProperty(this, "filter", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
45
51
  Object.defineProperty(this, "apiKey", {
46
52
  enumerable: true,
47
53
  configurable: true,
@@ -52,6 +58,7 @@ export class ChaindeskRetriever extends BaseRetriever {
52
58
  this.datastoreId = datastoreId;
53
59
  this.apiKey = apiKey;
54
60
  this.topK = topK;
61
+ this.filter = filter;
55
62
  }
56
63
  async getRelevantDocuments(query) {
57
64
  const r = await this.caller.call(fetch, `https://app.chaindesk.ai/api/datastores/${this.datastoreId}/query`, {
@@ -59,6 +66,7 @@ export class ChaindeskRetriever extends BaseRetriever {
59
66
  body: JSON.stringify({
60
67
  query,
61
68
  ...(this.topK ? { topK: this.topK } : {}),
69
+ ...(this.filter ? { filters: this.filter } : {}),
62
70
  }),
63
71
  headers: {
64
72
  "Content-Type": "application/json",
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VectaraTranslator = void 0;
4
+ const ir_js_1 = require("../../chains/query_constructor/ir.cjs");
5
+ const base_js_1 = require("./base.cjs");
6
+ const utils_js_1 = require("./utils.cjs");
7
+ function processValue(value) {
8
+ /** Convert a value to a string and add single quotes if it is a string. */
9
+ if (typeof value === "string") {
10
+ return `'${value}'`;
11
+ }
12
+ else {
13
+ return String(value);
14
+ }
15
+ }
16
+ class VectaraTranslator extends base_js_1.BaseTranslator {
17
+ constructor() {
18
+ super(...arguments);
19
+ Object.defineProperty(this, "allowedOperators", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: [ir_js_1.Operators.and, ir_js_1.Operators.or]
24
+ });
25
+ Object.defineProperty(this, "allowedComparators", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: [
30
+ ir_js_1.Comparators.eq,
31
+ ir_js_1.Comparators.ne,
32
+ ir_js_1.Comparators.lt,
33
+ ir_js_1.Comparators.lte,
34
+ ir_js_1.Comparators.gt,
35
+ ir_js_1.Comparators.gte,
36
+ ]
37
+ });
38
+ }
39
+ formatFunction(func) {
40
+ if (func in ir_js_1.Comparators) {
41
+ if (this.allowedComparators.length > 0 &&
42
+ this.allowedComparators.indexOf(func) === -1) {
43
+ throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
44
+ }
45
+ }
46
+ else if (func in ir_js_1.Operators) {
47
+ if (this.allowedOperators.length > 0 &&
48
+ this.allowedOperators.indexOf(func) === -1) {
49
+ throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
50
+ }
51
+ }
52
+ else {
53
+ throw new Error("Unknown comparator or operator");
54
+ }
55
+ const mapDict = {
56
+ and: " and ",
57
+ or: " or ",
58
+ eq: "=",
59
+ ne: "!=",
60
+ lt: "<",
61
+ lte: "<=",
62
+ gt: ">",
63
+ gte: ">=",
64
+ };
65
+ return mapDict[func];
66
+ }
67
+ /**
68
+ * Visits an operation and returns a VectaraOperationResult. The
69
+ * operation's arguments are visited and the operator is formatted.
70
+ * @param operation The operation to visit.
71
+ * @returns A VectaraOperationResult.
72
+ */
73
+ visitOperation(operation) {
74
+ const args = operation.args?.map((arg) => arg.accept(this));
75
+ const operator = this.formatFunction(operation.operator);
76
+ return `( ${args.join(operator)} )`;
77
+ }
78
+ /**
79
+ * Visits a comparison and returns a VectaraComparisonResult. The
80
+ * comparison's value is checked for type and the comparator is formatted.
81
+ * Throws an error if the value type is not supported.
82
+ * @param comparison The comparison to visit.
83
+ * @returns A VectaraComparisonResult.
84
+ */
85
+ visitComparison(comparison) {
86
+ const comparator = this.formatFunction(comparison.comparator);
87
+ return `( doc.${comparison.attribute} ${comparator} ${processValue(comparison.value)} )`;
88
+ }
89
+ /**
90
+ * Visits a structured query and returns a VectaraStructuredQueryResult.
91
+ * If the query has a filter, it is visited.
92
+ * @param query The structured query to visit.
93
+ * @returns A VectaraStructuredQueryResult.
94
+ */
95
+ visitStructuredQuery(query) {
96
+ let nextArg = {};
97
+ if (query.filter) {
98
+ nextArg = {
99
+ filter: { filter: query.filter.accept(this) },
100
+ };
101
+ }
102
+ return nextArg;
103
+ }
104
+ mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
105
+ if ((0, utils_js_1.isFilterEmpty)(defaultFilter) && (0, utils_js_1.isFilterEmpty)(generatedFilter)) {
106
+ return undefined;
107
+ }
108
+ if ((0, utils_js_1.isFilterEmpty)(defaultFilter) || mergeType === "replace") {
109
+ if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
110
+ return undefined;
111
+ }
112
+ return generatedFilter;
113
+ }
114
+ if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
115
+ if (forceDefaultFilter) {
116
+ return defaultFilter;
117
+ }
118
+ if (mergeType === "and") {
119
+ return undefined;
120
+ }
121
+ return defaultFilter;
122
+ }
123
+ if (mergeType === "and") {
124
+ return {
125
+ filter: `${defaultFilter} and ${generatedFilter}`,
126
+ };
127
+ }
128
+ else if (mergeType === "or") {
129
+ return {
130
+ filter: `${defaultFilter} or ${generatedFilter}`,
131
+ };
132
+ }
133
+ else {
134
+ throw new Error("Unknown merge type");
135
+ }
136
+ }
137
+ }
138
+ exports.VectaraTranslator = VectaraTranslator;
@@ -0,0 +1,41 @@
1
+ import { Comparator, Comparison, Operation, Operator, StructuredQuery } from "../../chains/query_constructor/ir.js";
2
+ import { VectaraFilter, VectaraStore } from "../../vectorstores/vectara.js";
3
+ import { BaseTranslator } from "./base.js";
4
+ export type VectaraVisitorResult = VectaraOperationResult | VectaraComparisonResult | VectaraVisitorStructuredQueryResult;
5
+ export type VectaraOperationResult = String;
6
+ export type VectaraComparisonResult = String;
7
+ export type VectaraVisitorStructuredQueryResult = {
8
+ filter?: {
9
+ filter?: VectaraOperationResult | VectaraComparisonResult;
10
+ };
11
+ };
12
+ export declare class VectaraTranslator<T extends VectaraStore> extends BaseTranslator<T> {
13
+ VisitOperationOutput: VectaraOperationResult;
14
+ VisitComparisonOutput: VectaraComparisonResult;
15
+ allowedOperators: Operator[];
16
+ allowedComparators: Comparator[];
17
+ formatFunction(func: Operator | Comparator): string;
18
+ /**
19
+ * Visits an operation and returns a VectaraOperationResult. The
20
+ * operation's arguments are visited and the operator is formatted.
21
+ * @param operation The operation to visit.
22
+ * @returns A VectaraOperationResult.
23
+ */
24
+ visitOperation(operation: Operation): this["VisitOperationOutput"];
25
+ /**
26
+ * Visits a comparison and returns a VectaraComparisonResult. The
27
+ * comparison's value is checked for type and the comparator is formatted.
28
+ * Throws an error if the value type is not supported.
29
+ * @param comparison The comparison to visit.
30
+ * @returns A VectaraComparisonResult.
31
+ */
32
+ visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
33
+ /**
34
+ * Visits a structured query and returns a VectaraStructuredQueryResult.
35
+ * If the query has a filter, it is visited.
36
+ * @param query The structured query to visit.
37
+ * @returns A VectaraStructuredQueryResult.
38
+ */
39
+ visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
40
+ mergeFilters(defaultFilter: VectaraFilter | undefined, generatedFilter: VectaraFilter | undefined, mergeType?: string, forceDefaultFilter?: boolean): VectaraFilter | undefined;
41
+ }
@@ -0,0 +1,134 @@
1
+ import { Comparators, Operators, } from "../../chains/query_constructor/ir.js";
2
+ import { BaseTranslator } from "./base.js";
3
+ import { isFilterEmpty } from "./utils.js";
4
+ function processValue(value) {
5
+ /** Convert a value to a string and add single quotes if it is a string. */
6
+ if (typeof value === "string") {
7
+ return `'${value}'`;
8
+ }
9
+ else {
10
+ return String(value);
11
+ }
12
+ }
13
+ export class VectaraTranslator extends BaseTranslator {
14
+ constructor() {
15
+ super(...arguments);
16
+ Object.defineProperty(this, "allowedOperators", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: [Operators.and, Operators.or]
21
+ });
22
+ Object.defineProperty(this, "allowedComparators", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: [
27
+ Comparators.eq,
28
+ Comparators.ne,
29
+ Comparators.lt,
30
+ Comparators.lte,
31
+ Comparators.gt,
32
+ Comparators.gte,
33
+ ]
34
+ });
35
+ }
36
+ formatFunction(func) {
37
+ if (func in Comparators) {
38
+ if (this.allowedComparators.length > 0 &&
39
+ this.allowedComparators.indexOf(func) === -1) {
40
+ throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
41
+ }
42
+ }
43
+ else if (func in Operators) {
44
+ if (this.allowedOperators.length > 0 &&
45
+ this.allowedOperators.indexOf(func) === -1) {
46
+ throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
47
+ }
48
+ }
49
+ else {
50
+ throw new Error("Unknown comparator or operator");
51
+ }
52
+ const mapDict = {
53
+ and: " and ",
54
+ or: " or ",
55
+ eq: "=",
56
+ ne: "!=",
57
+ lt: "<",
58
+ lte: "<=",
59
+ gt: ">",
60
+ gte: ">=",
61
+ };
62
+ return mapDict[func];
63
+ }
64
+ /**
65
+ * Visits an operation and returns a VectaraOperationResult. The
66
+ * operation's arguments are visited and the operator is formatted.
67
+ * @param operation The operation to visit.
68
+ * @returns A VectaraOperationResult.
69
+ */
70
+ visitOperation(operation) {
71
+ const args = operation.args?.map((arg) => arg.accept(this));
72
+ const operator = this.formatFunction(operation.operator);
73
+ return `( ${args.join(operator)} )`;
74
+ }
75
+ /**
76
+ * Visits a comparison and returns a VectaraComparisonResult. The
77
+ * comparison's value is checked for type and the comparator is formatted.
78
+ * Throws an error if the value type is not supported.
79
+ * @param comparison The comparison to visit.
80
+ * @returns A VectaraComparisonResult.
81
+ */
82
+ visitComparison(comparison) {
83
+ const comparator = this.formatFunction(comparison.comparator);
84
+ return `( doc.${comparison.attribute} ${comparator} ${processValue(comparison.value)} )`;
85
+ }
86
+ /**
87
+ * Visits a structured query and returns a VectaraStructuredQueryResult.
88
+ * If the query has a filter, it is visited.
89
+ * @param query The structured query to visit.
90
+ * @returns A VectaraStructuredQueryResult.
91
+ */
92
+ visitStructuredQuery(query) {
93
+ let nextArg = {};
94
+ if (query.filter) {
95
+ nextArg = {
96
+ filter: { filter: query.filter.accept(this) },
97
+ };
98
+ }
99
+ return nextArg;
100
+ }
101
+ mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
102
+ if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
103
+ return undefined;
104
+ }
105
+ if (isFilterEmpty(defaultFilter) || mergeType === "replace") {
106
+ if (isFilterEmpty(generatedFilter)) {
107
+ return undefined;
108
+ }
109
+ return generatedFilter;
110
+ }
111
+ if (isFilterEmpty(generatedFilter)) {
112
+ if (forceDefaultFilter) {
113
+ return defaultFilter;
114
+ }
115
+ if (mergeType === "and") {
116
+ return undefined;
117
+ }
118
+ return defaultFilter;
119
+ }
120
+ if (mergeType === "and") {
121
+ return {
122
+ filter: `${defaultFilter} and ${generatedFilter}`,
123
+ };
124
+ }
125
+ else if (mergeType === "or") {
126
+ return {
127
+ filter: `${defaultFilter} or ${generatedFilter}`,
128
+ };
129
+ }
130
+ else {
131
+ throw new Error("Unknown merge type");
132
+ }
133
+ }
134
+ }
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./src/index.cjs"), exports);
17
+ __exportStar(require("langchain-core/runnables"), exports);
@@ -0,0 +1 @@
1
+ export * from "langchain-core/runnables";
@@ -0,0 +1 @@
1
+ export * from "langchain-core/runnables";
@@ -1,44 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MappingDocumentTransformer = exports.BaseDocumentTransformer = void 0;
4
- const index_js_1 = require("./runnable/index.cjs");
5
- /**
6
- * Abstract base class for document transformation systems.
7
- *
8
- * A document transformation system takes an array of Documents and returns an
9
- * array of transformed Documents. These arrays do not necessarily have to have
10
- * the same length.
11
- *
12
- * One example of this is a text splitter that splits a large document into
13
- * many smaller documents.
14
- */
15
- class BaseDocumentTransformer extends index_js_1.Runnable {
16
- constructor() {
17
- super(...arguments);
18
- Object.defineProperty(this, "lc_namespace", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: ["langchain", "document_transformers"]
23
- });
24
- }
25
- /**
26
- * Method to invoke the document transformation. This method calls the
27
- * transformDocuments method with the provided input.
28
- * @param input The input documents to be transformed.
29
- * @param _options Optional configuration object to customize the behavior of callbacks.
30
- * @returns A Promise that resolves to the transformed documents.
31
- */
32
- invoke(input, _options) {
33
- return this.transformDocuments(input);
34
- }
35
- }
36
- exports.BaseDocumentTransformer = BaseDocumentTransformer;
4
+ const document_1 = require("langchain-core/schema/document");
5
+ Object.defineProperty(exports, "BaseDocumentTransformer", { enumerable: true, get: function () { return document_1.BaseDocumentTransformer; } });
37
6
  /**
38
7
  * Class for document transformers that return exactly one transformed document
39
8
  * for each input document.
40
9
  */
41
- class MappingDocumentTransformer extends BaseDocumentTransformer {
10
+ class MappingDocumentTransformer extends document_1.BaseDocumentTransformer {
42
11
  async transformDocuments(documents) {
43
12
  const newDocuments = [];
44
13
  for (const document of documents) {
@@ -1,33 +1,6 @@
1
- import { BaseCallbackConfig } from "../callbacks/manager.js";
1
+ import { BaseDocumentTransformer } from "langchain-core/schema/document";
2
2
  import { Document } from "../document.js";
3
- import { Runnable } from "./runnable/index.js";
4
- /**
5
- * Abstract base class for document transformation systems.
6
- *
7
- * A document transformation system takes an array of Documents and returns an
8
- * array of transformed Documents. These arrays do not necessarily have to have
9
- * the same length.
10
- *
11
- * One example of this is a text splitter that splits a large document into
12
- * many smaller documents.
13
- */
14
- export declare abstract class BaseDocumentTransformer<RunInput extends Document[] = Document[], RunOutput extends Document[] = Document[]> extends Runnable<RunInput, RunOutput> {
15
- lc_namespace: string[];
16
- /**
17
- * Transform a list of documents.
18
- * @param documents A sequence of documents to be transformed.
19
- * @returns A list of transformed documents.
20
- */
21
- abstract transformDocuments(documents: RunInput): Promise<RunOutput>;
22
- /**
23
- * Method to invoke the document transformation. This method calls the
24
- * transformDocuments method with the provided input.
25
- * @param input The input documents to be transformed.
26
- * @param _options Optional configuration object to customize the behavior of callbacks.
27
- * @returns A Promise that resolves to the transformed documents.
28
- */
29
- invoke(input: RunInput, _options?: Partial<BaseCallbackConfig>): Promise<RunOutput>;
30
- }
3
+ export { BaseDocumentTransformer };
31
4
  /**
32
5
  * Class for document transformers that return exactly one transformed document
33
6
  * for each input document.
@@ -1,35 +1,5 @@
1
- import { Runnable } from "./runnable/index.js";
2
- /**
3
- * Abstract base class for document transformation systems.
4
- *
5
- * A document transformation system takes an array of Documents and returns an
6
- * array of transformed Documents. These arrays do not necessarily have to have
7
- * the same length.
8
- *
9
- * One example of this is a text splitter that splits a large document into
10
- * many smaller documents.
11
- */
12
- export class BaseDocumentTransformer extends Runnable {
13
- constructor() {
14
- super(...arguments);
15
- Object.defineProperty(this, "lc_namespace", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: ["langchain", "document_transformers"]
20
- });
21
- }
22
- /**
23
- * Method to invoke the document transformation. This method calls the
24
- * transformDocuments method with the provided input.
25
- * @param input The input documents to be transformed.
26
- * @param _options Optional configuration object to customize the behavior of callbacks.
27
- * @returns A Promise that resolves to the transformed documents.
28
- */
29
- invoke(input, _options) {
30
- return this.transformDocuments(input);
31
- }
32
- }
1
+ import { BaseDocumentTransformer } from "langchain-core/schema/document";
2
+ export { BaseDocumentTransformer };
33
3
  /**
34
4
  * Class for document transformers that return exactly one transformed document
35
5
  * for each input document.