This repository contains a simple example of building an agent using LangGraph components. It demonstrates how to create a conversational agent that can search for information and respond to user queries using a state-based workflow.
LangGraph is a library for building stateful, multi-actor applications with LLMs. This example demonstrates:
- Creating a simple agent using the ReAct (Reasoning + Action) pattern
- Implementing a state-based workflow with LangGraph
- Integrating external tools (Tavily search) with a language model
- Building a travel planning assistant that can search for and provide information
- Python 3.8+
- OpenAI API key
- Tavily API key
-
Clone this repository:
git clone https://github.com/KansSoftware/langlraph-components-simple-example cd LangGraph\ Components\ -\ Simple\ Example -
Create a virtual environment:
python -m venv langraph-simple-agent-venv -
Activate the virtual environment:
- Windows:
langraph-simple-agent-venv\Scripts\activate - macOS/Linux:
source langraph-simple-agent-venv/bin/activate
- Windows:
-
Install the required packages:
pip install -r requirements.txt -
Create a
.envfile in the root directory with your API keys:OPENAI_API_KEY=your-openai-api-key TAVILY_API_KEY=your-tavily-api-key
Open the Jupyter notebook LangGraph Components.ipynb to see the example in action:
jupyter notebook "LangGraph Components.ipynb"
The notebook demonstrates:
- Setting up the environment with API keys
- Creating a search tool using Tavily
- Defining an agent state structure
- Implementing a ReAct pattern agent class
- Running example queries
The agent uses a typed dictionary to maintain state:
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], operator.add]The Agent class implements the ReAct pattern with:
- A language model node for processing messages
- An action node for executing tools
- Conditional edges for decision flow
- Methods for handling tool calls and responses
The notebook includes examples of:
- Simple factual queries (e.g., "What is the capital of Namibia?")
- Multi-part queries (e.g., "What's the best time to visit Kyoto, Japan?")
The agent follows a graph-based architecture:
- User query is processed by the language model
- If information is needed, the model calls the search tool
- Search results are returned to the model
- The model formulates a final response
[Include license information here]