102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.OpenAIFunctionsAgentOutputParser = void 0;
|
|
const messages_1 = require("@langchain/core/messages");
|
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
const types_js_1 = require("../types.cjs");
|
|
/**
|
|
* @example
|
|
* ```typescript
|
|
*
|
|
* const prompt = ChatPromptTemplate.fromMessages([
|
|
* ["ai", "You are a helpful assistant"],
|
|
* ["human", "{input}"],
|
|
* new MessagesPlaceholder("agent_scratchpad"),
|
|
* ]);
|
|
*
|
|
* const modelWithFunctions = new ChatOpenAI({
|
|
* modelName: "gpt-4",
|
|
* temperature: 0,
|
|
* }).bind({
|
|
* functions: tools.map((tool) => convertToOpenAIFunction(tool)),
|
|
* });
|
|
*
|
|
* const runnableAgent = RunnableSequence.from([
|
|
* {
|
|
* input: (i) => i.input,
|
|
* agent_scratchpad: (i) => formatAgentSteps(i.steps),
|
|
* },
|
|
* prompt,
|
|
* modelWithFunctions,
|
|
* new OpenAIFunctionsAgentOutputParser(),
|
|
* ]);
|
|
*
|
|
* const result = await runnableAgent.invoke({
|
|
* input: "What is the weather in New York?",
|
|
* steps: agentSteps,
|
|
* });
|
|
*
|
|
* ```
|
|
*/
|
|
class OpenAIFunctionsAgentOutputParser extends types_js_1.AgentActionOutputParser {
|
|
constructor() {
|
|
super(...arguments);
|
|
Object.defineProperty(this, "lc_namespace", {
|
|
enumerable: true,
|
|
configurable: true,
|
|
writable: true,
|
|
value: ["langchain", "agents", "openai"]
|
|
});
|
|
}
|
|
static lc_name() {
|
|
return "OpenAIFunctionsAgentOutputParser";
|
|
}
|
|
async parse(text) {
|
|
throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
|
|
}
|
|
async parseResult(generations) {
|
|
if ("message" in generations[0] && (0, messages_1.isBaseMessage)(generations[0].message)) {
|
|
return this.parseAIMessage(generations[0].message);
|
|
}
|
|
throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
|
|
}
|
|
/**
|
|
* Parses the output message into a FunctionsAgentAction or AgentFinish
|
|
* object.
|
|
* @param message The BaseMessage to parse.
|
|
* @returns A FunctionsAgentAction or AgentFinish object.
|
|
*/
|
|
parseAIMessage(message) {
|
|
if (message.content && typeof message.content !== "string") {
|
|
throw new Error("This agent cannot parse non-string model responses.");
|
|
}
|
|
if (message.additional_kwargs.function_call) {
|
|
// eslint-disable-next-line prefer-destructuring
|
|
const function_call = message.additional_kwargs.function_call;
|
|
try {
|
|
const toolInput = function_call.arguments
|
|
? JSON.parse(function_call.arguments)
|
|
: {};
|
|
return {
|
|
tool: function_call.name,
|
|
toolInput,
|
|
log: `Invoking "${function_call.name}" with ${function_call.arguments ?? "{}"}\n${message.content}`,
|
|
messageLog: [message],
|
|
};
|
|
}
|
|
catch (error) {
|
|
throw new output_parsers_1.OutputParserException(`Failed to parse function arguments from chat model response. Text: "${function_call.arguments}". ${error}`);
|
|
}
|
|
}
|
|
else {
|
|
return {
|
|
returnValues: { output: message.content },
|
|
log: message.content,
|
|
};
|
|
}
|
|
}
|
|
getFormatInstructions() {
|
|
throw new Error("getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.");
|
|
}
|
|
}
|
|
exports.OpenAIFunctionsAgentOutputParser = OpenAIFunctionsAgentOutputParser;
|