Developing complex multi-agent AI systems with CrewAI can quickly become challenging. This post demonstrates how to isolate and test individual agents and tasks for easier debugging and faster iteration.
The Benefits of Isolation
Isolating components in your CrewAI system is akin to unit testing. This approach provides several key advantages:
Let's examine the core elements:
The researcher
Agent (agents.yaml
):
<code class="language-yaml"> researcher: role: "Senior Research Analyst" goal: "Uncover groundbreaking technologies in AI" backstory: "A highly skilled researcher with a passion for AI advancements." llm: gemini/gemini-1.5-flash # Replace with your preferred LLM allow_delegation: false tools: - WebSearchTool</code>
The research_task
(tasks.yaml
):
<code class="language-yaml"> research_task: description: "Research the latest developments in AI for 2024." expected_output: "A report summarizing the key AI trends." agent: researcher</code>
The IndependentCrew
Class (crew.py
):
<code class="language-python"> from crewai import Agent, Crew, Process, Task from crewai.project import CrewBase, agent, crew, task from .tools import WebSearchTool @CrewBase class IndependentCrew(): """IndependentCrew crew""" agents_config = 'config/agents.yaml' tasks_config = 'config/tasks.yaml' @agent def researcher(self) -> Agent: return Agent( config=self.agents_config['researcher'], verbose=True, tools=[WebSearchTool()] ) @task def research_task(self) -> Task: return Task( config=self.tasks_config['research_task'], ) @crew def crew(self) -> Crew: """Creates the IndependentCrew crew""" return Crew( agents=self.agents, tasks=self.tasks, process=Process.sequential, verbose=True, )</code>
Running the Agent Independently (run_agent.py
): This example shows how to create and use an agent outside the Crew context, executing a custom task and reusing the defined agent and task.
Running the Task Independently (run_task.py
): This demonstrates creating and executing tasks independently, including synchronous and asynchronous execution, and using custom context and tools. It also shows reusing the defined task with custom context.
Conclusion
The ability to run agents and tasks independently provides significant flexibility and control in CrewAI development. This isolated testing approach streamlines debugging, accelerates iteration, and improves overall efficiency. The provided code examples offer a practical starting point for integrating this technique into your projects. Remember to consult the CrewAI documentation and GitHub repository for further details and support.
Resources:
The above is the detailed content of Debugging Your Crew: Isolating Agents and Tasks in CrewAI. For more information, please follow other related articles on the PHP Chinese website!