mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-29 03:09:45 +02:00
8.1 KiB
8.1 KiB
In [1]:
%%capture --no-stderr
%pip install -U langgraph langchain-openaiIn [ ]:
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 typing import Literal
from langchain_core.tools import tool
@tool
def get_weather(city: Literal["nyc", "sf"]):
"""Use this to get weather information."""
if city == "nyc":
return "It might be cloudy in nyc"
elif city == "sf":
return "It's always sunny in sf"
else:
raise AssertionError("Unknown city")
tools = [get_weather]
# Define the structured output schema
from pydantic import BaseModel, Field
class WeatherResponse(BaseModel):
"""Respond to the user in this format."""
conditions: str = Field(description="Weather conditions")
# Define the graph
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(
model,
tools=tools,
# specify the schema for the structured output using `response_format` parameter
response_format=WeatherResponse,
)In [4]:
inputs = {"messages": [("user", "What's the weather in NYC?")]}
response = graph.invoke(inputs)In [5]:
response["structured_response"]Out [5]:
WeatherResponse(conditions='cloudy')
In [6]:
graph = create_react_agent(
model,
tools=tools,
# specify both the system prompt and the schema for the structured output
response_format=("Always return capitalized weather conditions", WeatherResponse),
)
inputs = {"messages": [("user", "What's the weather in NYC?")]}
response = graph.invoke(inputs)In [7]:
response["structured_response"]Out [7]:
WeatherResponse(conditions='Cloudy')