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,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructuredChatOutputParserWithRetries = exports.StructuredChatOutputParser = void 0;
4
+ const types_js_1 = require("../types.cjs");
5
+ const prompt_js_1 = require("./prompt.cjs");
6
+ const fix_js_1 = require("../../output_parsers/fix.cjs");
7
+ const output_parser_js_1 = require("../../schema/output_parser.cjs");
8
+ const index_js_1 = require("../../prompts/index.cjs");
9
+ class StructuredChatOutputParser extends types_js_1.AgentActionOutputParser {
10
+ constructor(toolNames) {
11
+ super();
12
+ Object.defineProperty(this, "toolNames", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: toolNames
17
+ });
18
+ }
19
+ async parse(text) {
20
+ try {
21
+ const regex = /```(?:json)?(.*)(```)/gs;
22
+ const actionMatch = regex.exec(text);
23
+ if (actionMatch === null) {
24
+ throw new output_parser_js_1.OutputParserException(`Could not parse an action. The agent action must be within a markdown code block, and "action" must be a provided tool or "Final Answer"`);
25
+ }
26
+ const response = JSON.parse(actionMatch[1].trim());
27
+ const { action, action_input } = response;
28
+ if (action === "Final Answer") {
29
+ return { returnValues: { output: action_input }, log: text };
30
+ }
31
+ return { tool: action, toolInput: action_input || {}, log: text };
32
+ }
33
+ catch (e) {
34
+ throw new output_parser_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
35
+ }
36
+ }
37
+ getFormatInstructions() {
38
+ return (0, index_js_1.renderTemplate)(prompt_js_1.AGENT_ACTION_FORMAT_INSTRUCTIONS, "f-string", {
39
+ tool_names: this.toolNames.join(", "),
40
+ });
41
+ }
42
+ }
43
+ exports.StructuredChatOutputParser = StructuredChatOutputParser;
44
+ class StructuredChatOutputParserWithRetries extends types_js_1.AgentActionOutputParser {
45
+ constructor(fields) {
46
+ super();
47
+ Object.defineProperty(this, "baseParser", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "outputFixingParser", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "toolNames", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: []
64
+ });
65
+ this.toolNames = fields.toolNames ?? this.toolNames;
66
+ this.baseParser =
67
+ fields?.baseParser ?? new StructuredChatOutputParser(this.toolNames);
68
+ this.outputFixingParser = fields?.outputFixingParser;
69
+ }
70
+ async parse(text) {
71
+ if (this.outputFixingParser !== undefined) {
72
+ return this.outputFixingParser.parse(text);
73
+ }
74
+ return this.baseParser.parse(text);
75
+ }
76
+ getFormatInstructions() {
77
+ return (0, index_js_1.renderTemplate)(prompt_js_1.FORMAT_INSTRUCTIONS, "f-string", {
78
+ tool_names: this.toolNames.join(", "),
79
+ });
80
+ }
81
+ static fromLLM(llm, options) {
82
+ const baseParser = options.baseParser ??
83
+ new StructuredChatOutputParser(options.toolNames ?? []);
84
+ const outputFixingParser = fix_js_1.OutputFixingParser.fromLLM(llm, baseParser);
85
+ return new StructuredChatOutputParserWithRetries({
86
+ baseParser,
87
+ outputFixingParser,
88
+ toolNames: options.toolNames,
89
+ });
90
+ }
91
+ }
92
+ exports.StructuredChatOutputParserWithRetries = StructuredChatOutputParserWithRetries;
@@ -0,0 +1,24 @@
1
+ import { AgentActionOutputParser } from "../types.js";
2
+ import { OutputFixingParser } from "../../output_parsers/fix.js";
3
+ import { BaseLanguageModel } from "../../base_language/index.js";
4
+ import { AgentAction, AgentFinish } from "../../schema/index.js";
5
+ export declare class StructuredChatOutputParser extends AgentActionOutputParser {
6
+ private toolNames;
7
+ constructor(toolNames: string[]);
8
+ parse(text: string): Promise<AgentAction | AgentFinish>;
9
+ getFormatInstructions(): string;
10
+ }
11
+ export interface StructuredChatOutputParserArgs {
12
+ baseParser?: StructuredChatOutputParser;
13
+ outputFixingParser?: OutputFixingParser<AgentAction | AgentFinish>;
14
+ toolNames?: string[];
15
+ }
16
+ export declare class StructuredChatOutputParserWithRetries extends AgentActionOutputParser {
17
+ private baseParser;
18
+ private outputFixingParser?;
19
+ private toolNames;
20
+ constructor(fields: StructuredChatOutputParserArgs);
21
+ parse(text: string): Promise<AgentAction | AgentFinish>;
22
+ getFormatInstructions(): string;
23
+ static fromLLM(llm: BaseLanguageModel, options: Omit<StructuredChatOutputParserArgs, "outputFixingParser">): StructuredChatOutputParserWithRetries;
24
+ }
@@ -0,0 +1,87 @@
1
+ import { AgentActionOutputParser } from "../types.js";
2
+ import { AGENT_ACTION_FORMAT_INSTRUCTIONS, FORMAT_INSTRUCTIONS, } from "./prompt.js";
3
+ import { OutputFixingParser } from "../../output_parsers/fix.js";
4
+ import { OutputParserException } from "../../schema/output_parser.js";
5
+ import { renderTemplate } from "../../prompts/index.js";
6
+ export class StructuredChatOutputParser extends AgentActionOutputParser {
7
+ constructor(toolNames) {
8
+ super();
9
+ Object.defineProperty(this, "toolNames", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: toolNames
14
+ });
15
+ }
16
+ async parse(text) {
17
+ try {
18
+ const regex = /```(?:json)?(.*)(```)/gs;
19
+ const actionMatch = regex.exec(text);
20
+ if (actionMatch === null) {
21
+ throw new OutputParserException(`Could not parse an action. The agent action must be within a markdown code block, and "action" must be a provided tool or "Final Answer"`);
22
+ }
23
+ const response = JSON.parse(actionMatch[1].trim());
24
+ const { action, action_input } = response;
25
+ if (action === "Final Answer") {
26
+ return { returnValues: { output: action_input }, log: text };
27
+ }
28
+ return { tool: action, toolInput: action_input || {}, log: text };
29
+ }
30
+ catch (e) {
31
+ throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
32
+ }
33
+ }
34
+ getFormatInstructions() {
35
+ return renderTemplate(AGENT_ACTION_FORMAT_INSTRUCTIONS, "f-string", {
36
+ tool_names: this.toolNames.join(", "),
37
+ });
38
+ }
39
+ }
40
+ export class StructuredChatOutputParserWithRetries extends AgentActionOutputParser {
41
+ constructor(fields) {
42
+ super();
43
+ Object.defineProperty(this, "baseParser", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: void 0
48
+ });
49
+ Object.defineProperty(this, "outputFixingParser", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ Object.defineProperty(this, "toolNames", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: []
60
+ });
61
+ this.toolNames = fields.toolNames ?? this.toolNames;
62
+ this.baseParser =
63
+ fields?.baseParser ?? new StructuredChatOutputParser(this.toolNames);
64
+ this.outputFixingParser = fields?.outputFixingParser;
65
+ }
66
+ async parse(text) {
67
+ if (this.outputFixingParser !== undefined) {
68
+ return this.outputFixingParser.parse(text);
69
+ }
70
+ return this.baseParser.parse(text);
71
+ }
72
+ getFormatInstructions() {
73
+ return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
74
+ tool_names: this.toolNames.join(", "),
75
+ });
76
+ }
77
+ static fromLLM(llm, options) {
78
+ const baseParser = options.baseParser ??
79
+ new StructuredChatOutputParser(options.toolNames ?? []);
80
+ const outputFixingParser = OutputFixingParser.fromLLM(llm, baseParser);
81
+ return new StructuredChatOutputParserWithRetries({
82
+ baseParser,
83
+ outputFixingParser,
84
+ toolNames: options.toolNames,
85
+ });
86
+ }
87
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SUFFIX = exports.FORMAT_INSTRUCTIONS = exports.AGENT_ACTION_FORMAT_INSTRUCTIONS = exports.PREFIX = void 0;
4
+ exports.PREFIX = `Answer the following questions truthfully and as best you can.`;
5
+ exports.AGENT_ACTION_FORMAT_INSTRUCTIONS = `Output a JSON markdown code snippet containing a valid JSON blob (denoted below by $JSON_BLOB).
6
+ This $JSON_BLOB must have a "action" key (with the name of the tool to use) and an "action_input" key (tool input).
7
+
8
+ Valid "action" values: "Final Answer" (which you must use when giving your final response to the user), or one of [{tool_names}].
9
+
10
+ The $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. Here is an example of an acceptable output:
11
+
12
+ \`\`\`json
13
+ {{
14
+ "action": $TOOL_NAME
15
+ "action_input": $INPUT
16
+ }}
17
+ \`\`\`
18
+
19
+ Remember to include the surrounding markdown code snippet delimiters (begin with "\`\`\`" json and close with "\`\`\`")!
20
+ `;
21
+ exports.FORMAT_INSTRUCTIONS = `You have access to the following tools.
22
+ You must format your inputs to these tools to match their "JSON schema" definitions below.
23
+
24
+ "JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
25
+
26
+ For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
27
+ would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
28
+ Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
29
+
30
+ Here are the JSON Schema instances for the tools you have access to:
31
+
32
+ {tool_schemas}
33
+
34
+ The way you use the tools is as follows:
35
+
36
+ ------------------------
37
+
38
+ ${exports.AGENT_ACTION_FORMAT_INSTRUCTIONS}
39
+
40
+ If you are using a tool, "action_input" must adhere to the tool's input schema, given above.
41
+
42
+ ------------------------
43
+
44
+ ALWAYS use the following format:
45
+
46
+ Question: the input question you must answer
47
+ Thought: you should always think about what to do
48
+ Action:
49
+ \`\`\`json
50
+ $JSON_BLOB
51
+ \`\`\`
52
+ Observation: the result of the action
53
+ ... (this Thought/Action/Observation can repeat N times)
54
+ Thought: I now know the final answer
55
+ Action:
56
+ \`\`\`json
57
+ {{
58
+ "action": "Final Answer",
59
+ "action_input": "Final response to human"
60
+ }}
61
+ \`\`\``;
62
+ exports.SUFFIX = `Begin! Reminder to ALWAYS use the above format, and to use tools if appropriate.`;
@@ -0,0 +1,4 @@
1
+ export declare const PREFIX = "Answer the following questions truthfully and as best you can.";
2
+ export declare const AGENT_ACTION_FORMAT_INSTRUCTIONS = "Output a JSON markdown code snippet containing a valid JSON blob (denoted below by $JSON_BLOB).\nThis $JSON_BLOB must have a \"action\" key (with the name of the tool to use) and an \"action_input\" key (tool input).\n\nValid \"action\" values: \"Final Answer\" (which you must use when giving your final response to the user), or one of [{tool_names}].\n\nThe $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. Here is an example of an acceptable output:\n\n```json\n{{\n \"action\": $TOOL_NAME\n \"action_input\": $INPUT\n}}\n```\n\nRemember to include the surrounding markdown code snippet delimiters (begin with \"```\" json and close with \"```\")!\n";
3
+ export declare const FORMAT_INSTRUCTIONS: string;
4
+ export declare const SUFFIX = "Begin! Reminder to ALWAYS use the above format, and to use tools if appropriate.";
@@ -0,0 +1,59 @@
1
+ export const PREFIX = `Answer the following questions truthfully and as best you can.`;
2
+ export const AGENT_ACTION_FORMAT_INSTRUCTIONS = `Output a JSON markdown code snippet containing a valid JSON blob (denoted below by $JSON_BLOB).
3
+ This $JSON_BLOB must have a "action" key (with the name of the tool to use) and an "action_input" key (tool input).
4
+
5
+ Valid "action" values: "Final Answer" (which you must use when giving your final response to the user), or one of [{tool_names}].
6
+
7
+ The $JSON_BLOB must be valid, parseable JSON and only contain a SINGLE action. Here is an example of an acceptable output:
8
+
9
+ \`\`\`json
10
+ {{
11
+ "action": $TOOL_NAME
12
+ "action_input": $INPUT
13
+ }}
14
+ \`\`\`
15
+
16
+ Remember to include the surrounding markdown code snippet delimiters (begin with "\`\`\`" json and close with "\`\`\`")!
17
+ `;
18
+ export const FORMAT_INSTRUCTIONS = `You have access to the following tools.
19
+ You must format your inputs to these tools to match their "JSON schema" definitions below.
20
+
21
+ "JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
22
+
23
+ For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
24
+ would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
25
+ Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
26
+
27
+ Here are the JSON Schema instances for the tools you have access to:
28
+
29
+ {tool_schemas}
30
+
31
+ The way you use the tools is as follows:
32
+
33
+ ------------------------
34
+
35
+ ${AGENT_ACTION_FORMAT_INSTRUCTIONS}
36
+
37
+ If you are using a tool, "action_input" must adhere to the tool's input schema, given above.
38
+
39
+ ------------------------
40
+
41
+ ALWAYS use the following format:
42
+
43
+ Question: the input question you must answer
44
+ Thought: you should always think about what to do
45
+ Action:
46
+ \`\`\`json
47
+ $JSON_BLOB
48
+ \`\`\`
49
+ Observation: the result of the action
50
+ ... (this Thought/Action/Observation can repeat N times)
51
+ Thought: I now know the final answer
52
+ Action:
53
+ \`\`\`json
54
+ {{
55
+ "action": "Final Answer",
56
+ "action_input": "Final response to human"
57
+ }}
58
+ \`\`\``;
59
+ export const SUFFIX = `Begin! Reminder to ALWAYS use the above format, and to use tools if appropriate.`;
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LangChainTracer = void 0;
4
+ const async_caller_js_1 = require("../../util/async_caller.cjs");
4
5
  const env_js_1 = require("../../util/env.cjs");
5
6
  const tracer_js_1 = require("./tracer.cjs");
6
7
  class LangChainTracer extends tracer_js_1.BaseTracer {
7
- constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
8
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, } = {}) {
8
9
  super();
9
10
  Object.defineProperty(this, "name", {
10
11
  enumerable: true,
@@ -59,6 +60,12 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
59
60
  writable: true,
60
61
  value: void 0
61
62
  });
63
+ Object.defineProperty(this, "caller", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
62
69
  // eslint-disable-next-line no-process-env
63
70
  if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
64
71
  // eslint-disable-next-line no-process-env
@@ -79,6 +86,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
79
86
  "default";
80
87
  this.sessionExtra = sessionExtra;
81
88
  this.exampleId = exampleId;
89
+ this.caller = new async_caller_js_1.AsyncCaller(callerParams ?? {});
82
90
  }
83
91
  async ensureSession() {
84
92
  if (this.session) {
@@ -86,7 +94,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
86
94
  }
87
95
  const tenantId = await this.ensureTenantId();
88
96
  const endpoint = `${this.endpoint}/sessions?upsert=true`;
89
- const res = await fetch(endpoint, {
97
+ const res = await this.caller.call(fetch, endpoint, {
90
98
  method: "POST",
91
99
  headers: this.headers,
92
100
  body: JSON.stringify({
@@ -108,7 +116,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
108
116
  return this.tenantId;
109
117
  }
110
118
  const endpoint = `${this.endpoint}/tenants`;
111
- const response = await fetch(endpoint, {
119
+ const response = await this.caller.call(fetch, endpoint, {
112
120
  method: "GET",
113
121
  headers: this.headers,
114
122
  });
@@ -149,7 +157,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
149
157
  async persistRun(run) {
150
158
  const persistedRun = await this._convertToCreate(run, this.exampleId);
151
159
  const endpoint = `${this.endpoint}/runs`;
152
- const response = await fetch(endpoint, {
160
+ const response = await this.caller.call(fetch, endpoint, {
153
161
  method: "POST",
154
162
  headers: this.headers,
155
163
  body: JSON.stringify(persistedRun),
@@ -1,3 +1,4 @@
1
+ import { AsyncCaller, AsyncCallerParams } from "../../util/async_caller.js";
1
2
  import { BaseTracer, Run, BaseRun } from "./tracer.js";
2
3
  export interface RunCreate extends BaseRun {
3
4
  child_runs: this[];
@@ -21,6 +22,7 @@ export interface LangChainTracerFields {
21
22
  tenantId?: string;
22
23
  sessionName?: string;
23
24
  sessionExtra?: Record<string, unknown>;
25
+ callerParams?: AsyncCallerParams;
24
26
  }
25
27
  export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
26
28
  name: string;
@@ -31,7 +33,8 @@ export declare class LangChainTracer extends BaseTracer implements LangChainTrac
31
33
  protected session: TracerSession;
32
34
  exampleId?: string;
33
35
  tenantId?: string;
34
- constructor({ exampleId, tenantId, sessionName, sessionExtra, }?: LangChainTracerFields);
36
+ caller: AsyncCaller;
37
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, }?: LangChainTracerFields);
35
38
  protected ensureSession(): Promise<TracerSession>;
36
39
  protected ensureTenantId(): Promise<string>;
37
40
  private _convertToCreate;
@@ -1,7 +1,8 @@
1
+ import { AsyncCaller } from "../../util/async_caller.js";
1
2
  import { getRuntimeEnvironment } from "../../util/env.js";
2
3
  import { BaseTracer } from "./tracer.js";
3
4
  export class LangChainTracer extends BaseTracer {
4
- constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
5
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, } = {}) {
5
6
  super();
6
7
  Object.defineProperty(this, "name", {
7
8
  enumerable: true,
@@ -56,6 +57,12 @@ export class LangChainTracer extends BaseTracer {
56
57
  writable: true,
57
58
  value: void 0
58
59
  });
60
+ Object.defineProperty(this, "caller", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
59
66
  // eslint-disable-next-line no-process-env
60
67
  if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
61
68
  // eslint-disable-next-line no-process-env
@@ -76,6 +83,7 @@ export class LangChainTracer extends BaseTracer {
76
83
  "default";
77
84
  this.sessionExtra = sessionExtra;
78
85
  this.exampleId = exampleId;
86
+ this.caller = new AsyncCaller(callerParams ?? {});
79
87
  }
80
88
  async ensureSession() {
81
89
  if (this.session) {
@@ -83,7 +91,7 @@ export class LangChainTracer extends BaseTracer {
83
91
  }
84
92
  const tenantId = await this.ensureTenantId();
85
93
  const endpoint = `${this.endpoint}/sessions?upsert=true`;
86
- const res = await fetch(endpoint, {
94
+ const res = await this.caller.call(fetch, endpoint, {
87
95
  method: "POST",
88
96
  headers: this.headers,
89
97
  body: JSON.stringify({
@@ -105,7 +113,7 @@ export class LangChainTracer extends BaseTracer {
105
113
  return this.tenantId;
106
114
  }
107
115
  const endpoint = `${this.endpoint}/tenants`;
108
- const response = await fetch(endpoint, {
116
+ const response = await this.caller.call(fetch, endpoint, {
109
117
  method: "GET",
110
118
  headers: this.headers,
111
119
  });
@@ -146,7 +154,7 @@ export class LangChainTracer extends BaseTracer {
146
154
  async persistRun(run) {
147
155
  const persistedRun = await this._convertToCreate(run, this.exampleId);
148
156
  const endpoint = `${this.endpoint}/runs`;
149
- const response = await fetch(endpoint, {
157
+ const response = await this.caller.call(fetch, endpoint, {
150
158
  method: "POST",
151
159
  headers: this.headers,
152
160
  body: JSON.stringify(persistedRun),
@@ -334,6 +334,10 @@ class CallbackManager extends BaseCallbackManager {
334
334
  ? localHandlers.map(ensureHandler)
335
335
  : localHandlers?.handlers, false);
336
336
  }
337
+ const verboseEnabled = (typeof process !== "undefined"
338
+ ? // eslint-disable-next-line no-process-env
339
+ process.env?.LANGCHAIN_VERBOSE !== undefined
340
+ : false) || options?.verbose;
337
341
  const tracingV2Enabled = typeof process !== "undefined"
338
342
  ? // eslint-disable-next-line no-process-env
339
343
  process.env?.LANGCHAIN_TRACING_V2 !== undefined
@@ -343,11 +347,11 @@ class CallbackManager extends BaseCallbackManager {
343
347
  ? // eslint-disable-next-line no-process-env
344
348
  process.env?.LANGCHAIN_TRACING !== undefined
345
349
  : false);
346
- if (options?.verbose || tracingEnabled) {
350
+ if (verboseEnabled || tracingEnabled) {
347
351
  if (!callbackManager) {
348
352
  callbackManager = new CallbackManager();
349
353
  }
350
- if (options?.verbose &&
354
+ if (verboseEnabled &&
351
355
  !callbackManager.handlers.some((handler) => handler.name === console_js_1.ConsoleCallbackHandler.prototype.name)) {
352
356
  const consoleHandler = new console_js_1.ConsoleCallbackHandler();
353
357
  callbackManager.addHandler(consoleHandler, true);
@@ -327,6 +327,10 @@ export class CallbackManager extends BaseCallbackManager {
327
327
  ? localHandlers.map(ensureHandler)
328
328
  : localHandlers?.handlers, false);
329
329
  }
330
+ const verboseEnabled = (typeof process !== "undefined"
331
+ ? // eslint-disable-next-line no-process-env
332
+ process.env?.LANGCHAIN_VERBOSE !== undefined
333
+ : false) || options?.verbose;
330
334
  const tracingV2Enabled = typeof process !== "undefined"
331
335
  ? // eslint-disable-next-line no-process-env
332
336
  process.env?.LANGCHAIN_TRACING_V2 !== undefined
@@ -336,11 +340,11 @@ export class CallbackManager extends BaseCallbackManager {
336
340
  ? // eslint-disable-next-line no-process-env
337
341
  process.env?.LANGCHAIN_TRACING !== undefined
338
342
  : false);
339
- if (options?.verbose || tracingEnabled) {
343
+ if (verboseEnabled || tracingEnabled) {
340
344
  if (!callbackManager) {
341
345
  callbackManager = new CallbackManager();
342
346
  }
343
- if (options?.verbose &&
347
+ if (verboseEnabled &&
344
348
  !callbackManager.handlers.some((handler) => handler.name === ConsoleCallbackHandler.prototype.name)) {
345
349
  const consoleHandler = new ConsoleCallbackHandler();
346
350
  callbackManager.addHandler(consoleHandler, true);
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadQueryContstructorChain = exports.formatAttributeInfo = exports.StructuredQueryOutputParser = exports.EXAMPLE_PROMPT = exports.DEFAULT_SUFFIX = exports.DEFAULT_SCHEMA = exports.DEFAULT_PREFIX = exports.DEFAULT_EXAMPLES = exports.QueryTransformer = void 0;
4
+ const zod_1 = require("zod");
5
+ const parser_js_1 = require("./parser.cjs");
6
+ Object.defineProperty(exports, "QueryTransformer", { enumerable: true, get: function () { return parser_js_1.QueryTransformer; } });
7
+ const ir_js_1 = require("./ir.cjs");
8
+ const prompt_js_1 = require("./prompt.cjs");
9
+ Object.defineProperty(exports, "DEFAULT_EXAMPLES", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_EXAMPLES; } });
10
+ Object.defineProperty(exports, "DEFAULT_PREFIX", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_PREFIX; } });
11
+ Object.defineProperty(exports, "DEFAULT_SCHEMA", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_SCHEMA; } });
12
+ Object.defineProperty(exports, "DEFAULT_SUFFIX", { enumerable: true, get: function () { return prompt_js_1.DEFAULT_SUFFIX; } });
13
+ Object.defineProperty(exports, "EXAMPLE_PROMPT", { enumerable: true, get: function () { return prompt_js_1.EXAMPLE_PROMPT; } });
14
+ const template_js_1 = require("../../prompts/template.cjs");
15
+ const llm_chain_js_1 = require("../llm_chain.cjs");
16
+ const few_shot_js_1 = require("../../prompts/few_shot.cjs");
17
+ const structured_js_1 = require("../../output_parsers/structured.cjs");
18
+ const queryInputSchema = /* #__PURE__ */ zod_1.z.object({
19
+ query: /* #__PURE__ */ zod_1.z
20
+ .string()
21
+ .describe("text string to compare to document contents"),
22
+ filter: /* #__PURE__ */ zod_1.z
23
+ .string()
24
+ .optional()
25
+ .describe("logical condition statement for filtering documents"),
26
+ });
27
+ class StructuredQueryOutputParser extends structured_js_1.AsymmetricStructuredOutputParser {
28
+ constructor(parserFunction) {
29
+ super(queryInputSchema);
30
+ Object.defineProperty(this, "parserFunction", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: parserFunction
35
+ });
36
+ }
37
+ async outputProcessor(input) {
38
+ return this.parserFunction(input.query, input.filter);
39
+ }
40
+ static fromComponents(allowedComparators = [], allowedOperators = []) {
41
+ const queryTransformer = new parser_js_1.QueryTransformer(allowedComparators, allowedOperators);
42
+ return new StructuredQueryOutputParser(async (query, filter) => {
43
+ let myQuery = query;
44
+ if (myQuery.length === 0) {
45
+ myQuery = " ";
46
+ }
47
+ if (filter === "NO_FILTER" || filter === undefined) {
48
+ return new ir_js_1.StructuredQuery(query);
49
+ }
50
+ else {
51
+ const parsedFilter = await queryTransformer.parse(filter);
52
+ return new ir_js_1.StructuredQuery(query, parsedFilter);
53
+ }
54
+ });
55
+ }
56
+ }
57
+ exports.StructuredQueryOutputParser = StructuredQueryOutputParser;
58
+ function formatAttributeInfo(info) {
59
+ const infoObj = info.reduce((acc, attr) => {
60
+ acc[attr.name] = {
61
+ type: attr.type,
62
+ description: attr.description,
63
+ };
64
+ return acc;
65
+ }, {});
66
+ return JSON.stringify(infoObj, null, 2)
67
+ .replaceAll("{", "{{")
68
+ .replaceAll("}", "}}");
69
+ }
70
+ exports.formatAttributeInfo = formatAttributeInfo;
71
+ const defaultExample = prompt_js_1.DEFAULT_EXAMPLES.map((EXAMPLE) => EXAMPLE);
72
+ function _getPrompt(documentContents, attributeInfo, allowedComparators, allowedOperators, examples = defaultExample) {
73
+ const myAllowedComparators = allowedComparators ?? Object.values(ir_js_1.Comparators);
74
+ const myAllowedOperators = allowedOperators ?? Object.values(ir_js_1.Operators);
75
+ const attributeJSON = formatAttributeInfo(attributeInfo);
76
+ const schema = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_SCHEMA, {
77
+ allowed_comparators: myAllowedComparators.join(" | "),
78
+ allowed_operators: myAllowedOperators.join(" | "),
79
+ });
80
+ const prefix = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_PREFIX, {
81
+ schema,
82
+ });
83
+ const suffix = (0, template_js_1.interpolateFString)(prompt_js_1.DEFAULT_SUFFIX, {
84
+ i: examples.length + 1,
85
+ content: documentContents,
86
+ attributes: attributeJSON,
87
+ });
88
+ const outputParser = StructuredQueryOutputParser.fromComponents(allowedComparators, allowedOperators);
89
+ return new few_shot_js_1.FewShotPromptTemplate({
90
+ examples,
91
+ examplePrompt: prompt_js_1.EXAMPLE_PROMPT,
92
+ inputVariables: ["query"],
93
+ suffix,
94
+ prefix,
95
+ outputParser,
96
+ });
97
+ }
98
+ function loadQueryContstructorChain(opts) {
99
+ const prompt = _getPrompt(opts.documentContents, opts.attributeInfo, opts.allowedComparators, opts.allowedOperators, opts.examples);
100
+ return new llm_chain_js_1.LLMChain({
101
+ llm: opts.llm,
102
+ prompt,
103
+ });
104
+ }
105
+ exports.loadQueryContstructorChain = loadQueryContstructorChain;
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ import { QueryTransformer, TraverseType } from "./parser.js";
3
+ import { Comparator, Operator, StructuredQuery } from "./ir.js";
4
+ import { InputValues } from "../../schema/index.js";
5
+ import { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT } from "./prompt.js";
6
+ import { LLMChain } from "../llm_chain.js";
7
+ import { BaseLanguageModel } from "../../base_language/index.js";
8
+ import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
9
+ import { AttributeInfo } from "../../schema/query_constructor.js";
10
+ export { QueryTransformer, TraverseType };
11
+ export { DEFAULT_EXAMPLES, DEFAULT_PREFIX, DEFAULT_SCHEMA, DEFAULT_SUFFIX, EXAMPLE_PROMPT, };
12
+ declare const queryInputSchema: z.ZodObject<{
13
+ query: z.ZodString;
14
+ filter: z.ZodOptional<z.ZodString>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ query: string;
17
+ filter?: string | undefined;
18
+ }, {
19
+ query: string;
20
+ filter?: string | undefined;
21
+ }>;
22
+ export declare class StructuredQueryOutputParser extends AsymmetricStructuredOutputParser<typeof queryInputSchema, StructuredQuery> {
23
+ private parserFunction;
24
+ constructor(parserFunction: (query: string, filter?: string) => Promise<StructuredQuery>);
25
+ outputProcessor(input: z.infer<typeof queryInputSchema>): Promise<StructuredQuery>;
26
+ static fromComponents(allowedComparators?: Comparator[], allowedOperators?: Operator[]): StructuredQueryOutputParser;
27
+ }
28
+ export declare function formatAttributeInfo(info: AttributeInfo[]): string;
29
+ export type QueryConstructorChainOptions = {
30
+ llm: BaseLanguageModel;
31
+ documentContents: string;
32
+ attributeInfo: AttributeInfo[];
33
+ examples?: InputValues[];
34
+ allowedComparators?: Comparator[];
35
+ allowedOperators?: Operator[];
36
+ };
37
+ export declare function loadQueryContstructorChain(opts: QueryConstructorChainOptions): LLMChain<string>;