langchain 0.0.76 → 0.0.78

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 (162) hide show
  1. package/chains/query_constructor/ir.cjs +1 -0
  2. package/chains/query_constructor/ir.d.ts +1 -0
  3. package/chains/query_constructor/ir.js +1 -0
  4. package/chains/query_constructor.cjs +1 -0
  5. package/chains/query_constructor.d.ts +1 -0
  6. package/chains/query_constructor.js +1 -0
  7. package/dist/agents/agent.d.ts +5 -4
  8. package/dist/agents/agent_toolkits/sql/sql.cjs +2 -2
  9. package/dist/agents/agent_toolkits/sql/sql.d.ts +1 -1
  10. package/dist/agents/agent_toolkits/sql/sql.js +2 -2
  11. package/dist/agents/chat/index.cjs +3 -3
  12. package/dist/agents/chat/index.d.ts +1 -0
  13. package/dist/agents/chat/index.js +3 -3
  14. package/dist/agents/chat_convo/index.cjs +30 -14
  15. package/dist/agents/chat_convo/index.d.ts +5 -1
  16. package/dist/agents/chat_convo/index.js +31 -15
  17. package/dist/agents/chat_convo/outputParser.cjs +79 -7
  18. package/dist/agents/chat_convo/outputParser.d.ts +25 -13
  19. package/dist/agents/chat_convo/outputParser.js +77 -6
  20. package/dist/agents/chat_convo/prompt.cjs +11 -8
  21. package/dist/agents/chat_convo/prompt.d.ts +2 -2
  22. package/dist/agents/chat_convo/prompt.js +11 -8
  23. package/dist/agents/executor.d.ts +2 -3
  24. package/dist/agents/index.cjs +6 -1
  25. package/dist/agents/index.d.ts +2 -0
  26. package/dist/agents/index.js +2 -0
  27. package/dist/agents/initialize.cjs +19 -13
  28. package/dist/agents/initialize.d.ts +12 -2
  29. package/dist/agents/initialize.js +16 -10
  30. package/dist/agents/mrkl/index.cjs +3 -3
  31. package/dist/agents/mrkl/index.d.ts +1 -0
  32. package/dist/agents/mrkl/index.js +3 -3
  33. package/dist/agents/mrkl/outputParser.cjs +2 -2
  34. package/dist/agents/mrkl/outputParser.js +2 -2
  35. package/dist/agents/structured_chat/index.cjs +106 -0
  36. package/dist/agents/structured_chat/index.d.ts +44 -0
  37. package/dist/agents/structured_chat/index.js +102 -0
  38. package/dist/agents/structured_chat/outputParser.cjs +92 -0
  39. package/dist/agents/structured_chat/outputParser.d.ts +24 -0
  40. package/dist/agents/structured_chat/outputParser.js +87 -0
  41. package/dist/agents/structured_chat/prompt.cjs +62 -0
  42. package/dist/agents/structured_chat/prompt.d.ts +4 -0
  43. package/dist/agents/structured_chat/prompt.js +59 -0
  44. package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
  45. package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
  46. package/dist/callbacks/handlers/tracer_langchain.js +12 -4
  47. package/dist/callbacks/manager.cjs +6 -2
  48. package/dist/callbacks/manager.js +6 -2
  49. package/dist/chains/query_constructor/index.cjs +105 -0
  50. package/dist/chains/query_constructor/index.d.ts +37 -0
  51. package/dist/chains/query_constructor/index.js +95 -0
  52. package/dist/chains/query_constructor/ir.cjs +116 -0
  53. package/dist/chains/query_constructor/ir.d.ts +60 -0
  54. package/dist/chains/query_constructor/ir.js +107 -0
  55. package/dist/chains/query_constructor/parser.cjs +103 -0
  56. package/dist/chains/query_constructor/parser.d.ts +12 -0
  57. package/dist/chains/query_constructor/parser.js +99 -0
  58. package/dist/chains/query_constructor/prompt.cjs +127 -0
  59. package/dist/chains/query_constructor/prompt.d.ts +15 -0
  60. package/dist/chains/query_constructor/prompt.js +124 -0
  61. package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
  62. package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
  63. package/dist/chains/sql_db/sql_db_chain.js +13 -0
  64. package/dist/client/langchainplus.cjs +21 -15
  65. package/dist/client/langchainplus.d.ts +4 -2
  66. package/dist/client/langchainplus.js +21 -15
  67. package/dist/llms/sagemaker_endpoint.cjs +123 -0
  68. package/dist/llms/sagemaker_endpoint.d.ts +82 -0
  69. package/dist/llms/sagemaker_endpoint.js +118 -0
  70. package/dist/memory/buffer_memory.cjs +1 -1
  71. package/dist/memory/buffer_memory.js +1 -1
  72. package/dist/memory/buffer_window_memory.cjs +1 -1
  73. package/dist/memory/buffer_window_memory.js +1 -1
  74. package/dist/output_parsers/expression.cjs +68 -0
  75. package/dist/output_parsers/expression.d.ts +25 -0
  76. package/dist/output_parsers/expression.js +49 -0
  77. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
  78. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
  79. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
  80. package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
  81. package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
  82. package/dist/output_parsers/expression_type_handlers/base.js +62 -0
  83. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
  84. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
  85. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
  86. package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
  87. package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
  88. package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
  89. package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
  90. package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
  91. package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
  92. package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
  93. package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
  94. package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
  95. package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
  96. package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
  97. package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
  98. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
  99. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
  100. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
  101. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
  102. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
  103. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
  104. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
  105. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
  106. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
  107. package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
  108. package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
  109. package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
  110. package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
  111. package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
  112. package/dist/output_parsers/expression_type_handlers/types.js +1 -0
  113. package/dist/output_parsers/index.cjs +2 -1
  114. package/dist/output_parsers/index.d.ts +1 -1
  115. package/dist/output_parsers/index.js +1 -1
  116. package/dist/output_parsers/structured.cjs +81 -23
  117. package/dist/output_parsers/structured.d.ts +18 -0
  118. package/dist/output_parsers/structured.js +79 -22
  119. package/dist/retrievers/self_query/index.cjs +79 -0
  120. package/dist/retrievers/self_query/index.d.ts +33 -0
  121. package/dist/retrievers/self_query/index.js +74 -0
  122. package/dist/retrievers/self_query/translator.cjs +72 -0
  123. package/dist/retrievers/self_query/translator.d.ts +14 -0
  124. package/dist/retrievers/self_query/translator.js +67 -0
  125. package/dist/schema/query_constructor.cjs +26 -0
  126. package/dist/schema/query_constructor.d.ts +6 -0
  127. package/dist/schema/query_constructor.js +22 -0
  128. package/dist/text_splitter.cjs +2 -1
  129. package/dist/text_splitter.js +2 -1
  130. package/dist/tools/dynamic.cjs +39 -1
  131. package/dist/tools/dynamic.d.ts +18 -3
  132. package/dist/tools/dynamic.js +38 -1
  133. package/dist/tools/index.cjs +2 -1
  134. package/dist/tools/index.d.ts +1 -1
  135. package/dist/tools/index.js +1 -1
  136. package/dist/tools/json.cjs +3 -1
  137. package/dist/tools/json.js +3 -1
  138. package/dist/tools/sql.cjs +17 -11
  139. package/dist/tools/sql.d.ts +7 -1
  140. package/dist/tools/sql.js +17 -11
  141. package/dist/util/event-source-parse.cjs +31 -5
  142. package/dist/util/event-source-parse.d.ts +3 -3
  143. package/dist/util/event-source-parse.js +31 -5
  144. package/dist/vectorstores/faiss.cjs +245 -0
  145. package/dist/vectorstores/faiss.d.ts +39 -0
  146. package/dist/vectorstores/faiss.js +218 -0
  147. package/llms/sagemaker_endpoint.cjs +1 -0
  148. package/llms/sagemaker_endpoint.d.ts +1 -0
  149. package/llms/sagemaker_endpoint.js +1 -0
  150. package/output_parsers/expression.cjs +1 -0
  151. package/output_parsers/expression.d.ts +1 -0
  152. package/output_parsers/expression.js +1 -0
  153. package/package.json +79 -3
  154. package/retrievers/self_query.cjs +1 -0
  155. package/retrievers/self_query.d.ts +1 -0
  156. package/retrievers/self_query.js +1 -0
  157. package/schema/query_constructor.cjs +1 -0
  158. package/schema/query_constructor.d.ts +1 -0
  159. package/schema/query_constructor.js +1 -0
  160. package/vectorstores/faiss.cjs +1 -0
  161. package/vectorstores/faiss.d.ts +1 -0
  162. package/vectorstores/faiss.js +1 -0
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BasicTranslator = exports.BaseTranslator = void 0;
4
+ const ir_js_1 = require("../../chains/query_constructor/ir.cjs");
5
+ class BaseTranslator extends ir_js_1.Visitor {
6
+ }
7
+ exports.BaseTranslator = BaseTranslator;
8
+ class BasicTranslator extends BaseTranslator {
9
+ constructor() {
10
+ super(...arguments);
11
+ Object.defineProperty(this, "allowedOperators", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: [ir_js_1.Operators.and, ir_js_1.Operators.or]
16
+ });
17
+ Object.defineProperty(this, "allowedComparators", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: [
22
+ ir_js_1.Comparators.eq,
23
+ ir_js_1.Comparators.neq,
24
+ ir_js_1.Comparators.gt,
25
+ ir_js_1.Comparators.gte,
26
+ ir_js_1.Comparators.lt,
27
+ ir_js_1.Comparators.lte,
28
+ ]
29
+ });
30
+ }
31
+ formatFunction(func) {
32
+ if (func in ir_js_1.Comparators) {
33
+ if (this.allowedComparators.length > 0 &&
34
+ this.allowedComparators.indexOf(func) === -1) {
35
+ throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
36
+ }
37
+ }
38
+ else if (func in ir_js_1.Operators) {
39
+ if (this.allowedOperators.length > 0 &&
40
+ this.allowedOperators.indexOf(func) === -1) {
41
+ throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
42
+ }
43
+ }
44
+ else {
45
+ throw new Error("Unknown comparator or operator");
46
+ }
47
+ return `$${func}`;
48
+ }
49
+ visitOperation(operation) {
50
+ const args = operation.args?.map((arg) => arg.accept(this));
51
+ return {
52
+ [this.formatFunction(operation.operator)]: args,
53
+ };
54
+ }
55
+ visitComparison(comparison) {
56
+ return {
57
+ [comparison.attribute]: {
58
+ [this.formatFunction(comparison.comparator)]: comparison.value,
59
+ },
60
+ };
61
+ }
62
+ visitStructuredQuery(query) {
63
+ let nextArg = {};
64
+ if (query.filter) {
65
+ nextArg = {
66
+ filter: query.filter.accept(this),
67
+ };
68
+ }
69
+ return nextArg;
70
+ }
71
+ }
72
+ exports.BasicTranslator = BasicTranslator;
@@ -0,0 +1,14 @@
1
+ import { Comparator, Comparison, Operation, Operator, StructuredQuery, Visitor, VisitorComparisonResult, VisitorOperationResult, VisitorStructuredQueryResult } from "../../chains/query_constructor/ir.js";
2
+ export declare abstract class BaseTranslator extends Visitor {
3
+ abstract allowedOperators: Operator[];
4
+ abstract allowedComparators: Comparator[];
5
+ abstract formatFunction(func: Operator | Comparator): string;
6
+ }
7
+ export declare class BasicTranslator extends BaseTranslator {
8
+ allowedOperators: Operator[];
9
+ allowedComparators: Comparator[];
10
+ formatFunction(func: Operator | Comparator): string;
11
+ visitOperation(operation: Operation): VisitorOperationResult;
12
+ visitComparison(comparison: Comparison): VisitorComparisonResult;
13
+ visitStructuredQuery(query: StructuredQuery): VisitorStructuredQueryResult;
14
+ }
@@ -0,0 +1,67 @@
1
+ import { Comparators, Operators, Visitor, } from "../../chains/query_constructor/ir.js";
2
+ export class BaseTranslator extends Visitor {
3
+ }
4
+ export class BasicTranslator extends BaseTranslator {
5
+ constructor() {
6
+ super(...arguments);
7
+ Object.defineProperty(this, "allowedOperators", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: [Operators.and, Operators.or]
12
+ });
13
+ Object.defineProperty(this, "allowedComparators", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: [
18
+ Comparators.eq,
19
+ Comparators.neq,
20
+ Comparators.gt,
21
+ Comparators.gte,
22
+ Comparators.lt,
23
+ Comparators.lte,
24
+ ]
25
+ });
26
+ }
27
+ formatFunction(func) {
28
+ if (func in Comparators) {
29
+ if (this.allowedComparators.length > 0 &&
30
+ this.allowedComparators.indexOf(func) === -1) {
31
+ throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
32
+ }
33
+ }
34
+ else if (func in Operators) {
35
+ if (this.allowedOperators.length > 0 &&
36
+ this.allowedOperators.indexOf(func) === -1) {
37
+ throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
38
+ }
39
+ }
40
+ else {
41
+ throw new Error("Unknown comparator or operator");
42
+ }
43
+ return `$${func}`;
44
+ }
45
+ visitOperation(operation) {
46
+ const args = operation.args?.map((arg) => arg.accept(this));
47
+ return {
48
+ [this.formatFunction(operation.operator)]: args,
49
+ };
50
+ }
51
+ visitComparison(comparison) {
52
+ return {
53
+ [comparison.attribute]: {
54
+ [this.formatFunction(comparison.comparator)]: comparison.value,
55
+ },
56
+ };
57
+ }
58
+ visitStructuredQuery(query) {
59
+ let nextArg = {};
60
+ if (query.filter) {
61
+ nextArg = {
62
+ filter: query.filter.accept(this),
63
+ };
64
+ }
65
+ return nextArg;
66
+ }
67
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AttributeInfo = void 0;
4
+ class AttributeInfo {
5
+ constructor(name, type, description) {
6
+ Object.defineProperty(this, "name", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: name
11
+ });
12
+ Object.defineProperty(this, "type", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: type
17
+ });
18
+ Object.defineProperty(this, "description", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: description
23
+ });
24
+ }
25
+ }
26
+ exports.AttributeInfo = AttributeInfo;
@@ -0,0 +1,6 @@
1
+ export declare class AttributeInfo {
2
+ name: string;
3
+ type: string;
4
+ description: string;
5
+ constructor(name: string, type: string, description: string);
6
+ }
@@ -0,0 +1,22 @@
1
+ export class AttributeInfo {
2
+ constructor(name, type, description) {
3
+ Object.defineProperty(this, "name", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: name
8
+ });
9
+ Object.defineProperty(this, "type", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: type
14
+ });
15
+ Object.defineProperty(this, "description", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: description
20
+ });
21
+ }
22
+ }
@@ -80,7 +80,8 @@ class TextSplitter {
80
80
  let total = 0;
81
81
  for (const d of splits) {
82
82
  const _len = d.length;
83
- if (total + _len >= this.chunkSize) {
83
+ if (total + _len + (currentDoc.length > 0 ? separator.length : 0) >
84
+ this.chunkSize) {
84
85
  if (total > this.chunkSize) {
85
86
  console.warn(`Created a chunk of size ${total}, +
86
87
  which is longer than the specified ${this.chunkSize}`);
@@ -77,7 +77,8 @@ export class TextSplitter {
77
77
  let total = 0;
78
78
  for (const d of splits) {
79
79
  const _len = d.length;
80
- if (total + _len >= this.chunkSize) {
80
+ if (total + _len + (currentDoc.length > 0 ? separator.length : 0) >
81
+ this.chunkSize) {
81
82
  if (total > this.chunkSize) {
82
83
  console.warn(`Created a chunk of size ${total}, +
83
84
  which is longer than the specified ${this.chunkSize}`);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamicTool = void 0;
3
+ exports.DynamicStructuredTool = exports.DynamicTool = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
5
  /**
6
6
  * A tool that can be created dynamically from a function, name, and description.
@@ -37,3 +37,41 @@ class DynamicTool extends base_js_1.Tool {
37
37
  }
38
38
  }
39
39
  exports.DynamicTool = DynamicTool;
40
+ class DynamicStructuredTool extends base_js_1.StructuredTool {
41
+ constructor(fields) {
42
+ super(fields);
43
+ Object.defineProperty(this, "name", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: void 0
48
+ });
49
+ Object.defineProperty(this, "description", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ Object.defineProperty(this, "func", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ Object.defineProperty(this, "schema", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
67
+ this.name = fields.name;
68
+ this.description = fields.description;
69
+ this.func = fields.func;
70
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
71
+ this.schema = fields.schema;
72
+ }
73
+ _call(arg, runManager) {
74
+ return this.func(arg, runManager);
75
+ }
76
+ }
77
+ exports.DynamicStructuredTool = DynamicStructuredTool;
@@ -1,13 +1,20 @@
1
+ import { z } from "zod";
1
2
  import { CallbackManagerForToolRun, Callbacks } from "../callbacks/manager.js";
2
- import { Tool } from "./base.js";
3
- export interface DynamicToolInput {
3
+ import { StructuredTool, Tool } from "./base.js";
4
+ export interface BaseDynamicToolInput {
4
5
  name: string;
5
6
  description: string;
6
- func: (input: string, runManager?: CallbackManagerForToolRun) => Promise<string>;
7
7
  returnDirect?: boolean;
8
8
  verbose?: boolean;
9
9
  callbacks?: Callbacks;
10
10
  }
11
+ export interface DynamicToolInput extends BaseDynamicToolInput {
12
+ func: (input: string, runManager?: CallbackManagerForToolRun) => Promise<string>;
13
+ }
14
+ export interface DynamicStructuredToolInput<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends BaseDynamicToolInput {
15
+ func: (input: z.infer<T>, runManager?: CallbackManagerForToolRun) => Promise<string>;
16
+ schema: T;
17
+ }
11
18
  /**
12
19
  * A tool that can be created dynamically from a function, name, and description.
13
20
  */
@@ -19,3 +26,11 @@ export declare class DynamicTool extends Tool {
19
26
  /** @ignore */
20
27
  _call(input: string, runManager?: CallbackManagerForToolRun): Promise<string>;
21
28
  }
29
+ export declare class DynamicStructuredTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends StructuredTool {
30
+ name: string;
31
+ description: string;
32
+ func: DynamicStructuredToolInput["func"];
33
+ schema: T;
34
+ constructor(fields: DynamicStructuredToolInput<T>);
35
+ protected _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun): Promise<string>;
36
+ }
@@ -1,4 +1,4 @@
1
- import { Tool } from "./base.js";
1
+ import { StructuredTool, Tool } from "./base.js";
2
2
  /**
3
3
  * A tool that can be created dynamically from a function, name, and description.
4
4
  */
@@ -33,3 +33,40 @@ export class DynamicTool extends Tool {
33
33
  return this.func(input, runManager);
34
34
  }
35
35
  }
36
+ export class DynamicStructuredTool extends StructuredTool {
37
+ constructor(fields) {
38
+ super(fields);
39
+ Object.defineProperty(this, "name", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "description", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "func", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "schema", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ this.name = fields.name;
64
+ this.description = fields.description;
65
+ this.func = fields.func;
66
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
67
+ this.schema = fields.schema;
68
+ }
69
+ _call(arg, runManager) {
70
+ return this.func(arg, runManager);
71
+ }
72
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WriteFileTool = exports.ReadFileTool = exports.AIPluginTool = exports.Serper = exports.ZapierNLAWrapper = exports.ZapierNLARunAction = exports.VectorStoreQATool = exports.RequestsPostTool = exports.RequestsGetTool = exports.JsonGetValueTool = exports.JsonListKeysTool = exports.JsonSpec = exports.QueryCheckerTool = exports.ListTablesSqlTool = exports.InfoSqlTool = exports.QuerySqlTool = exports.ChainTool = exports.IFTTTWebhook = exports.DynamicTool = exports.StructuredTool = exports.Tool = exports.BingSerpAPI = exports.DadJokeAPI = exports.SerpAPI = void 0;
3
+ exports.WriteFileTool = exports.ReadFileTool = exports.AIPluginTool = exports.Serper = exports.ZapierNLAWrapper = exports.ZapierNLARunAction = exports.VectorStoreQATool = exports.RequestsPostTool = exports.RequestsGetTool = exports.JsonGetValueTool = exports.JsonListKeysTool = exports.JsonSpec = exports.QueryCheckerTool = exports.ListTablesSqlTool = exports.InfoSqlTool = exports.QuerySqlTool = exports.ChainTool = exports.IFTTTWebhook = exports.DynamicStructuredTool = exports.DynamicTool = exports.StructuredTool = exports.Tool = exports.BingSerpAPI = exports.DadJokeAPI = exports.SerpAPI = void 0;
4
4
  var serpapi_js_1 = require("./serpapi.cjs");
5
5
  Object.defineProperty(exports, "SerpAPI", { enumerable: true, get: function () { return serpapi_js_1.SerpAPI; } });
6
6
  var dadjokeapi_js_1 = require("./dadjokeapi.cjs");
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "Tool", { enumerable: true, get: function () { re
12
12
  Object.defineProperty(exports, "StructuredTool", { enumerable: true, get: function () { return base_js_1.StructuredTool; } });
13
13
  var dynamic_js_1 = require("./dynamic.cjs");
14
14
  Object.defineProperty(exports, "DynamicTool", { enumerable: true, get: function () { return dynamic_js_1.DynamicTool; } });
15
+ Object.defineProperty(exports, "DynamicStructuredTool", { enumerable: true, get: function () { return dynamic_js_1.DynamicStructuredTool; } });
15
16
  var IFTTTWebhook_js_1 = require("./IFTTTWebhook.cjs");
16
17
  Object.defineProperty(exports, "IFTTTWebhook", { enumerable: true, get: function () { return IFTTTWebhook_js_1.IFTTTWebhook; } });
17
18
  var chain_js_1 = require("./chain.cjs");
@@ -2,7 +2,7 @@ export { SerpAPI, SerpAPIParameters } from "./serpapi.js";
2
2
  export { DadJokeAPI } from "./dadjokeapi.js";
3
3
  export { BingSerpAPI } from "./bingserpapi.js";
4
4
  export { Tool, ToolParams, StructuredTool } from "./base.js";
5
- export { DynamicTool, DynamicToolInput } from "./dynamic.js";
5
+ export { DynamicTool, DynamicToolInput, DynamicStructuredTool, DynamicStructuredToolInput, } from "./dynamic.js";
6
6
  export { IFTTTWebhook } from "./IFTTTWebhook.js";
7
7
  export { ChainTool, ChainToolInput } from "./chain.js";
8
8
  export { QuerySqlTool, InfoSqlTool, ListTablesSqlTool, QueryCheckerTool, } from "./sql.js";
@@ -2,7 +2,7 @@ export { SerpAPI } from "./serpapi.js";
2
2
  export { DadJokeAPI } from "./dadjokeapi.js";
3
3
  export { BingSerpAPI } from "./bingserpapi.js";
4
4
  export { Tool, StructuredTool } from "./base.js";
5
- export { DynamicTool } from "./dynamic.js";
5
+ export { DynamicTool, DynamicStructuredTool, } from "./dynamic.js";
6
6
  export { IFTTTWebhook } from "./IFTTTWebhook.js";
7
7
  export { ChainTool } from "./chain.js";
8
8
  export { QuerySqlTool, InfoSqlTool, ListTablesSqlTool, QueryCheckerTool, } from "./sql.js";
@@ -27,7 +27,9 @@ class JsonSpec {
27
27
  const pointer = jsonpointer_1.default.compile(input);
28
28
  const res = pointer.get(this.obj);
29
29
  if (typeof res === "object" && !Array.isArray(res) && res !== null) {
30
- return Object.keys(res).join(", ");
30
+ return Object.keys(res)
31
+ .map((i) => i.replaceAll("~", "~0").replaceAll("/", "~1"))
32
+ .join(", ");
31
33
  }
32
34
  throw new Error(`Value at ${input} is not a dictionary, get the value directly instead.`);
33
35
  }
@@ -21,7 +21,9 @@ export class JsonSpec {
21
21
  const pointer = jsonpointer.compile(input);
22
22
  const res = pointer.get(this.obj);
23
23
  if (typeof res === "object" && !Array.isArray(res) && res !== null) {
24
- return Object.keys(res).join(", ");
24
+ return Object.keys(res)
25
+ .map((i) => i.replaceAll("~", "~0").replaceAll("/", "~1"))
26
+ .join(", ");
25
27
  }
26
28
  throw new Error(`Value at ${input} is not a dictionary, get the value directly instead.`);
27
29
  }
@@ -25,7 +25,7 @@ class QuerySqlTool extends base_js_1.Tool {
25
25
  configurable: true,
26
26
  writable: true,
27
27
  value: `Input to this tool is a detailed and correct SQL query, output is a result from the database.
28
- If the query is not correct, an error message will be returned.
28
+ If the query is not correct, an error message will be returned.
29
29
  If an error is returned, rewrite the query, check the query, and try again.`
30
30
  });
31
31
  this.db = db;
@@ -62,7 +62,7 @@ class InfoSqlTool extends base_js_1.Tool {
62
62
  writable: true,
63
63
  value: `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
64
64
  Be sure that the tables actually exist by calling list-tables-sql first!
65
-
65
+
66
66
  Example Input: "table1, table2, table3.`
67
67
  });
68
68
  this.db = db;
@@ -115,7 +115,7 @@ class ListTablesSqlTool extends base_js_1.Tool {
115
115
  }
116
116
  exports.ListTablesSqlTool = ListTablesSqlTool;
117
117
  class QueryCheckerTool extends base_js_1.Tool {
118
- constructor(llmChain) {
118
+ constructor(llmChainOrOptions) {
119
119
  super();
120
120
  Object.defineProperty(this, "name", {
121
121
  enumerable: true,
@@ -154,16 +154,22 @@ If there are any of the above mistakes, rewrite the query. If there are no mista
154
154
  value: `Use this tool to double check if your query is correct before executing it.
155
155
  Always use this tool before executing a query with query-sql!`
156
156
  });
157
- if (llmChain) {
158
- this.llmChain = llmChain;
157
+ if (typeof llmChainOrOptions?._chainType === "function") {
158
+ this.llmChain = llmChainOrOptions;
159
159
  }
160
160
  else {
161
- const model = new openai_js_1.OpenAI({ temperature: 0 });
162
- const prompt = new prompt_js_1.PromptTemplate({
163
- template: this.template,
164
- inputVariables: ["query"],
165
- });
166
- this.llmChain = new llm_chain_js_1.LLMChain({ llm: model, prompt });
161
+ const options = llmChainOrOptions;
162
+ if (options?.llmChain !== undefined) {
163
+ this.llmChain = options.llmChain;
164
+ }
165
+ else {
166
+ const prompt = new prompt_js_1.PromptTemplate({
167
+ template: this.template,
168
+ inputVariables: ["query"],
169
+ });
170
+ const llm = options?.llm ?? new openai_js_1.OpenAI({ temperature: 0 });
171
+ this.llmChain = new llm_chain_js_1.LLMChain({ llm, prompt });
172
+ }
167
173
  }
168
174
  }
169
175
  /** @ignore */
@@ -1,6 +1,7 @@
1
1
  import { Tool } from "./base.js";
2
2
  import { LLMChain } from "../chains/llm_chain.js";
3
3
  import type { SqlDatabase } from "../sql_db.js";
4
+ import { BaseLanguageModel } from "../base_language/index.js";
4
5
  interface SqlTool {
5
6
  db: SqlDatabase;
6
7
  }
@@ -28,11 +29,16 @@ export declare class ListTablesSqlTool extends Tool implements SqlTool {
28
29
  _call(_: string): Promise<string>;
29
30
  description: string;
30
31
  }
32
+ type QueryCheckerToolArgs = {
33
+ llmChain?: LLMChain;
34
+ llm?: BaseLanguageModel;
35
+ _chainType?: never;
36
+ };
31
37
  export declare class QueryCheckerTool extends Tool {
32
38
  name: string;
33
39
  template: string;
34
40
  llmChain: LLMChain;
35
- constructor(llmChain?: LLMChain);
41
+ constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs);
36
42
  /** @ignore */
37
43
  _call(input: string): Promise<string>;
38
44
  description: string;
package/dist/tools/sql.js CHANGED
@@ -22,7 +22,7 @@ export class QuerySqlTool extends Tool {
22
22
  configurable: true,
23
23
  writable: true,
24
24
  value: `Input to this tool is a detailed and correct SQL query, output is a result from the database.
25
- If the query is not correct, an error message will be returned.
25
+ If the query is not correct, an error message will be returned.
26
26
  If an error is returned, rewrite the query, check the query, and try again.`
27
27
  });
28
28
  this.db = db;
@@ -58,7 +58,7 @@ export class InfoSqlTool extends Tool {
58
58
  writable: true,
59
59
  value: `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
60
60
  Be sure that the tables actually exist by calling list-tables-sql first!
61
-
61
+
62
62
  Example Input: "table1, table2, table3.`
63
63
  });
64
64
  this.db = db;
@@ -109,7 +109,7 @@ export class ListTablesSqlTool extends Tool {
109
109
  }
110
110
  }
111
111
  export class QueryCheckerTool extends Tool {
112
- constructor(llmChain) {
112
+ constructor(llmChainOrOptions) {
113
113
  super();
114
114
  Object.defineProperty(this, "name", {
115
115
  enumerable: true,
@@ -148,16 +148,22 @@ If there are any of the above mistakes, rewrite the query. If there are no mista
148
148
  value: `Use this tool to double check if your query is correct before executing it.
149
149
  Always use this tool before executing a query with query-sql!`
150
150
  });
151
- if (llmChain) {
152
- this.llmChain = llmChain;
151
+ if (typeof llmChainOrOptions?._chainType === "function") {
152
+ this.llmChain = llmChainOrOptions;
153
153
  }
154
154
  else {
155
- const model = new OpenAI({ temperature: 0 });
156
- const prompt = new PromptTemplate({
157
- template: this.template,
158
- inputVariables: ["query"],
159
- });
160
- this.llmChain = new LLMChain({ llm: model, prompt });
155
+ const options = llmChainOrOptions;
156
+ if (options?.llmChain !== undefined) {
157
+ this.llmChain = options.llmChain;
158
+ }
159
+ else {
160
+ const prompt = new PromptTemplate({
161
+ template: this.template,
162
+ inputVariables: ["query"],
163
+ });
164
+ const llm = options?.llm ?? new OpenAI({ temperature: 0 });
165
+ this.llmChain = new LLMChain({ llm, prompt });
166
+ }
161
167
  }
162
168
  }
163
169
  /** @ignore */
@@ -16,9 +16,18 @@ exports.EventStreamContentType = "text/event-stream";
16
16
  */
17
17
  async function getBytes(stream, onChunk) {
18
18
  const reader = stream.getReader();
19
- let result;
20
- // eslint-disable-next-line no-cond-assign
21
- while (!(result = await reader.read()).done) {
19
+ // CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
20
+ // This change is essential to ensure that we capture every last piece of information from streams,
21
+ // such as those from Azure OpenAI, which may not terminate with a blank line. Without this
22
+ // mechanism, we risk ignoring a possibly significant last message.
23
+ // See https://github.com/hwchase17/langchainjs/issues/1299 for details.
24
+ // eslint-disable-next-line no-constant-condition
25
+ while (true) {
26
+ const result = await reader.read();
27
+ if (result.done) {
28
+ onChunk(new Uint8Array(), true);
29
+ break;
30
+ }
22
31
  onChunk(result.value);
23
32
  }
24
33
  }
@@ -35,7 +44,11 @@ function getLines(onLine) {
35
44
  let fieldLength; // length of the `field` portion of the line
36
45
  let discardTrailingNewline = false;
37
46
  // return a function that can process each incoming byte chunk:
38
- return function onChunk(arr) {
47
+ return function onChunk(arr, flush) {
48
+ if (flush) {
49
+ onLine(arr, 0, true);
50
+ return;
51
+ }
39
52
  if (buffer === undefined) {
40
53
  buffer = arr;
41
54
  position = 0;
@@ -107,7 +120,14 @@ function getMessages(onMessage, onId, onRetry) {
107
120
  let message = newMessage();
108
121
  const decoder = new TextDecoder();
109
122
  // return a function that can process each incoming line buffer:
110
- return function onLine(line, fieldLength) {
123
+ return function onLine(line, fieldLength, flush) {
124
+ if (flush) {
125
+ if (!isEmpty(message)) {
126
+ onMessage?.(message);
127
+ message = newMessage();
128
+ }
129
+ return;
130
+ }
111
131
  if (line.length === 0) {
112
132
  // empty line denotes end of message. Trigger the callback and start a new message:
113
133
  onMessage?.(message);
@@ -163,3 +183,9 @@ function newMessage() {
163
183
  retry: undefined,
164
184
  };
165
185
  }
186
+ function isEmpty(message) {
187
+ return (message.data === "" &&
188
+ message.event === "" &&
189
+ message.id === "" &&
190
+ message.retry === undefined);
191
+ }
@@ -19,14 +19,14 @@ export interface EventSourceMessage {
19
19
  * @param onChunk A function that will be called on each new byte chunk in the stream.
20
20
  * @returns {Promise<void>} A promise that will be resolved when the stream closes.
21
21
  */
22
- export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void): Promise<void>;
22
+ export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array, flush?: boolean) => void): Promise<void>;
23
23
  /**
24
24
  * Parses arbitary byte chunks into EventSource line buffers.
25
25
  * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
26
26
  * @param onLine A function that will be called on each new EventSource line.
27
27
  * @returns A function that should be called for each incoming byte chunk.
28
28
  */
29
- export declare function getLines(onLine: (line: Uint8Array, fieldLength: number) => void): (arr: Uint8Array) => void;
29
+ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void): (arr: Uint8Array, flush?: boolean) => void;
30
30
  /**
31
31
  * Parses line buffers into EventSourceMessages.
32
32
  * @param onId A function that will be called on each `id` field.
@@ -34,4 +34,4 @@ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number)
34
34
  * @param onMessage A function that will be called on each message.
35
35
  * @returns A function that should be called for each incoming line buffer.
36
36
  */
37
- export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number) => void;
37
+ export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;