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,32 @@
1
+ import { NodeHandler, ASTParser } from "./base.js";
2
+ export class PropertyAssignmentHandler extends NodeHandler {
3
+ async accepts(node) {
4
+ if (ASTParser.isProperty(node)) {
5
+ return node;
6
+ }
7
+ else {
8
+ return false;
9
+ }
10
+ }
11
+ async handle(node) {
12
+ if (!this.parentHandler) {
13
+ throw new Error("ArrayLiteralExpressionHandler must have a parent handler");
14
+ }
15
+ let name;
16
+ if (ASTParser.isIdentifier(node.key)) {
17
+ name = node.key.name;
18
+ }
19
+ else if (ASTParser.isStringLiteral(node.key)) {
20
+ name = node.key.value;
21
+ }
22
+ else {
23
+ throw new Error("Invalid property key type");
24
+ }
25
+ if (!name) {
26
+ throw new Error("Invalid property key");
27
+ }
28
+ const identifier = `${name}`.replace(/^["'](.+(?=["']$))["']$/, "$1");
29
+ const value = await this.parentHandler.handle(node.value);
30
+ return { type: "property_assignment", identifier, value };
31
+ }
32
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringLiteralHandler = void 0;
4
+ const base_js_1 = require("./base.cjs");
5
+ class StringLiteralHandler extends base_js_1.NodeHandler {
6
+ async accepts(node) {
7
+ if (base_js_1.ASTParser.isStringLiteral(node)) {
8
+ return node;
9
+ }
10
+ else {
11
+ return false;
12
+ }
13
+ }
14
+ async handle(node) {
15
+ if (!this.parentHandler) {
16
+ throw new Error("ArrayLiteralExpressionHandler must have a parent handler");
17
+ }
18
+ const text = `${node.value}`.replace(/^["'](.+(?=["']$))["']$/, "$1");
19
+ return { type: "string_literal", value: text };
20
+ }
21
+ }
22
+ exports.StringLiteralHandler = StringLiteralHandler;
@@ -0,0 +1,7 @@
1
+ import type { ESTree } from "meriyah";
2
+ import { NodeHandler } from "./base.js";
3
+ import { StringLiteralType } from "./types.js";
4
+ export declare class StringLiteralHandler extends NodeHandler {
5
+ accepts(node: ESTree.Node): Promise<ESTree.Literal | boolean>;
6
+ handle(node: ESTree.Literal): Promise<StringLiteralType>;
7
+ }
@@ -0,0 +1,18 @@
1
+ import { NodeHandler, ASTParser } from "./base.js";
2
+ export class StringLiteralHandler extends NodeHandler {
3
+ async accepts(node) {
4
+ if (ASTParser.isStringLiteral(node)) {
5
+ return node;
6
+ }
7
+ else {
8
+ return false;
9
+ }
10
+ }
11
+ async handle(node) {
12
+ if (!this.parentHandler) {
13
+ throw new Error("ArrayLiteralExpressionHandler must have a parent handler");
14
+ }
15
+ const text = `${node.value}`.replace(/^["'](.+(?=["']$))["']$/, "$1");
16
+ return { type: "string_literal", value: text };
17
+ }
18
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,41 @@
1
+ export type ArgumentsType = IdentifierType | StringLiteralType | NumericLiteralType | ArrayLiteralType | ObjectLiteralType | CallExpressionType | BooleanLiteralType;
2
+ export type ParsedType = ArgumentsType | PropertyAssignmentType;
3
+ export type ArrayLiteralType = {
4
+ type: "array_literal";
5
+ values: ArgumentsType[];
6
+ };
7
+ export type BooleanLiteralType = {
8
+ type: "boolean_literal";
9
+ value: boolean;
10
+ };
11
+ export type CallExpressionType = {
12
+ type: "call_expression";
13
+ funcCall: string | MemberExpressionType;
14
+ args?: ArgumentsType[];
15
+ };
16
+ export type NumericLiteralType = {
17
+ type: "numeric_literal";
18
+ value: number;
19
+ };
20
+ export type ObjectLiteralType = {
21
+ type: "object_literal";
22
+ values: PropertyAssignmentType[];
23
+ };
24
+ export type MemberExpressionType = {
25
+ type: "member_expression";
26
+ identifier: string;
27
+ property: string;
28
+ };
29
+ export type PropertyAssignmentType = {
30
+ type: "property_assignment";
31
+ identifier: string;
32
+ value: ArgumentsType;
33
+ };
34
+ export type StringLiteralType = {
35
+ type: "string_literal";
36
+ value: string;
37
+ };
38
+ export type IdentifierType = {
39
+ type: "identifier";
40
+ value: string;
41
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomListOutputParser = exports.RouterOutputParser = exports.CombiningOutputParser = exports.OutputFixingParser = exports.JsonMarkdownStructuredOutputParser = exports.StructuredOutputParser = exports.RegexParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
3
+ exports.CustomListOutputParser = exports.RouterOutputParser = exports.CombiningOutputParser = exports.OutputFixingParser = exports.JsonMarkdownStructuredOutputParser = exports.AsymmetricStructuredOutputParser = exports.StructuredOutputParser = exports.RegexParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
4
4
  var list_js_1 = require("./list.cjs");
5
5
  Object.defineProperty(exports, "ListOutputParser", { enumerable: true, get: function () { return list_js_1.ListOutputParser; } });
6
6
  Object.defineProperty(exports, "CommaSeparatedListOutputParser", { enumerable: true, get: function () { return list_js_1.CommaSeparatedListOutputParser; } });
@@ -8,6 +8,7 @@ var regex_js_1 = require("./regex.cjs");
8
8
  Object.defineProperty(exports, "RegexParser", { enumerable: true, get: function () { return regex_js_1.RegexParser; } });
9
9
  var structured_js_1 = require("./structured.cjs");
10
10
  Object.defineProperty(exports, "StructuredOutputParser", { enumerable: true, get: function () { return structured_js_1.StructuredOutputParser; } });
11
+ Object.defineProperty(exports, "AsymmetricStructuredOutputParser", { enumerable: true, get: function () { return structured_js_1.AsymmetricStructuredOutputParser; } });
11
12
  Object.defineProperty(exports, "JsonMarkdownStructuredOutputParser", { enumerable: true, get: function () { return structured_js_1.JsonMarkdownStructuredOutputParser; } });
12
13
  var fix_js_1 = require("./fix.cjs");
13
14
  Object.defineProperty(exports, "OutputFixingParser", { enumerable: true, get: function () { return fix_js_1.OutputFixingParser; } });
@@ -1,6 +1,6 @@
1
1
  export { ListOutputParser, CommaSeparatedListOutputParser } from "./list.js";
2
2
  export { RegexParser } from "./regex.js";
3
- export { StructuredOutputParser, JsonMarkdownStructuredOutputParser, JsonMarkdownFormatInstructionsOptions, JsonMarkdownStructuredOutputParserInput, } from "./structured.js";
3
+ export { StructuredOutputParser, AsymmetricStructuredOutputParser, JsonMarkdownStructuredOutputParser, JsonMarkdownFormatInstructionsOptions, JsonMarkdownStructuredOutputParserInput, } from "./structured.js";
4
4
  export { OutputFixingParser } from "./fix.js";
5
5
  export { CombiningOutputParser } from "./combining.js";
6
6
  export { RouterOutputParser, RouterOutputParserInput } from "./router.js";
@@ -1,6 +1,6 @@
1
1
  export { ListOutputParser, CommaSeparatedListOutputParser } from "./list.js";
2
2
  export { RegexParser } from "./regex.js";
3
- export { StructuredOutputParser, JsonMarkdownStructuredOutputParser, } from "./structured.js";
3
+ export { StructuredOutputParser, AsymmetricStructuredOutputParser, JsonMarkdownStructuredOutputParser, } from "./structured.js";
4
4
  export { OutputFixingParser } from "./fix.js";
5
5
  export { CombiningOutputParser } from "./combining.js";
6
6
  export { RouterOutputParser } from "./router.js";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonMarkdownStructuredOutputParser = exports.StructuredOutputParser = void 0;
3
+ exports.AsymmetricStructuredOutputParser = exports.JsonMarkdownStructuredOutputParser = exports.StructuredOutputParser = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const zod_to_json_schema_1 = require("zod-to-json-schema");
6
6
  const output_parser_js_1 = require("../schema/output_parser.cjs");
@@ -63,31 +63,89 @@ class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
63
63
  }
64
64
  _schemaToInstruction(schemaInput, indent = 2) {
65
65
  const schema = schemaInput;
66
- let nullable = false;
67
- if (Array.isArray(schema.type)) {
68
- const [actualType, nullStr] = schema.type;
69
- nullable = nullStr === "null";
70
- schema.type = actualType;
71
- }
72
- if (schema.type === "object" && schema.properties) {
66
+ if ("type" in schema) {
67
+ let nullable = false;
68
+ let type;
69
+ if (Array.isArray(schema.type)) {
70
+ const nullIdx = schema.type.findIndex((type) => type === "null");
71
+ if (nullIdx !== -1) {
72
+ nullable = true;
73
+ schema.type.splice(nullIdx, 1);
74
+ }
75
+ type = schema.type.join(" | ");
76
+ }
77
+ else {
78
+ type = schema.type;
79
+ }
80
+ if (schema.type === "object" && schema.properties) {
81
+ const description = schema.description
82
+ ? ` // ${schema.description}`
83
+ : "";
84
+ const properties = Object.entries(schema.properties)
85
+ .map(([key, value]) => {
86
+ const isOptional = schema.required?.includes(key)
87
+ ? ""
88
+ : " (optional)";
89
+ return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
90
+ })
91
+ .join("\n");
92
+ return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
93
+ }
94
+ if (schema.type === "array" && schema.items) {
95
+ const description = schema.description
96
+ ? ` // ${schema.description}`
97
+ : "";
98
+ return `array[\n${" ".repeat(indent)}${this._schemaToInstruction(schema.items, indent + 2)}\n${" ".repeat(indent - 2)}] ${description}`;
99
+ }
100
+ const isNullable = nullable ? " (nullable)" : "";
73
101
  const description = schema.description ? ` // ${schema.description}` : "";
74
- const properties = Object.entries(schema.properties)
75
- .map(([key, value]) => {
76
- const isOptional = schema.required?.includes(key)
77
- ? ""
78
- : " (optional)";
79
- return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
80
- })
81
- .join("\n");
82
- return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
102
+ return `${type}${description}${isNullable}`;
83
103
  }
84
- if (schema.type === "array" && schema.items) {
85
- const description = schema.description ? ` // ${schema.description}` : "";
86
- return `array[\n${" ".repeat(indent)}${this._schemaToInstruction(schema.items)}\n${" ".repeat(indent - 2)}] ${description}`;
104
+ if ("anyOf" in schema) {
105
+ return schema.anyOf
106
+ .map((s) => this._schemaToInstruction(s, indent))
107
+ .join(`\n${" ".repeat(indent - 2)}`);
87
108
  }
88
- const isNullable = nullable ? " (nullable)" : "";
89
- const description = schema.description ? ` // ${schema.description}` : "";
90
- return `${schema.type}${description}${isNullable}`;
109
+ throw new Error("unsupported schema type");
110
+ }
111
+ static fromZodSchema(schema) {
112
+ return new this(schema);
113
+ }
114
+ static fromNamesAndDescriptions(schemas) {
115
+ const zodSchema = zod_1.z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, zod_1.z.string().describe(description)])));
116
+ return new this(zodSchema);
91
117
  }
92
118
  }
93
119
  exports.JsonMarkdownStructuredOutputParser = JsonMarkdownStructuredOutputParser;
120
+ class AsymmetricStructuredOutputParser extends output_parser_js_1.BaseOutputParser {
121
+ constructor(inputSchema) {
122
+ super();
123
+ Object.defineProperty(this, "inputSchema", {
124
+ enumerable: true,
125
+ configurable: true,
126
+ writable: true,
127
+ value: inputSchema
128
+ });
129
+ Object.defineProperty(this, "structuredInputParser", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: void 0
134
+ });
135
+ this.structuredInputParser = new JsonMarkdownStructuredOutputParser(inputSchema);
136
+ }
137
+ async parse(text) {
138
+ let parsedInput;
139
+ try {
140
+ parsedInput = await this.structuredInputParser.parse(text);
141
+ }
142
+ catch (e) {
143
+ throw new output_parser_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
144
+ }
145
+ return this.outputProcessor(parsedInput);
146
+ }
147
+ getFormatInstructions() {
148
+ return this.structuredInputParser.getFormatInstructions();
149
+ }
150
+ }
151
+ exports.AsymmetricStructuredOutputParser = AsymmetricStructuredOutputParser;
@@ -25,4 +25,22 @@ export declare class StructuredOutputParser<T extends z.ZodTypeAny> extends Base
25
25
  export declare class JsonMarkdownStructuredOutputParser<T extends z.ZodTypeAny> extends StructuredOutputParser<T> {
26
26
  getFormatInstructions(options?: JsonMarkdownFormatInstructionsOptions): string;
27
27
  private _schemaToInstruction;
28
+ static fromZodSchema<T extends z.ZodTypeAny>(schema: T): JsonMarkdownStructuredOutputParser<T>;
29
+ static fromNamesAndDescriptions<S extends {
30
+ [key: string]: string;
31
+ }>(schemas: S): JsonMarkdownStructuredOutputParser<z.ZodObject<{
32
+ [k: string]: z.ZodString;
33
+ }, "strip", z.ZodTypeAny, {
34
+ [x: string]: string;
35
+ }, {
36
+ [x: string]: string;
37
+ }>>;
38
+ }
39
+ export declare abstract class AsymmetricStructuredOutputParser<T extends z.ZodTypeAny, Y = unknown> extends BaseOutputParser<Y> {
40
+ inputSchema: T;
41
+ private structuredInputParser;
42
+ constructor(inputSchema: T);
43
+ abstract outputProcessor(input: z.infer<T>): Promise<Y>;
44
+ parse(text: string): Promise<Y>;
45
+ getFormatInstructions(): string;
28
46
  }
@@ -59,30 +59,87 @@ export class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
59
59
  }
60
60
  _schemaToInstruction(schemaInput, indent = 2) {
61
61
  const schema = schemaInput;
62
- let nullable = false;
63
- if (Array.isArray(schema.type)) {
64
- const [actualType, nullStr] = schema.type;
65
- nullable = nullStr === "null";
66
- schema.type = actualType;
67
- }
68
- if (schema.type === "object" && schema.properties) {
62
+ if ("type" in schema) {
63
+ let nullable = false;
64
+ let type;
65
+ if (Array.isArray(schema.type)) {
66
+ const nullIdx = schema.type.findIndex((type) => type === "null");
67
+ if (nullIdx !== -1) {
68
+ nullable = true;
69
+ schema.type.splice(nullIdx, 1);
70
+ }
71
+ type = schema.type.join(" | ");
72
+ }
73
+ else {
74
+ type = schema.type;
75
+ }
76
+ if (schema.type === "object" && schema.properties) {
77
+ const description = schema.description
78
+ ? ` // ${schema.description}`
79
+ : "";
80
+ const properties = Object.entries(schema.properties)
81
+ .map(([key, value]) => {
82
+ const isOptional = schema.required?.includes(key)
83
+ ? ""
84
+ : " (optional)";
85
+ return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
86
+ })
87
+ .join("\n");
88
+ return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
89
+ }
90
+ if (schema.type === "array" && schema.items) {
91
+ const description = schema.description
92
+ ? ` // ${schema.description}`
93
+ : "";
94
+ return `array[\n${" ".repeat(indent)}${this._schemaToInstruction(schema.items, indent + 2)}\n${" ".repeat(indent - 2)}] ${description}`;
95
+ }
96
+ const isNullable = nullable ? " (nullable)" : "";
69
97
  const description = schema.description ? ` // ${schema.description}` : "";
70
- const properties = Object.entries(schema.properties)
71
- .map(([key, value]) => {
72
- const isOptional = schema.required?.includes(key)
73
- ? ""
74
- : " (optional)";
75
- return `${" ".repeat(indent)}"${key}": ${this._schemaToInstruction(value, indent + 2)}${isOptional}`;
76
- })
77
- .join("\n");
78
- return `{\n${properties}\n${" ".repeat(indent - 2)}}${description}`;
98
+ return `${type}${description}${isNullable}`;
79
99
  }
80
- if (schema.type === "array" && schema.items) {
81
- const description = schema.description ? ` // ${schema.description}` : "";
82
- return `array[\n${" ".repeat(indent)}${this._schemaToInstruction(schema.items)}\n${" ".repeat(indent - 2)}] ${description}`;
100
+ if ("anyOf" in schema) {
101
+ return schema.anyOf
102
+ .map((s) => this._schemaToInstruction(s, indent))
103
+ .join(`\n${" ".repeat(indent - 2)}`);
83
104
  }
84
- const isNullable = nullable ? " (nullable)" : "";
85
- const description = schema.description ? ` // ${schema.description}` : "";
86
- return `${schema.type}${description}${isNullable}`;
105
+ throw new Error("unsupported schema type");
106
+ }
107
+ static fromZodSchema(schema) {
108
+ return new this(schema);
109
+ }
110
+ static fromNamesAndDescriptions(schemas) {
111
+ const zodSchema = z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, z.string().describe(description)])));
112
+ return new this(zodSchema);
113
+ }
114
+ }
115
+ export class AsymmetricStructuredOutputParser extends BaseOutputParser {
116
+ constructor(inputSchema) {
117
+ super();
118
+ Object.defineProperty(this, "inputSchema", {
119
+ enumerable: true,
120
+ configurable: true,
121
+ writable: true,
122
+ value: inputSchema
123
+ });
124
+ Object.defineProperty(this, "structuredInputParser", {
125
+ enumerable: true,
126
+ configurable: true,
127
+ writable: true,
128
+ value: void 0
129
+ });
130
+ this.structuredInputParser = new JsonMarkdownStructuredOutputParser(inputSchema);
131
+ }
132
+ async parse(text) {
133
+ let parsedInput;
134
+ try {
135
+ parsedInput = await this.structuredInputParser.parse(text);
136
+ }
137
+ catch (e) {
138
+ throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
139
+ }
140
+ return this.outputProcessor(parsedInput);
141
+ }
142
+ getFormatInstructions() {
143
+ return this.structuredInputParser.getFormatInstructions();
87
144
  }
88
145
  }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SelfQueryRetriever = exports.BasicTranslator = exports.BaseTranslator = void 0;
4
+ const index_js_1 = require("../../chains/query_constructor/index.cjs");
5
+ const index_js_2 = require("../../schema/index.cjs");
6
+ const translator_js_1 = require("./translator.cjs");
7
+ Object.defineProperty(exports, "BaseTranslator", { enumerable: true, get: function () { return translator_js_1.BaseTranslator; } });
8
+ Object.defineProperty(exports, "BasicTranslator", { enumerable: true, get: function () { return translator_js_1.BasicTranslator; } });
9
+ class SelfQueryRetriever extends index_js_2.BaseRetriever {
10
+ constructor(options) {
11
+ super();
12
+ Object.defineProperty(this, "vectorStore", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "llmChain", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "verbose", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "structuredQueryTranslator", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "searchParams", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: { k: 4 }
41
+ });
42
+ this.vectorStore = options.vectorStore;
43
+ this.llmChain = options.llmChain;
44
+ this.verbose = options.verbose ?? false;
45
+ this.searchParams = options.searchParams ?? this.searchParams;
46
+ this.structuredQueryTranslator = options.structuredQueryTranslator;
47
+ }
48
+ async getRelevantDocuments(query) {
49
+ const { [this.llmChain.outputKey]: output } = await this.llmChain.call({
50
+ [this.llmChain.inputKeys[0]]: query,
51
+ });
52
+ const nextArg = this.structuredQueryTranslator.visitStructuredQuery(output);
53
+ if (nextArg.filter) {
54
+ return this.vectorStore.similaritySearch(query, this.searchParams?.k, nextArg.filter);
55
+ }
56
+ else {
57
+ return this.vectorStore.similaritySearch(query, this.searchParams?.k, this.searchParams?.filter);
58
+ }
59
+ }
60
+ static fromLLM(opts) {
61
+ const { structuredQueryTranslator } = opts;
62
+ const allowedComparators = opts.allowedComparators ?? structuredQueryTranslator.allowedComparators;
63
+ const allowedOperators = opts.allowedOperators ?? structuredQueryTranslator.allowedOperators;
64
+ const llmChain = (0, index_js_1.loadQueryContstructorChain)({
65
+ llm: opts.llm,
66
+ documentContents: opts.documentContents,
67
+ attributeInfo: opts.attributeInfo,
68
+ examples: opts.examples,
69
+ allowedComparators,
70
+ allowedOperators,
71
+ });
72
+ return new SelfQueryRetriever({
73
+ vectorStore: opts.vectorStore,
74
+ llmChain,
75
+ structuredQueryTranslator,
76
+ });
77
+ }
78
+ }
79
+ exports.SelfQueryRetriever = SelfQueryRetriever;
@@ -0,0 +1,33 @@
1
+ import { LLMChain } from "../../chains/llm_chain.js";
2
+ import { QueryConstructorChainOptions } from "../../chains/query_constructor/index.js";
3
+ import { Document } from "../../document.js";
4
+ import { BaseRetriever } from "../../schema/index.js";
5
+ import { VectorStore } from "../../vectorstores/base.js";
6
+ import { BaseTranslator, BasicTranslator } from "./translator.js";
7
+ export { BaseTranslator, BasicTranslator };
8
+ export type SelfQueryRetrieverArgs = {
9
+ vectorStore: VectorStore;
10
+ llmChain: LLMChain;
11
+ structuredQueryTranslator: BaseTranslator;
12
+ verbose?: boolean;
13
+ searchParams?: {
14
+ k?: number;
15
+ filter?: VectorStore["FilterType"];
16
+ };
17
+ };
18
+ export declare class SelfQueryRetriever extends BaseRetriever implements SelfQueryRetrieverArgs {
19
+ vectorStore: VectorStore;
20
+ llmChain: LLMChain;
21
+ verbose?: boolean;
22
+ structuredQueryTranslator: BaseTranslator;
23
+ searchParams?: {
24
+ k?: number;
25
+ filter?: VectorStore["FilterType"];
26
+ };
27
+ constructor(options: SelfQueryRetrieverArgs);
28
+ getRelevantDocuments(query: string): Promise<Document<Record<string, unknown>>[]>;
29
+ static fromLLM(opts: QueryConstructorChainOptions & {
30
+ vectorStore: VectorStore;
31
+ structuredQueryTranslator: BaseTranslator;
32
+ }): SelfQueryRetriever;
33
+ }
@@ -0,0 +1,74 @@
1
+ import { loadQueryContstructorChain, } from "../../chains/query_constructor/index.js";
2
+ import { BaseRetriever } from "../../schema/index.js";
3
+ import { BaseTranslator, BasicTranslator } from "./translator.js";
4
+ export { BaseTranslator, BasicTranslator };
5
+ export class SelfQueryRetriever extends BaseRetriever {
6
+ constructor(options) {
7
+ super();
8
+ Object.defineProperty(this, "vectorStore", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ Object.defineProperty(this, "llmChain", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "verbose", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "structuredQueryTranslator", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "searchParams", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: { k: 4 }
37
+ });
38
+ this.vectorStore = options.vectorStore;
39
+ this.llmChain = options.llmChain;
40
+ this.verbose = options.verbose ?? false;
41
+ this.searchParams = options.searchParams ?? this.searchParams;
42
+ this.structuredQueryTranslator = options.structuredQueryTranslator;
43
+ }
44
+ async getRelevantDocuments(query) {
45
+ const { [this.llmChain.outputKey]: output } = await this.llmChain.call({
46
+ [this.llmChain.inputKeys[0]]: query,
47
+ });
48
+ const nextArg = this.structuredQueryTranslator.visitStructuredQuery(output);
49
+ if (nextArg.filter) {
50
+ return this.vectorStore.similaritySearch(query, this.searchParams?.k, nextArg.filter);
51
+ }
52
+ else {
53
+ return this.vectorStore.similaritySearch(query, this.searchParams?.k, this.searchParams?.filter);
54
+ }
55
+ }
56
+ static fromLLM(opts) {
57
+ const { structuredQueryTranslator } = opts;
58
+ const allowedComparators = opts.allowedComparators ?? structuredQueryTranslator.allowedComparators;
59
+ const allowedOperators = opts.allowedOperators ?? structuredQueryTranslator.allowedOperators;
60
+ const llmChain = loadQueryContstructorChain({
61
+ llm: opts.llm,
62
+ documentContents: opts.documentContents,
63
+ attributeInfo: opts.attributeInfo,
64
+ examples: opts.examples,
65
+ allowedComparators,
66
+ allowedOperators,
67
+ });
68
+ return new SelfQueryRetriever({
69
+ vectorStore: opts.vectorStore,
70
+ llmChain,
71
+ structuredQueryTranslator,
72
+ });
73
+ }
74
+ }