mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 16:18:30 +00:00
2576 lines
97 KiB
Python
2576 lines
97 KiB
Python
"""Test Agent creation and execution basic functionality."""
|
||
|
||
import hashlib
|
||
import json
|
||
from concurrent.futures import Future
|
||
from unittest import mock
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
import pydantic_core
|
||
import pytest
|
||
from crewai.agent import Agent
|
||
from crewai.agents.cache import CacheHandler
|
||
from crewai.crew import Crew
|
||
from crewai.crews.crew_output import CrewOutput
|
||
from crewai.memory.contextual.contextual_memory import ContextualMemory
|
||
from crewai.process import Process
|
||
from crewai.task import Task
|
||
from crewai.tasks.conditional_task import ConditionalTask
|
||
from crewai.tasks.output_format import OutputFormat
|
||
from crewai.tasks.task_output import TaskOutput
|
||
from crewai.types.usage_metrics import UsageMetrics
|
||
from crewai.utilities import Logger
|
||
from crewai.utilities.rpm_controller import RPMController
|
||
from crewai.utilities.task_output_storage_handler import TaskOutputStorageHandler
|
||
|
||
ceo = Agent(
|
||
role="CEO",
|
||
goal="Make sure the writers in your company produce amazing content.",
|
||
backstory="You're an long time CEO of a content creation agency with a Senior Writer on the team. You're now working on a new project and want to make sure the content produced is amazing.",
|
||
allow_delegation=True,
|
||
)
|
||
|
||
researcher = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
writer = Agent(
|
||
role="Senior Writer",
|
||
goal="Write the best content about AI and AI agents.",
|
||
backstory="You're a senior writer, specialized in technology, software engineering, AI and startups. You work as a freelancer and are now working on writing content for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
|
||
def test_crew_config_conditional_requirement():
|
||
with pytest.raises(ValueError):
|
||
Crew(process=Process.sequential)
|
||
|
||
config = json.dumps(
|
||
{
|
||
"agents": [
|
||
{
|
||
"role": "Senior Researcher",
|
||
"goal": "Make the best research and analysis on content about AI and AI agents",
|
||
"backstory": "You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
},
|
||
{
|
||
"role": "Senior Writer",
|
||
"goal": "Write the best content about AI and AI agents.",
|
||
"backstory": "You're a senior writer, specialized in technology, software engineering, AI and startups. You work as a freelancer and are now working on writing content for a new customer.",
|
||
},
|
||
],
|
||
"tasks": [
|
||
{
|
||
"description": "Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
"expected_output": "Bullet point list of 5 important events.",
|
||
"agent": "Senior Researcher",
|
||
},
|
||
{
|
||
"description": "Write a 1 amazing paragraph highlight for each idea that showcases how good an article about this topic could be, check references if necessary or search for more content but make sure it's unique, interesting and well written. Return the list of ideas with their paragraph and your notes.",
|
||
"expected_output": "A 4 paragraph article about AI.",
|
||
"agent": "Senior Writer",
|
||
},
|
||
],
|
||
}
|
||
)
|
||
parsed_config = json.loads(config)
|
||
|
||
try:
|
||
crew = Crew(process=Process.sequential, config=config)
|
||
except ValueError:
|
||
pytest.fail("Unexpected ValidationError raised")
|
||
|
||
assert [agent.role for agent in crew.agents] == [
|
||
agent["role"] for agent in parsed_config["agents"]
|
||
]
|
||
assert [task.description for task in crew.tasks] == [
|
||
task["description"] for task in parsed_config["tasks"]
|
||
]
|
||
|
||
|
||
def test_async_task_cannot_include_sequential_async_tasks_in_context():
|
||
task1 = Task(
|
||
description="Task 1",
|
||
async_execution=True,
|
||
expected_output="output",
|
||
agent=researcher,
|
||
)
|
||
task2 = Task(
|
||
description="Task 2",
|
||
async_execution=True,
|
||
expected_output="output",
|
||
agent=researcher,
|
||
context=[task1],
|
||
)
|
||
task3 = Task(
|
||
description="Task 3",
|
||
async_execution=True,
|
||
expected_output="output",
|
||
agent=researcher,
|
||
context=[task2],
|
||
)
|
||
task4 = Task(
|
||
description="Task 4",
|
||
expected_output="output",
|
||
agent=writer,
|
||
)
|
||
task5 = Task(
|
||
description="Task 5",
|
||
async_execution=True,
|
||
expected_output="output",
|
||
agent=researcher,
|
||
context=[task4],
|
||
)
|
||
|
||
# This should raise an error because task2 is async and has task1 in its context without a sync task in between
|
||
with pytest.raises(
|
||
ValueError,
|
||
match="Task 'Task 2' is asynchronous and cannot include other sequential asynchronous tasks in its context.",
|
||
):
|
||
Crew(tasks=[task1, task2, task3, task4, task5], agents=[researcher, writer])
|
||
|
||
# This should not raise an error because task5 has a sync task (task4) in its context
|
||
try:
|
||
Crew(tasks=[task1, task4, task5], agents=[researcher, writer])
|
||
except ValueError:
|
||
pytest.fail("Unexpected ValidationError raised")
|
||
|
||
|
||
def test_context_no_future_tasks():
|
||
task2 = Task(
|
||
description="Task 2",
|
||
expected_output="output",
|
||
agent=researcher,
|
||
)
|
||
task3 = Task(
|
||
description="Task 3",
|
||
expected_output="output",
|
||
agent=researcher,
|
||
context=[task2],
|
||
)
|
||
task4 = Task(
|
||
description="Task 4",
|
||
expected_output="output",
|
||
agent=researcher,
|
||
)
|
||
task1 = Task(
|
||
description="Task 1",
|
||
expected_output="output",
|
||
agent=researcher,
|
||
context=[task4],
|
||
)
|
||
|
||
# This should raise an error because task1 has a context dependency on a future task (task4)
|
||
with pytest.raises(
|
||
ValueError,
|
||
match="Task 'Task 1' has a context dependency on a future task 'Task 4', which is not allowed.",
|
||
):
|
||
Crew(tasks=[task1, task2, task3, task4], agents=[researcher, writer])
|
||
|
||
|
||
def test_crew_config_with_wrong_keys():
|
||
no_tasks_config = json.dumps(
|
||
{
|
||
"agents": [
|
||
{
|
||
"role": "Senior Researcher",
|
||
"goal": "Make the best research and analysis on content about AI and AI agents",
|
||
"backstory": "You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
}
|
||
]
|
||
}
|
||
)
|
||
|
||
no_agents_config = json.dumps(
|
||
{
|
||
"tasks": [
|
||
{
|
||
"description": "Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
"agent": "Senior Researcher",
|
||
}
|
||
]
|
||
}
|
||
)
|
||
with pytest.raises(ValueError):
|
||
Crew(process=Process.sequential, config='{"wrong_key": "wrong_value"}')
|
||
with pytest.raises(ValueError):
|
||
Crew(process=Process.sequential, config=no_tasks_config)
|
||
with pytest.raises(ValueError):
|
||
Crew(process=Process.sequential, config=no_agents_config)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_creation():
|
||
tasks = [
|
||
Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
),
|
||
Task(
|
||
description="Write a 1 amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
),
|
||
]
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.sequential,
|
||
tasks=tasks,
|
||
)
|
||
|
||
result = crew.kickoff()
|
||
|
||
expected_string_output = "### Exploring Artificial Intelligence: The Game Changer Across Fields\n\nArtificial Intelligence (AI) holds immense promise in tackling one of humanity’s gravest challenges: climate change. By leveraging sophisticated machine learning algorithms, AI can predict climate patterns with greater accuracy, aiding in the timely and efficient mitigation of extreme weather events. Moreover, AI can optimize renewable energy production by fine-tuning wind turbines and solar panels for maximum efficiency. These technologies not only contribute to sustainable energy practices but also significantly reduce carbon footprints. Case studies like Google's project on improving energy efficiency through DeepMind's AI are stellar examples showcasing AI's potential in environmental sustainability.\n\nThe ethical dimensions of AI, particularly concerning bias and fairness, are becoming increasingly significant. The dialogue surrounding AI ethics addresses how biased data inputs can lead to discriminatory outcomes in various sectors, including law enforcement and hiring practices. For instance, there have been cases where facial recognition software has shown prejudiced behavior, leading to wrongful identifications and arrests predominantly affecting racial minorities. By discussing the importance of developing unbiased AI systems and the recent regulatory measures aimed at mitigating these issues, this topic highlights the societal implications of AI's expansive reach.\n\nIn the realm of healthcare, AI is revolutionizing personalized medicine by tailoring treatments and predicting patient outcomes with unparalleled precision. Through the analysis of genomic data, AI can suggest individualized treatment plans that are more effective than generalized approaches. This not only enhances patient care but also streamlines healthcare systems by focusing resources where they are needed most. Notable case studies include IBM Watson's application in oncology, which helps in formulating personalized treatment protocols based on the latest medical research and patient history.\n\nOne of the visible impacts of AI on everyday life is its transformation of customer service through AI agents and chatbots. While early iterations were limited to basic query handling, modern-day AI agents utilize advanced Natural Language Processing (NLP) techniques to understand and respond to complex customer queries efficiently. This advancement has led to significant improvements in customer satisfaction and reduced operational costs. Emerging trends such as emotion-detection AI agents suggest a future where customer service will be even more personalized and intuitive, highlighting how AI is becoming an indispensable tool in business operations.\n\nBy delving into these compelling areas, an article on AI can cover a range of topics from climate change to ethics, healthcare, and customer service. Each section promises a deep dive into how AI is not just a technological advancement but a crucial player in shaping a sustainable, fair, and efficient future."
|
||
|
||
assert str(result) == expected_string_output
|
||
assert result.raw == expected_string_output
|
||
assert isinstance(result, CrewOutput)
|
||
assert len(result.tasks_output) == len(tasks)
|
||
assert result.raw == expected_string_output
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_sync_task_execution():
|
||
from unittest.mock import patch
|
||
|
||
tasks = [
|
||
Task(
|
||
description="Give me a list of 5 interesting ideas to explore for an article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
),
|
||
Task(
|
||
description="Write an amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
),
|
||
]
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.sequential,
|
||
tasks=tasks,
|
||
)
|
||
|
||
mock_task_output = TaskOutput(
|
||
description="Mock description", raw="mocked output", agent="mocked agent"
|
||
)
|
||
|
||
# Because we are mocking execute_sync, we never hit the underlying _execute_core
|
||
# which sets the output attribute of the task
|
||
for task in tasks:
|
||
task.output = mock_task_output
|
||
|
||
with patch.object(
|
||
Task, "execute_sync", return_value=mock_task_output
|
||
) as mock_execute_sync:
|
||
crew.kickoff()
|
||
|
||
# Assert that execute_sync was called for each task
|
||
assert mock_execute_sync.call_count == len(tasks)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_process():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
tasks=[task],
|
||
)
|
||
|
||
result = crew.kickoff()
|
||
|
||
assert (
|
||
result.raw
|
||
== '1. **"The Rise of Autonomous AI Agents: Transforming Modern Workflows"**\n Autonomous AI agents are rapidly revolutionizing modern workflows across diverse industries. By automating repetitive tasks, optimizing resource allocation, and minimizing human error, these intelligent systems are paving the way for unprecedented efficiency and productivity. Case studies from leading enterprises demonstrate the transformative impact of incorporating AI agents, showcasing examples where routine administrative tasks are seamlessly handled by AI, freeing human employees to focus on strategic and creative endeavors. This article will delve into these success stories, explore the cutting-edge technologies enabling these advancements, and forecast future implications—an essential read for anyone interested in the frontier of automation.\n\n2. **"Ethics in AI: Balancing Innovation and Responsibility"**\n As artificial intelligence continues to evolve, the ethical landscape surrounding its development and deployment demands rigorous examination. This article will tackle critical issues such as algorithmic bias, privacy concerns, and the accountability of AI-driven decisions. By interviewing leading ethicists and AI developers, the piece aims to provide a multidimensional perspective on how society can harness the benefits of AI while mitigating potential harms. Readers will gain insights into the frameworks for responsible AI innovation, the importance of transparency, and the role of regulatory policies in ensuring ethical compliance in AI applications—a comprehensive guide for stakeholders committed to fostering ethical AI ecosystems.\n\n3. **"AI in Mental Health: Opportunities and Challenges"**\n Artificial intelligence is making significant inroads in the field of mental health, offering innovative solutions such as virtual therapy sessions, AI-driven diagnostics, and personalized mental health recommendations. This article will explore the profound opportunities AI presents in enhancing mental health care accessibility and efficacy, especially in underserved areas. It will also address the formidable challenges, including the reliability of AI assessments, ethical concerns about data privacy, and the importance of human oversight. By presenting a balanced view, complete with expert opinions and case studies, this piece aims to illuminate the potentials and pitfalls of integrating AI into mental health services.\n\n4. **"From Silicon Valley to Global Hubs: How AI Startups are Shaping International Innovation"**\n While Silicon Valley remains a beacon of technological innovation, AI startups are emerging as transformative forces in global hubs from Shenzhen to Tel Aviv and beyond. This article will highlight the dynamic ecosystems fostering AI advancements outside traditional tech epicenters, showcasing unique innovations and the local factors driving their success. Readers will discover compelling narratives of entrepreneurial ventures, pioneering research, and supportive public policies that contribute to these thriving AI landscapes. By painting a global picture of AI innovation, this piece will inspire investors, technologists, and policymakers to recognize and support the burgeoning potential worldwide.\n\n5. **"The Future of Human-AI Collaboration: Co-working with Intelligent Systems"**\n Advancements in AI are redefining the boundaries of human-AI collaboration, promising to transform professional environments in remarkable ways. This article will delve into current applications where intelligent systems enhance human capabilities, from decision-making processes to creative problem-solving. It will explore the tangible benefits of such collaborations, including increased efficiency and innovation. Additionally, the piece will address essential considerations like workforce training, skill development, and the psychological impact of working alongside AI. By providing a forward-looking analysis, this article aims to equip readers with a comprehensive understanding of how to navigate and thrive in the evolving landscape of human-AI synergy.'
|
||
)
|
||
|
||
|
||
def test_manager_llm_requirement_for_hierarchical_process():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
|
||
Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
tasks=[task],
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_manager_agent_delegating_to_assigned_task_agent():
|
||
"""
|
||
Test that the manager agent delegates to the assigned task agent.
|
||
"""
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
tasks=[task],
|
||
)
|
||
|
||
crew.kickoff()
|
||
|
||
# Check if the manager agent has the correct tools
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.tools is not None
|
||
|
||
assert len(crew.manager_agent.tools) == 2
|
||
assert (
|
||
"Delegate a specific task to one of the following coworkers: Researcher\n"
|
||
in crew.manager_agent.tools[0].description
|
||
)
|
||
assert (
|
||
"Ask a specific question to one of the following coworkers: Researcher\n"
|
||
in crew.manager_agent.tools[1].description
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_manager_agent_delegating_to_all_agents():
|
||
"""
|
||
Test that the manager agent delegates to all agents when none are specified.
|
||
"""
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
tasks=[task],
|
||
)
|
||
|
||
crew.kickoff()
|
||
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.tools is not None
|
||
|
||
assert len(crew.manager_agent.tools) == 2
|
||
assert (
|
||
"Delegate a specific task to one of the following coworkers: Researcher, Senior Writer\n"
|
||
in crew.manager_agent.tools[0].description
|
||
)
|
||
assert (
|
||
"Ask a specific question to one of the following coworkers: Researcher, Senior Writer\n"
|
||
in crew.manager_agent.tools[1].description
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_with_delegating_agents():
|
||
tasks = [
|
||
Task(
|
||
description="Produce and amazing 1 paragraph draft of an article about AI Agents.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=ceo,
|
||
)
|
||
]
|
||
|
||
crew = Crew(
|
||
agents=[ceo, writer],
|
||
process=Process.sequential,
|
||
tasks=tasks,
|
||
)
|
||
|
||
result = crew.kickoff()
|
||
|
||
assert (
|
||
result.raw
|
||
== "Artificial Intelligence (AI) Agents are autonomous systems capable of performing tasks that typically require human intelligence. These tasks range from simple automation to complex decision-making processes. AI Agents leverage various technologies, such as machine learning, natural language processing, and computer vision, to analyze data, learn from it, and make informed decisions. Their significance lies in their ability to operate independently, thus transforming industries by increasing efficiency, reducing error rates, and enabling new capabilities previously thought to be the realm of science fiction.\n\nAI Agents are already making significant impacts in a variety of real-world applications. In healthcare, for instance, AI Agents assist doctors by analyzing patient data to predict disease outbreaks, personalize treatment plans, and even perform robotic surgeries with precision. In the financial sector, AI Agents are employed to detect fraudulent transactions, manage trading strategies, and offer personalized financial advice. Their ability to process vast amounts of data in real-time means they can identify patterns and insights that humans might overlook, thereby enhancing operational efficiency and security.\n\nLooking ahead, the potential of AI Agents extends to numerous other sectors. In education, AI Agents can provide personalized learning experiences, adapting content to fit the individual needs of each student and offering real-time feedback. Smart cities stand to benefit from AI Agents through improved traffic management, energy efficiency, and enhanced public safety services. Furthermore, the development of autonomous vehicles is heavily reliant on advanced AI Agents to navigate complex environments, making transportation safer and more reliable. The versatility and adaptability of AI Agents make them crucial for future advancements across various domains.\n\nIn summary, AI Agents are not just a technological trend but a cornerstone for future innovation. Their ability to learn, adapt, and make decisions autonomously plays a pivotal role in various sectors, leading to increased connectivity and efficiency. As technology continues to advance, the impact of AI Agents will only grow, ushering in a new era of smart and interconnected systems. Ultimately, AI Agents represent the building blocks of a more intelligent and connected world, promising to revolutionize how we live, work, and interact with technology."
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_verbose_output(capsys):
|
||
tasks = [
|
||
Task(
|
||
description="Research AI advancements.",
|
||
expected_output="A full report on AI advancements.",
|
||
agent=researcher,
|
||
),
|
||
Task(
|
||
description="Write about AI in healthcare.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
),
|
||
]
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=tasks,
|
||
process=Process.sequential,
|
||
verbose=True,
|
||
)
|
||
|
||
crew.kickoff()
|
||
captured = capsys.readouterr()
|
||
expected_strings = [
|
||
"\x1b[1m\x1b[95m# Agent:\x1b[00m \x1b[1m\x1b[92mResearcher",
|
||
"\x1b[00m\n\x1b[95m## Task:\x1b[00m \x1b[92mResearch AI advancements.",
|
||
"\x1b[1m\x1b[95m# Agent:\x1b[00m \x1b[1m\x1b[92mSenior Writer",
|
||
"\x1b[95m## Task:\x1b[00m \x1b[92mWrite about AI in healthcare.",
|
||
"\n\n\x1b[1m\x1b[95m# Agent:\x1b[00m \x1b[1m\x1b[92mResearcher",
|
||
"\x1b[00m\n\x1b[95m## Final Answer:",
|
||
"\n\n\x1b[1m\x1b[95m# Agent:\x1b[00m \x1b[1m\x1b[92mSenior Writer",
|
||
"\x1b[00m\n\x1b[95m## Final Answer:",
|
||
]
|
||
|
||
for expected_string in expected_strings:
|
||
assert expected_string in captured.out
|
||
|
||
# Now test with verbose set to False
|
||
crew.verbose = False
|
||
crew._logger = Logger(verbose=False)
|
||
crew.kickoff()
|
||
captured = capsys.readouterr()
|
||
assert captured.out == ""
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_cache_hitting_between_agents():
|
||
from unittest.mock import call, patch
|
||
|
||
from crewai_tools import tool
|
||
|
||
@tool
|
||
def multiplier(first_number: int, second_number: int) -> float:
|
||
"""Useful for when you need to multiply two numbers together."""
|
||
return first_number * second_number
|
||
|
||
tasks = [
|
||
Task(
|
||
description="What is 2 tims 6? Return only the number.",
|
||
expected_output="the result of multiplication",
|
||
tools=[multiplier],
|
||
agent=ceo,
|
||
),
|
||
Task(
|
||
description="What is 2 times 6? Return only the number.",
|
||
expected_output="the result of multiplication",
|
||
tools=[multiplier],
|
||
agent=researcher,
|
||
),
|
||
]
|
||
|
||
crew = Crew(
|
||
agents=[ceo, researcher],
|
||
tasks=tasks,
|
||
)
|
||
|
||
with patch.object(CacheHandler, "read") as read:
|
||
read.return_value = "12"
|
||
crew.kickoff()
|
||
assert read.call_count == 2, "read was not called exactly twice"
|
||
# Check if read was called with the expected arguments
|
||
expected_calls = [
|
||
call(tool="multiplier", input={"first_number": 2, "second_number": 6}),
|
||
call(tool="multiplier", input={"first_number": 2, "second_number": 6}),
|
||
]
|
||
read.assert_has_calls(expected_calls, any_order=False)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_api_calls_throttling(capsys):
|
||
from unittest.mock import patch
|
||
from crewai_tools import tool
|
||
|
||
@tool
|
||
def get_final_answer() -> float:
|
||
"""Get the final answer but don't give it yet, just re-use this
|
||
tool non-stop."""
|
||
return 42
|
||
|
||
agent = Agent(
|
||
role="Very helpful assistant",
|
||
goal="Comply with necessary changes",
|
||
backstory="You obey orders",
|
||
max_iter=2,
|
||
allow_delegation=False,
|
||
verbose=True,
|
||
llm="gpt-4o",
|
||
)
|
||
|
||
task = Task(
|
||
description="Don't give a Final Answer unless explicitly told it's time to give the absolute best final answer.",
|
||
expected_output="The final answer.",
|
||
tools=[get_final_answer],
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task], max_rpm=1, verbose=True)
|
||
|
||
with patch.object(RPMController, "_wait_for_next_minute") as moveon:
|
||
moveon.return_value = True
|
||
crew.kickoff()
|
||
captured = capsys.readouterr()
|
||
assert "Max RPM reached, waiting for next minute to start." in captured.out
|
||
moveon.assert_called()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_kickoff_usage_metrics():
|
||
inputs = [
|
||
{"topic": "dog"},
|
||
{"topic": "cat"},
|
||
{"topic": "apple"},
|
||
]
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
results = crew.kickoff_for_each(inputs=inputs)
|
||
|
||
assert len(results) == len(inputs)
|
||
for result in results:
|
||
# Assert that all required keys are in usage_metrics and their values are not None
|
||
assert result.token_usage.total_tokens > 0
|
||
assert result.token_usage.prompt_tokens > 0
|
||
assert result.token_usage.completion_tokens > 0
|
||
assert result.token_usage.successful_requests > 0
|
||
|
||
|
||
def test_agents_rpm_is_never_set_if_crew_max_RPM_is_not_set():
|
||
agent = Agent(
|
||
role="test role",
|
||
goal="test goal",
|
||
backstory="test backstory",
|
||
allow_delegation=False,
|
||
verbose=True,
|
||
)
|
||
|
||
task = Task(
|
||
description="just say hi!",
|
||
expected_output="your greeting",
|
||
agent=agent,
|
||
)
|
||
|
||
Crew(agents=[agent], tasks=[task], verbose=True)
|
||
|
||
assert agent._rpm_controller is None
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_sequential_async_task_execution_completion():
|
||
list_ideas = Task(
|
||
description="Give me a list of 5 interesting ideas to explore for an article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
async_execution=True,
|
||
)
|
||
list_important_history = Task(
|
||
description="Research the history of AI and give me the 5 most important events that shaped the technology.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
)
|
||
write_article = Task(
|
||
description="Write an article about the history of AI and its most important events.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
context=[list_ideas, list_important_history],
|
||
)
|
||
|
||
sequential_crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas, list_important_history, write_article],
|
||
)
|
||
|
||
sequential_result = sequential_crew.kickoff()
|
||
assert sequential_result.raw.startswith(
|
||
"The fascinating history of artificial intelligence (AI) is marked by a series of pivotal events and breakthroughs that have significantly shaped its development."
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_single_task_with_async_execution():
|
||
researcher_agent = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
list_ideas = Task(
|
||
description="Generate a list of 5 interesting ideas to explore for an article, where each bulletpoint is under 15 words.",
|
||
expected_output="Bullet point list of 5 important events. No additional commentary.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher_agent],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas],
|
||
)
|
||
|
||
result = crew.kickoff()
|
||
assert result.raw.startswith("- AI in climate change: strategies and solutions.")
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_three_task_with_async_execution():
|
||
researcher_agent = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
bullet_list = Task(
|
||
description="Generate a list of 5 interesting ideas to explore for an article, where each bulletpoint is under 15 words.",
|
||
expected_output="Bullet point list of 5 important events. No additional commentary.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
numbered_list = Task(
|
||
description="Generate a list of 5 interesting ideas to explore for an article, where each bulletpoint is under 15 words.",
|
||
expected_output="Numbered list of 5 important events. No additional commentary.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
letter_list = Task(
|
||
description="Generate a list of 5 interesting ideas to explore for an article, where each bulletpoint is under 15 words.",
|
||
expected_output="Numbered list using [A), B), C)] list of 5 important events. No additional commentary.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
|
||
# Expected result is that we will get an error
|
||
# because a crew can end only end with one or less
|
||
# async tasks
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError) as error:
|
||
Crew(
|
||
agents=[researcher_agent],
|
||
process=Process.sequential,
|
||
tasks=[bullet_list, numbered_list, letter_list],
|
||
)
|
||
|
||
assert error.value.errors()[0]["type"] == "async_task_count"
|
||
assert (
|
||
"The crew must end with at most one asynchronous task."
|
||
in error.value.errors()[0]["msg"]
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
@pytest.mark.asyncio
|
||
async def test_crew_async_kickoff():
|
||
inputs = [
|
||
{"topic": "dog"},
|
||
{"topic": "cat"},
|
||
{"topic": "apple"},
|
||
]
|
||
|
||
agent = Agent(
|
||
role="mock agent",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
mock_task_output = (
|
||
CrewOutput(
|
||
raw="Test output from Crew 1",
|
||
tasks_output=[],
|
||
token_usage=UsageMetrics(
|
||
total_tokens=100,
|
||
prompt_tokens=10,
|
||
completion_tokens=90,
|
||
successful_requests=1,
|
||
),
|
||
json_dict={"output": "crew1"},
|
||
pydantic=None,
|
||
),
|
||
)
|
||
with patch.object(Crew, "kickoff_async", return_value=mock_task_output):
|
||
results = await crew.kickoff_for_each_async(inputs=inputs)
|
||
|
||
assert len(results) == len(inputs)
|
||
for result in results:
|
||
# Assert that all required keys are in usage_metrics and their values are not None
|
||
assert result[0].token_usage.total_tokens > 0 # type: ignore
|
||
assert result[0].token_usage.prompt_tokens > 0 # type: ignore
|
||
assert result[0].token_usage.completion_tokens > 0 # type: ignore
|
||
assert result[0].token_usage.successful_requests > 0 # type: ignore
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_async_task_execution_call_count():
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
list_ideas = Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
async_execution=True,
|
||
)
|
||
list_important_history = Task(
|
||
description="Research the history of AI and give me the 5 most important events that shaped the technology.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
async_execution=True,
|
||
)
|
||
write_article = Task(
|
||
description="Write an article about the history of AI and its most important events.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas, list_important_history, write_article],
|
||
)
|
||
|
||
# Create a valid TaskOutput instance to mock the return value
|
||
mock_task_output = TaskOutput(
|
||
description="Mock description", raw="mocked output", agent="mocked agent"
|
||
)
|
||
|
||
# Create a MagicMock Future instance
|
||
mock_future = MagicMock(spec=Future)
|
||
mock_future.result.return_value = mock_task_output
|
||
|
||
# Directly set the output attribute for each task
|
||
list_ideas.output = mock_task_output
|
||
list_important_history.output = mock_task_output
|
||
write_article.output = mock_task_output
|
||
|
||
with patch.object(
|
||
Task, "execute_sync", return_value=mock_task_output
|
||
) as mock_execute_sync, patch.object(
|
||
Task, "execute_async", return_value=mock_future
|
||
) as mock_execute_async:
|
||
crew.kickoff()
|
||
|
||
assert mock_execute_async.call_count == 2
|
||
assert mock_execute_sync.call_count == 1
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_kickoff_for_each_single_input():
|
||
"""Tests if kickoff_for_each works with a single input."""
|
||
|
||
inputs = [{"topic": "dog"}]
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
results = crew.kickoff_for_each(inputs=inputs)
|
||
|
||
assert len(results) == 1
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_kickoff_for_each_multiple_inputs():
|
||
"""Tests if kickoff_for_each works with multiple inputs."""
|
||
|
||
inputs = [
|
||
{"topic": "dog"},
|
||
{"topic": "cat"},
|
||
{"topic": "apple"},
|
||
]
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
results = crew.kickoff_for_each(inputs=inputs)
|
||
|
||
assert len(results) == len(inputs)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_kickoff_for_each_empty_input():
|
||
"""Tests if kickoff_for_each handles an empty input list."""
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
results = crew.kickoff_for_each(inputs=[])
|
||
assert results == []
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_kickoff_for_each_invalid_input():
|
||
"""Tests if kickoff_for_each raises TypeError for invalid input types."""
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
with pytest.raises(TypeError):
|
||
# Pass a string instead of a list
|
||
crew.kickoff_for_each("invalid input")
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_kickoff_for_each_error_handling():
|
||
"""Tests error handling in kickoff_for_each when kickoff raises an error."""
|
||
from unittest.mock import patch
|
||
|
||
inputs = [
|
||
{"topic": "dog"},
|
||
{"topic": "cat"},
|
||
{"topic": "apple"},
|
||
]
|
||
expected_outputs = [
|
||
"Dogs are loyal companions and popular pets.",
|
||
"Cats are independent and low-maintenance pets.",
|
||
"Apples are a rich source of dietary fiber and vitamin C.",
|
||
]
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
with patch.object(Crew, "kickoff") as mock_kickoff:
|
||
mock_kickoff.side_effect = expected_outputs[:2] + [
|
||
Exception("Simulated kickoff error")
|
||
]
|
||
with pytest.raises(Exception, match="Simulated kickoff error"):
|
||
crew.kickoff_for_each(inputs=inputs)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
@pytest.mark.asyncio
|
||
async def test_kickoff_async_basic_functionality_and_output():
|
||
"""Tests the basic functionality and output of kickoff_async."""
|
||
from unittest.mock import patch
|
||
|
||
inputs = {"topic": "dog"}
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
# Create the crew
|
||
crew = Crew(
|
||
agents=[agent],
|
||
tasks=[task],
|
||
)
|
||
|
||
expected_output = "This is a sample output from kickoff."
|
||
with patch.object(Crew, "kickoff", return_value=expected_output) as mock_kickoff:
|
||
result = await crew.kickoff_async(inputs)
|
||
|
||
assert isinstance(result, str), "Result should be a string"
|
||
assert result == expected_output, "Result should match expected output"
|
||
mock_kickoff.assert_called_once_with(inputs)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
@pytest.mark.asyncio
|
||
async def test_async_kickoff_for_each_async_basic_functionality_and_output():
|
||
"""Tests the basic functionality and output of kickoff_for_each_async."""
|
||
inputs = [
|
||
{"topic": "dog"},
|
||
{"topic": "cat"},
|
||
{"topic": "apple"},
|
||
]
|
||
|
||
# Define expected outputs for each input
|
||
expected_outputs = [
|
||
"Dogs are loyal companions and popular pets.",
|
||
"Cats are independent and low-maintenance pets.",
|
||
"Apples are a rich source of dietary fiber and vitamin C.",
|
||
]
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
async def mock_kickoff_async(**kwargs):
|
||
input_data = kwargs.get("inputs")
|
||
index = [input_["topic"] for input_ in inputs].index(input_data["topic"])
|
||
return expected_outputs[index]
|
||
|
||
with patch.object(
|
||
Crew, "kickoff_async", side_effect=mock_kickoff_async
|
||
) as mock_kickoff_async:
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
results = await crew.kickoff_for_each_async(inputs)
|
||
|
||
assert len(results) == len(inputs)
|
||
assert results == expected_outputs
|
||
for input_data in inputs:
|
||
mock_kickoff_async.assert_any_call(inputs=input_data)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
@pytest.mark.asyncio
|
||
async def test_async_kickoff_for_each_async_empty_input():
|
||
"""Tests if akickoff_for_each_async handles an empty input list."""
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="1 bullet point about {topic} that's under 15 words.",
|
||
agent=agent,
|
||
)
|
||
|
||
# Create the crew
|
||
crew = Crew(
|
||
agents=[agent],
|
||
tasks=[task],
|
||
)
|
||
|
||
# Call the function we are testing
|
||
results = await crew.kickoff_for_each_async([])
|
||
|
||
# Assertion
|
||
assert results == [], "Result should be an empty list when input is empty"
|
||
|
||
|
||
def test_set_agents_step_callback():
|
||
from unittest.mock import patch
|
||
|
||
researcher_agent = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
list_ideas = Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher_agent],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas],
|
||
step_callback=lambda: None,
|
||
)
|
||
|
||
with patch.object(Agent, "execute_task") as execute:
|
||
execute.return_value = "ok"
|
||
crew.kickoff()
|
||
assert researcher_agent.step_callback is not None
|
||
|
||
|
||
def test_dont_set_agents_step_callback_if_already_set():
|
||
from unittest.mock import patch
|
||
|
||
def agent_callback(_):
|
||
pass
|
||
|
||
def crew_callback(_):
|
||
pass
|
||
|
||
researcher_agent = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
step_callback=agent_callback,
|
||
)
|
||
|
||
list_ideas = Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher_agent],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas],
|
||
step_callback=crew_callback,
|
||
)
|
||
|
||
with patch.object(Agent, "execute_task") as execute:
|
||
execute.return_value = "ok"
|
||
crew.kickoff()
|
||
assert researcher_agent.step_callback is not crew_callback
|
||
assert researcher_agent.step_callback is agent_callback
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_function_calling_llm():
|
||
from unittest.mock import patch, Mock
|
||
from crewai_tools import tool
|
||
import instructor
|
||
|
||
llm = "gpt-4o"
|
||
|
||
@tool
|
||
def learn_about_AI() -> str:
|
||
"""Useful for when you need to learn about AI to write an paragraph about it."""
|
||
return "AI is a very broad field."
|
||
|
||
agent1 = Agent(
|
||
role="test role",
|
||
goal="test goal",
|
||
backstory="test backstory",
|
||
tools=[learn_about_AI],
|
||
llm="gpt-4o-mini",
|
||
function_calling_llm=llm,
|
||
)
|
||
|
||
essay = Task(
|
||
description="Write and then review an small paragraph on AI until it's AMAZING",
|
||
expected_output="The final paragraph.",
|
||
agent=agent1,
|
||
)
|
||
tasks = [essay]
|
||
crew = Crew(agents=[agent1], tasks=tasks)
|
||
|
||
with patch.object(instructor, "from_litellm") as mock_from_litellm:
|
||
mock_client = Mock()
|
||
mock_from_litellm.return_value = mock_client
|
||
mock_chat = Mock()
|
||
mock_client.chat = mock_chat
|
||
mock_completions = Mock()
|
||
mock_chat.completions = mock_completions
|
||
mock_create = Mock()
|
||
mock_completions.create = mock_create
|
||
|
||
crew.kickoff()
|
||
|
||
mock_from_litellm.assert_called()
|
||
mock_create.assert_called()
|
||
calls = mock_create.call_args_list
|
||
assert any(
|
||
call.kwargs.get("model") == "gpt-4o" for call in calls
|
||
), "Instructor was not created with the expected model"
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_task_with_no_arguments():
|
||
from crewai_tools import tool
|
||
|
||
@tool
|
||
def return_data() -> str:
|
||
"Useful to get the sales related data"
|
||
return "January: 5, February: 10, March: 15, April: 20, May: 25"
|
||
|
||
researcher = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
tools=[return_data],
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task = Task(
|
||
description="Look at the available data and give me a sense on the total number of sales.",
|
||
expected_output="The total number of sales as an integer",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(agents=[researcher], tasks=[task])
|
||
|
||
result = crew.kickoff()
|
||
assert result.raw == "The total number of sales is 75."
|
||
|
||
|
||
def test_code_execution_flag_adds_code_tool_upon_kickoff():
|
||
from crewai_tools import CodeInterpreterTool
|
||
|
||
programmer = Agent(
|
||
role="Programmer",
|
||
goal="Write code to solve problems.",
|
||
backstory="You're a programmer who loves to solve problems with code.",
|
||
allow_delegation=False,
|
||
allow_code_execution=True,
|
||
)
|
||
|
||
task = Task(
|
||
description="How much is 2 + 2?",
|
||
expected_output="The result of the sum as an integer.",
|
||
agent=programmer,
|
||
)
|
||
|
||
crew = Crew(agents=[programmer], tasks=[task])
|
||
|
||
with patch.object(Agent, "execute_task") as executor:
|
||
executor.return_value = "ok"
|
||
crew.kickoff()
|
||
assert len(programmer.tools) == 1
|
||
assert programmer.tools[0].__class__ == CodeInterpreterTool
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_delegation_is_not_enabled_if_there_are_only_one_agent():
|
||
researcher = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=True,
|
||
)
|
||
|
||
task = Task(
|
||
description="Look at the available data and give me a sense on the total number of sales.",
|
||
expected_output="The total number of sales as an integer",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(agents=[researcher], tasks=[task])
|
||
|
||
crew.kickoff()
|
||
assert task.tools == []
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_agents_do_not_get_delegation_tools_with_there_is_only_one_agent():
|
||
agent = Agent(
|
||
role="Researcher",
|
||
goal="Be super empathetic.",
|
||
backstory="You're love to sey howdy.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task = Task(description="say howdy", expected_output="Howdy!", agent=agent)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
result = crew.kickoff()
|
||
assert result.raw == "Howdy!"
|
||
assert len(agent.tools) == 0
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_sequential_crew_creation_tasks_without_agents():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
# agent=researcher, # not having an agent on the task should throw an error
|
||
)
|
||
|
||
# Expected Output: The sequential crew should fail to create because the task is missing an agent
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError) as exec_info:
|
||
Crew(
|
||
tasks=[task],
|
||
agents=[researcher],
|
||
process=Process.sequential,
|
||
)
|
||
|
||
assert exec_info.value.errors()[0]["type"] == "missing_agent_in_task"
|
||
assert (
|
||
"Agent is missing in the task with the following description"
|
||
in exec_info.value.errors()[0]["msg"]
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_agent_usage_metrics_are_captured_for_hierarchical_process():
|
||
agent = Agent(
|
||
role="Researcher",
|
||
goal="Be super empathetic.",
|
||
backstory="You're love to sey howdy.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task = Task(description="Ask the researched to say hi!", expected_output="Howdy!")
|
||
|
||
crew = Crew(
|
||
agents=[agent], tasks=[task], process=Process.hierarchical, manager_llm="gpt-4o"
|
||
)
|
||
|
||
result = crew.kickoff()
|
||
assert result.raw == "Howdy!"
|
||
|
||
assert result.token_usage == UsageMetrics(
|
||
total_tokens=2685,
|
||
prompt_tokens=2534,
|
||
completion_tokens=151,
|
||
successful_requests=5,
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_crew_creation_tasks_with_agents():
|
||
"""
|
||
Agents are not required for tasks in a hierarchical process but sometimes they are still added
|
||
This test makes sure that the manager still delegates the task to the agent even if the agent is passed in the task
|
||
"""
|
||
task = Task(
|
||
description="Write one amazing paragraph about AI.",
|
||
expected_output="A single paragraph with 4 sentences.",
|
||
agent=writer,
|
||
)
|
||
|
||
crew = Crew(
|
||
tasks=[task],
|
||
agents=[writer, researcher],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
)
|
||
crew.kickoff()
|
||
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.tools is not None
|
||
assert crew.manager_agent.tools[0].description.startswith(
|
||
"Delegate a specific task to one of the following coworkers: Senior Writer"
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_crew_creation_tasks_with_async_execution():
|
||
"""
|
||
Agents are not required for tasks in a hierarchical process but sometimes they are still added
|
||
This test makes sure that the manager still delegates the task to the agent even if the agent is passed in the task
|
||
"""
|
||
task = Task(
|
||
description="Write one amazing paragraph about AI.",
|
||
expected_output="A single paragraph with 4 sentences.",
|
||
agent=writer,
|
||
async_execution=True,
|
||
)
|
||
|
||
crew = Crew(
|
||
tasks=[task],
|
||
agents=[writer, researcher, ceo],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
)
|
||
|
||
crew.kickoff()
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.tools is not None
|
||
assert crew.manager_agent.tools[0].description.startswith(
|
||
"Delegate a specific task to one of the following coworkers: Senior Writer\n"
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_crew_creation_tasks_with_sync_last():
|
||
"""
|
||
Agents are not required for tasks in a hierarchical process but sometimes they are still added
|
||
This test makes sure that the manager still delegates the task to the agent even if the agent is passed in the task
|
||
"""
|
||
task = Task(
|
||
description="Write one amazing paragraph about AI.",
|
||
expected_output="A single paragraph with 4 sentences.",
|
||
agent=writer,
|
||
async_execution=True,
|
||
)
|
||
task2 = Task(
|
||
description="Write one amazing paragraph about AI.",
|
||
expected_output="A single paragraph with 4 sentences.",
|
||
async_execution=False,
|
||
)
|
||
|
||
crew = Crew(
|
||
tasks=[task, task2],
|
||
agents=[writer, researcher, ceo],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
)
|
||
|
||
crew.kickoff()
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.tools is not None
|
||
assert crew.manager_agent.tools[0].description.startswith(
|
||
"Delegate a specific task to one of the following coworkers: Senior Writer, Researcher, CEO\n"
|
||
)
|
||
|
||
|
||
def test_crew_inputs_interpolate_both_agents_and_tasks():
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="{points} bullet points about {topic}.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
inputs = {"topic": "AI", "points": 5}
|
||
crew._interpolate_inputs(inputs=inputs) # Manual call for now
|
||
|
||
assert crew.tasks[0].description == "Give me an analysis around AI."
|
||
assert crew.tasks[0].expected_output == "5 bullet points about AI."
|
||
assert crew.agents[0].role == "AI Researcher"
|
||
assert crew.agents[0].goal == "Express hot takes on AI."
|
||
assert crew.agents[0].backstory == "You have a lot of experience with AI."
|
||
|
||
|
||
def test_crew_inputs_interpolate_both_agents_and_tasks_diff():
|
||
from unittest.mock import patch
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="{points} bullet points about {topic}.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
with patch.object(Agent, "execute_task") as execute:
|
||
with patch.object(
|
||
Agent, "interpolate_inputs", wraps=agent.interpolate_inputs
|
||
) as interpolate_agent_inputs:
|
||
with patch.object(
|
||
Task, "interpolate_inputs", wraps=task.interpolate_inputs
|
||
) as interpolate_task_inputs:
|
||
execute.return_value = "ok"
|
||
crew.kickoff(inputs={"topic": "AI", "points": 5})
|
||
interpolate_agent_inputs.assert_called()
|
||
interpolate_task_inputs.assert_called()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_does_not_interpolate_without_inputs():
|
||
from unittest.mock import patch
|
||
|
||
agent = Agent(
|
||
role="{topic} Researcher",
|
||
goal="Express hot takes on {topic}.",
|
||
backstory="You have a lot of experience with {topic}.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Give me an analysis around {topic}.",
|
||
expected_output="{points} bullet points about {topic}.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
with patch.object(Agent, "interpolate_inputs") as interpolate_agent_inputs:
|
||
with patch.object(Task, "interpolate_inputs") as interpolate_task_inputs:
|
||
crew.kickoff()
|
||
interpolate_agent_inputs.assert_not_called()
|
||
interpolate_task_inputs.assert_not_called()
|
||
|
||
|
||
# def test_crew_partial_inputs():
|
||
# agent = Agent(
|
||
# role="{topic} Researcher",
|
||
# goal="Express hot takes on {topic}.",
|
||
# backstory="You have a lot of experience with {topic}.",
|
||
# )
|
||
|
||
# task = Task(
|
||
# description="Give me an analysis around {topic}.",
|
||
# expected_output="{points} bullet points about {topic}.",
|
||
# )
|
||
|
||
# crew = Crew(agents=[agent], tasks=[task], inputs={"topic": "AI"})
|
||
# inputs = {"topic": "AI"}
|
||
# crew._interpolate_inputs(inputs=inputs) # Manual call for now
|
||
|
||
# assert crew.tasks[0].description == "Give me an analysis around AI."
|
||
# assert crew.tasks[0].expected_output == "{points} bullet points about AI."
|
||
# assert crew.agents[0].role == "AI Researcher"
|
||
# assert crew.agents[0].goal == "Express hot takes on AI."
|
||
# assert crew.agents[0].backstory == "You have a lot of experience with AI."
|
||
|
||
|
||
# def test_crew_invalid_inputs():
|
||
# agent = Agent(
|
||
# role="{topic} Researcher",
|
||
# goal="Express hot takes on {topic}.",
|
||
# backstory="You have a lot of experience with {topic}.",
|
||
# )
|
||
|
||
# task = Task(
|
||
# description="Give me an analysis around {topic}.",
|
||
# expected_output="{points} bullet points about {topic}.",
|
||
# )
|
||
|
||
# crew = Crew(agents=[agent], tasks=[task], inputs={"subject": "AI"})
|
||
# inputs = {"subject": "AI"}
|
||
# crew._interpolate_inputs(inputs=inputs) # Manual call for now
|
||
|
||
# assert crew.tasks[0].description == "Give me an analysis around {topic}."
|
||
# assert crew.tasks[0].expected_output == "{points} bullet points about {topic}."
|
||
# assert crew.agents[0].role == "{topic} Researcher"
|
||
# assert crew.agents[0].goal == "Express hot takes on {topic}."
|
||
# assert crew.agents[0].backstory == "You have a lot of experience with {topic}."
|
||
|
||
|
||
def test_task_callback_on_crew():
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
researcher_agent = Agent(
|
||
role="Researcher",
|
||
goal="Make the best research and analysis on content about AI and AI agents",
|
||
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
list_ideas = Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher_agent,
|
||
async_execution=True,
|
||
)
|
||
|
||
mock_callback = MagicMock()
|
||
|
||
crew = Crew(
|
||
agents=[researcher_agent],
|
||
process=Process.sequential,
|
||
tasks=[list_ideas],
|
||
task_callback=mock_callback,
|
||
)
|
||
|
||
with patch.object(Agent, "execute_task") as execute:
|
||
execute.return_value = "ok"
|
||
crew.kickoff()
|
||
|
||
assert list_ideas.callback is not None
|
||
mock_callback.assert_called_once()
|
||
args, _ = mock_callback.call_args
|
||
assert isinstance(args[0], TaskOutput)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_tools_with_custom_caching():
|
||
from unittest.mock import patch
|
||
|
||
from crewai_tools import tool
|
||
|
||
@tool
|
||
def multiplcation_tool(first_number: int, second_number: int) -> int:
|
||
"""Useful for when you need to multiply two numbers together."""
|
||
return first_number * second_number
|
||
|
||
def cache_func(args, result):
|
||
cache = result % 2 == 0
|
||
return cache
|
||
|
||
multiplcation_tool.cache_function = cache_func
|
||
|
||
writer1 = Agent(
|
||
role="Writer",
|
||
goal="You write lessons of math for kids.",
|
||
backstory="You're an expert in writing and you love to teach kids but you know nothing of math.",
|
||
tools=[multiplcation_tool],
|
||
allow_delegation=False,
|
||
)
|
||
|
||
writer2 = Agent(
|
||
role="Writer",
|
||
goal="You write lessons of math for kids.",
|
||
backstory="You're an expert in writing and you love to teach kids but you know nothing of math.",
|
||
tools=[multiplcation_tool],
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task1 = Task(
|
||
description="What is 2 times 6? Return only the number after using the multiplication tool.",
|
||
expected_output="the result of multiplication",
|
||
agent=writer1,
|
||
)
|
||
|
||
task2 = Task(
|
||
description="What is 3 times 1? Return only the number after using the multiplication tool.",
|
||
expected_output="the result of multiplication",
|
||
agent=writer1,
|
||
)
|
||
|
||
task3 = Task(
|
||
description="What is 2 times 6? Return only the number after using the multiplication tool.",
|
||
expected_output="the result of multiplication",
|
||
agent=writer2,
|
||
)
|
||
|
||
task4 = Task(
|
||
description="What is 3 times 1? Return only the number after using the multiplication tool.",
|
||
expected_output="the result of multiplication",
|
||
agent=writer2,
|
||
)
|
||
|
||
crew = Crew(agents=[writer1, writer2], tasks=[task1, task2, task3, task4])
|
||
|
||
with patch.object(
|
||
CacheHandler, "add", wraps=crew._cache_handler.add
|
||
) as add_to_cache:
|
||
with patch.object(CacheHandler, "read", wraps=crew._cache_handler.read) as _:
|
||
result = crew.kickoff()
|
||
add_to_cache.assert_called_once_with(
|
||
tool="multiplcation_tool",
|
||
input={"first_number": 2, "second_number": 6},
|
||
output=12,
|
||
)
|
||
assert result.raw == "3"
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_using_contextual_memory():
|
||
from unittest.mock import patch
|
||
|
||
math_researcher = Agent(
|
||
role="Researcher",
|
||
goal="You research about math.",
|
||
backstory="You're an expert in research and you love to learn new things.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task1 = Task(
|
||
description="Research a topic to teach a kid aged 6 about math.",
|
||
expected_output="A topic, explanation, angle, and examples.",
|
||
agent=math_researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[math_researcher],
|
||
tasks=[task1],
|
||
memory=True,
|
||
)
|
||
|
||
with patch.object(ContextualMemory, "build_context_for_task") as contextual_mem:
|
||
crew.kickoff()
|
||
contextual_mem.assert_called_once()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_disabled_memory_using_contextual_memory():
|
||
from unittest.mock import patch
|
||
|
||
math_researcher = Agent(
|
||
role="Researcher",
|
||
goal="You research about math.",
|
||
backstory="You're an expert in research and you love to learn new things.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
task1 = Task(
|
||
description="Research a topic to teach a kid aged 6 about math.",
|
||
expected_output="A topic, explanation, angle, and examples.",
|
||
agent=math_researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[math_researcher],
|
||
tasks=[task1],
|
||
memory=False,
|
||
)
|
||
|
||
with patch.object(ContextualMemory, "build_context_for_task") as contextual_mem:
|
||
crew.kickoff()
|
||
contextual_mem.assert_not_called()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_log_file_output(tmp_path):
|
||
test_file = tmp_path / "logs.txt"
|
||
tasks = [
|
||
Task(
|
||
description="Say Hi",
|
||
expected_output="The word: Hi",
|
||
agent=researcher,
|
||
)
|
||
]
|
||
|
||
crew = Crew(agents=[researcher], tasks=tasks, output_log_file=str(test_file))
|
||
crew.kickoff()
|
||
assert test_file.exists()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_manager_agent():
|
||
from unittest.mock import patch
|
||
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
manager = Agent(
|
||
role="Manager",
|
||
goal="Manage the crew and ensure the tasks are completed efficiently.",
|
||
backstory="You're an experienced manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
manager_agent=manager,
|
||
tasks=[task],
|
||
)
|
||
|
||
mock_task_output = TaskOutput(
|
||
description="Mock description", raw="mocked output", agent="mocked agent"
|
||
)
|
||
|
||
# Because we are mocking execute_sync, we never hit the underlying _execute_core
|
||
# which sets the output attribute of the task
|
||
task.output = mock_task_output
|
||
|
||
with patch.object(
|
||
Task, "execute_sync", return_value=mock_task_output
|
||
) as mock_execute_sync:
|
||
crew.kickoff()
|
||
assert manager.allow_delegation is True
|
||
mock_execute_sync.assert_called()
|
||
|
||
|
||
def test_manager_agent_in_agents_raises_exception():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
manager = Agent(
|
||
role="Manager",
|
||
goal="Manage the crew and ensure the tasks are completed efficiently.",
|
||
backstory="You're an experienced manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.",
|
||
allow_delegation=False,
|
||
)
|
||
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
|
||
Crew(
|
||
agents=[researcher, writer, manager],
|
||
process=Process.hierarchical,
|
||
manager_agent=manager,
|
||
tasks=[task],
|
||
)
|
||
|
||
|
||
def test_manager_agent_with_tools_raises_exception():
|
||
from crewai_tools import tool
|
||
|
||
@tool
|
||
def testing_tool(first_number: int, second_number: int) -> int:
|
||
"""Useful for when you need to multiply two numbers together."""
|
||
return first_number * second_number
|
||
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
manager = Agent(
|
||
role="Manager",
|
||
goal="Manage the crew and ensure the tasks are completed efficiently.",
|
||
backstory="You're an experienced manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.",
|
||
allow_delegation=False,
|
||
tools=[testing_tool],
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.hierarchical,
|
||
manager_agent=manager,
|
||
tasks=[task],
|
||
)
|
||
|
||
with pytest.raises(Exception):
|
||
crew.kickoff()
|
||
|
||
|
||
@patch("crewai.crew.Crew.kickoff")
|
||
@patch("crewai.crew.CrewTrainingHandler")
|
||
@patch("crewai.crew.TaskEvaluator")
|
||
def test_crew_train_success(task_evaluator, crew_training_handler, kickoff):
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
)
|
||
crew.train(
|
||
n_iterations=2, inputs={"topic": "AI"}, filename="trained_agents_data.pkl"
|
||
)
|
||
task_evaluator.assert_has_calls(
|
||
[
|
||
mock.call(researcher),
|
||
mock.call().evaluate_training_data(
|
||
training_data=crew_training_handler().load(),
|
||
agent_id=str(researcher.id),
|
||
),
|
||
mock.call().evaluate_training_data().model_dump(),
|
||
mock.call(writer),
|
||
mock.call().evaluate_training_data(
|
||
training_data=crew_training_handler().load(),
|
||
agent_id=str(writer.id),
|
||
),
|
||
mock.call().evaluate_training_data().model_dump(),
|
||
]
|
||
)
|
||
|
||
crew_training_handler.assert_has_calls(
|
||
[
|
||
mock.call("training_data.pkl"),
|
||
mock.call().load(),
|
||
mock.call("trained_agents_data.pkl"),
|
||
mock.call().save_trained_data(
|
||
agent_id="Researcher",
|
||
trained_data=task_evaluator().evaluate_training_data().model_dump(),
|
||
),
|
||
mock.call("trained_agents_data.pkl"),
|
||
mock.call().save_trained_data(
|
||
agent_id="Senior Writer",
|
||
trained_data=task_evaluator().evaluate_training_data().model_dump(),
|
||
),
|
||
mock.call(),
|
||
mock.call().load(),
|
||
mock.call(),
|
||
mock.call().load(),
|
||
]
|
||
)
|
||
|
||
kickoff.assert_has_calls(
|
||
[mock.call(inputs={"topic": "AI"}), mock.call(inputs={"topic": "AI"})]
|
||
)
|
||
|
||
|
||
def test_crew_train_error():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
)
|
||
|
||
with pytest.raises(TypeError) as e:
|
||
crew.train()
|
||
assert "train() missing 1 required positional argument: 'n_iterations'" in str(
|
||
e
|
||
)
|
||
|
||
|
||
def test__setup_for_training():
|
||
researcher.allow_delegation = True
|
||
writer.allow_delegation = True
|
||
agents = [researcher, writer]
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=agents,
|
||
tasks=[task],
|
||
)
|
||
|
||
assert crew._train is False
|
||
assert task.human_input is False
|
||
|
||
for agent in agents:
|
||
assert agent.allow_delegation is True
|
||
|
||
crew._setup_for_training("trained_agents_data.pkl")
|
||
|
||
assert crew._train is True
|
||
assert task.human_input is True
|
||
|
||
for agent in agents:
|
||
assert agent.allow_delegation is False
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_replay_feature():
|
||
list_ideas = Task(
|
||
description="Generate a list of 5 interesting ideas to explore for an article, where each bulletpoint is under 15 words.",
|
||
expected_output="Bullet point list of 5 important events. No additional commentary.",
|
||
agent=researcher,
|
||
)
|
||
write = Task(
|
||
description="Write a sentence about the events",
|
||
expected_output="A sentence about the events",
|
||
agent=writer,
|
||
context=[list_ideas],
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[list_ideas, write],
|
||
process=Process.sequential,
|
||
)
|
||
|
||
with patch.object(Task, "execute_sync") as mock_execute_task:
|
||
mock_execute_task.return_value = TaskOutput(
|
||
description="Mock description",
|
||
raw="Mocked output for list of ideas",
|
||
agent="Researcher",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Mocked output for list of ideas",
|
||
)
|
||
|
||
crew.kickoff()
|
||
crew.replay(str(write.id))
|
||
# Ensure context was passed correctly
|
||
assert mock_execute_task.call_count == 3
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_replay_error():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
)
|
||
|
||
with pytest.raises(TypeError) as e:
|
||
crew.replay() # type: ignore purposefully throwing err
|
||
assert "task_id is required" in str(e)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_crew_task_db_init():
|
||
agent = Agent(
|
||
role="Content Writer",
|
||
goal="Write engaging content on various topics.",
|
||
backstory="You have a background in journalism and creative writing.",
|
||
)
|
||
|
||
task = Task(
|
||
description="Write a detailed article about AI in healthcare.",
|
||
expected_output="A 1 paragraph article about AI.",
|
||
agent=agent,
|
||
)
|
||
|
||
crew = Crew(agents=[agent], tasks=[task])
|
||
|
||
with patch.object(Task, "execute_sync") as mock_execute_task:
|
||
mock_execute_task.return_value = TaskOutput(
|
||
description="Write about AI in healthcare.",
|
||
raw="Artificial Intelligence (AI) is revolutionizing healthcare by enhancing diagnostic accuracy, personalizing treatment plans, and streamlining administrative tasks.",
|
||
agent="Content Writer",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Write about AI in healthcare...",
|
||
)
|
||
|
||
crew.kickoff()
|
||
|
||
# Check if this runs without raising an exception
|
||
try:
|
||
db_handler = TaskOutputStorageHandler()
|
||
db_handler.load()
|
||
assert True # If we reach this point, no exception was raised
|
||
except Exception as e:
|
||
pytest.fail(f"An exception was raised: {str(e)}")
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_replay_task_with_context():
|
||
agent1 = Agent(
|
||
role="Researcher",
|
||
goal="Research AI advancements.",
|
||
backstory="You are an expert in AI research.",
|
||
)
|
||
agent2 = Agent(
|
||
role="Writer",
|
||
goal="Write detailed articles on AI.",
|
||
backstory="You have a background in journalism and AI.",
|
||
)
|
||
|
||
task1 = Task(
|
||
description="Research the latest advancements in AI.",
|
||
expected_output="A detailed report on AI advancements.",
|
||
agent=agent1,
|
||
)
|
||
task2 = Task(
|
||
description="Summarize the AI advancements report.",
|
||
expected_output="A summary of the AI advancements report.",
|
||
agent=agent2,
|
||
)
|
||
task3 = Task(
|
||
description="Write an article based on the AI advancements summary.",
|
||
expected_output="An article on AI advancements.",
|
||
agent=agent2,
|
||
)
|
||
task4 = Task(
|
||
description="Create a presentation based on the AI advancements article.",
|
||
expected_output="A presentation on AI advancements.",
|
||
agent=agent2,
|
||
context=[task1],
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[agent1, agent2],
|
||
tasks=[task1, task2, task3, task4],
|
||
process=Process.sequential,
|
||
)
|
||
|
||
mock_task_output1 = TaskOutput(
|
||
description="Research the latest advancements in AI.",
|
||
raw="Detailed report on AI advancements...",
|
||
agent="Researcher",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Detailed report on AI advancements...",
|
||
)
|
||
mock_task_output2 = TaskOutput(
|
||
description="Summarize the AI advancements report.",
|
||
raw="Summary of the AI advancements report...",
|
||
agent="Writer",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Summary of the AI advancements report...",
|
||
)
|
||
mock_task_output3 = TaskOutput(
|
||
description="Write an article based on the AI advancements summary.",
|
||
raw="Article on AI advancements...",
|
||
agent="Writer",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Article on AI advancements...",
|
||
)
|
||
mock_task_output4 = TaskOutput(
|
||
description="Create a presentation based on the AI advancements article.",
|
||
raw="Presentation on AI advancements...",
|
||
agent="Writer",
|
||
json_dict=None,
|
||
output_format=OutputFormat.RAW,
|
||
pydantic=None,
|
||
summary="Presentation on AI advancements...",
|
||
)
|
||
|
||
with patch.object(Task, "execute_sync") as mock_execute_task:
|
||
mock_execute_task.side_effect = [
|
||
mock_task_output1,
|
||
mock_task_output2,
|
||
mock_task_output3,
|
||
mock_task_output4,
|
||
]
|
||
|
||
crew.kickoff()
|
||
db_handler = TaskOutputStorageHandler()
|
||
assert db_handler.load() != []
|
||
|
||
with patch.object(Task, "execute_sync") as mock_replay_task:
|
||
mock_replay_task.return_value = mock_task_output4
|
||
|
||
replayed_output = crew.replay(str(task4.id))
|
||
assert replayed_output.raw == "Presentation on AI advancements..."
|
||
|
||
db_handler.reset()
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_replay_with_context():
|
||
agent = Agent(role="test_agent", backstory="Test Description", goal="Test Goal")
|
||
task1 = Task(
|
||
description="Context Task", expected_output="Say Task Output", agent=agent
|
||
)
|
||
task2 = Task(
|
||
description="Test Task", expected_output="Say Hi", agent=agent, context=[task1]
|
||
)
|
||
|
||
context_output = TaskOutput(
|
||
description="Context Task Output",
|
||
agent="test_agent",
|
||
raw="context raw output",
|
||
pydantic=None,
|
||
json_dict={},
|
||
output_format=OutputFormat.RAW,
|
||
)
|
||
task1.output = context_output
|
||
|
||
crew = Crew(agents=[agent], tasks=[task1, task2], process=Process.sequential)
|
||
|
||
with patch(
|
||
"crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
|
||
return_value=[
|
||
{
|
||
"task_id": str(task1.id),
|
||
"output": {
|
||
"description": context_output.description,
|
||
"summary": context_output.summary,
|
||
"raw": context_output.raw,
|
||
"pydantic": context_output.pydantic,
|
||
"json_dict": context_output.json_dict,
|
||
"output_format": context_output.output_format,
|
||
"agent": context_output.agent,
|
||
},
|
||
"inputs": {},
|
||
},
|
||
{
|
||
"task_id": str(task2.id),
|
||
"output": {
|
||
"description": "Test Task Output",
|
||
"summary": None,
|
||
"raw": "test raw output",
|
||
"pydantic": None,
|
||
"json_dict": {},
|
||
"output_format": "json",
|
||
"agent": "test_agent",
|
||
},
|
||
"inputs": {},
|
||
},
|
||
],
|
||
):
|
||
crew.replay(str(task2.id))
|
||
|
||
assert crew.tasks[1].context[0].output.raw == "context raw output"
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_replay_with_invalid_task_id():
|
||
agent = Agent(role="test_agent", backstory="Test Description", goal="Test Goal")
|
||
task1 = Task(
|
||
description="Context Task", expected_output="Say Task Output", agent=agent
|
||
)
|
||
task2 = Task(
|
||
description="Test Task", expected_output="Say Hi", agent=agent, context=[task1]
|
||
)
|
||
|
||
context_output = TaskOutput(
|
||
description="Context Task Output",
|
||
agent="test_agent",
|
||
raw="context raw output",
|
||
pydantic=None,
|
||
json_dict={},
|
||
output_format=OutputFormat.RAW,
|
||
)
|
||
task1.output = context_output
|
||
|
||
crew = Crew(agents=[agent], tasks=[task1, task2], process=Process.sequential)
|
||
|
||
with patch(
|
||
"crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
|
||
return_value=[
|
||
{
|
||
"task_id": str(task1.id),
|
||
"output": {
|
||
"description": context_output.description,
|
||
"summary": context_output.summary,
|
||
"raw": context_output.raw,
|
||
"pydantic": context_output.pydantic,
|
||
"json_dict": context_output.json_dict,
|
||
"output_format": context_output.output_format,
|
||
"agent": context_output.agent,
|
||
},
|
||
"inputs": {},
|
||
},
|
||
{
|
||
"task_id": str(task2.id),
|
||
"output": {
|
||
"description": "Test Task Output",
|
||
"summary": None,
|
||
"raw": "test raw output",
|
||
"pydantic": None,
|
||
"json_dict": {},
|
||
"output_format": "json",
|
||
"agent": "test_agent",
|
||
},
|
||
"inputs": {},
|
||
},
|
||
],
|
||
):
|
||
with pytest.raises(
|
||
ValueError,
|
||
match="Task with id bf5b09c9-69bd-4eb8-be12-f9e5bae31c2d not found in the crew's tasks.",
|
||
):
|
||
crew.replay("bf5b09c9-69bd-4eb8-be12-f9e5bae31c2d")
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
@patch.object(Crew, "_interpolate_inputs")
|
||
def test_replay_interpolates_inputs_properly(mock_interpolate_inputs):
|
||
agent = Agent(role="test_agent", backstory="Test Description", goal="Test Goal")
|
||
task1 = Task(description="Context Task", expected_output="Say {name}", agent=agent)
|
||
task2 = Task(
|
||
description="Test Task",
|
||
expected_output="Say Hi to {name}",
|
||
agent=agent,
|
||
context=[task1],
|
||
)
|
||
|
||
context_output = TaskOutput(
|
||
description="Context Task Output",
|
||
agent="test_agent",
|
||
raw="context raw output",
|
||
pydantic=None,
|
||
json_dict={},
|
||
output_format=OutputFormat.RAW,
|
||
)
|
||
task1.output = context_output
|
||
|
||
crew = Crew(agents=[agent], tasks=[task1, task2], process=Process.sequential)
|
||
crew.kickoff(inputs={"name": "John"})
|
||
|
||
with patch(
|
||
"crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
|
||
return_value=[
|
||
{
|
||
"task_id": str(task1.id),
|
||
"output": {
|
||
"description": context_output.description,
|
||
"summary": context_output.summary,
|
||
"raw": context_output.raw,
|
||
"pydantic": context_output.pydantic,
|
||
"json_dict": context_output.json_dict,
|
||
"output_format": context_output.output_format,
|
||
"agent": context_output.agent,
|
||
},
|
||
"inputs": {"name": "John"},
|
||
},
|
||
{
|
||
"task_id": str(task2.id),
|
||
"output": {
|
||
"description": "Test Task Output",
|
||
"summary": None,
|
||
"raw": "test raw output",
|
||
"pydantic": None,
|
||
"json_dict": {},
|
||
"output_format": "json",
|
||
"agent": "test_agent",
|
||
},
|
||
"inputs": {"name": "John"},
|
||
},
|
||
],
|
||
):
|
||
crew.replay(str(task2.id))
|
||
assert crew._inputs == {"name": "John"}
|
||
assert mock_interpolate_inputs.call_count == 2
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_replay_setup_context():
|
||
agent = Agent(role="test_agent", backstory="Test Description", goal="Test Goal")
|
||
task1 = Task(description="Context Task", expected_output="Say {name}", agent=agent)
|
||
task2 = Task(
|
||
description="Test Task",
|
||
expected_output="Say Hi to {name}",
|
||
agent=agent,
|
||
)
|
||
context_output = TaskOutput(
|
||
description="Context Task Output",
|
||
agent="test_agent",
|
||
raw="context raw output",
|
||
pydantic=None,
|
||
json_dict={},
|
||
output_format=OutputFormat.RAW,
|
||
)
|
||
task1.output = context_output
|
||
crew = Crew(agents=[agent], tasks=[task1, task2], process=Process.sequential)
|
||
with patch(
|
||
"crewai.utilities.task_output_storage_handler.TaskOutputStorageHandler.load",
|
||
return_value=[
|
||
{
|
||
"task_id": str(task1.id),
|
||
"output": {
|
||
"description": context_output.description,
|
||
"summary": context_output.summary,
|
||
"raw": context_output.raw,
|
||
"pydantic": context_output.pydantic,
|
||
"json_dict": context_output.json_dict,
|
||
"output_format": context_output.output_format,
|
||
"agent": context_output.agent,
|
||
},
|
||
"inputs": {"name": "John"},
|
||
},
|
||
{
|
||
"task_id": str(task2.id),
|
||
"output": {
|
||
"description": "Test Task Output",
|
||
"summary": None,
|
||
"raw": "test raw output",
|
||
"pydantic": None,
|
||
"json_dict": {},
|
||
"output_format": "json",
|
||
"agent": "test_agent",
|
||
},
|
||
"inputs": {"name": "John"},
|
||
},
|
||
],
|
||
):
|
||
crew.replay(str(task2.id))
|
||
|
||
# Check if the first task's output was set correctly
|
||
assert crew.tasks[0].output is not None
|
||
assert isinstance(crew.tasks[0].output, TaskOutput)
|
||
assert crew.tasks[0].output.description == "Context Task Output"
|
||
assert crew.tasks[0].output.agent == "test_agent"
|
||
assert crew.tasks[0].output.raw == "context raw output"
|
||
assert crew.tasks[0].output.output_format == OutputFormat.RAW
|
||
|
||
assert crew.tasks[1].prompt_context == "context raw output"
|
||
|
||
|
||
def test_key():
|
||
tasks = [
|
||
Task(
|
||
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
|
||
expected_output="Bullet point list of 5 important events.",
|
||
agent=researcher,
|
||
),
|
||
Task(
|
||
description="Write a 1 amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="A 4 paragraph article about AI.",
|
||
agent=writer,
|
||
),
|
||
]
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
process=Process.sequential,
|
||
tasks=tasks,
|
||
)
|
||
hash = hashlib.md5(
|
||
f"{researcher.key}|{writer.key}|{tasks[0].key}|{tasks[1].key}".encode()
|
||
).hexdigest()
|
||
|
||
assert crew.key == hash
|
||
|
||
|
||
def test_conditional_task_requirement_breaks_when_singular_conditional_task():
|
||
def condition_fn(output) -> bool:
|
||
return output.raw.startswith("Andrew Ng has!!")
|
||
|
||
task = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
condition=condition_fn,
|
||
)
|
||
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
|
||
Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_conditional_task_last_task_when_conditional_is_true():
|
||
def condition_fn(output) -> bool:
|
||
return True
|
||
|
||
task1 = Task(
|
||
description="Say Hi",
|
||
expected_output="Hi",
|
||
agent=researcher,
|
||
)
|
||
task2 = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
condition=condition_fn,
|
||
agent=writer,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task1, task2],
|
||
)
|
||
result = crew.kickoff()
|
||
assert result.raw.startswith(
|
||
"1. **The Evolution of AI and Its Impact on Modern Businesses**"
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_conditional_task_last_task_when_conditional_is_false():
|
||
def condition_fn(output) -> bool:
|
||
return False
|
||
|
||
task1 = Task(
|
||
description="Say Hi",
|
||
expected_output="Hi",
|
||
agent=researcher,
|
||
)
|
||
task2 = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
condition=condition_fn,
|
||
agent=writer,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task1, task2],
|
||
)
|
||
result = crew.kickoff()
|
||
assert result.raw == "Hi"
|
||
|
||
|
||
def test_conditional_task_requirement_breaks_when_task_async():
|
||
def my_condition(context):
|
||
return context.get("some_value") > 10
|
||
|
||
task = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
execute_async=True,
|
||
condition=my_condition,
|
||
agent=researcher,
|
||
)
|
||
task2 = Task(
|
||
description="Say Hi",
|
||
expected_output="Hi",
|
||
agent=writer,
|
||
)
|
||
|
||
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
|
||
Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task, task2],
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_conditional_should_skip():
|
||
task1 = Task(description="Return hello", expected_output="say hi", agent=researcher)
|
||
|
||
condition_mock = MagicMock(return_value=False)
|
||
task2 = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
condition=condition_mock,
|
||
agent=writer,
|
||
)
|
||
crew_met = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task1, task2],
|
||
)
|
||
with patch.object(Task, "execute_sync") as mock_execute_sync:
|
||
mock_execute_sync.return_value = TaskOutput(
|
||
description="Task 1 description",
|
||
raw="Task 1 output",
|
||
agent="Researcher",
|
||
)
|
||
|
||
result = crew_met.kickoff()
|
||
assert mock_execute_sync.call_count == 1
|
||
|
||
assert condition_mock.call_count == 1
|
||
assert condition_mock() is False
|
||
|
||
assert task2.output is None
|
||
assert result.raw.startswith("Task 1 output")
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_conditional_should_execute():
|
||
task1 = Task(description="Return hello", expected_output="say hi", agent=researcher)
|
||
|
||
condition_mock = MagicMock(
|
||
return_value=True
|
||
) # should execute this conditional task
|
||
task2 = ConditionalTask(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
condition=condition_mock,
|
||
agent=writer,
|
||
)
|
||
crew_met = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task1, task2],
|
||
)
|
||
with patch.object(Task, "execute_sync") as mock_execute_sync:
|
||
mock_execute_sync.return_value = TaskOutput(
|
||
description="Task 1 description",
|
||
raw="Task 1 output",
|
||
agent="Researcher",
|
||
)
|
||
|
||
crew_met.kickoff()
|
||
|
||
assert condition_mock.call_count == 1
|
||
assert condition_mock() is True
|
||
assert mock_execute_sync.call_count == 2
|
||
|
||
|
||
@mock.patch("crewai.crew.CrewEvaluator")
|
||
@mock.patch("crewai.crew.Crew.kickoff")
|
||
def test_crew_testing_function(mock_kickoff, crew_evaluator):
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
agent=researcher,
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher],
|
||
tasks=[task],
|
||
)
|
||
n_iterations = 2
|
||
crew.test(n_iterations, openai_model_name="gpt-4o-mini", inputs={"topic": "AI"})
|
||
|
||
assert len(mock_kickoff.mock_calls) == n_iterations
|
||
mock_kickoff.assert_has_calls(
|
||
[mock.call(inputs={"topic": "AI"}), mock.call(inputs={"topic": "AI"})]
|
||
)
|
||
|
||
crew_evaluator.assert_has_calls(
|
||
[
|
||
mock.call(crew, "gpt-4o-mini"),
|
||
mock.call().set_iteration(1),
|
||
mock.call().set_iteration(2),
|
||
mock.call().print_crew_evaluation_result(),
|
||
]
|
||
)
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_verbose_manager_agent():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
verbose=True,
|
||
)
|
||
|
||
crew.kickoff()
|
||
|
||
assert crew.manager_agent is not None
|
||
assert crew.manager_agent.verbose
|
||
|
||
|
||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||
def test_hierarchical_verbose_false_manager_agent():
|
||
task = Task(
|
||
description="Come up with a list of 5 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
|
||
expected_output="5 bullet points with a paragraph for each idea.",
|
||
)
|
||
|
||
crew = Crew(
|
||
agents=[researcher, writer],
|
||
tasks=[task],
|
||
process=Process.hierarchical,
|
||
manager_llm="gpt-4o",
|
||
verbose=False,
|
||
)
|
||
|
||
crew.kickoff()
|
||
|
||
assert crew.manager_agent is not None
|
||
assert not crew.manager_agent.verbose
|