使用 Cerebras 和 CrewAI 构建多 AI 智能体工作流
利用 CrewAI 和 Cerebras 的快速推理速度构建多 AI 智能体工作流
在本教程中,我们将创建一个简单的多AI代理工作流,其中一个代理使用CrewAI和Cerebras研究给定领域或主题中的新兴技术。
关于Cerebras
Cerebras的推理速度是世界上最快的,我们正在为下一代AI应用提供动力。借助Cerebras推理API,您可以比在GPU上快10倍地运行Llama3.1 8B和Llama3.1 70B。我们的推理运行在晶圆级引擎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 LLM
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中,一个代理是一个自主实体,它根据定义的角色、目标和背景故事执行任务。代理可以利用工具并由语言模型(LLM)驱动。
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是代理和任务的集合,它们协同工作以执行一个过程。它充当编排者,根据指定的过程模式管理代理之间的任务流。
通过组建一个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():启动Crew任务的执行,可选择接受动态输入以替换
{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段报告,由researcher代理使用Cerebras LLM生成。
示例输出
# 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",看看它如何影响性能。 - 添加更多代理:引入其他代理来处理其他任务,例如数据分析或内容生成。
- 增强任务:通过添加子任务或集成更多工具来使任务更复杂。
评论 ()