Copyright | (c) 2025 Tushar Adhatrao |
---|---|
License | MIT |
Maintainer | Tushar Adhatrao <tusharadhatrao@gmail.com> |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Langchain.Agents.React
Description
Implements the ReAct pattern where the agent alternates between:
- Reasoning (generating thoughts)
- Acting (executing tools)
Example agent interaction:
agent <- createReactAgent llm [wikipediaTool, calculatorTool] result <- runAgentExecutor executor "What's the population of Paris?" -- Agent might: -- 1. Use Wikipedia tool to find current population data -- 2. Use calculator tool to verify numbers -- 3. Return final answer
Synopsis
- newtype ReactAgentOutputParser = ReactAgentOutputParser AgentStep
- parseReactOutput :: Text -> Either String ReactAgentOutputParser
- data LLM llm => ReactAgent llm = ReactAgent {
- reactLLM :: llm
- reactTools :: [AnyTool]
- reactPromptTemplate :: PromptTemplate
- createReactAgent :: LLM llm => llm -> [AnyTool] -> IO (Either String (ReactAgent llm))
- formatToolDescriptions :: [AnyTool] -> Text
- formatToolNames :: [AnyTool] -> Text
- getLastUserInput :: ChatMessage -> Text
Documentation
newtype ReactAgentOutputParser Source #
Output parser for ReAct agent responses Handles two primary formats:
- Final answers containing "Final Answer:"
- Action requests with "Action:" and "Action Input:"
Example parsing:
parseReactOutput "Final Answer: 42" -- Right (Finish ...) parseReactOutput "Action: calculator\nAction Input: 5+3" -- Right (Continue ...)
Constructors
ReactAgentOutputParser AgentStep |
Instances
parseReactOutput :: Text -> Either String ReactAgentOutputParser Source #
Parses the output from a React agent
data LLM llm => ReactAgent llm Source #
Core ReAct agent configuration. Contains:
- LLM for reasoning
- Available tools
- Prompt template for interaction
Example creation:
agent <- createReactAgent openAIGPT [ AnyTool wikipediaTool , AnyTool calculatorTool ]
Constructors
ReactAgent | |
Fields
|
Instances
LLM llm => Agent (ReactAgent llm) Source # | |
Defined in Langchain.Agents.React Methods planNextAction :: BaseMemory m => ReactAgent llm -> AgentState m -> IO (Either String AgentStep) Source # agentPrompt :: ReactAgent llm -> IO PromptTemplate Source # agentTools :: ReactAgent llm -> IO [AnyTool] Source # |
createReactAgent :: LLM llm => llm -> [AnyTool] -> IO (Either String (ReactAgent llm)) Source #
Creates a ReAct agent with standard prompt structure The prompt instructs the LLM to:
- List available tools
- Follow thought-action-observation pattern
- Provide final answers
Example prompt excerpt:
"Use the following format: Thought: ... Action: [tool_name] Action Input: ..."
formatToolDescriptions :: [AnyTool] -> Text Source #
Formats tool descriptions for LLM consumption Creates a list like:
"Tool: wikipedia Description: Search Wikipedia..."
formatToolNames :: [AnyTool] -> Text Source #
Creates comma-separated tool names for prompt inclusion Example output: "wikipedia, calculator, weather"
getLastUserInput :: ChatMessage -> Text Source #
Extracts latest user query from chat history Handles cases where:
- Multiple user messages exist
- No user input found