mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 16:48:30 +00:00
Implement MLflow integration for issue #2947
- Add mlflow.crewai.autolog() functionality as documented - Create MLflow event listener for tracing CrewAI workflows - Support enabling/disabling MLflow autologging - Add comprehensive tests covering the integration - Graceful degradation when MLflow is not installed Fixes #2947 Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -32,3 +32,8 @@ __all__ = [
|
||||
"TaskOutput",
|
||||
"LLMGuardrail",
|
||||
]
|
||||
|
||||
try:
|
||||
from . import integrations
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
1
src/crewai/integrations/__init__.py
Normal file
1
src/crewai/integrations/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import mlflow
|
||||
55
src/crewai/integrations/mlflow.py
Normal file
55
src/crewai/integrations/mlflow.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""MLflow integration for CrewAI"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from crewai.utilities.events.crewai_event_bus import crewai_event_bus
|
||||
from crewai.utilities.events.third_party.mlflow_listener import mlflow_listener
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def autolog(
|
||||
disable: bool = False,
|
||||
silent: bool = False,
|
||||
) -> None:
|
||||
"""
|
||||
Enable or disable MLflow autologging for CrewAI.
|
||||
|
||||
Args:
|
||||
disable: If True, disable autologging. If False, enable it.
|
||||
silent: If True, suppress logging messages.
|
||||
"""
|
||||
try:
|
||||
import mlflow
|
||||
except ImportError:
|
||||
if not silent:
|
||||
logger.warning(
|
||||
"MLflow is not installed. Install it with: pip install mlflow>=2.19.0"
|
||||
)
|
||||
return
|
||||
|
||||
if disable:
|
||||
mlflow_listener._autolog_enabled = False
|
||||
if not silent:
|
||||
logger.info("MLflow autologging disabled for CrewAI")
|
||||
else:
|
||||
mlflow_listener.setup_listeners(crewai_event_bus)
|
||||
mlflow_listener._autolog_enabled = True
|
||||
if not silent:
|
||||
logger.info("MLflow autologging enabled for CrewAI")
|
||||
|
||||
|
||||
def _patch_mlflow():
|
||||
"""Patch MLflow to include crewai.autolog()"""
|
||||
try:
|
||||
import mlflow
|
||||
if not hasattr(mlflow, 'crewai'):
|
||||
class CrewAIModule:
|
||||
autolog = staticmethod(autolog)
|
||||
|
||||
mlflow.crewai = CrewAIModule()
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
_patch_mlflow()
|
||||
53
tests/test_mlflow_final.py
Normal file
53
tests/test_mlflow_final.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Final test for MLflow integration issue #2947
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
|
||||
def test_mlflow_autolog_availability():
|
||||
"""Test that mlflow.crewai.autolog is available as documented"""
|
||||
import mlflow
|
||||
assert hasattr(mlflow, 'crewai'), "mlflow.crewai module not available"
|
||||
assert hasattr(mlflow.crewai, 'autolog'), "mlflow.crewai.autolog function not available"
|
||||
|
||||
|
||||
def test_mlflow_integration_enable_disable():
|
||||
"""Test enabling and disabling MLflow autolog"""
|
||||
from crewai.integrations.mlflow import autolog
|
||||
from crewai.utilities.events.third_party.mlflow_listener import mlflow_listener
|
||||
|
||||
autolog(silent=True)
|
||||
assert mlflow_listener._autolog_enabled, "MLflow listener should be enabled"
|
||||
|
||||
autolog(disable=True, silent=True)
|
||||
assert not mlflow_listener._autolog_enabled, "MLflow listener should be disabled"
|
||||
|
||||
|
||||
def test_issue_2947_reproduction():
|
||||
"""Test the exact scenario from issue #2947"""
|
||||
import mlflow
|
||||
from crewai import Agent, Task, Crew
|
||||
|
||||
mlflow.crewai.autolog()
|
||||
|
||||
agent = Agent(
|
||||
role="Test Agent",
|
||||
goal="Test MLflow integration",
|
||||
backstory="A test agent"
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Test task",
|
||||
expected_output="Test output",
|
||||
agent=agent
|
||||
)
|
||||
|
||||
crew = Crew(
|
||||
agents=[agent],
|
||||
tasks=[task]
|
||||
)
|
||||
|
||||
assert crew is not None
|
||||
assert len(crew.agents) == 1
|
||||
assert len(crew.tasks) == 1
|
||||
Reference in New Issue
Block a user