langchain-hs-0.0.1.0: Haskell implementation of Langchain
Copyright(c) 2025 Tushar Adhatrao
LicenseMIT
MaintainerTushar Adhatrao <tusharadhatrao@gmail.com>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Langchain.Agents.React

Description

Implements the ReAct pattern where the agent alternates between:

  1. Reasoning (generating thoughts)
  2. 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

Documentation

newtype ReactAgentOutputParser Source #

Output parser for ReAct agent responses Handles two primary formats:

  1. Final answers containing "Final Answer:"
  2. Action requests with "Action:" and "Action Input:"

Example parsing:

parseReactOutput "Final Answer: 42"
-- Right (Finish ...)

parseReactOutput "Action: calculator\nAction Input: 5+3"
-- Right (Continue ...)

Instances

Instances details
OutputParser ReactAgentOutputParser Source # 
Instance details

Defined in Langchain.Agents.React

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
  ]

Instances

Instances details
LLM llm => Agent (ReactAgent llm) Source # 
Instance details

Defined in Langchain.Agents.React

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:

  1. List available tools
  2. Follow thought-action-observation pattern
  3. 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