mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-21 15:12:26 +02:00
Since we are demonstrating thread-level memory not human-in-the-loop, a string is more straightforward and reliable than AssertionError(), when dealing with 'Unknown Location'.
9.9 KiB
9.9 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")In [3]:
# First we initialize the model we want to use.
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o", temperature=0)
# For this tutorial we will use custom tool that returns pre-defined values for weather in two cities (NYC & SF)
from langchain_core.tools import tool
@tool
def get_weather(location: str) -> str:
"""Use this to get weather information."""
if any([city in location.lower() for city in ["nyc", "new york city"]]):
return "It might be cloudy in nyc"
elif any([city in location.lower() for city in ["sf", "san francisco"]]):
return "It's always sunny in sf"
else:
return f"I am not sure what the weather is in {location}"
tools = [get_weather]
# We can add "chat memory" to the graph with LangGraph's checkpointer
# to retain the chat context between interactions
from langgraph.checkpoint.memory import MemorySaver
memory = MemorySaver()
# Define the graph
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(model, tools=tools, checkpointer=memory)In [5]:
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()In [6]:
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_xM1suIq26KXvRFqJIvLVGfqG) Call ID: call_xM1suIq26KXvRFqJIvLVGfqG 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 [7]:
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 a variety of iconic landmarks, cultural institutions, and vibrant neighborhoods. Some of the most notable aspects include: 1. **Statue of Liberty**: A symbol of freedom and democracy. 2. **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere. 3. **Central Park**: A large urban park offering a green oasis in the middle of the city. 4. **Empire State Building**: An iconic skyscraper with an observation deck offering panoramic views of the city. 5. **Broadway**: Famous for its world-class theater productions. 6. **Wall Street**: The financial hub of the United States. 7. **Museums**: Including the Metropolitan Museum of Art, the Museum of Modern Art (MoMA), and the American Museum of Natural History. 8. **Diverse Cuisine**: A melting pot of culinary experiences from around the world. 9. **Cultural Diversity**: A rich tapestry of cultures, languages, and traditions. 10. **Fashion**: A global fashion capital, home to New York Fashion Week. These are just a few highlights of what makes NYC a unique and vibrant city.
In [ ]: