fix: validation for tool name (#4790)
validation for tool name Tool name validation based on open ai Schema for tool names
This commit is contained in:
parent
f440552ee9
commit
230a01973e
2 changed files with 15 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import re
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import TYPE_CHECKING, cast
|
from typing import TYPE_CHECKING, cast
|
||||||
|
|
||||||
|
|
@ -178,6 +179,18 @@ class LCAgentComponent(Component):
|
||||||
def create_agent_runnable(self) -> Runnable:
|
def create_agent_runnable(self) -> Runnable:
|
||||||
"""Create the agent."""
|
"""Create the agent."""
|
||||||
|
|
||||||
|
def validate_tool_names(self) -> None:
|
||||||
|
"""Validate tool names to ensure they match the required pattern."""
|
||||||
|
pattern = re.compile(r"^[a-zA-Z0-9_-]+$")
|
||||||
|
if hasattr(self, "tools") and self.tools:
|
||||||
|
for tool in self.tools:
|
||||||
|
if not pattern.match(tool.name):
|
||||||
|
msg = (
|
||||||
|
f"Invalid tool name '{tool.name}': must only contain letters, numbers, underscores, dashes,"
|
||||||
|
" and cannot contain spaces."
|
||||||
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
class LCToolsAgentComponent(LCAgentComponent):
|
class LCToolsAgentComponent(LCAgentComponent):
|
||||||
_base_inputs = [
|
_base_inputs = [
|
||||||
|
|
@ -193,6 +206,7 @@ class LCToolsAgentComponent(LCAgentComponent):
|
||||||
]
|
]
|
||||||
|
|
||||||
def build_agent(self) -> AgentExecutor:
|
def build_agent(self) -> AgentExecutor:
|
||||||
|
self.validate_tool_names()
|
||||||
agent = self.create_agent_runnable()
|
agent = self.create_agent_runnable()
|
||||||
return AgentExecutor.from_agent_and_tools(
|
return AgentExecutor.from_agent_and_tools(
|
||||||
agent=RunnableAgent(runnable=agent, input_keys_arg=["input"], return_keys_arg=["output"]),
|
agent=RunnableAgent(runnable=agent, input_keys_arg=["input"], return_keys_arg=["output"]),
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ class ToolCallingAgentComponent(LCToolsAgentComponent):
|
||||||
("placeholder", "{agent_scratchpad}"),
|
("placeholder", "{agent_scratchpad}"),
|
||||||
]
|
]
|
||||||
prompt = ChatPromptTemplate.from_messages(messages)
|
prompt = ChatPromptTemplate.from_messages(messages)
|
||||||
|
self.validate_tool_names()
|
||||||
try:
|
try:
|
||||||
return create_tool_calling_agent(self.llm, self.tools or [], prompt)
|
return create_tool_calling_agent(self.llm, self.tools or [], prompt)
|
||||||
except NotImplementedError as e:
|
except NotImplementedError as e:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue