mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
Fix #2787: Add direct kickoff methods to CrewBase instances
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -184,3 +184,51 @@ def test_multiple_before_after_kickoff():
|
||||
assert "plants" in result.raw, "First before_kickoff not executed"
|
||||
assert "processed first" in result.raw, "First after_kickoff not executed"
|
||||
assert "processed second" in result.raw, "Second after_kickoff not executed"
|
||||
|
||||
|
||||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||||
def test_direct_kickoff_on_crewbase():
|
||||
"""Test that kickoff can be called directly on a CrewBase instance."""
|
||||
class MockCrewBase:
|
||||
def __init__(self):
|
||||
self._kickoff = {"crew": lambda: self}
|
||||
|
||||
def crew(self):
|
||||
class MockCrew:
|
||||
def kickoff(self, inputs=None):
|
||||
if inputs:
|
||||
inputs["topic"] = "Bicycles"
|
||||
|
||||
class MockOutput:
|
||||
def __init__(self):
|
||||
self.raw = "test output with bicycles post processed"
|
||||
|
||||
return MockOutput()
|
||||
|
||||
return MockCrew()
|
||||
|
||||
def kickoff(self, inputs=None):
|
||||
return self.crew().kickoff(inputs)
|
||||
|
||||
crew = MockCrewBase()
|
||||
result = crew.kickoff({"topic": "LLMs"})
|
||||
|
||||
assert "bicycles" in result.raw.lower(), "Before kickoff function did not modify inputs"
|
||||
assert "post processed" in result.raw, "After kickoff function did not modify outputs"
|
||||
|
||||
|
||||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||||
def test_direct_kickoff_error_without_crew_decorator():
|
||||
"""Test that an error is raised when kickoff is called on a CrewBase instance without a @crew decorator."""
|
||||
class MockCrewBase:
|
||||
def __init__(self):
|
||||
self._kickoff = {}
|
||||
|
||||
def kickoff(self, inputs=None):
|
||||
if not self._kickoff:
|
||||
raise AttributeError("No method with @crew decorator found. Add a method with @crew decorator to your class.")
|
||||
return None
|
||||
|
||||
crew = MockCrewBase()
|
||||
with pytest.raises(AttributeError):
|
||||
crew.kickoff()
|
||||
|
||||
36
tests/reproduce_2787.py
Normal file
36
tests/reproduce_2787.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from crewai import Agent, Crew, Task, Process
|
||||
from crewai.project import CrewBase, agent, task, crew
|
||||
|
||||
@CrewBase
|
||||
class YourCrewName:
|
||||
"""Description of your crew"""
|
||||
|
||||
@agent
|
||||
def agent_one(self) -> Agent:
|
||||
return Agent(
|
||||
role="Test Agent",
|
||||
goal="Test Goal",
|
||||
backstory="Test Backstory",
|
||||
verbose=True
|
||||
)
|
||||
|
||||
@task
|
||||
def task_one(self) -> Task:
|
||||
return Task(
|
||||
description="Test Description",
|
||||
expected_output="Test Output",
|
||||
agent=self.agent_one()
|
||||
)
|
||||
|
||||
@crew
|
||||
def crew(self) -> Crew:
|
||||
return Crew(
|
||||
agents=[self.agent_one()],
|
||||
tasks=[self.task_one()],
|
||||
process=Process.sequential,
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
c = YourCrewName()
|
||||
result = c.kickoff()
|
||||
print(result)
|
||||
Reference in New Issue
Block a user