Modify input before it reaches a specific agent #25478
Replies: 1 comment
-
To modify the user's input prompt specifically for the Text2SQL DBAgent in your workflow, you can use the from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain_community.utilities import SQLDatabase
from langchain.chains.sql_database.query import create_sql_query_chain
# Initialize the database and LLM as shown in the setup
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = OpenAI(temperature=0, verbose=True)
# Define the custom prompt template
_DEFAULT_TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Use the following format:
Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"
Only use the following tables:
{table_info}
If someone asks for the table foobar, they really mean the employee table.
Question: {input}"""
PROMPT = PromptTemplate.from_template(_DEFAULT_TEMPLATE)
# Initialize the SQLDatabaseChain with the custom prompt
db_chain = create_sql_query_chain(llm, db, prompt=PROMPT, k=5)
# Example usage
result = db_chain.invoke({"question": "How many employees are there in the foobar table?"})
print(result) This code sets up a custom prompt for the Text2SQL DBAgent, ensuring that the agent follows the specified instructions when processing user input. The Additionally, LangChain provides examples of modifying input prompts for specific agents using callbacks. For instance, the from langchain_core.messages import SystemMessage
from langgraph.prebuilt import create_react_agent
system_message = "You are a helpful assistant. Respond only in Spanish."
app = create_react_agent(model, tools, state_modifier=system_message)
messages = app.invoke({"messages": [("user", query)]}) You can also pass an arbitrary function to modify the state messages: from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant. Respond only in Spanish."),
("placeholder", "{messages}"),
]
)
def _modify_state_messages(state: AgentState):
return prompt.invoke({"messages": state["messages"]}).to_messages() + [
("user", "Also say 'Pandamonium!' after the answer.")
]
app = create_react_agent(model, tools, state_modifier=_modify_state_messages)
messages = app.invoke({"messages": [("human", query)]})
print(
{
"input": query,
"output": messages["messages"][-1].content,
}
) These examples demonstrate how you can modify the input prompt dynamically for specific agents using callbacks [6]. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
None
Description
We have a workflow with multiple agents including a Text2SQL DBAgent
We want to modify the user's input prompt only for the DBAgent if DBAgent is called, maybe using a callback or any other method.
System Info
fastapi==0.110.2
pydantic==2.0.2
pydantic-settings==2.0.3
langchain==0.2.0
langchain-core==0.2.0
langchain-openai==0.1.7
langchain-community==0.2.0
faiss-cpu==1.8.0
python-dotenv==1.0.1
uvicorn==0.23.1
psycopg2-binary==2.9.9
docx2txt==0.8
pandas==2.2.2
langgraph==0.0.55
openai==1.33.0
slack-sdk==3.30.0
SQLAlchemy==2.0.29
Beta Was this translation helpful? Give feedback.
All reactions