mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-07 15:18:29 +00:00
- Fix lint errors (E712) for == True/False comparisons - Update test_selective_execution_with_invalid_tags to match actual Pydantic error format - Pydantic's built-in validation runs before custom validators, producing different error messages Co-Authored-By: João <joao@crewai.com>
74 lines
3.8 KiB
Python
74 lines
3.8 KiB
Python
"""Example demonstrating selective execution for issue #2941."""
|
|
|
|
from crewai import Agent, Crew, Task, Process
|
|
|
|
|
|
def test_issue_2941_example():
|
|
"""Reproduce and test the exact scenario from issue #2941."""
|
|
|
|
holiday_agent = Agent(role="Holiday Researcher", goal="Research holidays", backstory="Expert in holidays")
|
|
macro_agent = Agent(role="Macro Analyst", goal="Analyze macro data", backstory="Expert in macroeconomics")
|
|
news_agent = Agent(role="News Summarizer", goal="Summarize news", backstory="Expert in news analysis")
|
|
forecast_agent = Agent(role="Forecaster", goal="Create forecasts", backstory="Expert in forecasting")
|
|
query_agent = Agent(role="Query Handler", goal="Handle user queries", backstory="Expert in query processing")
|
|
|
|
holiday_task = Task(description="Research holiday information", expected_output="Holiday data", agent=holiday_agent, tags=["holiday"])
|
|
macro_task = Task(description="Extract macroeconomic data", expected_output="Macro data", agent=macro_agent, tags=["macro"])
|
|
news_task = Task(description="Summarize relevant news", expected_output="News summary", agent=news_agent, tags=["news"])
|
|
forecast_task = Task(description="Generate forecast", expected_output="Forecast result", agent=forecast_agent, tags=["forecast"])
|
|
query_task = Task(description="Handle user query", expected_output="Query response", agent=query_agent, tags=["query"])
|
|
|
|
crew = Crew(
|
|
agents=[holiday_agent, macro_agent, news_agent, forecast_agent, query_agent],
|
|
tasks=[holiday_task, macro_task, news_task, forecast_task, query_task],
|
|
process=Process.selective,
|
|
task_selector=Crew.create_tag_selector()
|
|
)
|
|
|
|
inputs = {
|
|
'data_file': 'sample.csv',
|
|
'action': 'forecast',
|
|
'country_code': 'US',
|
|
'topic': 'Egg_prices',
|
|
'query': "Provide forecasted result on the input data"
|
|
}
|
|
|
|
selector = crew.task_selector
|
|
assert selector(inputs, forecast_task) is True
|
|
assert selector(inputs, holiday_task) is False
|
|
assert selector(inputs, macro_task) is False
|
|
assert selector(inputs, news_task) is False
|
|
assert selector(inputs, query_task) is False
|
|
|
|
|
|
def test_multiple_actions_example():
|
|
"""Test crew that can handle multiple different actions."""
|
|
|
|
researcher = Agent(role="Researcher", goal="Research topics", backstory="Expert researcher")
|
|
analyst = Agent(role="Analyst", goal="Analyze data", backstory="Expert analyst")
|
|
writer = Agent(role="Writer", goal="Write reports", backstory="Expert writer")
|
|
|
|
research_task = Task(description="Research the topic", expected_output="Research findings", agent=researcher, tags=["research", "data_gathering"])
|
|
analysis_task = Task(description="Analyze the data", expected_output="Analysis results", agent=analyst, tags=["analysis", "forecast"])
|
|
writing_task = Task(description="Write the report", expected_output="Final report", agent=writer, tags=["writing", "summary"])
|
|
|
|
crew = Crew(
|
|
agents=[researcher, analyst, writer],
|
|
tasks=[research_task, analysis_task, writing_task],
|
|
task_selector=Crew.create_tag_selector()
|
|
)
|
|
|
|
selector = crew.task_selector
|
|
|
|
assert selector({"action": "research"}, research_task) is True
|
|
assert selector({"action": "research"}, analysis_task) is False
|
|
assert selector({"action": "research"}, writing_task) is False
|
|
|
|
assert selector({"action": "analysis"}, research_task) is False
|
|
assert selector({"action": "analysis"}, analysis_task) is True
|
|
assert selector({"action": "analysis"}, writing_task) is False
|
|
|
|
assert selector({"action": "writing"}, research_task) is False
|
|
assert selector({"action": "writing"}, analysis_task) is False
|
|
assert selector({"action": "writing"}, writing_task) is True
|