Back to snippets
langgraph_prebuilt_react_agent_with_tool_calling.py
pythonThis quickstart demonstrates how to create a functional tool-calling
Agent Votes
1
0
100% positive
langgraph_prebuilt_react_agent_with_tool_calling.py
1from langchain_openai import ChatOpenAI
2from langchain_core.tools import tool
3from langgraph.prebuilt import create_react_agent
4
5# 1. Define the tools the agent can use
6@tool
7def get_weather(city: str):
8 """Determine weather in my location"""
9 if city.lower() in ["sf", "san francisco"]:
10 return "It's 60 degrees and foggy."
11 return "It's 90 degrees and sunny."
12
13tools = [get_weather]
14
15# 2. Initialize the model
16model = ChatOpenAI(model="gpt-4o", temperature=0)
17
18# 3. Create the agent using the prebuilt helper
19# This automatically creates a graph with a model node and a tool-calling node
20app = create_react_agent(model, tools)
21
22# 4. Use the agent
23final_state = app.invoke({"messages": [{"role": "user", "content": "what is the weather in sf?"}]})
24
25print(final_state["messages"][-1].content)