From e16b1f4a5350bfdca2a8e5b1610cdfb75a2f2591 Mon Sep 17 00:00:00 2001 From: deadlious <48437032+deadlious@users.noreply.github.com> Date: Thu, 2 May 2024 09:15:34 +0300 Subject: [PATCH] Tool name recognition based on string distance (#521) * adding variations of ask question and delegate work tools * Revert "adding variations of ask question and delegate work tools" This reverts commit 38d4589be8883c22ca1baabc44de3a4d3df1653f. * adding distance calculation for tool names. * proper formatting * remove brackets --- src/crewai/tools/tool_usage.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/crewai/tools/tool_usage.py b/src/crewai/tools/tool_usage.py index b3bf3c0a3..ebc81c0cf 100644 --- a/src/crewai/tools/tool_usage.py +++ b/src/crewai/tools/tool_usage.py @@ -1,6 +1,7 @@ import ast from textwrap import dedent from typing import Any, List, Union +from difflib import SequenceMatcher from langchain_core.tools import BaseTool from langchain_openai import ChatOpenAI @@ -215,12 +216,15 @@ class ToolUsage: def _select_tool(self, tool_name: str) -> BaseTool: for tool in self.tools: - if tool.name.lower().strip() == tool_name.lower().strip(): + if ( + tool.name.lower().strip() == tool_name.lower().strip() + or SequenceMatcher(None, tool.name.lower().strip(), tool_name.lower().strip()).ratio() > 0.9 + ): return tool self.task.increment_tools_errors() if tool_name and tool_name != "": raise Exception( - f"Action '{tool_name}' don't exist, these are the only available Actions: {self.tools_description}" + f"Action '{tool_name}' don't exist, these are the only available Actions:\n {self.tools_description}" ) else: raise Exception(