登录注册
返回

通过Cerebras和CrewAI构建多AI代理工作流

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

Crew AI 工厂阅读需 3 分钟|
  • João Moura
    João (Joe) Moura
  • Sarah Chieng
    Sarah Chieng
Build a Multi-AI Agent Workflow with Cerebras and CrewAI

在本教程中,我们将使用 CrewAI 和 Cerebras 创建一个简单的多 AI 智能体工作流,让智能体针对特定领域或主题研究新兴技术。

关于 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 工作流的开源框架。它允许开发者定义具有特定角色、目标和背景故事的自主智能体。这些智能体可以利用工具、处理任务并相互交互,以实现复杂的目标。

CrewAI 由语言模型 (LLMs) 驱动,简化了需要多个智能体协作的 AI 应用开发,使得构建复杂且可扩展的 AI 系统变得更加容易。

先决条件

在开始之前,请确保具备以下条件

  • Python 3.7 或更高版本

一个 Cerebras 推理 API 密钥。请将其设置在您的 .env 文件中

CEREBRAS_API_KEY=csk-*************************************

安装 crewaicrewai-tools 包。使用以下命令进行安装

pip install crewai crewai_tools

在 CrewAI 中配置 Cerebras LLM

💡 It's HIGHLY recommended to store your API key securely, such as in environment variables, rather than hardcoding it.
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 中,智能体 (Agent) 是一个根据定义的角色 (Role)、目标 (Goal)背景故事 (Backstory) 执行任务 (Tasks) 的自主实体。智能体可以利用工具并由语言模型 (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
)
  • 角色 (Role):定义智能体的职位,使用 {topic} 作为精细化动态分配的占位符。

  • 目标 (Goal):智能体旨在实现的目标。

  • 背景故事 (Backstory):增加智能体的深度,从而影响其行为。

  • 工具 (Tools):智能体可以利用的外部工具(例如,使用 SerperDevTool 进行网络搜索)。

  • LLM:CrewAI 类,用于指定智能体使用的语言模型,在本例中为 Cerebras。

定义任务

CrewAI 中的任务 (Task) 代表分配给智能体的工作单元。

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
)
  • 描述 (Description):详细说明任务包含的内容。

  • 预期输出 (Expected Output):指定期望的结果。

  • 智能体 (Agent):将任务分配给指定的智能体。

执行工作流

现在,让我们设置团队 (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)

    • 智能体 (Agents):参与工作流的智能体列表。

    • 任务 (Tasks):要执行的任务列表。

    • 流程 (Process):定义执行策略。Process.sequential 意味着任务按顺序依次执行。

    • 详细信息 (Verbose):当设置为 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()

运行智能体脚本

要运行脚本

  1. 确保满足所有先决条件。

  2. 将脚本保存到文件,例如 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",看看它如何影响性能。

  • 添加更多智能体:引入额外的智能体来处理其他任务,例如数据分析或内容生成。

  • 增强任务:通过添加子任务或集成更多工具,使任务变得更复杂。

参考资料

立即开始

CrewAI 支持您智能体之旅的任何阶段

  • Icon

    初学者

    您已准备好使用智能体。您需要一个坚实的基础来开始构建。

  • Vector (22)

    扩展中

    您已经启动了试点项目。现在您需要将智能体投入生产。

  • Shell

    大规模应用

    智能体正在运行。您需要管理日益庞大的体系。