optional dependency usage

This commit is contained in:
Braelyn Boynton
2024-04-03 23:14:37 -07:00
parent 67bc1de4d6
commit 215e39833a
4 changed files with 42 additions and 9 deletions

View File

@@ -9,7 +9,10 @@ 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
import agentops
try:
import agentops
except ImportError:
agentops = None
OPENAI_BIGGER_MODELS = ["gpt-4"]
@@ -98,7 +101,7 @@ class ToolUsage:
tool: BaseTool,
calling: Union[ToolCalling, InstructorToolCalling],
) -> None:
tool_event = agentops.ToolEvent(name=calling.tool_name)
tool_event = agentops.ToolEvent(name=calling.tool_name) if agentops else None
if self._check_tool_repeated_usage(calling=calling):
try:
result = self._i18n.errors("task_repeated_usage").format(
@@ -162,7 +165,8 @@ 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=e, trigger_event=tool_event))
if agentops:
agentops.record(agentops.ErrorEvent(details=e, trigger_event=tool_event))
return self.use(calling=calling, tool_string=tool_string)
if self.tools_handler:
@@ -183,7 +187,8 @@ class ToolUsage:
)
self._printer.print(content=f"\n\n{result}\n", color="yellow")
agentops.record(tool_event)
if agentops:
agentops.record(tool_event)
self._telemetry.tool_usage(
llm=self.function_calling_llm,
tool_name=tool.name,