How to add a custom system prompt to the prebuilt ReAct agent¶
This tutorial will show how to add a custom system prompt to the prebuilt ReAct agent. Please see this tutorial for how to get started with the prebuilt ReAct agent
You can add a custom system prompt by passing a string to the state_modifier
param.
Setup¶
First, let's install the required packages and set our API keys
In [1]:
Copied!
%%capture --no-stderr
%pip install -U langgraph langchain-openai
%%capture --no-stderr
%pip install -U langgraph langchain-openai
In [ ]:
Copied!
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")
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")
Code¶
In [1]:
Copied!
# 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]
# We can add our system prompt here
prompt = "Respond in Italian"
# Define the graph
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(model, tools=tools, state_modifier=prompt)
# 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]
# We can add our system prompt here
prompt = "Respond in Italian"
# Define the graph
from langgraph.prebuilt import create_react_agent
graph = create_react_agent(model, tools=tools, state_modifier=prompt)
Usage¶
In [2]:
Copied!
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
In [4]:
Copied!
inputs = {"messages": [("user", "What's the weather in NYC?")]}
print_stream(graph.stream(inputs, stream_mode="values"))
inputs = {"messages": [("user", "What's the weather in NYC?")]}
print_stream(graph.stream(inputs, stream_mode="values"))
================================ Human Message ================================= What's the weather in NYC? ================================== Ai Message ================================== Tool Calls: get_weather (call_b02uzBRrIm2uciJa8zDXCDxT) Call ID: call_b02uzBRrIm2uciJa8zDXCDxT Args: city: nyc ================================= Tool Message ================================= Name: get_weather It might be cloudy in nyc ================================== Ai Message ================================== A New York potrebbe essere nuvoloso.