-
Notifications
You must be signed in to change notification settings - Fork 2k
/
outputParser.ts
168 lines (150 loc) Β· 5.96 KB
/
outputParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { AgentAction, AgentFinish } from "@langchain/core/agents";
import {
FormatInstructionsOptions,
OutputParserException,
} from "@langchain/core/output_parsers";
import { renderTemplate } from "@langchain/core/prompts";
import { AgentActionOutputParser } from "../types.js";
import { FORMAT_INSTRUCTIONS } from "./prompt.js";
import { OutputFixingParser } from "../../output_parsers/fix.js";
export type ChatConversationalAgentOutputParserFormatInstructionsOptions =
FormatInstructionsOptions & {
toolNames: string[];
raw?: boolean;
};
/**
* Class that represents an output parser for the ChatConversationalAgent
* class. It extends the AgentActionOutputParser class and provides
* methods for parsing the output of the MRKL chain into agent actions.
*/
export class ChatConversationalAgentOutputParser extends AgentActionOutputParser {
lc_namespace = ["langchain", "agents", "chat_convo"];
private toolNames: string[];
constructor(fields: { toolNames: string[] }) {
super(...arguments);
this.toolNames = fields.toolNames;
}
/**
* Parses the given text into an AgentAction or AgentFinish object. If an
* output fixing parser is defined, uses it to parse the text.
* @param text Text to parse.
* @returns Promise that resolves to an AgentAction or AgentFinish object.
*/
async parse(text: string): Promise<AgentAction | AgentFinish> {
let jsonOutput = text.trim();
if (jsonOutput.includes("```json") || jsonOutput.includes("```")) {
const testString = jsonOutput.includes("```json") ? "```json" : "```";
const firstIndex = jsonOutput.indexOf(testString);
const actionInputIndex = jsonOutput.indexOf("action_input");
if (actionInputIndex > firstIndex) {
jsonOutput = jsonOutput
.slice(firstIndex + testString.length)
.trimStart();
const lastIndex = jsonOutput.lastIndexOf("```");
if (lastIndex !== -1) {
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
}
}
}
try {
const response = JSON.parse(jsonOutput);
const { action, action_input } = response;
if (action === "Final Answer") {
return { returnValues: { output: action_input }, log: text };
}
return { tool: action, toolInput: action_input, log: text };
} catch (e) {
throw new OutputParserException(
`Failed to parse. Text: "${text}". Error: ${e}`
);
}
}
/**
* Returns the format instructions as a string. If the 'raw' option is
* true, returns the raw FORMAT_INSTRUCTIONS.
* @param options Options for getting the format instructions.
* @returns Format instructions as a string.
*/
getFormatInstructions(): string {
return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
tool_names: this.toolNames.join(", "),
});
}
}
export type ChatConversationalAgentOutputParserArgs = {
baseParser?: ChatConversationalAgentOutputParser;
outputFixingParser?: OutputFixingParser<AgentAction | AgentFinish>;
toolNames?: string[];
};
/**
* Class that represents an output parser with retries for the
* ChatConversationalAgent class. It extends the AgentActionOutputParser
* class and provides methods for parsing the output of the MRKL chain
* into agent actions with retry functionality.
*/
export class ChatConversationalAgentOutputParserWithRetries extends AgentActionOutputParser {
lc_namespace = ["langchain", "agents", "chat_convo"];
private baseParser: ChatConversationalAgentOutputParser;
private outputFixingParser?: OutputFixingParser<AgentAction | AgentFinish>;
private toolNames: string[] = [];
constructor(fields: ChatConversationalAgentOutputParserArgs) {
super(fields);
this.toolNames = fields.toolNames ?? this.toolNames;
this.baseParser =
fields?.baseParser ??
new ChatConversationalAgentOutputParser({ toolNames: this.toolNames });
this.outputFixingParser = fields?.outputFixingParser;
}
/**
* Returns the format instructions as a string.
* @returns Format instructions as a string.
*/
getFormatInstructions(
options: ChatConversationalAgentOutputParserFormatInstructionsOptions
): string {
if (options.raw) {
return FORMAT_INSTRUCTIONS;
}
return renderTemplate(FORMAT_INSTRUCTIONS, "f-string", {
tool_names: options.toolNames.join(", "),
});
}
/**
* Parses the given text into an AgentAction or AgentFinish object.
* @param text Text to parse.
* @returns Promise that resolves to an AgentAction or AgentFinish object.
*/
async parse(text: string): Promise<AgentAction | AgentFinish> {
if (this.outputFixingParser !== undefined) {
return this.outputFixingParser.parse(text);
}
return this.baseParser.parse(text);
}
/**
* Static method to create a new
* ChatConversationalAgentOutputParserWithRetries from a BaseLanguageModelInterface
* and options. If no base parser is provided in the options, a new
* ChatConversationalAgentOutputParser is created. An OutputFixingParser
* is also created from the BaseLanguageModelInterface and the base parser.
* @param llm BaseLanguageModelInterface instance used to create the OutputFixingParser.
* @param options Options for creating the ChatConversationalAgentOutputParserWithRetries instance.
* @returns A new instance of ChatConversationalAgentOutputParserWithRetries.
*/
static fromLLM(
llm: BaseLanguageModelInterface,
options: Omit<ChatConversationalAgentOutputParserArgs, "outputFixingParser">
): ChatConversationalAgentOutputParserWithRetries {
const baseParser =
options.baseParser ??
new ChatConversationalAgentOutputParser({
toolNames: options.toolNames ?? [],
});
const outputFixingParser = OutputFixingParser.fromLLM(llm, baseParser);
return new ChatConversationalAgentOutputParserWithRetries({
baseParser,
outputFixingParser,
toolNames: options.toolNames,
});
}
}