agsamantha/node_modules/langchain/dist/agents/react/index.cjs

83 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-10-02 15:15:21 -05:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReactAgent = void 0;
const runnables_1 = require("@langchain/core/runnables");
const render_js_1 = require("../../tools/render.cjs");
const log_js_1 = require("../format_scratchpad/log.cjs");
const output_parser_js_1 = require("./output_parser.cjs");
const agent_js_1 = require("../agent.cjs");
/**
* Create an agent that uses ReAct prompting.
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
* @returns A runnable sequence representing an agent. It takes as input all the same input
* variables as the prompt passed in does. It returns as output either an
* AgentAction or AgentFinish.
*
* @example
* ```typescript
* import { AgentExecutor, createReactAgent } from "langchain/agents";
* import { pull } from "langchain/hub";
* import type { PromptTemplate } from "@langchain/core/prompts";
*
* import { OpenAI } from "@langchain/openai";
*
* // Define the tools the agent will have access to.
* const tools = [...];
*
* // Get the prompt to use - you can modify this!
* // If you want to see the prompt in full, you can at:
* // https://smith.langchain.com/hub/hwchase17/react
* const prompt = await pull<PromptTemplate>("hwchase17/react");
*
* const llm = new OpenAI({
* temperature: 0,
* });
*
* const agent = await createReactAgent({
* llm,
* tools,
* prompt,
* });
*
* const agentExecutor = new AgentExecutor({
* agent,
* tools,
* });
*
* const result = await agentExecutor.invoke({
* input: "what is LangChain?",
* });
* ```
*/
async function createReactAgent({ llm, tools, prompt, streamRunnable, }) {
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
if (missingVariables.length > 0) {
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
}
const toolNames = tools.map((tool) => tool.name);
const partialedPrompt = await prompt.partial({
tools: (0, render_js_1.renderTextDescription)(tools),
tool_names: toolNames.join(", "),
});
// TODO: Add .bind to core runnable interface.
const llmWithStop = llm.bind({
stop: ["\nObservation:"],
});
const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
runnables_1.RunnablePassthrough.assign({
agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
}),
partialedPrompt,
llmWithStop,
new output_parser_js_1.ReActSingleInputOutputParser({
toolNames,
}),
], {
name: "ReactAgent",
streamRunnable,
singleAction: true,
});
return agent;
}
exports.createReactAgent = createReactAgent;