track tool usage time

This commit is contained in:
Braelyn Boynton
2024-03-29 14:28:33 -07:00
parent a9339fcef6
commit c44c2b6808

View File

@@ -9,7 +9,7 @@ from crewai.agents.tools_handler import ToolsHandler
from crewai.telemetry import Telemetry
from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling
from crewai.utilities import I18N, Converter, ConverterError, Printer
from agentops import ToolEvent, ErrorEvent, record
import agentops
OPENAI_BIGGER_MODELS = ["gpt-4"]
@@ -80,15 +80,12 @@ class ToolUsage:
self.task.increment_tools_errors()
return error
event = ToolEvent(name=calling.tool_name)
record(event)
try:
tool = self._select_tool(calling.tool_name)
except Exception as e:
error = getattr(e, "message", str(e))
self.task.increment_tools_errors()
self._printer.print(content=f"\n\n{error}\n", color="red")
record(ErrorEvent(details=error, trigger_event=event))
return error
return f"{self._use(tool_string=tool_string, tool=tool, calling=calling)}"
@@ -98,6 +95,7 @@ class ToolUsage:
tool: BaseTool,
calling: Union[ToolCalling, InstructorToolCalling],
) -> None:
tool_event = agentops.ToolEvent(name=calling.tool_name)
if self._check_tool_repeated_usage(calling=calling):
try:
result = self._i18n.errors("task_repeated_usage").format(
@@ -158,11 +156,13 @@ class ToolUsage:
self._printer.print(content=f"\n\n{error_message}\n", color="red")
return error
self.task.increment_tools_errors()
agentops.record(agentops.ErrorEvent(details=error, trigger_event=tool_event))
return self.use(calling=calling, tool_string=tool_string)
self.tools_handler.on_tool_use(calling=calling, output=result)
self._printer.print(content=f"\n\n{result}\n", color="yellow")
agentops.record(tool_event)
self._telemetry.tool_usage(
llm=self.function_calling_llm,
tool_name=tool.name,