Refactor event system and improve crew testing

- Extract base CrewEvent class to a new base_events.py module
- Update event imports across multiple event-related files
- Modify CrewTestStartedEvent to use eval_llm instead of openai_model_name
- Add LLM creation validation in crew testing method
- Improve type handling and event consistency
This commit is contained in:
Lorenze Jay
2025-02-18 14:29:39 -08:00
parent 6fea26d223
commit c64c0698c5
8 changed files with 24 additions and 16 deletions

View File

@@ -1201,6 +1201,10 @@ class Crew(BaseModel):
) -> None:
"""Test and evaluate the Crew with the given inputs for n iterations concurrently using concurrent.futures."""
try:
eval_llm = create_llm(eval_llm)
if not eval_llm:
raise ValueError("Failed to create LLM instance.")
crewai_event_bus.emit(
self,
CrewTestStartedEvent(
@@ -1211,7 +1215,7 @@ class Crew(BaseModel):
),
)
test_crew = self.copy()
evaluator = CrewEvaluator(test_crew, eval_llm)
evaluator = CrewEvaluator(test_crew, eval_llm) # type: ignore[arg-type]
for i in range(1, n_iterations + 1):
evaluator.set_iteration(i)

View File

@@ -4,7 +4,7 @@ from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.tools.base_tool import BaseTool
from crewai.tools.structured_tool import CrewStructuredTool
from .crew_events import CrewEvent
from .base_events import CrewEvent
if TYPE_CHECKING:
from crewai.agents.agent_builder.base_agent import BaseAgent

View File

@@ -0,0 +1,10 @@
from datetime import datetime
from pydantic import BaseModel, Field
class CrewEvent(BaseModel):
"""Base class for all crew events"""
timestamp: datetime = Field(default_factory=datetime.now)
type: str

View File

@@ -1,14 +1,8 @@
from datetime import datetime
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Union
from pydantic import BaseModel, Field
from pydantic import InstanceOf
class CrewEvent(BaseModel):
"""Base class for all crew events"""
timestamp: datetime = Field(default_factory=datetime.now)
type: str
from crewai.utilities.events.base_events import CrewEvent
class CrewKickoffStartedEvent(CrewEvent):
@@ -67,7 +61,7 @@ class CrewTestStartedEvent(CrewEvent):
crew_name: Optional[str]
n_iterations: int
openai_model_name: Optional[str]
eval_llm: Optional[Union[str, Any]]
inputs: Optional[Dict[str, Any]]
type: str = "crew_test_started"

View File

@@ -4,7 +4,7 @@ from typing import Any, Callable, Dict, List, Type, TypeVar, cast
from blinker import Signal
from crewai.utilities.events.crew_events import CrewEvent
from crewai.utilities.events.base_events import CrewEvent
from crewai.utilities.events.event_types import EventTypes
EventT = TypeVar("EventT", bound=CrewEvent)

View File

@@ -85,7 +85,7 @@ class EventListener(BaseEventListener):
cloned_crew,
event.n_iterations,
event.inputs,
event.openai_model_name,
event.eval_llm,
)
self.logger.log(
f"🚀 Crew '{event.crew_name}' started test",

View File

@@ -2,7 +2,7 @@ from typing import Any, Dict, Optional, Union
from pydantic import BaseModel
from .crew_events import CrewEvent
from .base_events import CrewEvent
class FlowEvent(CrewEvent):

View File

@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Any, Callable, Dict
from .crew_events import CrewEvent
from .base_events import CrewEvent
class ToolUsageEvent(CrewEvent):