mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-20 14:42:28 +02:00
9.1 KiB
9.1 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")OPENAI_API_KEY: ········
In [3]:
from langchain_core.callbacks import Callbacks
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAIIn [4]:
@tool
async def get_items(
place: str, callbacks: Callbacks
) -> str: # <--- Accept callbacks (Python <= 3.10)
"""Use this tool to look up which items are in the given place."""
template = ChatPromptTemplate.from_messages(
[
(
"human",
"Can you tell me what kind of items i might find in the following place: '{place}'. "
"List at least 3 such items separating them by a comma. And include a brief description of each item..",
)
]
)
chain = template | llm.with_config(
{
"run_name": "Get Items LLM",
"tags": ["tool_llm"],
"callbacks": callbacks, # <-- Propagate callbacks (Python <= 3.10)
}
)
chunks = [chunk async for chunk in chain.astream({"place": place})]
return "".join(chunk.content for chunk in chunks)In [5]:
llm = ChatOpenAI(model_name="gpt-3.5-turbo")
tools = [get_items]
agent = create_react_agent(llm, tools=tools)In [6]:
async for event in agent.astream_events(
{"messages": [("human", "what items are on the shelf?")]}, version="v2"
):
tags = event.get("tags", [])
if event["event"] == "on_chat_model_stream" and "tool_llm" in tags:
print(event["data"]["chunk"].content, end="", flush=True)/Users/vadymbarda/.virtualenvs/langgraph/lib/python3.12/site-packages/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: This API is in beta and may change in the future. warn_beta(
1. Books - A collection of written or printed works bound together with covers. They can be fiction or non-fiction and come in various genres. 2. Picture frames - A decorative border for a photograph or artwork, typically made of wood, metal, or plastic. Picture frames are used to display and protect a picture or painting. 3. Candles - A cylinder of wax with a central wick that is lit to produce light or fragrance. Candles are often used for decoration, ambiance, or religious ceremonies.
In [7]:
final_messages = event["data"]["output"]["messages"]In [8]:
for message in final_messages:
message.pretty_print()================================[1m Human Message [0m================================= what items are on the shelf? ==================================[1m Ai Message [0m================================== Tool Calls: get_items (call_5CAMZ3asoLsZm9ocMbCOWxYQ) Call ID: call_5CAMZ3asoLsZm9ocMbCOWxYQ Args: place: shelf =================================[1m Tool Message [0m================================= Name: get_items 1. Books - A collection of written or printed works bound together with covers. They can be fiction or non-fiction and come in various genres. 2. Picture frames - A decorative border for a photograph or artwork, typically made of wood, metal, or plastic. Picture frames are used to display and protect a picture or painting. 3. Candles - A cylinder of wax with a central wick that is lit to produce light or fragrance. Candles are often used for decoration, ambiance, or religious ceremonies. ==================================[1m Ai Message [0m================================== The items on the shelf are: 1. Books 2. Picture frames 3. Candles