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 @@
1
+ module.exports = require('../../dist/chains/query_constructor/ir.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/query_constructor/ir.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/query_constructor/ir.js'
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/chains/query_constructor/index.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/chains/query_constructor/index.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/chains/query_constructor/index.js'
@@ -3,10 +3,11 @@ import { CallbackManager, Callbacks } from "../callbacks/manager.js";
3
3
  import { LLMChain } from "../chains/llm_chain.js";
4
4
  import { BasePromptTemplate } from "../prompts/base.js";
5
5
  import { AgentAction, AgentFinish, AgentStep, BaseChatMessage, ChainValues } from "../schema/index.js";
6
- import { Tool } from "../tools/base.js";
6
+ import { StructuredTool, Tool } from "../tools/base.js";
7
7
  import { AgentActionOutputParser, AgentInput, SerializedAgent, StoppingMethod } from "./types.js";
8
8
  export type OutputParserArgs = Record<string, any>;
9
9
  export declare abstract class BaseAgent {
10
+ ToolType: StructuredTool;
10
11
  abstract get inputKeys(): string[];
11
12
  get returnValues(): string[];
12
13
  get allowedTools(): string[] | undefined;
@@ -121,13 +122,13 @@ export declare abstract class Agent extends BaseSingleActionAgent {
121
122
  *
122
123
  * @returns A PromptTemplate assembled from the given tools and fields.
123
124
  * */
124
- static createPrompt(_tools: Tool[], _fields?: Record<string, any>): BasePromptTemplate;
125
+ static createPrompt(_tools: StructuredTool[], _fields?: Record<string, any>): BasePromptTemplate;
125
126
  /** Construct an agent from an LLM and a list of tools */
126
- static fromLLMAndTools(_llm: BaseLanguageModel, _tools: Tool[], _args?: AgentArgs): Agent;
127
+ static fromLLMAndTools(_llm: BaseLanguageModel, _tools: StructuredTool[], _args?: AgentArgs): Agent;
127
128
  /**
128
129
  * Validate that appropriate tools are passed in
129
130
  */
130
- static validateTools(_tools: Tool[]): void;
131
+ static validateTools(_tools: StructuredTool[]): void;
131
132
  _stop(): string[];
132
133
  /**
133
134
  * Name of tool to use to terminate the chain.
@@ -9,7 +9,7 @@ const llm_chain_js_1 = require("../../../chains/llm_chain.cjs");
9
9
  const index_js_1 = require("../../mrkl/index.cjs");
10
10
  const executor_js_1 = require("../../executor.cjs");
11
11
  class SqlToolkit extends base_js_1.Toolkit {
12
- constructor(db) {
12
+ constructor(db, llm) {
13
13
  super();
14
14
  Object.defineProperty(this, "tools", {
15
15
  enumerable: true,
@@ -34,7 +34,7 @@ class SqlToolkit extends base_js_1.Toolkit {
34
34
  new sql_js_1.QuerySqlTool(db),
35
35
  new sql_js_1.InfoSqlTool(db),
36
36
  new sql_js_1.ListTablesSqlTool(db),
37
- new sql_js_1.QueryCheckerTool(),
37
+ new sql_js_1.QueryCheckerTool({ llm }),
38
38
  ];
39
39
  }
40
40
  }
@@ -12,6 +12,6 @@ export declare class SqlToolkit extends Toolkit {
12
12
  tools: Tool[];
13
13
  db: SqlDatabase;
14
14
  dialect: string;
15
- constructor(db: SqlDatabase);
15
+ constructor(db: SqlDatabase, llm?: BaseLanguageModel);
16
16
  }
17
17
  export declare function createSqlAgent(llm: BaseLanguageModel, toolkit: SqlToolkit, args?: SqlCreatePromptArgs): AgentExecutor;
@@ -6,7 +6,7 @@ import { LLMChain } from "../../../chains/llm_chain.js";
6
6
  import { ZeroShotAgent } from "../../mrkl/index.js";
7
7
  import { AgentExecutor } from "../../executor.js";
8
8
  export class SqlToolkit extends Toolkit {
9
- constructor(db) {
9
+ constructor(db, llm) {
10
10
  super();
11
11
  Object.defineProperty(this, "tools", {
12
12
  enumerable: true,
@@ -31,7 +31,7 @@ export class SqlToolkit extends Toolkit {
31
31
  new QuerySqlTool(db),
32
32
  new InfoSqlTool(db),
33
33
  new ListTablesSqlTool(db),
34
- new QueryCheckerTool(),
34
+ new QueryCheckerTool({ llm }),
35
35
  ];
36
36
  }
37
37
  }
@@ -29,9 +29,9 @@ class ChatAgent extends agent_js_1.Agent {
29
29
  return ["Observation:"];
30
30
  }
31
31
  static validateTools(tools) {
32
- const invalidTool = tools.find((tool) => !tool.description);
33
- if (invalidTool) {
34
- const msg = `Got a tool ${invalidTool.name} without a description.` +
32
+ const descriptionlessTool = tools.find((tool) => !tool.description);
33
+ if (descriptionlessTool) {
34
+ const msg = `Got a tool ${descriptionlessTool.name} without a description.` +
35
35
  ` This agent requires descriptions for all tools.`;
36
36
  throw new Error(msg);
37
37
  }
@@ -22,6 +22,7 @@ export type ChatAgentInput = Optional<AgentInput, "outputParser">;
22
22
  * @augments Agent
23
23
  */
24
24
  export declare class ChatAgent extends Agent {
25
+ ToolType: Tool;
25
26
  constructor(input: ChatAgentInput);
26
27
  _agentType(): "chat-zero-shot-react-description";
27
28
  observationPrefix(): string;
@@ -26,9 +26,9 @@ export class ChatAgent extends Agent {
26
26
  return ["Observation:"];
27
27
  }
28
28
  static validateTools(tools) {
29
- const invalidTool = tools.find((tool) => !tool.description);
30
- if (invalidTool) {
31
- const msg = `Got a tool ${invalidTool.name} without a description.` +
29
+ const descriptionlessTool = tools.find((tool) => !tool.description);
30
+ if (descriptionlessTool) {
31
+ const msg = `Got a tool ${descriptionlessTool.name} without a description.` +
32
32
  ` This agent requires descriptions for all tools.`;
33
33
  throw new Error(msg);
34
34
  }
@@ -30,9 +30,9 @@ class ChatConversationalAgent extends agent_js_1.Agent {
30
30
  return ["Observation:"];
31
31
  }
32
32
  static validateTools(tools) {
33
- const invalidTool = tools.find((tool) => !tool.description);
34
- if (invalidTool) {
35
- const msg = `Got a tool ${invalidTool.name} without a description.` +
33
+ const descriptionlessTool = tools.find((tool) => !tool.description);
34
+ if (descriptionlessTool) {
35
+ const msg = `Got a tool ${descriptionlessTool.name} without a description.` +
36
36
  ` This agent requires descriptions for all tools.`;
37
37
  throw new Error(msg);
38
38
  }
@@ -47,8 +47,15 @@ class ChatConversationalAgent extends agent_js_1.Agent {
47
47
  }
48
48
  return thoughts;
49
49
  }
50
- static getDefaultOutputParser(_fields) {
51
- return new outputParser_js_1.ChatConversationalAgentOutputParser();
50
+ static getDefaultOutputParser(fields) {
51
+ if (fields?.llm) {
52
+ return outputParser_js_1.ChatConversationalAgentOutputParserWithRetries.fromLLM(fields.llm, {
53
+ toolNames: fields.toolNames,
54
+ });
55
+ }
56
+ return new outputParser_js_1.ChatConversationalAgentOutputParserWithRetries({
57
+ toolNames: fields?.toolNames,
58
+ });
52
59
  }
53
60
  /**
54
61
  * Create prompt in the style of the ChatConversationAgent.
@@ -57,39 +64,48 @@ class ChatConversationalAgent extends agent_js_1.Agent {
57
64
  * @param args - Arguments to create the prompt with.
58
65
  * @param args.systemMessage - String to put before the list of tools.
59
66
  * @param args.humanMessage - String to put after the list of tools.
67
+ * @param args.outputParser - Output parser to use for formatting.
60
68
  */
61
69
  static createPrompt(tools, args) {
62
70
  const systemMessage = (args?.systemMessage ?? prompt_js_1.DEFAULT_PREFIX) + prompt_js_1.PREFIX_END;
63
71
  const humanMessage = args?.humanMessage ?? prompt_js_1.DEFAULT_SUFFIX;
64
- const outputParser = args?.outputParser ?? new outputParser_js_1.ChatConversationalAgentOutputParser();
65
72
  const toolStrings = tools
66
73
  .map((tool) => `${tool.name}: ${tool.description}`)
67
74
  .join("\n");
68
- const formatInstructions = (0, template_js_1.renderTemplate)(humanMessage, "f-string", {
69
- format_instructions: outputParser.getFormatInstructions(),
75
+ const toolNames = tools.map((tool) => tool.name);
76
+ const outputParser = args?.outputParser ??
77
+ ChatConversationalAgent.getDefaultOutputParser({ toolNames });
78
+ const formatInstructions = outputParser.getFormatInstructions({
79
+ toolNames,
70
80
  });
71
- const toolNames = tools.map((tool) => tool.name).join("\n");
72
- const finalPrompt = (0, template_js_1.renderTemplate)(formatInstructions, "f-string", {
81
+ const renderedHumanMessage = (0, template_js_1.renderTemplate)(humanMessage, "f-string", {
82
+ format_instructions: formatInstructions,
73
83
  tools: toolStrings,
74
- tool_names: toolNames,
75
84
  });
76
85
  const messages = [
77
86
  chat_js_1.SystemMessagePromptTemplate.fromTemplate(systemMessage),
78
87
  new chat_js_1.MessagesPlaceholder("chat_history"),
79
- chat_js_1.HumanMessagePromptTemplate.fromTemplate(finalPrompt),
88
+ chat_js_1.HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
80
89
  new chat_js_1.MessagesPlaceholder("agent_scratchpad"),
81
90
  ];
82
91
  return chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
83
92
  }
84
93
  static fromLLMAndTools(llm, tools, args) {
85
94
  ChatConversationalAgent.validateTools(tools);
86
- const prompt = ChatConversationalAgent.createPrompt(tools, args);
95
+ const outputParser = args?.outputParser ??
96
+ ChatConversationalAgent.getDefaultOutputParser({
97
+ llm,
98
+ toolNames: tools.map((tool) => tool.name),
99
+ });
100
+ const prompt = ChatConversationalAgent.createPrompt(tools, {
101
+ ...args,
102
+ outputParser,
103
+ });
87
104
  const chain = new llm_chain_js_1.LLMChain({
88
105
  prompt,
89
106
  llm,
90
107
  callbacks: args?.callbacks ?? args?.callbackManager,
91
108
  });
92
- const outputParser = args?.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
93
109
  return new ChatConversationalAgent({
94
110
  llmChain: chain,
95
111
  outputParser,
@@ -21,6 +21,7 @@ export type ChatConversationalAgentInput = Optional<AgentInput, "outputParser">;
21
21
  * @augments Agent
22
22
  */
23
23
  export declare class ChatConversationalAgent extends Agent {
24
+ ToolType: Tool;
24
25
  constructor(input: ChatConversationalAgentInput);
25
26
  _agentType(): "chat-conversational-react-description";
26
27
  observationPrefix(): string;
@@ -28,7 +29,9 @@ export declare class ChatConversationalAgent extends Agent {
28
29
  _stop(): string[];
29
30
  static validateTools(tools: Tool[]): void;
30
31
  constructScratchPad(steps: AgentStep[]): Promise<BaseChatMessage[]>;
31
- static getDefaultOutputParser(_fields?: OutputParserArgs): AgentActionOutputParser;
32
+ static getDefaultOutputParser(fields?: OutputParserArgs & {
33
+ toolNames: string[];
34
+ }): AgentActionOutputParser;
32
35
  /**
33
36
  * Create prompt in the style of the ChatConversationAgent.
34
37
  *
@@ -36,6 +39,7 @@ export declare class ChatConversationalAgent extends Agent {
36
39
  * @param args - Arguments to create the prompt with.
37
40
  * @param args.systemMessage - String to put before the list of tools.
38
41
  * @param args.humanMessage - String to put after the list of tools.
42
+ * @param args.outputParser - Output parser to use for formatting.
39
43
  */
40
44
  static createPrompt(tools: Tool[], args?: ChatConversationalCreatePromptArgs): ChatPromptTemplate;
41
45
  static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: ChatConversationalCreatePromptArgs & AgentArgs): ChatConversationalAgent;
@@ -3,7 +3,7 @@ import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, Sy
3
3
  import { renderTemplate } from "../../prompts/template.js";
4
4
  import { AIChatMessage, HumanChatMessage, } from "../../schema/index.js";
5
5
  import { Agent } from "../agent.js";
6
- import { ChatConversationalAgentOutputParser } from "./outputParser.js";
6
+ import { ChatConversationalAgentOutputParserWithRetries } from "./outputParser.js";
7
7
  import { PREFIX_END, DEFAULT_PREFIX, DEFAULT_SUFFIX, TEMPLATE_TOOL_RESPONSE, } from "./prompt.js";
8
8
  /**
9
9
  * Agent for the MRKL chain.
@@ -27,9 +27,9 @@ export class ChatConversationalAgent extends Agent {
27
27
  return ["Observation:"];
28
28
  }
29
29
  static validateTools(tools) {
30
- const invalidTool = tools.find((tool) => !tool.description);
31
- if (invalidTool) {
32
- const msg = `Got a tool ${invalidTool.name} without a description.` +
30
+ const descriptionlessTool = tools.find((tool) => !tool.description);
31
+ if (descriptionlessTool) {
32
+ const msg = `Got a tool ${descriptionlessTool.name} without a description.` +
33
33
  ` This agent requires descriptions for all tools.`;
34
34
  throw new Error(msg);
35
35
  }
@@ -44,8 +44,15 @@ export class ChatConversationalAgent extends Agent {
44
44
  }
45
45
  return thoughts;
46
46
  }
47
- static getDefaultOutputParser(_fields) {
48
- return new ChatConversationalAgentOutputParser();
47
+ static getDefaultOutputParser(fields) {
48
+ if (fields?.llm) {
49
+ return ChatConversationalAgentOutputParserWithRetries.fromLLM(fields.llm, {
50
+ toolNames: fields.toolNames,
51
+ });
52
+ }
53
+ return new ChatConversationalAgentOutputParserWithRetries({
54
+ toolNames: fields?.toolNames,
55
+ });
49
56
  }
50
57
  /**
51
58
  * Create prompt in the style of the ChatConversationAgent.
@@ -54,39 +61,48 @@ export class ChatConversationalAgent extends Agent {
54
61
  * @param args - Arguments to create the prompt with.
55
62
  * @param args.systemMessage - String to put before the list of tools.
56
63
  * @param args.humanMessage - String to put after the list of tools.
64
+ * @param args.outputParser - Output parser to use for formatting.
57
65
  */
58
66
  static createPrompt(tools, args) {
59
67
  const systemMessage = (args?.systemMessage ?? DEFAULT_PREFIX) + PREFIX_END;
60
68
  const humanMessage = args?.humanMessage ?? DEFAULT_SUFFIX;
61
- const outputParser = args?.outputParser ?? new ChatConversationalAgentOutputParser();
62
69
  const toolStrings = tools
63
70
  .map((tool) => `${tool.name}: ${tool.description}`)
64
71
  .join("\n");
65
- const formatInstructions = renderTemplate(humanMessage, "f-string", {
66
- format_instructions: outputParser.getFormatInstructions(),
72
+ const toolNames = tools.map((tool) => tool.name);
73
+ const outputParser = args?.outputParser ??
74
+ ChatConversationalAgent.getDefaultOutputParser({ toolNames });
75
+ const formatInstructions = outputParser.getFormatInstructions({
76
+ toolNames,
67
77
  });
68
- const toolNames = tools.map((tool) => tool.name).join("\n");
69
- const finalPrompt = renderTemplate(formatInstructions, "f-string", {
78
+ const renderedHumanMessage = renderTemplate(humanMessage, "f-string", {
79
+ format_instructions: formatInstructions,
70
80
  tools: toolStrings,
71
- tool_names: toolNames,
72
81
  });
73
82
  const messages = [
74
83
  SystemMessagePromptTemplate.fromTemplate(systemMessage),
75
84
  new MessagesPlaceholder("chat_history"),
76
- HumanMessagePromptTemplate.fromTemplate(finalPrompt),
85
+ HumanMessagePromptTemplate.fromTemplate(renderedHumanMessage),
77
86
  new MessagesPlaceholder("agent_scratchpad"),
78
87
  ];
79
88
  return ChatPromptTemplate.fromPromptMessages(messages);
80
89
  }
81
90
  static fromLLMAndTools(llm, tools, args) {
82
91
  ChatConversationalAgent.validateTools(tools);
83
- const prompt = ChatConversationalAgent.createPrompt(tools, args);
92
+ const outputParser = args?.outputParser ??
93
+ ChatConversationalAgent.getDefaultOutputParser({
94
+ llm,
95
+ toolNames: tools.map((tool) => tool.name),
96
+ });
97
+ const prompt = ChatConversationalAgent.createPrompt(tools, {
98
+ ...args,
99
+ outputParser,
100
+ });
84
101
  const chain = new LLMChain({
85
102
  prompt,
86
103
  llm,
87
104
  callbacks: args?.callbacks ?? args?.callbackManager,
88
105
  });
89
- const outputParser = args?.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
90
106
  return new ChatConversationalAgent({
91
107
  llmChain: chain,
92
108
  outputParser,
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatConversationalAgentOutputParser = void 0;
3
+ exports.ChatConversationalAgentOutputParserWithRetries = exports.ChatConversationalAgentOutputParser = void 0;
4
+ const output_parser_js_1 = require("../../schema/output_parser.cjs");
5
+ const template_js_1 = require("../../prompts/template.cjs");
4
6
  const types_js_1 = require("../types.cjs");
5
7
  const prompt_js_1 = require("./prompt.cjs");
8
+ const fix_js_1 = require("../../output_parsers/fix.cjs");
6
9
  class ChatConversationalAgentOutputParser 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
+ }
7
19
  async parse(text) {
8
20
  let jsonOutput = text.trim();
9
21
  if (jsonOutput.includes("```json")) {
@@ -17,15 +29,75 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
17
29
  if (lastIndex !== -1) {
18
30
  jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
19
31
  }
20
- const response = JSON.parse(jsonOutput);
21
- const { action, action_input } = response;
22
- if (action === "Final Answer") {
23
- return { returnValues: { output: action_input }, log: text };
32
+ try {
33
+ const response = JSON.parse(jsonOutput);
34
+ const { action, action_input } = response;
35
+ if (action === "Final Answer") {
36
+ return { returnValues: { output: action_input }, log: text };
37
+ }
38
+ return { tool: action, toolInput: action_input, log: text };
39
+ }
40
+ catch (e) {
41
+ throw new output_parser_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
24
42
  }
25
- return { tool: action, toolInput: action_input, log: text };
26
43
  }
27
44
  getFormatInstructions() {
28
- return prompt_js_1.FORMAT_INSTRUCTIONS;
45
+ return (0, template_js_1.renderTemplate)(prompt_js_1.FORMAT_INSTRUCTIONS, "f-string", {
46
+ tool_names: this.toolNames.join(", "),
47
+ });
29
48
  }
30
49
  }
31
50
  exports.ChatConversationalAgentOutputParser = ChatConversationalAgentOutputParser;
51
+ class ChatConversationalAgentOutputParserWithRetries extends types_js_1.AgentActionOutputParser {
52
+ constructor(fields) {
53
+ super();
54
+ Object.defineProperty(this, "baseParser", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "outputFixingParser", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ Object.defineProperty(this, "toolNames", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: []
71
+ });
72
+ this.toolNames = fields.toolNames ?? this.toolNames;
73
+ this.baseParser =
74
+ fields?.baseParser ??
75
+ new ChatConversationalAgentOutputParser(this.toolNames);
76
+ this.outputFixingParser = fields?.outputFixingParser;
77
+ }
78
+ getFormatInstructions(options) {
79
+ if (options.raw) {
80
+ return prompt_js_1.FORMAT_INSTRUCTIONS;
81
+ }
82
+ return (0, template_js_1.renderTemplate)(prompt_js_1.FORMAT_INSTRUCTIONS, "f-string", {
83
+ tool_names: options.toolNames.join(", "),
84
+ });
85
+ }
86
+ async parse(text) {
87
+ if (this.outputFixingParser !== undefined) {
88
+ return this.outputFixingParser.parse(text);
89
+ }
90
+ return this.baseParser.parse(text);
91
+ }
92
+ static fromLLM(llm, options) {
93
+ const baseParser = options.baseParser ??
94
+ new ChatConversationalAgentOutputParser(options.toolNames ?? []);
95
+ const outputFixingParser = fix_js_1.OutputFixingParser.fromLLM(llm, baseParser);
96
+ return new ChatConversationalAgentOutputParserWithRetries({
97
+ baseParser,
98
+ outputFixingParser,
99
+ toolNames: options.toolNames,
100
+ });
101
+ }
102
+ }
103
+ exports.ChatConversationalAgentOutputParserWithRetries = ChatConversationalAgentOutputParserWithRetries;
@@ -1,17 +1,29 @@
1
+ import { FormatInstructionsOptions } from "../../schema/output_parser.js";
1
2
  import { AgentActionOutputParser } from "../types.js";
3
+ import { AgentAction, AgentFinish } from "../../schema/index.js";
4
+ import { OutputFixingParser } from "../../output_parsers/fix.js";
5
+ import { BaseLanguageModel } from "../../base_language/index.js";
6
+ export type ChatConversationalAgentOutputParserFormatInstructionsOptions = FormatInstructionsOptions & {
7
+ toolNames: string[];
8
+ raw?: boolean;
9
+ };
2
10
  export declare class ChatConversationalAgentOutputParser extends AgentActionOutputParser {
3
- parse(text: string): Promise<{
4
- returnValues: {
5
- output: any;
6
- };
7
- log: string;
8
- tool?: undefined;
9
- toolInput?: undefined;
10
- } | {
11
- tool: any;
12
- toolInput: any;
13
- log: string;
14
- returnValues?: undefined;
15
- }>;
11
+ private toolNames;
12
+ constructor(toolNames: string[]);
13
+ parse(text: string): Promise<AgentAction | AgentFinish>;
16
14
  getFormatInstructions(): string;
17
15
  }
16
+ export type ChatConversationalAgentOutputParserArgs = {
17
+ baseParser?: ChatConversationalAgentOutputParser;
18
+ outputFixingParser?: OutputFixingParser<AgentAction | AgentFinish>;
19
+ toolNames?: string[];
20
+ };
21
+ export declare class ChatConversationalAgentOutputParserWithRetries extends AgentActionOutputParser {
22
+ private baseParser;
23
+ private outputFixingParser?;
24
+ private toolNames;
25
+ constructor(fields: ChatConversationalAgentOutputParserArgs);
26
+ getFormatInstructions(options: ChatConversationalAgentOutputParserFormatInstructionsOptions): string;
27
+ parse(text: string): Promise<AgentAction | AgentFinish>;
28
+ static fromLLM(llm: BaseLanguageModel, options: Omit<ChatConversationalAgentOutputParserArgs, "outputFixingParser">): ChatConversationalAgentOutputParserWithRetries;
29
+ }
@@ -1,6 +1,18 @@
1
+ import { OutputParserException, } from "../../schema/output_parser.js";
2
+ import { renderTemplate } from "../../prompts/template.js";
1
3
  import { AgentActionOutputParser } from "../types.js";
2
4
  import { FORMAT_INSTRUCTIONS } from "./prompt.js";
5
+ import { OutputFixingParser } from "../../output_parsers/fix.js";
3
6
  export class ChatConversationalAgentOutputParser 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
+ }
4
16
  async parse(text) {
5
17
  let jsonOutput = text.trim();
6
18
  if (jsonOutput.includes("```json")) {
@@ -14,14 +26,73 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
14
26
  if (lastIndex !== -1) {
15
27
  jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
16
28
  }
17
- const response = JSON.parse(jsonOutput);
18
- const { action, action_input } = response;
19
- if (action === "Final Answer") {
20
- return { returnValues: { output: action_input }, log: text };
29
+ try {
30
+ const response = JSON.parse(jsonOutput);
31
+ const { action, action_input } = response;
32
+ if (action === "Final Answer") {
33
+ return { returnValues: { output: action_input }, log: text };
34
+ }
35
+ return { tool: action, toolInput: action_input, log: text };
36
+ }
37
+ catch (e) {
38
+ throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`);
21
39
  }
22
- return { tool: action, toolInput: action_input, log: text };
23
40
  }
24
41
  getFormatInstructions() {
25
- return FORMAT_INSTRUCTIONS;
42
+ return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
43
+ tool_names: this.toolNames.join(", "),
44
+ });
45
+ }
46
+ }
47
+ export class ChatConversationalAgentOutputParserWithRetries extends AgentActionOutputParser {
48
+ constructor(fields) {
49
+ super();
50
+ Object.defineProperty(this, "baseParser", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "outputFixingParser", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "toolNames", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: []
67
+ });
68
+ this.toolNames = fields.toolNames ?? this.toolNames;
69
+ this.baseParser =
70
+ fields?.baseParser ??
71
+ new ChatConversationalAgentOutputParser(this.toolNames);
72
+ this.outputFixingParser = fields?.outputFixingParser;
73
+ }
74
+ getFormatInstructions(options) {
75
+ if (options.raw) {
76
+ return FORMAT_INSTRUCTIONS;
77
+ }
78
+ return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
79
+ tool_names: options.toolNames.join(", "),
80
+ });
81
+ }
82
+ async parse(text) {
83
+ if (this.outputFixingParser !== undefined) {
84
+ return this.outputFixingParser.parse(text);
85
+ }
86
+ return this.baseParser.parse(text);
87
+ }
88
+ static fromLLM(llm, options) {
89
+ const baseParser = options.baseParser ??
90
+ new ChatConversationalAgentOutputParser(options.toolNames ?? []);
91
+ const outputFixingParser = OutputFixingParser.fromLLM(llm, baseParser);
92
+ return new ChatConversationalAgentOutputParserWithRetries({
93
+ baseParser,
94
+ outputFixingParser,
95
+ toolNames: options.toolNames,
96
+ });
26
97
  }
27
98
  }
@@ -12,7 +12,7 @@ exports.PREFIX_END = ` However, above all else, all responses must adhere to the
12
12
  exports.FORMAT_INSTRUCTIONS = `RESPONSE FORMAT INSTRUCTIONS
13
13
  ----------------------------
14
14
 
15
- When responding to me please, please output a response in one of two formats:
15
+ Output a JSON markdown code snippet containing a valid JSON object in one of two formats:
16
16
 
17
17
  **Option 1:**
18
18
  Use this if you want the human to use a tool.
@@ -20,25 +20,28 @@ Markdown code snippet formatted in the following schema:
20
20
 
21
21
  \`\`\`json
22
22
  {{{{
23
- "action": string \\ The action to take. Must be one of {tool_names}
24
- "action_input": string \\ The input to the action
23
+ "action": string // The action to take. Must be one of [{tool_names}]
24
+ "action_input": string // The input to the action. May be a stringified object.
25
25
  }}}}
26
26
  \`\`\`
27
27
 
28
28
  **Option #2:**
29
- Use this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:
29
+ Use this if you want to respond directly and conversationally to the human. Markdown code snippet formatted in the following schema:
30
30
 
31
31
  \`\`\`json
32
32
  {{{{
33
33
  "action": "Final Answer",
34
- "action_input": string \\ You should put what you want to return to use here and make sure to use valid json newline characters.
34
+ "action_input": string // You should put what you want to return to use here and make sure to use valid json newline characters.
35
35
  }}}}
36
- \`\`\``;
36
+ \`\`\`
37
+
38
+ For both options, remember to always include the surrounding markdown code snippet delimiters (begin with "\`\`\`json" and end with "\`\`\`")!
39
+ `;
37
40
  exports.DEFAULT_SUFFIX = `TOOLS
38
41
  ------
39
42
  Assistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:
40
43
 
41
- {{tools}}
44
+ {tools}
42
45
 
43
46
  {format_instructions}
44
47
 
@@ -46,7 +49,7 @@ USER'S INPUT
46
49
  --------------------
47
50
  Here is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):
48
51
 
49
- {{{{input}}}}`;
52
+ {{input}}`;
50
53
  exports.TEMPLATE_TOOL_RESPONSE = `TOOL RESPONSE:
51
54
  ---------------------
52
55
  {observation}