使用 Cerebras 和 CrewAI 构建多 AI 智能体工作流
使用 CrewAI 和 Cerebras 的高速推理能力构建多 AI 智能体工作流

在本教程中,我们将创建一个简单的多 AI 智能体工作流,其中一个智能体使用 CrewAI 和 Cerebras 研究给定领域或主题的新兴技术。
关于 Cerebras
Cerebras 的推理速度全球领先,我们正在为下一代 AI 应用提供支持。使用 Cerebras 推理 API,您可以以比 GPU 快 10 倍的速度运行 Llama3.1 8B 和 Llama3.1 70B。我们的推理运行在 Wafer Scale Engine 3 (WSE3) 上,这是 Cerebras 为 AI 设计的定制硬件。
加入我们,以前所未有的推理速度构建下一代创新应用。在此开始使用 Cerebras:cloud.cerebras.ai
附加链接
关于 CrewAI
CrewAI 是一个开源框架,用于构建和编排多智能体 AI 工作流。它允许开发者定义具有特定角色、目标和背景故事的自主智能体。这些智能体可以利用工具、处理任务并相互协作,以完成复杂的目标。
由大型语言模型 (LLM) 提供支持,CrewAI 简化了需要多个智能体之间协调的 AI 应用开发,使得构建复杂且可扩展的 AI 系统变得更加容易。
前置条件
在开始之前请确保您已满足以下条件
- Python 3.7 或更高版本
一个 Cerebras 推理 API 密钥。像这样将其设置在您的 .env
文件中
CEREBRAS_API_KEY=csk-*************************************
安装 crewai
和 crewai-tools
包。使用此命令安装它们
pip install crewai crewai_tools
在 CrewAI 中配置 Cerebras LLMs
from crewai import LLM
import os
# Configure the LLM to use Cerebras
cerebras_llm = LLM(
model="cerebras/llama3.1-70b", # Replace with your chosen Cerebras model name, e.g., "cerebras/llama3.1-8b"
api_key=os.environ.get("CEREBRAS_API_KEY"), # Your Cerebras API key
base_url="https://api.cerebras.ai/v1",
temperature=0.5,
# Optional parameters:
# top_p=1,
# max_completion_tokens=8192, # Max tokens for the response
# response_format={"type": "json_object"} # Ensures the response is in JSON format
)
定义一个智能体
在 CrewAI 中,一个 智能体 是一个自主实体,它根据定义的 角色、目标 和 背景故事 执行 任务 。智能体可以利用工具并由语言模型 (LLMs) 提供支持。
from crewai import Agent
from crewai_tools import SerperDevTool
# Agent definition
researcher = Agent(
role='{topic} Senior Researcher',
goal='Uncover groundbreaking technologies in {topic} for the year 2024',
backstory='Driven by curiosity, you explore and share the latest innovations.',
tools=[SerperDevTool()],
llm=cerebras_llm
)
- 角色:定义智能体的位置,使用
{topic}
作为细粒度动态分配的占位符。 - 目标:智能体旨在实现的目标。
- 背景故事:增加智能体的深度,影响其行为。
- 工具:智能体可以利用的外部工具(例如,使用
SerperDevTool
进行网页搜索)。 - LLM:指定智能体使用的语言模型的 CrewAI 类,在本例中是 Cerebras。
定义一个任务
在 CrewAI 中,一个 任务 代表分配给智能体的工作单元。
from crewai import Task
# Define a research task for the Senior Researcher agent
research_task = Task(
description='Identify the next big trend in {topic} with pros and cons.',
expected_output='A 3-paragraph report on emerging {topic} technologies.',
agent=researcher
)
- 描述:详细说明任务内容。
- 预期输出:指定期望的结果。
- 智能体:将任务分配给定义的智能体。
执行工作流
现在,让我们设置 协作组 并运行流程。
什么是协作组 (Crew)?
一个协作组(Crew)是智能体和任务的集合,它们协同工作以执行流程。它充当编排器,根据指定的流程模式管理智能体之间的任务流程。
通过组建协作组,您可以
- 组织智能体和任务:将相关的智能体及其对应任务分组,组成一个内聚单元。
- 控制执行流程:定义任务的执行方式和顺序(例如,按顺序或并行)。
- 管理输入和输出:将动态输入传递给智能体并收集它们的输出。
设置协作组 (Crew)
from crewai import Crew, Process
def main():
# Forming the crew and kicking off the process
crew = Crew(
agents=[researcher],
tasks=[research_task],
process=Process.sequential,
verbose=True # Enables detailed logging
)
result = crew.kickoff(inputs={'topic': 'AI Agents'})
print(result)
if __name__ == "__main__":
main()
- 协作组 (Crew)
- 智能体:工作流中涉及的智能体列表。
- 任务:要执行的任务列表。
- 流程:定义执行策略。
Process.sequential
表示任务按顺序一个接一个地执行。 - 详细输出:当设置为
True
时,在执行期间提供详细输出。
- kickoff():启动协作组任务的执行,可选接受动态输入以替换诸如
{topic}
的占位符。
完整示例代码
您可以在此GitHub 仓库中找到包含所有步骤的完整代码。
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool
import os
# Configure the LLM to use Cerebras
cerebras_llm = LLM(
model="cerebras/llama3.1-70b", # Replace with your chosen Cerebras model name
api_key=os.environ.get("CEREBRAS_API_KEY"), # Your Cerebras API key
base_url="https://api.cerebras.ai/v1",
temperature=0.5,
)
# Agent definition
researcher = Agent(
role='{topic} Senior Researcher',
goal='Uncover groundbreaking technologies in {topic} for the year 2024',
backstory='Driven by curiosity, you explore and share the latest innovations.',
tools=[SerperDevTool()],
llm=cerebras_llm
)
# Define a research task for the Senior Researcher agent
research_task = Task(
description='Identify the next big trend in {topic} with pros and cons.',
expected_output='A 3-paragraph report on emerging {topic} technologies.',
agent=researcher
)
def main():
# Forming the crew and kicking off the process
crew = Crew(
agents=[researcher],
tasks=[research_task],
process=Process.sequential,
verbose=True
)
result = crew.kickoff(inputs={'topic': 'AI Agents'})
print(result)
if __name__ == "__main__":
main()
运行智能体脚本
运行脚本的步骤
- 确保满足所有前置条件。
- 将脚本保存到文件,例如
crewai_cerebras_integration.py
。
运行脚本
python crewai_cerebras_integration_demo.py
预期输出
输出将是一份关于 2024 年新兴 AI 智能体技术的 3 段报告,由使用 Cerebras LLM 的 researcher
智能体生成。
示例输出
# Emerging AI Agents Technologies: A 3-Paragraph Report
The year 2024 is expected to be a significant year for AI Agents technologies.
According to various sources, including Forbes, CNBC, and PCG, AI Agents are going to revolutionize the way businesses operate.
These agents are expected to autonomously manage supply chains, optimize inventory levels, forecast demand, and even handle complex logistics planning.
Moreover, AI Agents will transform business processes, increase automation in workflows, improve customer service and satisfaction, and provide cost savings by reducing operational costs.
However, there are also concerns about the pros and cons of AI Agents.
Some of the cons include issues like ethics and dependency on technology.
Furthermore, there are risks associated with the use of AI Agents, such as new security risks and the potential for job displacement.
Despite these concerns, many experts believe that the benefits of AI Agents outweigh the drawbacks. As Agentic AI becomes more prevalent, it is expected to change the tech stack, HR practices, and the way of getting things done.
In conclusion, AI Agents are going to play a significant role in shaping the future of businesses.
With their ability to autonomously manage tasks and processes, they are expected to bring about increased efficiency, accuracy, and cost savings.
However, it is essential to be aware of the potential risks and challenges associated with the use of AI Agents and to take steps to mitigate them.
As the technology continues to evolve, it will be interesting to see how AI Agents transform various industries and revolutionize the way we work.
您还可以在此处观看关于相同代码的完整视频教程
结论
通过将 Cerebras 闪电般的快速推理与 CrewAI 灵活的多智能体框架集成,开发者可以构建高效执行复杂任务的复杂 AI 应用。这种组合对于速度和可扩展性至关重要的研究密集型应用尤其强大。
后续步骤
- 尝试不同的模型:尝试使用不同的 Cerebras 模型,例如
"cerebras/llama3.1-8b"
,看看它如何影响性能。 - 添加更多智能体:引入更多智能体来处理其他任务,例如数据分析或内容生成。
- 增强任务:通过添加子任务或集成更多工具来使任务更复杂。
评论 ()