fix: Initialize agentops in Crew setup (#2102)

- Add proper agentops initialization in Crew's set_private_attrs
- Add test coverage for agentops initialization
- Add test coverage for Gemini LLM with agentops

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-12 06:24:05 +00:00
parent 47818f4f41
commit 420f826e56
2 changed files with 100 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import asyncio
import json
import os
import re
import uuid
import warnings
@@ -248,6 +249,20 @@ class Crew(BaseModel):
if self.output_log_file:
self._file_handler = FileHandler(self.output_log_file)
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
# Initialize agentops if available and API key is present
if agentops:
api_key = os.getenv("AGENTOPS_API_KEY")
if api_key:
try:
agentops.init(api_key)
except Exception as e:
self._logger.log(
"warning",
f"Failed to initialize agentops: {e}",
color="yellow"
)
if self.function_calling_llm and not isinstance(self.function_calling_llm, LLM):
self.function_calling_llm = create_llm(self.function_calling_llm)

View File

@@ -27,6 +27,91 @@ from crewai.utilities import Logger
from crewai.utilities.rpm_controller import RPMController
from crewai.utilities.task_output_storage_handler import TaskOutputStorageHandler
def test_agentops_initialization_with_api_key(monkeypatch):
"""Test that agentops is properly initialized when API key is present."""
import agentops
# Mock agentops.init
mock_init = MagicMock()
monkeypatch.setattr(agentops, "init", mock_init)
# Set API key
monkeypatch.setenv("AGENTOPS_API_KEY", "test-key")
# Create crew
task = Task(
description="Test task",
expected_output="Test output",
agent=researcher,
)
crew = Crew(agents=[researcher], tasks=[task])
# Verify agentops was initialized
mock_init.assert_called_once_with("test-key")
def test_agentops_initialization_without_api_key(monkeypatch):
"""Test that agentops is not initialized when API key is not present."""
import agentops
# Mock agentops.init
mock_init = MagicMock()
monkeypatch.setattr(agentops, "init", mock_init)
# Create crew without setting API key
task = Task(
description="Test task",
expected_output="Test output",
agent=researcher,
)
crew = Crew(agents=[researcher], tasks=[task])
# Verify agentops.init was not called
mock_init.assert_not_called()
def test_gemini_llm_with_agentops(monkeypatch):
"""Test that Gemini LLM works correctly with agentops."""
from crewai.llm import LLM
import agentops
# Mock agentops
mock_agentops = MagicMock()
monkeypatch.setattr("crewai.crew.agentops", mock_agentops)
# Set API keys
monkeypatch.setenv("AGENTOPS_API_KEY", "test-key")
monkeypatch.setenv("GOOGLE_API_KEY", "test-key")
# Create crew with Gemini LLM
llm = LLM(model="gemini-pro")
agent = Agent(
role="test",
goal="test",
backstory="test",
llm=llm
)
task = Task(
description="test task",
expected_output="test output",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task])
# Mock the agent execution to avoid actual API calls
with patch.object(Task, 'execute_sync', return_value=TaskOutput(
description="test",
raw="test output",
agent=agent.role
)):
# Run crew
crew.kickoff()
# Verify agentops.end_session was called correctly
mock_agentops.end_session.assert_called_once_with(
end_state="Success",
end_state_reason="Finished Execution",
is_auto_end=True
)
ceo = Agent(
role="CEO",
goal="Make sure the writers in your company produce amazing content.",