diff --git a/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx b/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx index 073a571f20876..1cf332c5cdda0 100644 --- a/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx +++ b/docs/snippets/modules/agents/how_to/custom_llm_agent.mdx @@ -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) ``` diff --git a/docs/snippets/modules/agents/how_to/custom_llm_chat_agent.mdx b/docs/snippets/modules/agents/how_to/custom_llm_chat_agent.mdx index 8f61f9a64175d..c5a18892fefdc 100644 --- a/docs/snippets/modules/agents/how_to/custom_llm_chat_agent.mdx +++ b/docs/snippets/modules/agents/how_to/custom_llm_chat_agent.mdx @@ -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)] ``` diff --git a/libs/langchain/langchain/agents/agent_toolkits/openapi/planner.py b/libs/langchain/langchain/agents/agent_toolkits/openapi/planner.py index 759891d030599..e4ab1ef9328e3 100644 --- a/libs/langchain/langchain/agents/agent_toolkits/openapi/planner.py +++ b/libs/langchain/langchain/agents/agent_toolkits/openapi/planner.py @@ -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] ), @@ -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] ), diff --git a/libs/langchain/langchain/agents/chat/base.py b/libs/langchain/langchain/agents/chat/base.py index dfe97b222f764..6e1dd1f93ae6e 100644 --- a/libs/langchain/langchain/agents/chat/base.py +++ b/libs/langchain/langchain/agents/chat/base.py @@ -1,3 +1,4 @@ +import json from typing import Any, List, Optional, Sequence, Tuple from langchain.agents.agent import Agent, AgentOutputParser @@ -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( [ diff --git a/libs/langchain/langchain/agents/conversational/base.py b/libs/langchain/langchain/agents/conversational/base.py index ab5f041ec9b4c..3d8a7a26eb0d7 100644 --- a/libs/langchain/langchain/agents/conversational/base.py +++ b/libs/langchain/langchain/agents/conversational/base.py @@ -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 @@ -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 ) diff --git a/libs/langchain/langchain/agents/conversational_chat/base.py b/libs/langchain/langchain/agents/conversational_chat/base.py index 5b99593b68e83..0fa079d9a7dcb 100644 --- a/libs/langchain/langchain/agents/conversational_chat/base.py +++ b/libs/langchain/langchain/agents/conversational_chat/base.py @@ -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 @@ -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() diff --git a/libs/langchain/langchain/agents/mrkl/base.py b/libs/langchain/langchain/agents/mrkl/base.py index 177291c03f1da..dbcbf698709f4 100644 --- a/libs/langchain/langchain/agents/mrkl/base.py +++ b/libs/langchain/langchain/agents/mrkl/base.py @@ -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 @@ -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: diff --git a/libs/langchain/langchain/agents/structured_chat/base.py b/libs/langchain/langchain/agents/structured_chat/base.py index d325e3463cee8..af780d7c1cbf9 100644 --- a/libs/langchain/langchain/agents/structured_chat/base.py +++ b/libs/langchain/langchain/agents/structured_chat/base.py @@ -1,3 +1,4 @@ +import json import re from typing import Any, List, Optional, Sequence, Tuple @@ -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: diff --git a/libs/langchain/langchain/agents/tools.py b/libs/langchain/langchain/agents/tools.py index 91e80bc4ee477..dd7d369bf77e0 100644 --- a/libs/langchain/langchain/agents/tools.py +++ b/libs/langchain/langchain/agents/tools.py @@ -1,4 +1,5 @@ """Interface for tools.""" +import json from typing import List, Optional from langchain.callbacks.manager import ( @@ -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}]." @@ -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}]." diff --git a/libs/langchain/tests/unit_tests/agents/test_mrkl.py b/libs/langchain/tests/unit_tests/agents/test_mrkl.py index 0fda94fa6479e..a6c34475dd8c3 100644 --- a/libs/langchain/tests/unit_tests/agents/test_mrkl.py +++ b/libs/langchain/tests/unit_tests/agents/test_mrkl.py @@ -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,