({ llm, tools, prompt }: CreateReactAgentParams)
| 768 | } |
| 769 | |
| 770 | export const createReactAgent = async ({ llm, tools, prompt }: CreateReactAgentParams) => { |
| 771 | const missingVariables = ['tools', 'tool_names', 'agent_scratchpad'].filter((v) => !prompt.inputVariables.includes(v)) |
| 772 | if (missingVariables.length > 0) { |
| 773 | throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`) |
| 774 | } |
| 775 | const toolNames = tools.map((tool) => tool.name) |
| 776 | const partialedPrompt = await prompt.partial({ |
| 777 | tools: renderTextDescription(tools), |
| 778 | tool_names: toolNames.join(', ') |
| 779 | }) |
| 780 | // TODO: Add .bind to core runnable interface. |
| 781 | const llmWithStop = (llm as BaseLanguageModel).withConfig({ |
| 782 | stop: ['\nObservation:'] |
| 783 | }) |
| 784 | const agent = RunnableSequence.from([ |
| 785 | RunnablePassthrough.assign({ |
| 786 | //@ts-ignore |
| 787 | agent_scratchpad: (input: { steps: AgentStep[] }) => formatLogToString(input.steps) |
| 788 | }), |
| 789 | partialedPrompt, |
| 790 | llmWithStop, |
| 791 | new ReActSingleInputOutputParser({ |
| 792 | toolNames |
| 793 | }) |
| 794 | ]) |
| 795 | return agent |
| 796 | } |
| 797 | |
| 798 | class ReActSingleInputOutputParser extends AgentActionOutputParser { |
| 799 | lc_namespace = ['langchain', 'agents', 'react'] |
no test coverage detected