langchain 0.0.56 → 0.0.57

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 (132) hide show
  1. package/dist/agents/agent.cjs +10 -15
  2. package/dist/agents/agent.d.ts +13 -10
  3. package/dist/agents/agent.js +10 -15
  4. package/dist/agents/chat/index.cjs +15 -21
  5. package/dist/agents/chat/index.d.ts +10 -11
  6. package/dist/agents/chat/index.js +16 -22
  7. package/dist/agents/chat/outputParser.cjs +32 -0
  8. package/dist/agents/chat/outputParser.d.ts +18 -0
  9. package/dist/agents/chat/outputParser.js +28 -0
  10. package/dist/agents/chat_convo/index.cjs +25 -54
  11. package/dist/agents/chat_convo/index.d.ts +14 -21
  12. package/dist/agents/chat_convo/index.js +26 -54
  13. package/dist/agents/chat_convo/outputParser.cjs +32 -0
  14. package/dist/agents/chat_convo/outputParser.d.ts +17 -0
  15. package/dist/agents/chat_convo/outputParser.js +28 -0
  16. package/dist/agents/chat_convo/prompt.cjs +5 -4
  17. package/dist/agents/chat_convo/prompt.d.ts +4 -3
  18. package/dist/agents/chat_convo/prompt.js +4 -3
  19. package/dist/agents/executor.cjs +3 -0
  20. package/dist/agents/executor.d.ts +1 -0
  21. package/dist/agents/executor.js +3 -0
  22. package/dist/agents/index.cjs +28 -22
  23. package/dist/agents/index.d.ts +9 -6
  24. package/dist/agents/index.js +9 -6
  25. package/dist/agents/initialize.cjs +53 -9
  26. package/dist/agents/initialize.d.ts +16 -2
  27. package/dist/agents/initialize.js +46 -3
  28. package/dist/agents/mrkl/index.cjs +21 -23
  29. package/dist/agents/mrkl/index.d.ts +8 -10
  30. package/dist/agents/mrkl/index.js +21 -23
  31. package/dist/agents/mrkl/outputParser.cjs +31 -0
  32. package/dist/agents/mrkl/outputParser.d.ts +18 -0
  33. package/dist/agents/mrkl/outputParser.js +27 -0
  34. package/dist/agents/mrkl/prompt.cjs +3 -4
  35. package/dist/agents/mrkl/prompt.d.ts +1 -1
  36. package/dist/agents/mrkl/prompt.js +2 -2
  37. package/dist/agents/types.d.ts +3 -3
  38. package/dist/chains/analyze_documents_chain.cjs +3 -7
  39. package/dist/chains/analyze_documents_chain.d.ts +1 -1
  40. package/dist/chains/analyze_documents_chain.js +3 -7
  41. package/dist/chains/base.cjs +12 -0
  42. package/dist/chains/base.d.ts +1 -0
  43. package/dist/chains/base.js +12 -0
  44. package/dist/chains/chat_vector_db_chain.cjs +3 -0
  45. package/dist/chains/chat_vector_db_chain.d.ts +1 -0
  46. package/dist/chains/chat_vector_db_chain.js +3 -0
  47. package/dist/chains/combine_docs_chain.cjs +10 -14
  48. package/dist/chains/combine_docs_chain.d.ts +17 -35
  49. package/dist/chains/combine_docs_chain.js +10 -14
  50. package/dist/chains/conversation.cjs +5 -5
  51. package/dist/chains/conversation.d.ts +2 -10
  52. package/dist/chains/conversation.js +5 -5
  53. package/dist/chains/conversational_retrieval_chain.cjs +3 -7
  54. package/dist/chains/conversational_retrieval_chain.d.ts +4 -11
  55. package/dist/chains/conversational_retrieval_chain.js +3 -7
  56. package/dist/chains/index.cjs +3 -1
  57. package/dist/chains/index.d.ts +2 -1
  58. package/dist/chains/index.js +1 -0
  59. package/dist/chains/llm_chain.cjs +3 -0
  60. package/dist/chains/llm_chain.d.ts +1 -0
  61. package/dist/chains/llm_chain.js +3 -0
  62. package/dist/chains/retrieval_qa.cjs +3 -7
  63. package/dist/chains/retrieval_qa.d.ts +3 -10
  64. package/dist/chains/retrieval_qa.js +3 -7
  65. package/dist/chains/serde.d.ts +5 -1
  66. package/dist/chains/simple_sequential_chain.cjs +122 -0
  67. package/dist/chains/simple_sequential_chain.d.ts +59 -0
  68. package/dist/chains/simple_sequential_chain.js +118 -0
  69. package/dist/chains/sql_db/sql_db_chain.cjs +7 -4
  70. package/dist/chains/sql_db/sql_db_chain.d.ts +11 -7
  71. package/dist/chains/sql_db/sql_db_chain.js +7 -4
  72. package/dist/chains/vector_db_qa.cjs +3 -7
  73. package/dist/chains/vector_db_qa.d.ts +4 -12
  74. package/dist/chains/vector_db_qa.js +3 -7
  75. package/dist/document.cjs +2 -2
  76. package/dist/document.d.ts +2 -2
  77. package/dist/document.js +2 -2
  78. package/dist/document_loaders/fs/docx.cjs +2 -0
  79. package/dist/document_loaders/fs/docx.js +2 -0
  80. package/dist/document_loaders/fs/epub.cjs +6 -1
  81. package/dist/document_loaders/fs/epub.js +6 -1
  82. package/dist/document_loaders/fs/pdf.cjs +6 -0
  83. package/dist/document_loaders/fs/pdf.js +6 -0
  84. package/dist/document_loaders/fs/srt.cjs +6 -1
  85. package/dist/document_loaders/fs/srt.js +6 -1
  86. package/dist/document_loaders/fs/unstructured.cjs +1 -1
  87. package/dist/document_loaders/fs/unstructured.js +1 -1
  88. package/dist/memory/buffer_memory.cjs +3 -2
  89. package/dist/memory/buffer_memory.js +3 -2
  90. package/dist/memory/buffer_window_memory.cjs +3 -2
  91. package/dist/memory/buffer_window_memory.js +3 -2
  92. package/dist/memory/chat_memory.cjs +6 -27
  93. package/dist/memory/chat_memory.d.ts +3 -10
  94. package/dist/memory/chat_memory.js +4 -24
  95. package/dist/memory/index.cjs +2 -1
  96. package/dist/memory/index.d.ts +2 -1
  97. package/dist/memory/index.js +2 -1
  98. package/dist/memory/motorhead_memory.cjs +7 -6
  99. package/dist/memory/motorhead_memory.js +7 -6
  100. package/dist/memory/stores/message/in_memory.cjs +29 -0
  101. package/dist/memory/stores/message/in_memory.d.ts +9 -0
  102. package/dist/memory/stores/message/in_memory.js +25 -0
  103. package/dist/memory/summary.cjs +2 -1
  104. package/dist/memory/summary.js +2 -1
  105. package/dist/output_parsers/fix.d.ts +6 -6
  106. package/dist/output_parsers/list.d.ts +1 -2
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.cjs +15 -14
  109. package/dist/output_parsers/structured.d.ts +1 -1
  110. package/dist/output_parsers/structured.js +15 -14
  111. package/dist/prompts/base.d.ts +1 -1
  112. package/dist/prompts/chat.cjs +5 -5
  113. package/dist/prompts/chat.d.ts +1 -1
  114. package/dist/prompts/chat.js +5 -5
  115. package/dist/prompts/prompt.d.ts +1 -1
  116. package/dist/schema/index.d.ts +7 -6
  117. package/dist/text_splitter.cjs +3 -2
  118. package/dist/text_splitter.js +3 -2
  119. package/dist/tools/aws_lambda.cjs +9 -5
  120. package/dist/tools/aws_lambda.d.ts +3 -3
  121. package/dist/tools/aws_lambda.js +9 -5
  122. package/dist/tools/webbrowser.cjs +4 -2
  123. package/dist/tools/webbrowser.js +4 -2
  124. package/dist/types/type-utils.cjs +2 -0
  125. package/dist/types/type-utils.d.ts +1 -0
  126. package/dist/types/type-utils.js +1 -0
  127. package/dist/vectorstores/milvus.cjs +6 -5
  128. package/dist/vectorstores/milvus.d.ts +1 -4
  129. package/dist/vectorstores/milvus.js +6 -5
  130. package/dist/vectorstores/prisma.cjs +3 -3
  131. package/dist/vectorstores/prisma.js +3 -3
  132. package/package.json +1 -1
@@ -126,6 +126,12 @@ class Agent extends BaseSingleActionAgent {
126
126
  writable: true,
127
127
  value: void 0
128
128
  });
129
+ Object.defineProperty(this, "outputParser", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: void 0
134
+ });
129
135
  Object.defineProperty(this, "_allowedTools", {
130
136
  enumerable: true,
131
137
  configurable: true,
@@ -134,11 +140,12 @@ class Agent extends BaseSingleActionAgent {
134
140
  });
135
141
  this.llmChain = input.llmChain;
136
142
  this._allowedTools = input.allowedTools;
143
+ this.outputParser = input.outputParser;
137
144
  }
138
145
  /**
139
- * Extract tool and tool input from LLM output.
146
+ * Get the default output parser for this agent.
140
147
  */
141
- async extractToolAndInput(_input) {
148
+ static getDefaultOutputParser() {
142
149
  throw new Error("Not implemented");
143
150
  }
144
151
  /**
@@ -194,19 +201,7 @@ class Agent extends BaseSingleActionAgent {
194
201
  newInputs.stop = this._stop();
195
202
  }
196
203
  const output = await this.llmChain.predict(newInputs);
197
- const parsed = await this.extractToolAndInput(output);
198
- if (!parsed) {
199
- throw new ParseError(`Invalid output: ${output}`, output);
200
- }
201
- const action = {
202
- tool: parsed.tool,
203
- toolInput: parsed.input,
204
- log: output,
205
- };
206
- if (action.tool === this.finishToolName()) {
207
- return { returnValues: { output: action.toolInput }, log: action.log };
208
- }
209
- return action;
204
+ return this.outputParser.parse(output);
210
205
  }
211
206
  /**
212
207
  * Decide what to do given some input.
@@ -1,9 +1,10 @@
1
1
  import { BaseLanguageModel } from "../base_language/index.js";
2
+ import { CallbackManager } from "../callbacks/base.js";
2
3
  import { LLMChain } from "../chains/llm_chain.js";
3
4
  import { BasePromptTemplate } from "../prompts/base.js";
4
- import { AgentAction, AgentFinish, AgentStep, ChainValues, BaseChatMessage } from "../schema/index.js";
5
- import { AgentInput, SerializedAgent, StoppingMethod, AgentActionOutputParser } from "./types.js";
5
+ import { AgentAction, AgentFinish, AgentStep, BaseChatMessage, ChainValues } from "../schema/index.js";
6
6
  import { Tool } from "../tools/base.js";
7
+ import { AgentActionOutputParser, AgentInput, SerializedAgent, StoppingMethod } from "./types.js";
7
8
  export declare abstract class BaseAgent {
8
9
  abstract get inputKeys(): string[];
9
10
  get returnValues(): string[];
@@ -70,6 +71,10 @@ export declare class LLMSingleActionAgent extends BaseSingleActionAgent {
70
71
  */
71
72
  plan(steps: AgentStep[], inputs: ChainValues): Promise<AgentAction | AgentFinish>;
72
73
  }
74
+ export interface AgentArgs {
75
+ outputParser?: AgentActionOutputParser;
76
+ callbackManager?: CallbackManager;
77
+ }
73
78
  /**
74
79
  * Class responsible for calling a language model and deciding an action.
75
80
  *
@@ -79,17 +84,11 @@ export declare class LLMSingleActionAgent extends BaseSingleActionAgent {
79
84
  */
80
85
  export declare abstract class Agent extends BaseSingleActionAgent {
81
86
  llmChain: LLMChain;
87
+ outputParser: AgentActionOutputParser;
82
88
  private _allowedTools?;
83
89
  get allowedTools(): string[] | undefined;
84
90
  get inputKeys(): string[];
85
91
  constructor(input: AgentInput);
86
- /**
87
- * Extract tool and tool input from LLM output.
88
- */
89
- extractToolAndInput(_input: string): Promise<{
90
- tool: string;
91
- input: string;
92
- } | null>;
93
92
  /**
94
93
  * Prefix to append the observation with.
95
94
  */
@@ -102,6 +101,10 @@ export declare abstract class Agent extends BaseSingleActionAgent {
102
101
  * Return the string type key uniquely identifying this class of agent.
103
102
  */
104
103
  abstract _agentType(): string;
104
+ /**
105
+ * Get the default output parser for this agent.
106
+ */
107
+ static getDefaultOutputParser(): AgentActionOutputParser;
105
108
  /**
106
109
  * Create a prompt for this class
107
110
  *
@@ -112,7 +115,7 @@ export declare abstract class Agent extends BaseSingleActionAgent {
112
115
  * */
113
116
  static createPrompt(_tools: Tool[], _fields?: Record<string, any>): BasePromptTemplate;
114
117
  /** Construct an agent from an LLM and a list of tools */
115
- static fromLLMAndTools(_llm: BaseLanguageModel, _tools: Tool[], _args?: Record<string, any>): Agent;
118
+ static fromLLMAndTools(_llm: BaseLanguageModel, _tools: Tool[], _args?: AgentArgs): Agent;
116
119
  /**
117
120
  * Validate that appropriate tools are passed in
118
121
  */
@@ -119,6 +119,12 @@ export class Agent extends BaseSingleActionAgent {
119
119
  writable: true,
120
120
  value: void 0
121
121
  });
122
+ Object.defineProperty(this, "outputParser", {
123
+ enumerable: true,
124
+ configurable: true,
125
+ writable: true,
126
+ value: void 0
127
+ });
122
128
  Object.defineProperty(this, "_allowedTools", {
123
129
  enumerable: true,
124
130
  configurable: true,
@@ -127,11 +133,12 @@ export class Agent extends BaseSingleActionAgent {
127
133
  });
128
134
  this.llmChain = input.llmChain;
129
135
  this._allowedTools = input.allowedTools;
136
+ this.outputParser = input.outputParser;
130
137
  }
131
138
  /**
132
- * Extract tool and tool input from LLM output.
139
+ * Get the default output parser for this agent.
133
140
  */
134
- async extractToolAndInput(_input) {
141
+ static getDefaultOutputParser() {
135
142
  throw new Error("Not implemented");
136
143
  }
137
144
  /**
@@ -187,19 +194,7 @@ export class Agent extends BaseSingleActionAgent {
187
194
  newInputs.stop = this._stop();
188
195
  }
189
196
  const output = await this.llmChain.predict(newInputs);
190
- const parsed = await this.extractToolAndInput(output);
191
- if (!parsed) {
192
- throw new ParseError(`Invalid output: ${output}`, output);
193
- }
194
- const action = {
195
- tool: parsed.tool,
196
- toolInput: parsed.input,
197
- log: output,
198
- };
199
- if (action.tool === this.finishToolName()) {
200
- return { returnValues: { output: action.toolInput }, log: action.log };
201
- }
202
- return action;
197
+ return this.outputParser.parse(output);
203
198
  }
204
199
  /**
205
200
  * Decide what to do given some input.
@@ -2,20 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ChatAgent = void 0;
4
4
  const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
5
- const agent_js_1 = require("../agent.cjs");
6
5
  const chat_js_1 = require("../../prompts/chat.cjs");
6
+ const agent_js_1 = require("../agent.cjs");
7
+ const outputParser_js_1 = require("./outputParser.cjs");
7
8
  const prompt_js_1 = require("./prompt.cjs");
8
- const FINAL_ANSWER_ACTION = "Final Answer:";
9
9
  /**
10
10
  * Agent for the MRKL chain.
11
11
  * @augments Agent
12
12
  */
13
13
  class ChatAgent extends agent_js_1.Agent {
14
14
  constructor(input) {
15
- super(input);
15
+ const outputParser = input?.outputParser ?? ChatAgent.getDefaultOutputParser();
16
+ super({ ...input, outputParser });
16
17
  }
17
18
  _agentType() {
18
- return "zero-shot-react-description";
19
+ return "chat-zero-shot-react-description";
19
20
  }
20
21
  observationPrefix() {
21
22
  return "Observation: ";
@@ -34,6 +35,9 @@ class ChatAgent extends agent_js_1.Agent {
34
35
  throw new Error(msg);
35
36
  }
36
37
  }
38
+ static getDefaultOutputParser() {
39
+ return new outputParser_js_1.ChatAgentOutputParser();
40
+ }
37
41
  constructScratchPad(steps) {
38
42
  const agentScratchpad = super.constructScratchPad(steps);
39
43
  if (agentScratchpad) {
@@ -64,27 +68,17 @@ class ChatAgent extends agent_js_1.Agent {
64
68
  static fromLLMAndTools(llm, tools, args) {
65
69
  ChatAgent.validateTools(tools);
66
70
  const prompt = ChatAgent.createPrompt(tools, args);
67
- const chain = new llm_chain_js_1.LLMChain({ prompt, llm });
71
+ const chain = new llm_chain_js_1.LLMChain({
72
+ prompt,
73
+ llm,
74
+ callbackManager: args?.callbackManager,
75
+ });
76
+ const outputParser = args?.outputParser ?? ChatAgent.getDefaultOutputParser();
68
77
  return new ChatAgent({
69
78
  llmChain: chain,
79
+ outputParser,
70
80
  allowedTools: tools.map((t) => t.name),
71
81
  });
72
82
  }
73
- async extractToolAndInput(text) {
74
- if (text.includes(FINAL_ANSWER_ACTION)) {
75
- const parts = text.split(FINAL_ANSWER_ACTION);
76
- const input = parts[parts.length - 1].trim();
77
- return { tool: "Final Answer", input };
78
- }
79
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
80
- const [_, action, __] = text.split("```");
81
- try {
82
- const response = JSON.parse(action.trim());
83
- return { tool: response.action, input: response.action_input };
84
- }
85
- catch {
86
- throw new Error(`Unable to parse JSON response from chat agent.\n\n${text}`);
87
- }
88
- }
89
83
  }
90
84
  exports.ChatAgent = ChatAgent;
@@ -1,9 +1,11 @@
1
- import { Agent } from "../agent.js";
2
- import { ChatPromptTemplate } from "../../prompts/chat.js";
3
1
  import { BaseLanguageModel } from "../../base_language/index.js";
2
+ import { ChatPromptTemplate } from "../../prompts/chat.js";
4
3
  import { AgentStep } from "../../schema/index.js";
5
- import { AgentInput } from "../types.js";
6
4
  import { Tool } from "../../tools/base.js";
5
+ import { Optional } from "../../types/type-utils.js";
6
+ import { Agent, AgentArgs } from "../agent.js";
7
+ import { AgentInput } from "../types.js";
8
+ import { ChatAgentOutputParser } from "./outputParser.js";
7
9
  export type CreatePromptArgs = {
8
10
  /** String to put after the list of tools. */
9
11
  suffix?: string;
@@ -12,18 +14,19 @@ export type CreatePromptArgs = {
12
14
  /** List of input variables the final prompt will expect. */
13
15
  inputVariables?: string[];
14
16
  };
15
- type ZeroShotAgentInput = AgentInput;
17
+ type ChatAgentInput = Optional<AgentInput, "outputParser">;
16
18
  /**
17
19
  * Agent for the MRKL chain.
18
20
  * @augments Agent
19
21
  */
20
22
  export declare class ChatAgent extends Agent {
21
- constructor(input: ZeroShotAgentInput);
22
- _agentType(): "zero-shot-react-description";
23
+ constructor(input: ChatAgentInput);
24
+ _agentType(): "chat-zero-shot-react-description";
23
25
  observationPrefix(): string;
24
26
  llmPrefix(): string;
25
27
  _stop(): string[];
26
28
  static validateTools(tools: Tool[]): void;
29
+ static getDefaultOutputParser(): ChatAgentOutputParser;
27
30
  constructScratchPad(steps: AgentStep[]): string;
28
31
  /**
29
32
  * Create prompt in the style of the zero shot agent.
@@ -34,10 +37,6 @@ export declare class ChatAgent extends Agent {
34
37
  * @param args.prefix - String to put before the list of tools.
35
38
  */
36
39
  static createPrompt(tools: Tool[], args?: CreatePromptArgs): ChatPromptTemplate;
37
- static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: CreatePromptArgs): ChatAgent;
38
- extractToolAndInput(text: string): Promise<{
39
- tool: string;
40
- input: string;
41
- } | null>;
40
+ static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: CreatePromptArgs & AgentArgs): ChatAgent;
42
41
  }
43
42
  export {};
@@ -1,18 +1,19 @@
1
1
  import { LLMChain } from "../../chains/llm_chain.js";
2
+ import { ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "../../prompts/chat.js";
2
3
  import { Agent } from "../agent.js";
3
- import { SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, } from "../../prompts/chat.js";
4
- import { PREFIX, SUFFIX, FORMAT_INSTRUCTIONS } from "./prompt.js";
5
- const FINAL_ANSWER_ACTION = "Final Answer:";
4
+ import { ChatAgentOutputParser } from "./outputParser.js";
5
+ import { FORMAT_INSTRUCTIONS, PREFIX, SUFFIX } from "./prompt.js";
6
6
  /**
7
7
  * Agent for the MRKL chain.
8
8
  * @augments Agent
9
9
  */
10
10
  export class ChatAgent extends Agent {
11
11
  constructor(input) {
12
- super(input);
12
+ const outputParser = input?.outputParser ?? ChatAgent.getDefaultOutputParser();
13
+ super({ ...input, outputParser });
13
14
  }
14
15
  _agentType() {
15
- return "zero-shot-react-description";
16
+ return "chat-zero-shot-react-description";
16
17
  }
17
18
  observationPrefix() {
18
19
  return "Observation: ";
@@ -31,6 +32,9 @@ export class ChatAgent extends Agent {
31
32
  throw new Error(msg);
32
33
  }
33
34
  }
35
+ static getDefaultOutputParser() {
36
+ return new ChatAgentOutputParser();
37
+ }
34
38
  constructScratchPad(steps) {
35
39
  const agentScratchpad = super.constructScratchPad(steps);
36
40
  if (agentScratchpad) {
@@ -61,26 +65,16 @@ export class ChatAgent extends Agent {
61
65
  static fromLLMAndTools(llm, tools, args) {
62
66
  ChatAgent.validateTools(tools);
63
67
  const prompt = ChatAgent.createPrompt(tools, args);
64
- const chain = new LLMChain({ prompt, llm });
68
+ const chain = new LLMChain({
69
+ prompt,
70
+ llm,
71
+ callbackManager: args?.callbackManager,
72
+ });
73
+ const outputParser = args?.outputParser ?? ChatAgent.getDefaultOutputParser();
65
74
  return new ChatAgent({
66
75
  llmChain: chain,
76
+ outputParser,
67
77
  allowedTools: tools.map((t) => t.name),
68
78
  });
69
79
  }
70
- async extractToolAndInput(text) {
71
- if (text.includes(FINAL_ANSWER_ACTION)) {
72
- const parts = text.split(FINAL_ANSWER_ACTION);
73
- const input = parts[parts.length - 1].trim();
74
- return { tool: "Final Answer", input };
75
- }
76
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
77
- const [_, action, __] = text.split("```");
78
- try {
79
- const response = JSON.parse(action.trim());
80
- return { tool: response.action, input: response.action_input };
81
- }
82
- catch {
83
- throw new Error(`Unable to parse JSON response from chat agent.\n\n${text}`);
84
- }
85
- }
86
80
  }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChatAgentOutputParser = exports.FINAL_ANSWER_ACTION = void 0;
4
+ const types_js_1 = require("../../agents/types.cjs");
5
+ const prompt_js_1 = require("./prompt.cjs");
6
+ exports.FINAL_ANSWER_ACTION = "Final Answer:";
7
+ class ChatAgentOutputParser extends types_js_1.AgentActionOutputParser {
8
+ async parse(text) {
9
+ if (text.includes(exports.FINAL_ANSWER_ACTION)) {
10
+ const parts = text.split(exports.FINAL_ANSWER_ACTION);
11
+ const output = parts[parts.length - 1].trim();
12
+ return { returnValues: { output }, log: text };
13
+ }
14
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
+ const [_, action, __] = text.split(/```(?:json)?/g);
16
+ try {
17
+ const response = JSON.parse(action.trim());
18
+ return {
19
+ tool: response.action,
20
+ toolInput: response.action_input,
21
+ log: text,
22
+ };
23
+ }
24
+ catch {
25
+ throw new Error(`Unable to parse JSON response from chat agent.\n\n${text}`);
26
+ }
27
+ }
28
+ getFormatInstructions() {
29
+ return prompt_js_1.FORMAT_INSTRUCTIONS;
30
+ }
31
+ }
32
+ exports.ChatAgentOutputParser = ChatAgentOutputParser;
@@ -0,0 +1,18 @@
1
+ import { AgentActionOutputParser } from "../../agents/types.js";
2
+ export declare const FINAL_ANSWER_ACTION = "Final Answer:";
3
+ export declare class ChatAgentOutputParser extends AgentActionOutputParser {
4
+ parse(text: string): Promise<{
5
+ returnValues: {
6
+ output: string;
7
+ };
8
+ log: string;
9
+ tool?: undefined;
10
+ toolInput?: undefined;
11
+ } | {
12
+ tool: any;
13
+ toolInput: any;
14
+ log: string;
15
+ returnValues?: undefined;
16
+ }>;
17
+ getFormatInstructions(): string;
18
+ }
@@ -0,0 +1,28 @@
1
+ import { AgentActionOutputParser } from "../../agents/types.js";
2
+ import { FORMAT_INSTRUCTIONS } from "./prompt.js";
3
+ export const FINAL_ANSWER_ACTION = "Final Answer:";
4
+ export class ChatAgentOutputParser extends AgentActionOutputParser {
5
+ async parse(text) {
6
+ if (text.includes(FINAL_ANSWER_ACTION)) {
7
+ const parts = text.split(FINAL_ANSWER_ACTION);
8
+ const output = parts[parts.length - 1].trim();
9
+ return { returnValues: { output }, log: text };
10
+ }
11
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
+ const [_, action, __] = text.split(/```(?:json)?/g);
13
+ try {
14
+ const response = JSON.parse(action.trim());
15
+ return {
16
+ tool: response.action,
17
+ toolInput: response.action_input,
18
+ log: text,
19
+ };
20
+ }
21
+ catch {
22
+ throw new Error(`Unable to parse JSON response from chat agent.\n\n${text}`);
23
+ }
24
+ }
25
+ getFormatInstructions() {
26
+ return FORMAT_INSTRUCTIONS;
27
+ }
28
+ }
@@ -1,54 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatConversationalAgent = exports.ChatConversationalAgentOutputParser = void 0;
3
+ exports.ChatConversationalAgent = void 0;
4
4
  const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
5
- const agent_js_1 = require("../agent.cjs");
6
5
  const chat_js_1 = require("../../prompts/chat.cjs");
7
6
  const template_js_1 = require("../../prompts/template.cjs");
8
- const prompt_js_1 = require("./prompt.cjs");
9
7
  const index_js_1 = require("../../schema/index.cjs");
10
- class ChatConversationalAgentOutputParser extends index_js_1.BaseOutputParser {
11
- async parse(text) {
12
- let jsonOutput = text.trim();
13
- if (jsonOutput.includes("```json")) {
14
- jsonOutput = jsonOutput.split("```json")[1].trimStart();
15
- }
16
- if (jsonOutput.includes("```")) {
17
- jsonOutput = jsonOutput.split("```")[0].trimEnd();
18
- }
19
- if (jsonOutput.startsWith("```")) {
20
- jsonOutput = jsonOutput.slice(3).trimStart();
21
- }
22
- if (jsonOutput.endsWith("```")) {
23
- jsonOutput = jsonOutput.slice(0, -3).trimEnd();
24
- }
25
- const response = JSON.parse(jsonOutput);
26
- return { action: response.action, action_input: response.action_input };
27
- }
28
- getFormatInstructions() {
29
- return prompt_js_1.FORMAT_INSTRUCTIONS;
30
- }
31
- }
32
- exports.ChatConversationalAgentOutputParser = ChatConversationalAgentOutputParser;
8
+ const agent_js_1 = require("../agent.cjs");
9
+ const outputParser_js_1 = require("./outputParser.cjs");
10
+ const prompt_js_1 = require("./prompt.cjs");
33
11
  /**
34
12
  * Agent for the MRKL chain.
35
13
  * @augments Agent
36
14
  */
37
15
  class ChatConversationalAgent extends agent_js_1.Agent {
38
- constructor(input, outputParser) {
39
- super(input);
40
- Object.defineProperty(this, "outputParser", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: void 0
45
- });
46
- this.outputParser =
47
- outputParser ?? new ChatConversationalAgentOutputParser();
16
+ constructor(input) {
17
+ const outputParser = input.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
18
+ super({ ...input, outputParser });
48
19
  }
49
20
  _agentType() {
50
- /** Not turning on serialization until more sure of abstractions. */
51
- throw new Error("Method not implemented.");
21
+ return "chat-conversational-react-description";
52
22
  }
53
23
  observationPrefix() {
54
24
  return "Observation: ";
@@ -77,16 +47,21 @@ class ChatConversationalAgent extends agent_js_1.Agent {
77
47
  }
78
48
  return thoughts;
79
49
  }
50
+ static getDefaultOutputParser() {
51
+ return new outputParser_js_1.ChatConversationalAgentOutputParser();
52
+ }
80
53
  /**
81
- * Create prompt in the style of the zero shot agent.
54
+ * Create prompt in the style of the ChatConversationAgent.
82
55
  *
83
56
  * @param tools - List of tools the agent will have access to, used to format the prompt.
84
57
  * @param args - Arguments to create the prompt with.
85
- * @param args.suffix - String to put after the list of tools.
86
- * @param args.prefix - String to put before the list of tools.
58
+ * @param args.systemMessage - String to put before the list of tools.
59
+ * @param args.humanMessage - String to put after the list of tools.
87
60
  */
88
61
  static createPrompt(tools, args) {
89
- const { systemMessage = prompt_js_1.PREFIX, humanMessage = prompt_js_1.SUFFIX, outputParser = new ChatConversationalAgentOutputParser(), } = args ?? {};
62
+ const systemMessage = (args?.systemMessage ?? prompt_js_1.DEFAULT_PREFIX) + prompt_js_1.PREFIX_END;
63
+ const humanMessage = args?.humanMessage ?? prompt_js_1.DEFAULT_SUFFIX;
64
+ const outputParser = args?.outputParser ?? new outputParser_js_1.ChatConversationalAgentOutputParser();
90
65
  const toolStrings = tools
91
66
  .map((tool) => `${tool.name}: ${tool.description}`)
92
67
  .join("\n");
@@ -109,21 +84,17 @@ class ChatConversationalAgent extends agent_js_1.Agent {
109
84
  static fromLLMAndTools(llm, tools, args) {
110
85
  ChatConversationalAgent.validateTools(tools);
111
86
  const prompt = ChatConversationalAgent.createPrompt(tools, args);
112
- const chain = new llm_chain_js_1.LLMChain({ prompt, llm });
113
- const { outputParser = new ChatConversationalAgentOutputParser() } = args ?? {};
87
+ const chain = new llm_chain_js_1.LLMChain({
88
+ prompt,
89
+ llm,
90
+ callbackManager: args?.callbackManager,
91
+ });
92
+ const outputParser = args?.outputParser ?? ChatConversationalAgent.getDefaultOutputParser();
114
93
  return new ChatConversationalAgent({
115
94
  llmChain: chain,
95
+ outputParser,
116
96
  allowedTools: tools.map((t) => t.name),
117
- }, outputParser);
118
- }
119
- async extractToolAndInput(text) {
120
- try {
121
- const response = (await this.outputParser.parse(text));
122
- return { tool: response.action, input: response.action_input };
123
- }
124
- catch {
125
- throw new Error(`Unable to parse JSON response from chat agent.\n\n${text}`);
126
- }
97
+ });
127
98
  }
128
99
  }
129
100
  exports.ChatConversationalAgent = ChatConversationalAgent;
@@ -1,13 +1,10 @@
1
- import { Agent } from "../agent.js";
2
- import { ChatPromptTemplate } from "../../prompts/chat.js";
3
1
  import { BaseLanguageModel } from "../../base_language/index.js";
4
- import { AgentStep, BaseChatMessage, BaseOutputParser } from "../../schema/index.js";
5
- import { AgentInput } from "../types.js";
2
+ import { ChatPromptTemplate } from "../../prompts/chat.js";
3
+ import { AgentStep, BaseChatMessage } from "../../schema/index.js";
6
4
  import { Tool } from "../../tools/base.js";
7
- export declare class ChatConversationalAgentOutputParser extends BaseOutputParser {
8
- parse(text: string): Promise<unknown>;
9
- getFormatInstructions(): string;
10
- }
5
+ import { Optional } from "../../types/type-utils.js";
6
+ import { Agent, AgentArgs } from "../agent.js";
7
+ import { AgentActionOutputParser, AgentInput } from "../types.js";
11
8
  export type CreatePromptArgs = {
12
9
  /** String to put after the list of tools. */
13
10
  systemMessage?: string;
@@ -16,34 +13,30 @@ export type CreatePromptArgs = {
16
13
  /** List of input variables the final prompt will expect. */
17
14
  inputVariables?: string[];
18
15
  /** Output parser to use for formatting. */
19
- outputParser?: BaseOutputParser;
16
+ outputParser?: AgentActionOutputParser;
20
17
  };
21
- export type ChatConversationalAgentInput = AgentInput;
18
+ export type ChatConversationalAgentInput = Optional<AgentInput, "outputParser">;
22
19
  /**
23
20
  * Agent for the MRKL chain.
24
21
  * @augments Agent
25
22
  */
26
23
  export declare class ChatConversationalAgent extends Agent {
27
- outputParser: BaseOutputParser;
28
- constructor(input: ChatConversationalAgentInput, outputParser?: BaseOutputParser);
29
- _agentType(): string;
24
+ constructor(input: ChatConversationalAgentInput);
25
+ _agentType(): "chat-conversational-react-description";
30
26
  observationPrefix(): string;
31
27
  llmPrefix(): string;
32
28
  _stop(): string[];
33
29
  static validateTools(tools: Tool[]): void;
34
30
  constructScratchPad(steps: AgentStep[]): BaseChatMessage[];
31
+ static getDefaultOutputParser(): AgentActionOutputParser;
35
32
  /**
36
- * Create prompt in the style of the zero shot agent.
33
+ * Create prompt in the style of the ChatConversationAgent.
37
34
  *
38
35
  * @param tools - List of tools the agent will have access to, used to format the prompt.
39
36
  * @param args - Arguments to create the prompt with.
40
- * @param args.suffix - String to put after the list of tools.
41
- * @param args.prefix - String to put before the list of tools.
37
+ * @param args.systemMessage - String to put before the list of tools.
38
+ * @param args.humanMessage - String to put after the list of tools.
42
39
  */
43
40
  static createPrompt(tools: Tool[], args?: CreatePromptArgs): ChatPromptTemplate;
44
- static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: CreatePromptArgs): ChatConversationalAgent;
45
- extractToolAndInput(text: string): Promise<{
46
- tool: string;
47
- input: string;
48
- } | null>;
41
+ static fromLLMAndTools(llm: BaseLanguageModel, tools: Tool[], args?: CreatePromptArgs & AgentArgs): ChatConversationalAgent;
49
42
  }