mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
32 KiB
32 KiB
In [1]:
%%capture --no-stderr
%pip install -U langgraph langchain-openaiIn [2]:
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("OPENAI_API_KEY")
# Recommended
_set_env("LANGCHAIN_API_KEY")
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_PROJECT"] = "Create ReAct Agent Tutorial"OPENAI_API_KEY: ········
In [3]:
from typing import Literal
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o", temperature=0)
@tool
def get_weather(city: Literal["nyc", "sf"]):
"""Use this to get weather information."""
if city == "nyc":
return "It might be cloudy in nyc"
elif city == "sf":
return "It's always sunny in sf"
else:
raise AssertionError("Unknown city")
tools = [get_weather]In [4]:
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(model, tools=tools)In [5]:
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))In [6]:
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()In [8]:
inputs = {"messages": [("user", "what is the weather in sf")]}
print_stream(graph.stream(inputs, stream_mode="values"))================================[1m Human Message [0m================================= what is the weather in sf ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_g6w9lHn3fxYo2ABE3Ihhprbr) Call ID: call_g6w9lHn3fxYo2ABE3Ihhprbr Args: city: sf =================================[1m Tool Message [0m================================= Name: get_weather It's always sunny in sf ==================================[1m Ai Message [0m================================== The weather in San Francisco is currently sunny.
In [9]:
inputs = {"messages": [("user", "who built you?")]}
print_stream(graph.stream(inputs, stream_mode="values"))================================[1m Human Message [0m================================= who built you? ==================================[1m Ai Message [0m================================== I was created by OpenAI, a research organization focused on developing and advancing artificial intelligence technology.
In [10]:
system_prompt = "You are a helpful bot named Fred."
graph = create_react_agent(model, tools, messages_modifier=system_prompt)
inputs = {"messages": [("user", "What's your name? And what's the weather in SF?")]}
print_stream(graph.stream(inputs, stream_mode="values"))================================[1m Human Message [0m================================= What's your name? And what's the weather in SF? ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_PGwTiytTVAAvNKWp4nznPi4s) Call ID: call_PGwTiytTVAAvNKWp4nznPi4s Args: city: sf =================================[1m Tool Message [0m================================= Name: get_weather It's always sunny in sf ==================================[1m Ai Message [0m================================== My name is Fred. The weather in San Francisco is currently sunny.
In [11]:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful bot named Fred."),
("user", "My name is Joe"),
("placeholder", "{messages}"),
]
)
def modify_messages(messages: list):
# You can do more complex modifications here
return prompt.invoke({"messages": messages})
graph = create_react_agent(model, tools, messages_modifier=modify_messages)
inputs = {"messages": [("user", "What's my name? And what's the weather in SF?")]}
print_stream(graph.stream(inputs, stream_mode="values"))================================[1m Human Message [0m================================= What's my name? And what's the weather in SF? ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_scpzIjdK3l411zcEn2T00Xm0) Call ID: call_scpzIjdK3l411zcEn2T00Xm0 Args: city: sf =================================[1m Tool Message [0m================================= Name: get_weather It's always sunny in sf ==================================[1m Ai Message [0m================================== Your name is Joe. The weather in San Francisco is always sunny.
In [12]:
from langgraph.checkpoint import MemorySaver
graph = create_react_agent(model, tools, checkpointer=MemorySaver())
config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("user", "What's the weather in NYC?")]}
print_stream(graph.stream(inputs, config=config, stream_mode="values"))================================[1m Human Message [0m================================= What's the weather in NYC? ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_C5yEOD1GhlVPX9Gc5nnqgUXF) Call ID: call_C5yEOD1GhlVPX9Gc5nnqgUXF Args: city: nyc =================================[1m Tool Message [0m================================= Name: get_weather It might be cloudy in nyc ==================================[1m Ai Message [0m================================== The weather in NYC might be cloudy.
In [13]:
inputs = {"messages": [("user", "What's it known for?")]}
print_stream(graph.stream(inputs, config=config, stream_mode="values"))================================[1m Human Message [0m================================= What's it known for? ==================================[1m Ai Message [0m================================== New York City (NYC) is known for many things, including: 1. **Landmarks and Attractions**: - **Statue of Liberty**: A symbol of freedom and democracy. - **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere. - **Central Park**: A large urban park offering various recreational activities. - **Empire State Building**: An iconic skyscraper with an observation deck offering panoramic views of the city. - **Brooklyn Bridge**: A historic bridge connecting Manhattan and Brooklyn. 2. **Cultural Diversity**: NYC is a melting pot of cultures, languages, and cuisines, making it one of the most diverse cities in the world. 3. **Arts and Entertainment**: - **Broadway**: Renowned for its world-class theater productions. - **Museums**: Such as the Metropolitan Museum of Art, the Museum of Modern Art (MoMA), and the American Museum of Natural History. - **Music and Nightlife**: A vibrant scene with numerous music venues, bars, and clubs. 4. **Financial Hub**: - **Wall Street**: The financial district is home to the New York Stock Exchange and numerous financial institutions. 5. **Fashion and Shopping**: - **Fifth Avenue**: Known for its high-end shopping. - **Fashion Week**: One of the major fashion events held twice a year. 6. **Cuisine**: - **Diverse Food Scene**: From street food like hot dogs and pretzels to fine dining and international cuisines. - **Famous Foods**: New York-style pizza, bagels, and cheesecake. 7. **Media and Publishing**: - Home to major media companies, newspapers like The New York Times, and numerous publishing houses. 8. **Sports**: - Home to several major sports teams, including the New York Yankees (baseball), New York Mets (baseball), New York Knicks (basketball), Brooklyn Nets (basketball), New York Giants (football), and New York Jets (football). 9. **Education and Research**: - Prestigious institutions like Columbia University, New York University (NYU), and Rockefeller University. 10. **Public Transportation**: - An extensive subway system, buses, and taxis that make getting around the city convenient. NYC is a city that never sleeps, offering endless opportunities for exploration and experiences.
In [14]:
inputs = {"messages": [("user", "What's it known for?")]}
print_stream(
graph.stream(
inputs, config={"configurable": {"thread_id": 2}}, stream_mode="values"
)
)================================[1m Human Message [0m================================= What's it known for? ==================================[1m Ai Message [0m================================== Could you please specify what "it" refers to? Are you asking about a specific city, person, object, or something else?
In [15]:
graph = create_react_agent(
model, tools, interrupt_before=["tools"], checkpointer=MemorySaver()
)
config = {"configurable": {"thread_id": "42"}}
inputs = {"messages": [("user", "What's the weather in SF?")]}
print_stream(graph.stream(inputs, config, stream_mode="values"))================================[1m Human Message [0m================================= What's the weather in SF? ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_I7B9YW4ENth7QXYzDpiIoLCE) Call ID: call_I7B9YW4ENth7QXYzDpiIoLCE Args: city: sf
In [16]:
snapshot = graph.get_state(config)
print("Next step: ", snapshot.next)Next step: ('tools',)
In [17]:
print_stream(graph.stream(None, config, stream_mode="values"))=================================[1m Tool Message [0m================================= Name: get_weather It's always sunny in sf ==================================[1m Ai Message [0m================================== The weather in San Francisco is currently sunny.