A simple implementation of the ReAct (Reasoning and Acting) agent pattern using LangChain and Ollama.
This project demonstrates how to build a ReAct agent using LangChain. The ReAct pattern combines reasoning and acting in an iterative process:
- Reasoning: The agent thinks about how to solve a problem
- Acting: The agent takes an action using available tools
- Observing: The agent observes the result
- Repeating: The cycle continues until the agent reaches a final answer
- Simple ReAct agent implementation with LangChain
- Uses Ollama with Phi-4 as the LLM
- Custom callback handler to display prompts and responses
- Example tool implementation (
get_text_length) - LangSmith integration for tracing and debugging
This project includes integration with LangSmith, which provides tracing, monitoring, and evaluation capabilities for LangChain applications:
- Tracing: Track and visualize the execution of your agent's reasoning steps
- Debugging: Easily identify issues with your agent's reasoning process
- Performance Monitoring: Analyze latency, token usage, and other metrics
To use LangSmith, you need to:
- Create a LangSmith account at https://smith.langchain.com/
- Get your API key from the LangSmith dashboard
- Configure the environment variables in your
.envfile
- Python 3.8+
- Ollama running locally with the Phi-4 model
- LangSmith account (for tracing and debugging)
- Clone the repository
- Install dependencies:
pip install -r requirements.txt- Set up your environment variables in a
.envfile (copy from.env.example):
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
LANGSMITH_API_KEY="your-langsmith-api-key"
LANGSMITH_PROJECT="your-project-name"
Run the main script:
python main.pyThis will execute a ReAct agent that answers the question "What is the length of Capibara?" by using the get_text_length tool.
After running the agent, you can view the trace in your LangSmith dashboard to analyze the agent's reasoning steps and performance.
main.py- Contains the ReAct agent implementation and tool definitioncallbacks.py- Defines a custom callback handler to display prompts and responsesrequirements.txt- Lists required dependencies.env.example- Template for environment variables including LangSmith configuration
- The agent receives a question
- It thinks about how to solve the problem
- It selects a tool to use
- It provides input to the tool
- It observes the result
- It continues this process until it has enough information to provide a final answer
The agent follows a specific prompt template that instructs it on how to reason and act using the available tools.
To add new tools, follow this pattern:
@tool
def your_new_tool(input_param: str) -> Any:
"""Description of what the tool does."""
# Tool implementation
return resultThen, add the tool to the tools list in main.py.
MIT