mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
43 KiB
43 KiB
In [1]:
%%capture --no-stderr
%pip install --quiet -U langgraph langchain langchain_openai tavily-pythonIn [ ]:
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
os.environ["TAVILY_API_KEY"] = getpass.getpass("Tavily API Key:")In [ ]:
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("LangSmith API Key:")In [1]:
from langchain_community.tools.tavily_search import TavilySearchResults
tools = [TavilySearchResults(max_results=1)]In [2]:
from langgraph.prebuilt import ToolExecutor
tool_executor = ToolExecutor(tools)In [3]:
from langchain_openai import ChatOpenAI
# We will set streaming=True so that we can stream tokens
# See the streaming section for more information on this.
model = ChatOpenAI(temperature=0, streaming=True)In [4]:
model = model.bind_tools(tools)In [5]:
import operator
from typing import Annotated, Sequence, TypedDict
from langchain_core.messages import BaseMessage
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]In [6]:
from langchain_core.messages import ToolMessage
from langgraph.prebuilt import ToolInvocation
# Define the function that determines whether to continue or not
def should_continue(state):
messages = state["messages"]
last_message = messages[-1]
# If there is no function call, then we finish
if not last_message.tool_calls:
return "end"
# Otherwise if there is, we continue
else:
return "continue"
# Define the function that calls the model
def call_model(state):
messages = state["messages"]
response = model.invoke(messages)
# We return a list, because this will get added to the existing list
return {"messages": [response]}
# Define the function to execute tools
def call_tool(state):
messages = state["messages"]
# Based on the continue condition
# we know the last message involves a function call
last_message = messages[-1]
# We construct an ToolInvocation from the function_call
tool_call = last_message.tool_calls[0]
action = ToolInvocation(
tool=tool_call["name"],
tool_input=tool_call["args"],
)
# We call the tool_executor and get back a response
response = tool_executor.invoke(action)
# We use the response to create a FunctionMessage
function_message = ToolMessage(
content=str(response), name=action.tool, tool_call_id=tool_call["id"]
)
# We return a list, because this will get added to the existing list
return {"messages": [function_message]}In [7]:
from langgraph.graph import END, StateGraph, START
# Define a new graph
workflow = StateGraph(AgentState)
# Define the two nodes we will cycle between
workflow.add_node("agent", call_model)
workflow.add_node("action", call_tool)
# Set the entrypoint as `agent`
# This means that this node is the first one called
workflow.add_edge(START, "agent")
# We now add a conditional edge
workflow.add_conditional_edges(
# First, we define the start node. We use `agent`.
# This means these are the edges taken after the `agent` node is called.
"agent",
# Next, we pass in the function that will determine which node is called next.
should_continue,
# Finally we pass in a mapping.
# The keys are strings, and the values are other nodes.
# END is a special node marking that the graph should finish.
# What will happen is we will call `should_continue`, and then the output of that
# will be matched against the keys in this mapping.
# Based on which one it matches, that node will then be called.
{
# If `tools`, then we call the tool node.
"continue": "action",
# Otherwise we finish.
"end": END,
},
)
# We now add a normal edge from `tools` to `agent`.
# This means that after `tools` is called, `agent` node is called next.
workflow.add_edge("action", "agent")
# Finally, we compile it!
# This compiles it into a LangChain Runnable,
# meaning you can use it as you would any other runnable
app = workflow.compile()In [11]:
from IPython.display import Image, display
try:
display(Image(app.get_graph(xray=True).draw_mermaid_png()))
except Exception:
# This requires some extra dependencies and is optional
passIn [8]:
from langchain_core.messages import HumanMessage
inputs = {"messages": [HumanMessage(content="what is the weather in sf")]}
app.invoke(inputs)Out [8]:
{'messages': [HumanMessage(content='what is the weather in sf'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_GWqmwbBTTMPniOg7gqn1XsID', 'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-aeabfb65-72bf-499a-8922-aae4878dcae6-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_GWqmwbBTTMPniOg7gqn1XsID'}]),
ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1714807910, \'localtime\': \'2024-05-04 0:31\'}, \'current\': {\'last_updated_epoch\': 1714807800, \'last_updated\': \'2024-05-04 00:30\', \'temp_c\': 12.8, \'temp_f\': 55.0, \'is_day\': 0, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/122.png\', \'code\': 1009}, \'wind_mph\': 11.9, \'wind_kph\': 19.1, \'wind_degree\': 240, \'wind_dir\': \'WSW\', \'pressure_mb\': 1013.0, \'pressure_in\': 29.9, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 96, \'cloud\': 100, \'feelslike_c\': 11.4, \'feelslike_f\': 52.4, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 1.0, \'gust_mph\': 14.9, \'gust_kph\': 23.9}}"}]', name='tavily_search_results_json', tool_call_id='call_GWqmwbBTTMPniOg7gqn1XsID'),
AIMessage(content='The current weather in San Francisco is as follows:\n- Temperature: 55.0°F (12.8°C)\n- Condition: Overcast\n- Wind: 11.9 mph from WSW\n- Humidity: 96%\n- Cloud Cover: 100%\n- Feels like: 52.4°F (11.4°C)\n- Visibility: 9.0 miles\n\nFor more detailed information, you can visit [Weather API](https://www.weatherapi.com/).', response_metadata={'finish_reason': 'stop'}, id='run-925ab339-7da5-4fd0-851f-e765710408fd-0')]}In [9]:
inputs = {"messages": [HumanMessage(content="what is the weather in sf")]}
for output in app.stream(inputs):
# stream() yields dictionaries with output keyed by node name
for key, value in output.items():
print(f"Output from node '{key}':")
print("---")
print(value)
print("\n---\n")Output from node 'agent':
---
{'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_ccF3KsXlSfJbl6JQcTpgDLbt', 'function': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-e784ed37-cab3-4363-a9fd-cf48929246de-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_ccF3KsXlSfJbl6JQcTpgDLbt'}])]}
---
Output from node 'action':
---
{'messages': [ToolMessage(content='[{\'url\': \'https://www.weatherapi.com/\', \'content\': "{\'location\': {\'name\': \'San Francisco\', \'region\': \'California\', \'country\': \'United States of America\', \'lat\': 37.78, \'lon\': -122.42, \'tz_id\': \'America/Los_Angeles\', \'localtime_epoch\': 1714807910, \'localtime\': \'2024-05-04 0:31\'}, \'current\': {\'last_updated_epoch\': 1714807800, \'last_updated\': \'2024-05-04 00:30\', \'temp_c\': 12.8, \'temp_f\': 55.0, \'is_day\': 0, \'condition\': {\'text\': \'Overcast\', \'icon\': \'//cdn.weatherapi.com/weather/64x64/night/122.png\', \'code\': 1009}, \'wind_mph\': 11.9, \'wind_kph\': 19.1, \'wind_degree\': 240, \'wind_dir\': \'WSW\', \'pressure_mb\': 1013.0, \'pressure_in\': 29.9, \'precip_mm\': 0.0, \'precip_in\': 0.0, \'humidity\': 96, \'cloud\': 100, \'feelslike_c\': 11.4, \'feelslike_f\': 52.4, \'vis_km\': 16.0, \'vis_miles\': 9.0, \'uv\': 1.0, \'gust_mph\': 14.9, \'gust_kph\': 23.9}}"}]', name='tavily_search_results_json', tool_call_id='call_ccF3KsXlSfJbl6JQcTpgDLbt')]}
---
Output from node 'agent':
---
{'messages': [AIMessage(content='The current weather in San Francisco is as follows:\n- Temperature: 55.0°F (12.8°C)\n- Condition: Overcast\n- Wind: 11.9 mph from WSW\n- Humidity: 96%\n- Cloud Cover: 100%\n- Visibility: 9.0 miles\n\nFor more detailed information, you can visit [Weather API](https://www.weatherapi.com/).', response_metadata={'finish_reason': 'stop'}, id='run-4e9656bd-bf8f-484a-8e44-e5cb1f0e8d34-0')]}
---
In [10]:
inputs = {"messages": [HumanMessage(content="what is the weather in sf?")]}
async for output in app.astream_log(inputs, include_types=["llm"]):
# astream_log() yields the requested logs (here LLMs) in JSONPatch format
for op in output.ops:
if op["path"] == "/streamed_output/-":
# this is the output from .stream()
...
elif op["path"].startswith("/logs/") and op["path"].endswith(
"/streamed_output/-"
):
# because we chose to only include LLMs, these are LLM tokens
print(op["value"])content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_n0NjukNhoDLRhoFbNLBJDHtr', 'function': {'arguments': '', 'name': 'tavily_search_results_json'}, 'type': 'function'}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': 'tavily_search_results_json', 'args': '', 'id': 'call_n0NjukNhoDLRhoFbNLBJDHtr', 'error': None}] tool_call_chunks=[{'name': 'tavily_search_results_json', 'args': '', 'id': 'call_n0NjukNhoDLRhoFbNLBJDHtr', 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '{"', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' tool_calls=[{'name': '', 'args': {}, 'id': None}] tool_call_chunks=[{'name': None, 'args': '{"', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'query', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': 'query', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': 'query', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '":"', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': '":"', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': '":"', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'weather', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': 'weather', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': 'weather', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' in', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': ' in', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': ' in', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' San', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': ' San', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': ' San', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' Francisco', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': ' Francisco', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': ' Francisco', 'id': None, 'index': 0}]
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '"}', 'name': None}, 'type': None}]} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc' invalid_tool_calls=[{'name': None, 'args': '"}', 'id': None, 'error': None}] tool_call_chunks=[{'name': None, 'args': '"}', 'id': None, 'index': 0}]
content='' response_metadata={'finish_reason': 'tool_calls'} id='run-dbd086ea-6277-4e0b-8c05-4c0e979445bc'
content='' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='The' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' current' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' weather' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' in' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' San' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Francisco' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' is' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' as' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' follows' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Temperature' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='12' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='8' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='°C' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' (' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='55' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='0' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='°F' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=')\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Condition' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Over' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='cast' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Wind' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='11' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='9' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' mph' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' from' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' W' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='SW' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Hum' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='idity' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='96' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='%\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Cloud' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Cover' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='100' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='%\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Visibility' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='16' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='0' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' km' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' (' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='9' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='0' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' miles' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=')\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='-' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' UV' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' Index' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=':' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' ' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='1' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='0' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='\n\n' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='For' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' more' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' details' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=',' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' you' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' can' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' visit' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' [' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='Weather' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=' API' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='](' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='https' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='://' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='www' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.weather' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='api' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='.com' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='/' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content=').' id='run-62ad24a3-0db2-40a1-8905-109438327a83'
content='' response_metadata={'finish_reason': 'stop'} id='run-62ad24a3-0db2-40a1-8905-109438327a83'
In [ ]: