mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
6.0 KiB
6.0 KiB
In [1]:
!pip install --quiet -U langchain_openai[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip available: [0m[31;49m22.3.1[0m[39;49m -> [0m[32;49m24.0[0m [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
In [2]:
import os
import getpass
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")In [11]:
from typing import List
from langchain_openai import ChatOpenAI
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph import END, MessageGraph
model = ChatOpenAI(temperature=0)
graph = MessageGraph()
def invoke_model(state: List[BaseMessage]):
return model.invoke(state)
graph.add_node("oracle", invoke_model)
graph.add_edge("oracle", END)
graph.set_entry_point("oracle")
runnable = graph.compile()In [12]:
runnable.invoke(HumanMessage("What is 1 + 1?"))Out [12]:
[HumanMessage(content='What is 1 + 1?'), AIMessage(content='1 + 1 equals 2.')]
In [17]:
import json
from langchain_core.messages import ToolMessage
from langchain_core.tools import tool
from langchain_core.utils.function_calling import convert_to_openai_tool
@tool
def multiply(first_number: int, second_number: int):
"""Multiplies two numbers together."""
return first_number * second_number
model = ChatOpenAI(temperature=0)
model_with_tools = model.bind(tools=[convert_to_openai_tool(multiply)])
graph = MessageGraph()
def invoke_model(state: List[BaseMessage]):
return model_with_tools.invoke(state)
graph.add_node("oracle", invoke_model)
def invoke_tool(state: List[BaseMessage]):
tool_calls = state[-1].additional_kwargs.get("tool_calls", [])
multiply_call = None
for tool_call in tool_calls:
if tool_call.get("function").get("name") == "multiply":
multiply_call = tool_call
if multiply_call is None:
raise Exception("No adder input found.")
res = multiply.invoke(
json.loads(multiply_call.get("function").get("arguments"))
)
return ToolMessage(
tool_call_id=multiply_call.get("id"),
content=res
)
graph.add_node("multiply", invoke_tool)
graph.add_edge("multiply", END)
graph.set_entry_point("oracle")In [18]:
def router(state: List[BaseMessage]):
tool_calls = state[-1].additional_kwargs.get("tool_calls", [])
if len(tool_calls):
return "multiply"
else:
return "end"
graph.add_conditional_edges("oracle", router, {
"multiply": "multiply",
"end": END,
})In [22]:
runnable = graph.compile()
runnable.invoke(HumanMessage("What is 123 * 456?"))Out [22]:
[HumanMessage(content='What is 123 * 456?'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_cKnzV4f7sEjnuetuyW8glC7u', 'function': {'arguments': '{"first_number":123,"second_number":456}', 'name': 'multiply'}, 'type': 'function'}]}),
ToolMessage(content='56088', tool_call_id='call_cKnzV4f7sEjnuetuyW8glC7u')]In [24]:
runnable.invoke(HumanMessage("What is your name?"))Out [24]:
[HumanMessage(content='What is your name?'), AIMessage(content='My name is Assistant. How can I assist you today?')]
In [ ]: