A simple example of an AI agent that can understand natural language queries about planet masses and perform calculations in scientific notation, with additional support for real-time web search using LangChain and LangGraph.
- Calculate the combined masses of multiple planets
- Handle scientific notation (e.g., 1.898e24)
- Interactive command-line interface
- Comprehensive test suite
- Earth (5.972e24 kg)
- Mars (6.4171e24 kg)
- Jupiter (1.898e24 kg)
- Saturn (5.688e24 kg)
A new integration using LangChain's ChatOpenAI model with tool support:
def agent_langgraph_with_memory(state: MessagesState):
messages = state["messages"]
return {"messages": [model_with_tools.invoke(messages)]}
The agent has access to the Tavily search tool for real-time information retrieval:
-
Simple queries (answered directly):
- "What's 2+2?"
- "Who was Albert Einstein?"
-
Complex queries (uses search tool):
- "Find a hotel near Manhattan in New York"
- "What are the latest developments in AI?"
The agent uses SQLite for conversation state persistence:
# Create a checkpointer for conversation memory
with get_checkpointer() as checkpointer:
agent = build_graph_agent(checkpointer)
# Agent now has persistent memory across interactions
graph TD
A[Start] --> B[Agent]
B --> C{Tool Condition}
C -->|Needs Search| D[Tool Node]
D --> B
C -->|No Tools Needed| E[End]
M[Memory] <-->|State| B
# Clone the repository
git clone https://github.com/lenaar/ai-agent-simple-and-langgraph.git
cd ai-agent-simple-and-langgraph
# Install dependencies
pip install -r requirements.txt
Create a .env
file with:
OPENAI_API_KEY
: For ChatGPT model accessTAVILY_API_KEY
: For web search functionality (only needed for search features)
- Run the main application:
python main.py
- Select the agent type when prompted:
Enter agent type (simple_agent or agent_langgraph_with_memory): agent_langgraph_with_memory
- Start chatting:
Your question (or 'quit' to exit): find a hotel near Manhatten in New York
For planet mass calculations. Example questions:
- "What is the combined mass of Jupiter and Saturn?"
- "What is the combined mass of Earth, Mars, and Jupiter?"
For web search and real-time information. Example questions:
- "Find a hotel near Manhattan in New York"
- "What are the latest developments in AI?"
- "What's the current weather in Tokyo?"
- "What did I ask before?" (tests memory)
You can also use the agents programmatically:
# For planet mass calculations
from simple_agent import SimpleAgent
agent = SimpleAgent()
response = agent.run("What is the mass of Jupiter?")
# For web search queries with memory
from langgraph_agent import build_graph_agent
from langgraph_agent_memory import get_checkpointer
with get_checkpointer() as checkpointer:
agent = build_graph_agent(checkpointer)
response = agent.stream({
"messages": [("user", "Find me a hotel in Manhattan")]
}, get_thread_config(), stream_mode="values")
- Use clear, specific questions
- For web searches, provide enough context
- Type 'exit' or 'quit' to end the chat
- Use 'help' to see available commands
python -m pytest tests/ -v
simple_agent.py
: Original agent implementation for planet calculationssimple_agent_tools.py
: Tools for calculations and planet mass lookupsimple_agent_query.py
: Query processing and skill executionsimple_agent_prompt.py
: System prompt and examples
langgraph_agent.py
: Graph-based agent implementationlanggraph_agent_tools.py
: Tool definitions (Tavily search)langgraph_agent_memory.py
: Memory management with SQLitelanggraph_agent_query.py
: Streaming response handling
- Added support for calculating the masses of any number of planets
- Improved scientific notation handling
- Enhanced error handling for unknown planets
- Integrated LangGraph for better tool handling
- Added Tavily search for real-time information
- Improved message routing with tool conditions
- Added memory management with SQLite for conversation persistence
- Implemented streaming responses for real-time interaction