mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
* ocd * x * spelling * changes * edits * nits * changes * last nit
11 KiB
11 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 [1]:
# 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 typing import Literal
from langchain_core.tools import tool
@tool
def get_weather(location: str):
"""Use this to get weather information from a given location."""
if location.lower() in ["nyc", "new york"]:
return "It might be cloudy in nyc"
elif location.lower() in ["sf", "san francisco"]:
return "It's always sunny in sf"
else:
raise AssertionError("Unknown Location")
tools = [get_weather]
# We need a checkpointer to enable human-in-the-loop patterns
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, interrupt_before=["tools"], checkpointer=memory
)In [2]:
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()In [3]:
from langchain_core.messages import HumanMessage
config = {"configurable": {"thread_id": "42"}}
inputs = {"messages": [("user", "what is the weather in SF?")]}
print_stream(graph.stream(inputs, config, stream_mode="values"))================================[1m Human Message [0m================================= what is the weather in SF? ==================================[1m Ai Message [0m================================== Tool Calls: get_weather (call_TcDfLuoCKLmQ7eG71SedxLZ6) Call ID: call_TcDfLuoCKLmQ7eG71SedxLZ6 Args: location: San Francisco, CA
In [4]:
snapshot = graph.get_state(config)
print("Next step: ", snapshot.next)Next step: ('tools',)
In [5]:
print_stream(graph.stream(None, config, stream_mode="values"))=================================[1m Tool Message [0m================================= Name: get_weather Error: AssertionError('Unknown Location') Please fix your mistakes. ==================================[1m Ai Message [0m================================== It seems there was an issue with the location provided. Let's try specifying "San Francisco, California" more clearly. Tool Calls: get_weather (call_TZm9HCShGNEreglVJcmUdXqG) Call ID: call_TZm9HCShGNEreglVJcmUdXqG Args: location: San Francisco, California
In [6]:
state = graph.get_state(config)
last_message = state.values['messages'][-1]
last_message.tool_calls[0]['args'] = {"location": "San Francisco"}
graph.update_state(config, {"messages": [ last_message]})Out [6]:
{'configurable': {'thread_id': '42',
'checkpoint_ns': '',
'checkpoint_id': '1ef66368-9772-67ea-8004-07c779869a0a'}}In [7]:
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. Enjoy the sunshine!