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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote tool names #11463

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/snippets/modules/agents/how_to/custom_llm_agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CustomPromptTemplate(StringPromptTemplate):
# Create a tools variable from the list of tools provided
kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools])
# Create a list of tool names for the tools provided
kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools])
kwargs["tool_names"] = ", ".join([json.dumps(tool.name) for tool in self.tools])
return self.template.format(**kwargs)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CustomPromptTemplate(BaseChatPromptTemplate):
# Create a tools variable from the list of tools provided
kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools])
# Create a list of tool names for the tools provided
kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools])
kwargs["tool_names"] = ", ".join([json.dumps(tool.name) for tool in self.tools])
formatted = self.template.format(**kwargs)
return [HumanMessage(content=formatted)]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def _create_api_controller_agent(
partial_variables={
"api_url": api_url,
"api_docs": api_docs,
"tool_names": ", ".join([tool.name for tool in tools]),
"tool_names": ", ".join([json.dumps(tool.name) for tool in tools]),
"tool_descriptions": "\n".join(
[f"{tool.name}: {tool.description}" for tool in tools]
),
Expand Down Expand Up @@ -335,7 +335,7 @@ def create_openapi_agent(
template=API_ORCHESTRATOR_PROMPT,
input_variables=["input", "agent_scratchpad"],
partial_variables={
"tool_names": ", ".join([tool.name for tool in tools]),
"tool_names": ", ".join([json.dumps(tool.name) for tool in tools]),
"tool_descriptions": "\n".join(
[f"{tool.name}: {tool.description}" for tool in tools]
),
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain/langchain/agents/chat/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any, List, Optional, Sequence, Tuple

from langchain.agents.agent import Agent, AgentOutputParser
Expand Down Expand Up @@ -77,7 +78,7 @@ def create_prompt(
input_variables: Optional[List[str]] = None,
) -> BasePromptTemplate:
tool_strings = "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
tool_names = ", ".join([tool.name for tool in tools])
tool_names = ", ".join([json.dumps(tool.name) for tool in tools])
format_instructions = format_instructions.format(tool_names=tool_names)
template = "\n\n".join(
[
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain/langchain/agents/conversational/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""An agent designed to hold a conversation in addition to using tools."""
from __future__ import annotations

import json
from typing import Any, List, Optional, Sequence

from langchain.agents.agent import Agent, AgentOutputParser
Expand Down Expand Up @@ -73,7 +74,7 @@ def create_prompt(
tool_strings = "\n".join(
[f"> {tool.name}: {tool.description}" for tool in tools]
)
tool_names = ", ".join([tool.name for tool in tools])
tool_names = ", ".join([json.dumps(tool.name) for tool in tools])
format_instructions = format_instructions.format(
tool_names=tool_names, ai_prefix=ai_prefix, human_prefix=human_prefix
)
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain/langchain/agents/conversational_chat/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""An agent designed to hold a conversation in addition to using tools."""
from __future__ import annotations

import json
from typing import Any, List, Optional, Sequence, Tuple

from langchain.agents.agent import Agent, AgentOutputParser
Expand Down Expand Up @@ -67,7 +68,7 @@ def create_prompt(
tool_strings = "\n".join(
[f"> {tool.name}: {tool.description}" for tool in tools]
)
tool_names = ", ".join([tool.name for tool in tools])
tool_names = ", ".join([json.dumps(tool.name) for tool in tools])
_output_parser = output_parser or cls._get_default_output_parser()
format_instructions = human_message.format(
format_instructions=_output_parser.get_format_instructions()
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain/langchain/agents/mrkl/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Attempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf."""
from __future__ import annotations

import json
from typing import Any, Callable, List, NamedTuple, Optional, Sequence

from langchain.agents.agent import Agent, AgentExecutor, AgentOutputParser
Expand Down Expand Up @@ -77,7 +78,7 @@ def create_prompt(
A PromptTemplate with the template assembled from the pieces here.
"""
tool_strings = "\n".join([f"{tool.name}: {tool.description}" for tool in tools])
tool_names = ", ".join([tool.name for tool in tools])
tool_names = ", ".join([json.dumps(tool.name) for tool in tools])
format_instructions = format_instructions.format(tool_names=tool_names)
template = "\n\n".join([prefix, tool_strings, format_instructions, suffix])
if input_variables is None:
Expand Down
3 changes: 2 additions & 1 deletion libs/langchain/langchain/agents/structured_chat/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import re
from typing import Any, List, Optional, Sequence, Tuple

Expand Down Expand Up @@ -84,7 +85,7 @@ def create_prompt(
args_schema = re.sub("}", "}}}}", re.sub("{", "{{{{", str(tool.args)))
tool_strings.append(f"{tool.name}: {tool.description}, args: {args_schema}")
formatted_tools = "\n".join(tool_strings)
tool_names = ", ".join([tool.name for tool in tools])
tool_names = ", ".join([json.dumps(tool.name) for tool in tools])
format_instructions = format_instructions.format(tool_names=tool_names)
template = "\n\n".join([prefix, formatted_tools, format_instructions, suffix])
if input_variables is None:
Expand Down
9 changes: 7 additions & 2 deletions libs/langchain/langchain/agents/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Interface for tools."""
import json
from typing import List, Optional

from langchain.callbacks.manager import (
Expand All @@ -21,7 +22,9 @@ def _run(
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
"""Use the tool."""
available_tool_names_str = ", ".join([tool for tool in available_tool_names])
available_tool_names_str = ", ".join(
[json.dumps(tool) for tool in available_tool_names]
)
return (
f"{requested_tool_name} is not a valid tool, "
f"try one of [{available_tool_names_str}]."
Expand All @@ -34,7 +37,9 @@ async def _arun(
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
) -> str:
"""Use the tool asynchronously."""
available_tool_names_str = ", ".join([tool for tool in available_tool_names])
available_tool_names_str = ", ".join(
[json.dumps(tool) for tool in available_tool_names]
)
return (
f"{requested_tool_name} is not a valid tool, "
f"try one of [{available_tool_names_str}]."
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain/tests/unit_tests/agents/test_mrkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_from_chains() -> None:
]
agent = ZeroShotAgent.from_llm_and_tools(FakeLLM(), chain_configs)
expected_tools_prompt = "foo: foobar1\nbar: foobar2"
expected_tool_names = "foo, bar"
expected_tool_names = '"foo", "bar"'
expected_template = "\n\n".join(
[
PREFIX,
Expand Down
Loading