Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain[patch]: agents check prompt partial vars #20303

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/langchain/langchain/agents/json_chat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def create_json_chat_agent(
)
""" # noqa: E501
missing_vars = {"tools", "tool_names", "agent_scratchpad"}.difference(
prompt.input_variables
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ def create_openai_functions_agent(
]
)
"""
if "agent_scratchpad" not in prompt.input_variables:
if "agent_scratchpad" not in (
prompt.input_variables + list(prompt.partial_variables)
):
raise ValueError(
"Prompt must have input variable `agent_scratchpad`, but wasn't found. "
f"Found {prompt.input_variables} instead."
Expand Down
4 changes: 3 additions & 1 deletion libs/langchain/langchain/agents/openai_tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def create_openai_tools_agent(
]
)
"""
missing_vars = {"agent_scratchpad"}.difference(prompt.input_variables)
missing_vars = {"agent_scratchpad"}.difference(
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")

Expand Down
2 changes: 1 addition & 1 deletion libs/langchain/langchain/agents/react/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def create_react_agent(
prompt = PromptTemplate.from_template(template)
""" # noqa: E501
missing_vars = {"tools", "tool_names", "agent_scratchpad"}.difference(
prompt.input_variables
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")
Expand Down
4 changes: 3 additions & 1 deletion libs/langchain/langchain/agents/self_ask_with_search/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def create_self_ask_with_search_agent(

prompt = PromptTemplate.from_template(template)
""" # noqa: E501
missing_vars = {"agent_scratchpad"}.difference(prompt.input_variables)
missing_vars = {"agent_scratchpad"}.difference(
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")

Expand Down
2 changes: 1 addition & 1 deletion libs/langchain/langchain/agents/structured_chat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def create_structured_chat_agent(
)
""" # noqa: E501
missing_vars = {"tools", "tool_names", "agent_scratchpad"}.difference(
prompt.input_variables
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")
Expand Down
10 changes: 6 additions & 4 deletions libs/langchain/langchain/agents/tool_calling_agent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def create_tool_calling_agent(

from langchain.agents import AgentExecutor, create_tool_calling_agent, tool
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant"),
MessagesPlaceholder("chat_history", optional=True),
("placeholder", "{chat_history}",
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
("placeholder", "{agent_scratchpad}"),
]
)
model = ChatAnthropic(model="claude-3-opus-20240229")
Expand Down Expand Up @@ -75,7 +75,9 @@ def magic_function(input: int) -> int:
``MessagesPlaceholder``. Intermediate agent actions and tool output
messages will be passed in here.
"""
missing_vars = {"agent_scratchpad"}.difference(prompt.input_variables)
missing_vars = {"agent_scratchpad"}.difference(
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")

Expand Down
4 changes: 3 additions & 1 deletion libs/langchain/langchain/agents/xml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def create_xml_agent(
{agent_scratchpad}'''
prompt = PromptTemplate.from_template(template)
""" # noqa: E501
missing_vars = {"tools", "agent_scratchpad"}.difference(prompt.input_variables)
missing_vars = {"tools", "agent_scratchpad"}.difference(
prompt.input_variables + list(prompt.partial_variables)
)
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")

Expand Down