langchain 0.0.76 → 0.0.77

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 (117) 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/chat_convo/index.cjs +27 -11
  8. package/dist/agents/chat_convo/index.d.ts +4 -1
  9. package/dist/agents/chat_convo/index.js +28 -12
  10. package/dist/agents/chat_convo/outputParser.cjs +79 -7
  11. package/dist/agents/chat_convo/outputParser.d.ts +25 -13
  12. package/dist/agents/chat_convo/outputParser.js +77 -6
  13. package/dist/agents/chat_convo/prompt.cjs +11 -8
  14. package/dist/agents/chat_convo/prompt.d.ts +2 -2
  15. package/dist/agents/chat_convo/prompt.js +11 -8
  16. package/dist/callbacks/handlers/tracer_langchain.cjs +12 -4
  17. package/dist/callbacks/handlers/tracer_langchain.d.ts +4 -1
  18. package/dist/callbacks/handlers/tracer_langchain.js +12 -4
  19. package/dist/callbacks/manager.cjs +6 -2
  20. package/dist/callbacks/manager.js +6 -2
  21. package/dist/chains/query_constructor/index.cjs +105 -0
  22. package/dist/chains/query_constructor/index.d.ts +37 -0
  23. package/dist/chains/query_constructor/index.js +95 -0
  24. package/dist/chains/query_constructor/ir.cjs +116 -0
  25. package/dist/chains/query_constructor/ir.d.ts +60 -0
  26. package/dist/chains/query_constructor/ir.js +107 -0
  27. package/dist/chains/query_constructor/parser.cjs +103 -0
  28. package/dist/chains/query_constructor/parser.d.ts +12 -0
  29. package/dist/chains/query_constructor/parser.js +99 -0
  30. package/dist/chains/query_constructor/prompt.cjs +127 -0
  31. package/dist/chains/query_constructor/prompt.d.ts +15 -0
  32. package/dist/chains/query_constructor/prompt.js +124 -0
  33. package/dist/chains/sql_db/sql_db_chain.cjs +13 -0
  34. package/dist/chains/sql_db/sql_db_chain.d.ts +2 -0
  35. package/dist/chains/sql_db/sql_db_chain.js +13 -0
  36. package/dist/client/langchainplus.cjs +21 -15
  37. package/dist/client/langchainplus.d.ts +4 -2
  38. package/dist/client/langchainplus.js +21 -15
  39. package/dist/llms/sagemaker_endpoint.cjs +123 -0
  40. package/dist/llms/sagemaker_endpoint.d.ts +82 -0
  41. package/dist/llms/sagemaker_endpoint.js +118 -0
  42. package/dist/memory/buffer_memory.cjs +1 -1
  43. package/dist/memory/buffer_memory.js +1 -1
  44. package/dist/memory/buffer_window_memory.cjs +1 -1
  45. package/dist/memory/buffer_window_memory.js +1 -1
  46. package/dist/output_parsers/expression.cjs +68 -0
  47. package/dist/output_parsers/expression.d.ts +25 -0
  48. package/dist/output_parsers/expression.js +49 -0
  49. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.cjs +26 -0
  50. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.d.ts +7 -0
  51. package/dist/output_parsers/expression_type_handlers/array_literal_expression_handler.js +22 -0
  52. package/dist/output_parsers/expression_type_handlers/base.cjs +67 -0
  53. package/dist/output_parsers/expression_type_handlers/base.d.ts +23 -0
  54. package/dist/output_parsers/expression_type_handlers/base.js +62 -0
  55. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.cjs +24 -0
  56. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.d.ts +7 -0
  57. package/dist/output_parsers/expression_type_handlers/boolean_literal_handler.js +20 -0
  58. package/dist/output_parsers/expression_type_handlers/call_expression_handler.cjs +52 -0
  59. package/dist/output_parsers/expression_type_handlers/call_expression_handler.d.ts +7 -0
  60. package/dist/output_parsers/expression_type_handlers/call_expression_handler.js +48 -0
  61. package/dist/output_parsers/expression_type_handlers/factory.cjs +56 -0
  62. package/dist/output_parsers/expression_type_handlers/factory.d.ts +9 -0
  63. package/dist/output_parsers/expression_type_handlers/factory.js +52 -0
  64. package/dist/output_parsers/expression_type_handlers/identifier_handler.cjs +22 -0
  65. package/dist/output_parsers/expression_type_handlers/identifier_handler.d.ts +7 -0
  66. package/dist/output_parsers/expression_type_handlers/identifier_handler.js +18 -0
  67. package/dist/output_parsers/expression_type_handlers/member_expression_handler.cjs +45 -0
  68. package/dist/output_parsers/expression_type_handlers/member_expression_handler.d.ts +7 -0
  69. package/dist/output_parsers/expression_type_handlers/member_expression_handler.js +41 -0
  70. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.cjs +24 -0
  71. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.d.ts +7 -0
  72. package/dist/output_parsers/expression_type_handlers/numeric_literal_handler.js +20 -0
  73. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.cjs +29 -0
  74. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.d.ts +7 -0
  75. package/dist/output_parsers/expression_type_handlers/object_literal_expression_handler.js +25 -0
  76. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.cjs +36 -0
  77. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.d.ts +7 -0
  78. package/dist/output_parsers/expression_type_handlers/property_assignment_handler.js +32 -0
  79. package/dist/output_parsers/expression_type_handlers/string_literal_handler.cjs +22 -0
  80. package/dist/output_parsers/expression_type_handlers/string_literal_handler.d.ts +7 -0
  81. package/dist/output_parsers/expression_type_handlers/string_literal_handler.js +18 -0
  82. package/dist/output_parsers/expression_type_handlers/types.cjs +2 -0
  83. package/dist/output_parsers/expression_type_handlers/types.d.ts +41 -0
  84. package/dist/output_parsers/expression_type_handlers/types.js +1 -0
  85. package/dist/output_parsers/index.cjs +2 -1
  86. package/dist/output_parsers/index.d.ts +1 -1
  87. package/dist/output_parsers/index.js +1 -1
  88. package/dist/output_parsers/structured.cjs +81 -23
  89. package/dist/output_parsers/structured.d.ts +18 -0
  90. package/dist/output_parsers/structured.js +79 -22
  91. package/dist/retrievers/self_query/index.cjs +79 -0
  92. package/dist/retrievers/self_query/index.d.ts +33 -0
  93. package/dist/retrievers/self_query/index.js +74 -0
  94. package/dist/retrievers/self_query/translator.cjs +72 -0
  95. package/dist/retrievers/self_query/translator.d.ts +14 -0
  96. package/dist/retrievers/self_query/translator.js +67 -0
  97. package/dist/schema/query_constructor.cjs +26 -0
  98. package/dist/schema/query_constructor.d.ts +6 -0
  99. package/dist/schema/query_constructor.js +22 -0
  100. package/dist/tools/json.cjs +3 -1
  101. package/dist/tools/json.js +3 -1
  102. package/dist/util/event-source-parse.cjs +31 -5
  103. package/dist/util/event-source-parse.d.ts +3 -3
  104. package/dist/util/event-source-parse.js +31 -5
  105. package/llms/sagemaker_endpoint.cjs +1 -0
  106. package/llms/sagemaker_endpoint.d.ts +1 -0
  107. package/llms/sagemaker_endpoint.js +1 -0
  108. package/output_parsers/expression.cjs +1 -0
  109. package/output_parsers/expression.d.ts +1 -0
  110. package/output_parsers/expression.js +1 -0
  111. package/package.json +61 -3
  112. package/retrievers/self_query.cjs +1 -0
  113. package/retrievers/self_query.d.ts +1 -0
  114. package/retrievers/self_query.js +1 -0
  115. package/schema/query_constructor.cjs +1 -0
  116. package/schema/query_constructor.d.ts +1 -0
  117. package/schema/query_constructor.js +1 -0
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryTransformer = void 0;
4
+ const ir_js_1 = require("./ir.cjs");
5
+ const expression_js_1 = require("../../output_parsers/expression.cjs");
6
+ class QueryTransformer {
7
+ constructor(allowedComparators = [], allowedOperators = []) {
8
+ Object.defineProperty(this, "allowedComparators", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: allowedComparators
13
+ });
14
+ Object.defineProperty(this, "allowedOperators", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: allowedOperators
19
+ });
20
+ }
21
+ matchFunctionName(funcName) {
22
+ if (funcName in ir_js_1.Comparators) {
23
+ if (this.allowedComparators.length > 0) {
24
+ if (this.allowedComparators.includes(funcName)) {
25
+ return funcName;
26
+ }
27
+ else {
28
+ throw new Error("Received comparator not allowed");
29
+ }
30
+ }
31
+ else {
32
+ return funcName;
33
+ }
34
+ }
35
+ if (funcName in ir_js_1.Operators) {
36
+ if (this.allowedOperators.length > 0) {
37
+ if (this.allowedOperators.includes(funcName)) {
38
+ return funcName;
39
+ }
40
+ else {
41
+ throw new Error("Received operator not allowed");
42
+ }
43
+ }
44
+ else {
45
+ return funcName;
46
+ }
47
+ }
48
+ throw new Error("Unknown function name");
49
+ }
50
+ transform(parsed) {
51
+ const traverse = (node) => {
52
+ switch (node.type) {
53
+ case "call_expression": {
54
+ if (typeof node.funcCall !== "string") {
55
+ throw new Error("Property access expression and element access expression not supported");
56
+ }
57
+ const funcName = this.matchFunctionName(node.funcCall);
58
+ if (funcName in ir_js_1.Operators) {
59
+ return new ir_js_1.Operation(funcName, node.args?.map((arg) => traverse(arg)));
60
+ }
61
+ if (funcName in ir_js_1.Comparators) {
62
+ if (node.args && node.args.length === 2) {
63
+ return new ir_js_1.Comparison(funcName, traverse(node.args[0]), traverse(node.args[1]));
64
+ }
65
+ throw new Error("Comparator must have exactly 2 arguments");
66
+ }
67
+ throw new Error("Function name neither operator nor comparator");
68
+ }
69
+ case "string_literal": {
70
+ return node.value;
71
+ }
72
+ case "numeric_literal": {
73
+ return node.value;
74
+ }
75
+ case "array_literal": {
76
+ return node.values.map((value) => traverse(value));
77
+ }
78
+ case "object_literal": {
79
+ return node.values.reduce((acc, value) => {
80
+ acc[value.identifier] = traverse(value.value);
81
+ return acc;
82
+ }, {});
83
+ }
84
+ case "boolean_literal": {
85
+ return node.value;
86
+ }
87
+ default: {
88
+ throw new Error("Unknown node type");
89
+ }
90
+ }
91
+ };
92
+ return traverse(parsed);
93
+ }
94
+ async parse(expression) {
95
+ const expressionParser = new expression_js_1.ExpressionParser();
96
+ const parsed = (await expressionParser.parse(expression));
97
+ if (!parsed) {
98
+ throw new Error("Could not parse expression");
99
+ }
100
+ return this.transform(parsed);
101
+ }
102
+ }
103
+ exports.QueryTransformer = QueryTransformer;
@@ -0,0 +1,12 @@
1
+ import { Comparator, Comparison, Operation, Operator } from "./ir.js";
2
+ export type TraverseType = boolean | Operation | Comparison | string | number | {
3
+ [key: string]: TraverseType;
4
+ } | TraverseType[];
5
+ export declare class QueryTransformer {
6
+ allowedComparators: Comparator[];
7
+ allowedOperators: Operator[];
8
+ constructor(allowedComparators?: Comparator[], allowedOperators?: Operator[]);
9
+ private matchFunctionName;
10
+ private transform;
11
+ parse(expression: string): Promise<Operation | Comparison>;
12
+ }
@@ -0,0 +1,99 @@
1
+ import { Comparators, Comparison, Operation, Operators, } from "./ir.js";
2
+ import { ExpressionParser, } from "../../output_parsers/expression.js";
3
+ export class QueryTransformer {
4
+ constructor(allowedComparators = [], allowedOperators = []) {
5
+ Object.defineProperty(this, "allowedComparators", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: allowedComparators
10
+ });
11
+ Object.defineProperty(this, "allowedOperators", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: allowedOperators
16
+ });
17
+ }
18
+ matchFunctionName(funcName) {
19
+ if (funcName in Comparators) {
20
+ if (this.allowedComparators.length > 0) {
21
+ if (this.allowedComparators.includes(funcName)) {
22
+ return funcName;
23
+ }
24
+ else {
25
+ throw new Error("Received comparator not allowed");
26
+ }
27
+ }
28
+ else {
29
+ return funcName;
30
+ }
31
+ }
32
+ if (funcName in Operators) {
33
+ if (this.allowedOperators.length > 0) {
34
+ if (this.allowedOperators.includes(funcName)) {
35
+ return funcName;
36
+ }
37
+ else {
38
+ throw new Error("Received operator not allowed");
39
+ }
40
+ }
41
+ else {
42
+ return funcName;
43
+ }
44
+ }
45
+ throw new Error("Unknown function name");
46
+ }
47
+ transform(parsed) {
48
+ const traverse = (node) => {
49
+ switch (node.type) {
50
+ case "call_expression": {
51
+ if (typeof node.funcCall !== "string") {
52
+ throw new Error("Property access expression and element access expression not supported");
53
+ }
54
+ const funcName = this.matchFunctionName(node.funcCall);
55
+ if (funcName in Operators) {
56
+ return new Operation(funcName, node.args?.map((arg) => traverse(arg)));
57
+ }
58
+ if (funcName in Comparators) {
59
+ if (node.args && node.args.length === 2) {
60
+ return new Comparison(funcName, traverse(node.args[0]), traverse(node.args[1]));
61
+ }
62
+ throw new Error("Comparator must have exactly 2 arguments");
63
+ }
64
+ throw new Error("Function name neither operator nor comparator");
65
+ }
66
+ case "string_literal": {
67
+ return node.value;
68
+ }
69
+ case "numeric_literal": {
70
+ return node.value;
71
+ }
72
+ case "array_literal": {
73
+ return node.values.map((value) => traverse(value));
74
+ }
75
+ case "object_literal": {
76
+ return node.values.reduce((acc, value) => {
77
+ acc[value.identifier] = traverse(value.value);
78
+ return acc;
79
+ }, {});
80
+ }
81
+ case "boolean_literal": {
82
+ return node.value;
83
+ }
84
+ default: {
85
+ throw new Error("Unknown node type");
86
+ }
87
+ }
88
+ };
89
+ return traverse(parsed);
90
+ }
91
+ async parse(expression) {
92
+ const expressionParser = new ExpressionParser();
93
+ const parsed = (await expressionParser.parse(expression));
94
+ if (!parsed) {
95
+ throw new Error("Could not parse expression");
96
+ }
97
+ return this.transform(parsed);
98
+ }
99
+ }
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_SUFFIX = exports.DEFAULT_PREFIX = exports.DEFAULT_SCHEMA = exports.EXAMPLE_PROMPT = exports.EXAMPLE_PROMPT_TEMPLATE = exports.DEFAULT_EXAMPLES = exports.NO_FILTER_ANSWER = exports.FULL_ANSWER = exports.SONG_DATA_SOURCE = void 0;
4
+ const prompt_js_1 = require("../../prompts/prompt.cjs");
5
+ exports.SONG_DATA_SOURCE = `\
6
+ \`\`\`json
7
+ {
8
+ content: "Lyrics of a song",
9
+ attributes: {
10
+ "artist": {
11
+ "type": "string",
12
+ "description": "Name of the song artist"
13
+ },
14
+ "length": {
15
+ "type": "integer",
16
+ "description": "Length of the song in seconds"
17
+ },
18
+ "genre": {
19
+ "type": "string",
20
+ "description": "The song genre, one of 'pop', 'rock' or 'rap'"
21
+ }
22
+ }
23
+ }
24
+ \`\`\`\
25
+ `
26
+ .replaceAll("{", "{{")
27
+ .replaceAll("}", "}}");
28
+ exports.FULL_ANSWER = `\
29
+ \`\`\`json
30
+ {{
31
+ "query": "teenager love",
32
+ "filter": "and(or(eq(\\"artist\\", \\"Taylor Swift\\"), eq(\\"artist\\", \\"Katy Perry\\")), \
33
+ lt(\\"length\\", 180), eq(\\"genre\\", \\"pop\\"))"
34
+ }}`;
35
+ exports.NO_FILTER_ANSWER = `\
36
+ \`\`\`json
37
+ {{
38
+ "query": "",
39
+ "filter": "NO_FILTER"
40
+ }}
41
+ \`\`\`\
42
+ `;
43
+ exports.DEFAULT_EXAMPLES = [
44
+ {
45
+ i: "1",
46
+ data_source: exports.SONG_DATA_SOURCE,
47
+ user_query: "What are songs by Taylor Swift or Katy Perry about teenage romance under 3 minutes long in the dance pop genre",
48
+ structured_request: exports.FULL_ANSWER,
49
+ },
50
+ {
51
+ i: "2",
52
+ data_source: exports.SONG_DATA_SOURCE,
53
+ user_query: "What are songs that were not published on Spotify",
54
+ structured_request: exports.NO_FILTER_ANSWER,
55
+ },
56
+ ];
57
+ exports.EXAMPLE_PROMPT_TEMPLATE = `\
58
+ << Example {i}. >>
59
+ Data Source:
60
+ {data_source}
61
+
62
+ User Query:
63
+ {user_query}
64
+
65
+ Structured Request:
66
+ {structured_request}
67
+ `;
68
+ exports.EXAMPLE_PROMPT = new prompt_js_1.PromptTemplate({
69
+ inputVariables: ["i", "data_source", "user_query", "structured_request"],
70
+ template: exports.EXAMPLE_PROMPT_TEMPLATE,
71
+ });
72
+ exports.DEFAULT_SCHEMA = `\
73
+ << Structured Request Schema >>
74
+ When responding use a markdown code snippet with a JSON object formatted in the \
75
+ following schema:
76
+
77
+ \`\`\`json
78
+ {{{{
79
+ "query": string \\ text string to compare to document contents
80
+ "filter": string \\ logical condition statement for filtering documents
81
+ }}}}
82
+ \`\`\`
83
+
84
+ The query string should contain only text that is expected to match the contents of \
85
+ documents. Any conditions in the filter should not be mentioned in the query as well.
86
+
87
+ A logical condition statement is composed of one or more comparison and logical \
88
+ operation statements.
89
+
90
+ A comparison statement takes the form: \`comp(attr, val)\`:
91
+ - \`comp\` ({allowed_comparators}): comparator
92
+ - \`attr\` (string): name of attribute to apply the comparison to
93
+ - \`val\` (string): is the comparison value
94
+
95
+ A logical operation statement takes the form \`op(statement1, statement2, ...)\`:
96
+ - \`op\` ({allowed_operators}): logical operator
97
+ - \`statement1\`, \`statement2\`, ... (comparison statements or logical operation \
98
+ statements): one or more statements to appy the operation to
99
+
100
+ Make sure that you only use the comparators and logical operators listed above and \
101
+ no others.
102
+ Make sure that filters only refer to attributes that exist in the data source.
103
+ Make sure that filters take into account the descriptions of attributes and only make \
104
+ comparisons that are feasible given the type of data being stored.
105
+ Make sure that filters are only used as needed. If there are no filters that should be \
106
+ applied return "NO_FILTER" for the filter value.\
107
+ `;
108
+ exports.DEFAULT_PREFIX = `\
109
+ Your goal is to structure the user's query to match the request schema provided below.
110
+
111
+ {schema}\
112
+ `;
113
+ exports.DEFAULT_SUFFIX = `\
114
+ << Example {i}. >>
115
+ Data Source:
116
+ \`\`\`json
117
+ {{{{
118
+ content: {content},
119
+ attributes: {attributes}
120
+ }}}}
121
+ \`\`\`
122
+
123
+ User Query:
124
+ {{query}}
125
+
126
+ Structured Request:
127
+ `;
@@ -0,0 +1,15 @@
1
+ import { PromptTemplate } from "../../prompts/prompt.js";
2
+ export declare const SONG_DATA_SOURCE: string;
3
+ export declare const FULL_ANSWER = "```json\n{{\n \"query\": \"teenager love\",\n \"filter\": \"and(or(eq(\\\"artist\\\", \\\"Taylor Swift\\\"), eq(\\\"artist\\\", \\\"Katy Perry\\\")), lt(\\\"length\\\", 180), eq(\\\"genre\\\", \\\"pop\\\"))\"\n}}";
4
+ export declare const NO_FILTER_ANSWER = "```json\n{{\n \"query\": \"\",\n \"filter\": \"NO_FILTER\"\n}}\n```";
5
+ export declare const DEFAULT_EXAMPLES: {
6
+ i: string;
7
+ data_source: string;
8
+ user_query: string;
9
+ structured_request: string;
10
+ }[];
11
+ export declare const EXAMPLE_PROMPT_TEMPLATE = "<< Example {i}. >>\nData Source:\n{data_source}\n\nUser Query:\n{user_query}\n\nStructured Request:\n{structured_request}\n";
12
+ export declare const EXAMPLE_PROMPT: PromptTemplate;
13
+ export declare const DEFAULT_SCHEMA = "<< Structured Request Schema >>\nWhen responding use a markdown code snippet with a JSON object formatted in the following schema:\n\n```json\n{{{{\n \"query\": string \\ text string to compare to document contents\n \"filter\": string \\ logical condition statement for filtering documents\n}}}}\n```\n\nThe query string should contain only text that is expected to match the contents of documents. Any conditions in the filter should not be mentioned in the query as well.\n\nA logical condition statement is composed of one or more comparison and logical operation statements.\n\nA comparison statement takes the form: `comp(attr, val)`:\n- `comp` ({allowed_comparators}): comparator\n- `attr` (string): name of attribute to apply the comparison to\n- `val` (string): is the comparison value\n\nA logical operation statement takes the form `op(statement1, statement2, ...)`:\n- `op` ({allowed_operators}): logical operator\n- `statement1`, `statement2`, ... (comparison statements or logical operation statements): one or more statements to appy the operation to\n\nMake sure that you only use the comparators and logical operators listed above and no others.\nMake sure that filters only refer to attributes that exist in the data source.\nMake sure that filters take into account the descriptions of attributes and only make comparisons that are feasible given the type of data being stored.\nMake sure that filters are only used as needed. If there are no filters that should be applied return \"NO_FILTER\" for the filter value.";
14
+ export declare const DEFAULT_PREFIX = "Your goal is to structure the user's query to match the request schema provided below.\n\n{schema}";
15
+ export declare const DEFAULT_SUFFIX = "<< Example {i}. >>\nData Source:\n```json\n{{{{\n content: {content},\n attributes: {attributes}\n}}}}\n```\n\nUser Query:\n{{query}}\n\nStructured Request:\n";
@@ -0,0 +1,124 @@
1
+ import { PromptTemplate } from "../../prompts/prompt.js";
2
+ export const SONG_DATA_SOURCE = `\
3
+ \`\`\`json
4
+ {
5
+ content: "Lyrics of a song",
6
+ attributes: {
7
+ "artist": {
8
+ "type": "string",
9
+ "description": "Name of the song artist"
10
+ },
11
+ "length": {
12
+ "type": "integer",
13
+ "description": "Length of the song in seconds"
14
+ },
15
+ "genre": {
16
+ "type": "string",
17
+ "description": "The song genre, one of 'pop', 'rock' or 'rap'"
18
+ }
19
+ }
20
+ }
21
+ \`\`\`\
22
+ `
23
+ .replaceAll("{", "{{")
24
+ .replaceAll("}", "}}");
25
+ export const FULL_ANSWER = `\
26
+ \`\`\`json
27
+ {{
28
+ "query": "teenager love",
29
+ "filter": "and(or(eq(\\"artist\\", \\"Taylor Swift\\"), eq(\\"artist\\", \\"Katy Perry\\")), \
30
+ lt(\\"length\\", 180), eq(\\"genre\\", \\"pop\\"))"
31
+ }}`;
32
+ export const NO_FILTER_ANSWER = `\
33
+ \`\`\`json
34
+ {{
35
+ "query": "",
36
+ "filter": "NO_FILTER"
37
+ }}
38
+ \`\`\`\
39
+ `;
40
+ export const DEFAULT_EXAMPLES = [
41
+ {
42
+ i: "1",
43
+ data_source: SONG_DATA_SOURCE,
44
+ user_query: "What are songs by Taylor Swift or Katy Perry about teenage romance under 3 minutes long in the dance pop genre",
45
+ structured_request: FULL_ANSWER,
46
+ },
47
+ {
48
+ i: "2",
49
+ data_source: SONG_DATA_SOURCE,
50
+ user_query: "What are songs that were not published on Spotify",
51
+ structured_request: NO_FILTER_ANSWER,
52
+ },
53
+ ];
54
+ export const EXAMPLE_PROMPT_TEMPLATE = `\
55
+ << Example {i}. >>
56
+ Data Source:
57
+ {data_source}
58
+
59
+ User Query:
60
+ {user_query}
61
+
62
+ Structured Request:
63
+ {structured_request}
64
+ `;
65
+ export const EXAMPLE_PROMPT = /* #__PURE__ */ new PromptTemplate({
66
+ inputVariables: ["i", "data_source", "user_query", "structured_request"],
67
+ template: EXAMPLE_PROMPT_TEMPLATE,
68
+ });
69
+ export const DEFAULT_SCHEMA = `\
70
+ << Structured Request Schema >>
71
+ When responding use a markdown code snippet with a JSON object formatted in the \
72
+ following schema:
73
+
74
+ \`\`\`json
75
+ {{{{
76
+ "query": string \\ text string to compare to document contents
77
+ "filter": string \\ logical condition statement for filtering documents
78
+ }}}}
79
+ \`\`\`
80
+
81
+ The query string should contain only text that is expected to match the contents of \
82
+ documents. Any conditions in the filter should not be mentioned in the query as well.
83
+
84
+ A logical condition statement is composed of one or more comparison and logical \
85
+ operation statements.
86
+
87
+ A comparison statement takes the form: \`comp(attr, val)\`:
88
+ - \`comp\` ({allowed_comparators}): comparator
89
+ - \`attr\` (string): name of attribute to apply the comparison to
90
+ - \`val\` (string): is the comparison value
91
+
92
+ A logical operation statement takes the form \`op(statement1, statement2, ...)\`:
93
+ - \`op\` ({allowed_operators}): logical operator
94
+ - \`statement1\`, \`statement2\`, ... (comparison statements or logical operation \
95
+ statements): one or more statements to appy the operation to
96
+
97
+ Make sure that you only use the comparators and logical operators listed above and \
98
+ no others.
99
+ Make sure that filters only refer to attributes that exist in the data source.
100
+ Make sure that filters take into account the descriptions of attributes and only make \
101
+ comparisons that are feasible given the type of data being stored.
102
+ Make sure that filters are only used as needed. If there are no filters that should be \
103
+ applied return "NO_FILTER" for the filter value.\
104
+ `;
105
+ export const DEFAULT_PREFIX = `\
106
+ Your goal is to structure the user's query to match the request schema provided below.
107
+
108
+ {schema}\
109
+ `;
110
+ export const DEFAULT_SUFFIX = `\
111
+ << Example {i}. >>
112
+ Data Source:
113
+ \`\`\`json
114
+ {{{{
115
+ content: {content},
116
+ attributes: {attributes}
117
+ }}}}
118
+ \`\`\`
119
+
120
+ User Query:
121
+ {{query}}
122
+
123
+ Structured Request:
124
+ `;
@@ -50,6 +50,12 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
50
50
  writable: true,
51
51
  value: "result"
52
52
  });
53
+ Object.defineProperty(this, "sqlOutputKey", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: undefined
58
+ });
53
59
  // Whether to return the result of querying the SQL table directly.
54
60
  Object.defineProperty(this, "returnDirect", {
55
61
  enumerable: true,
@@ -62,6 +68,7 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
62
68
  this.topK = fields.topK ?? this.topK;
63
69
  this.inputKey = fields.inputKey ?? this.inputKey;
64
70
  this.outputKey = fields.outputKey ?? this.outputKey;
71
+ this.sqlOutputKey = fields.sqlOutputKey ?? this.sqlOutputKey;
65
72
  this.prompt =
66
73
  fields.prompt ??
67
74
  (0, sql_utils_js_1.getPromptTemplateFromDataSource)(this.database.appDataSource);
@@ -111,6 +118,9 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
111
118
  [this.outputKey]: await llmChain.predict(llmInputs, runManager?.getChild()),
112
119
  };
113
120
  }
121
+ if (this.sqlOutputKey != null) {
122
+ finalResult[this.sqlOutputKey] = sqlCommand;
123
+ }
114
124
  return finalResult;
115
125
  }
116
126
  _chainType() {
@@ -120,6 +130,9 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
120
130
  return [this.inputKey];
121
131
  }
122
132
  get outputKeys() {
133
+ if (this.sqlOutputKey != null) {
134
+ return [this.outputKey, this.sqlOutputKey];
135
+ }
123
136
  return [this.outputKey];
124
137
  }
125
138
  static async deserialize(data, SqlDatabaseFromOptionsParams) {
@@ -11,6 +11,7 @@ export interface SqlDatabaseChainInput extends ChainInputs {
11
11
  topK?: number;
12
12
  inputKey?: string;
13
13
  outputKey?: string;
14
+ sqlOutputKey?: string;
14
15
  prompt?: PromptTemplate;
15
16
  }
16
17
  export declare class SqlDatabaseChain extends BaseChain {
@@ -20,6 +21,7 @@ export declare class SqlDatabaseChain extends BaseChain {
20
21
  topK: number;
21
22
  inputKey: string;
22
23
  outputKey: string;
24
+ sqlOutputKey: string | undefined;
23
25
  returnDirect: boolean;
24
26
  constructor(fields: SqlDatabaseChainInput);
25
27
  /** @ignore */
@@ -47,6 +47,12 @@ export class SqlDatabaseChain extends BaseChain {
47
47
  writable: true,
48
48
  value: "result"
49
49
  });
50
+ Object.defineProperty(this, "sqlOutputKey", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: undefined
55
+ });
50
56
  // Whether to return the result of querying the SQL table directly.
51
57
  Object.defineProperty(this, "returnDirect", {
52
58
  enumerable: true,
@@ -59,6 +65,7 @@ export class SqlDatabaseChain extends BaseChain {
59
65
  this.topK = fields.topK ?? this.topK;
60
66
  this.inputKey = fields.inputKey ?? this.inputKey;
61
67
  this.outputKey = fields.outputKey ?? this.outputKey;
68
+ this.sqlOutputKey = fields.sqlOutputKey ?? this.sqlOutputKey;
62
69
  this.prompt =
63
70
  fields.prompt ??
64
71
  getPromptTemplateFromDataSource(this.database.appDataSource);
@@ -108,6 +115,9 @@ export class SqlDatabaseChain extends BaseChain {
108
115
  [this.outputKey]: await llmChain.predict(llmInputs, runManager?.getChild()),
109
116
  };
110
117
  }
118
+ if (this.sqlOutputKey != null) {
119
+ finalResult[this.sqlOutputKey] = sqlCommand;
120
+ }
111
121
  return finalResult;
112
122
  }
113
123
  _chainType() {
@@ -117,6 +127,9 @@ export class SqlDatabaseChain extends BaseChain {
117
127
  return [this.inputKey];
118
128
  }
119
129
  get outputKeys() {
130
+ if (this.sqlOutputKey != null) {
131
+ return [this.outputKey, this.sqlOutputKey];
132
+ }
120
133
  return [this.outputKey];
121
134
  }
122
135
  static async deserialize(data, SqlDatabaseFromOptionsParams) {