diff --git a/src/crewai/agent.py b/src/crewai/agent.py index 08ff2ad702..80bd022db2 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -19,17 +19,24 @@ from crewai.utilities.token_counter_callback import TokenCalcHandler from crewai.utilities.training_handler import CrewTrainingHandler -agentops = None -try: - import agentops # type: ignore # Name "agentops" already defined on line 21 - from agentops import track_agent -except ImportError: - +def mock_agent_ops_provider(): def track_agent(): def noop(f): return f return noop + return track_agent + +agentops = None + +if os.environ.get("AGENTOPS_API_KEY"): + try: + import agentops # type: ignore # Name "agentops" already defined on line 21 + from agentops import track_agent + except ImportError: + track_agent = mock_agent_ops_provider() +else: + track_agent = mock_agent_ops_provider() @track_agent() diff --git a/src/crewai/crew.py b/src/crewai/crew.py index 109cbd28f1..98f7b0ae0d 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -3,6 +3,7 @@ import uuid from concurrent.futures import Future from hashlib import md5 +import os from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union from langchain_core.callbacks import BaseCallbackHandler @@ -47,10 +48,13 @@ from crewai.utilities.task_output_storage_handler import TaskOutputStorageHandler from crewai.utilities.training_handler import CrewTrainingHandler -try: - import agentops -except ImportError: - agentops = None + +agentops = None +if os.environ.get("AGENTOPS_API_KEY"): + try: + import agentops + except ImportError: + pass if TYPE_CHECKING: from crewai.pipeline.pipeline import Pipeline diff --git a/src/crewai/tools/tool_usage.py b/src/crewai/tools/tool_usage.py index f205dd0987..3beb994616 100644 --- a/src/crewai/tools/tool_usage.py +++ b/src/crewai/tools/tool_usage.py @@ -1,5 +1,6 @@ import ast from difflib import SequenceMatcher +import os from textwrap import dedent from typing import Any, List, Union @@ -11,10 +12,12 @@ from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling from crewai.utilities import I18N, Converter, ConverterError, Printer -try: - import agentops -except ImportError: - agentops = None +agentops = None +if os.environ.get("AGENTOPS_API_KEY"): + try: + import agentops + except ImportError: + pass OPENAI_BIGGER_MODELS = ["gpt-4o"] diff --git a/src/crewai/utilities/evaluators/task_evaluator.py b/src/crewai/utilities/evaluators/task_evaluator.py index 46cbef2d82..254bfc5734 100644 --- a/src/crewai/utilities/evaluators/task_evaluator.py +++ b/src/crewai/utilities/evaluators/task_evaluator.py @@ -1,3 +1,4 @@ +import os from typing import List from langchain_openai import ChatOpenAI @@ -6,16 +7,23 @@ from crewai.utilities import Converter from crewai.utilities.pydantic_schema_parser import PydanticSchemaParser -agentops = None -try: - from agentops import track_agent -except ImportError: - - def track_agent(name): +def mock_agent_ops_provider(): + def track_agent(): def noop(f): return f return noop + return track_agent + +agentops = None + +if os.environ.get("AGENTOPS_API_KEY"): + try: + from agentops import track_agent + except ImportError: + track_agent = mock_agent_ops_provider() +else: + track_agent = mock_agent_ops_provider() class Entity(BaseModel):