使用 CrewAI 和 Gemini 1.5 构建多智能体系统实战指南
本文介绍了如何利用 CrewAI 框架结合 Google Gemini 1.5 大模型构建多智能体系统。通过定义主题研究员、博客作者和 LinkedIn 帖子创作者三个智能体,实现从网络搜索到内容生成的自动化流程。文章涵盖了环境配置、智能体与任务定义、流程运行及优化建议,展示了 AI Agent 在内容创作领域的应用潜力。

本文介绍了如何利用 CrewAI 框架结合 Google Gemini 1.5 大模型构建多智能体系统。通过定义主题研究员、博客作者和 LinkedIn 帖子创作者三个智能体,实现从网络搜索到内容生成的自动化流程。文章涵盖了环境配置、智能体与任务定义、流程运行及优化建议,展示了 AI Agent 在内容创作领域的应用潜力。

正如许多专家预测的那样,2025 年正逐渐成为代理人工智能(AI Agent)的年份。这个新兴领域准备重新定义我们与技术互动的方式,通过引入高度自主的系统,这些系统可以在最小的人工干预下做出决策并执行复杂任务。
在这篇文章中,我将带你了解如何构建一个不仅能执行特定任务还能根据用户定义的主题创建有意义内容的多智能体系统。我们将探讨系统背后的愿景、工作原理以及在实际开发中遇到的挑战与经验教训。
代理人工智能(AI Agents)指的是设计用来独立执行任务而无需持续人类监督的系统。与传统的人工智能不同,后者在每一步都需要明确的指令,代理人工智能能够做出自主决策,并根据变化的情况调整其行动。
到 2025 年,我们预计这些系统将成为从医疗保健到物流等行业的核心,有能力处理从自动化重复任务到自主解决复杂问题的一切。事实上,许多预测显示,超过 60% 的企业人工智能实施将以某种形式整合代理人工智能。
在代理人工智能的巨大前景下,我决定构建一个简单的多智能体系统,以展示其在内容创作和摘要领域的潜力。我构建的系统接受用户输入的主题,执行相关的网络研究,并生成两个输出:一篇深入的博客文章和一个简洁的 LinkedIn 帖子。
这个项目依赖于 Gemini 1.5 模型的力量,这是一个强大的人工智能语言模型,它在生成内容方面起着关键作用。Gemini 1.5 提供了令人印象深刻的文本生成能力,确保输出既连贯又吸引人。同时结合 CrewAI,这是一个设计用来构建和管理由 AI 驱动的多智能体系统的平台。它允许你通过定义具有特定角色和任务的智能体来创建复杂的工作流程,然后协调它们无缝地一起工作。
我构建的多智能体系统利用了三个主要智能体协同工作:
首先,你需要安装 crewai 及其相关工具包:
pip install crewai crewai-tools python-dotenv
为了安全起见,建议将 API 密钥存储在 .env 文件中,而不是直接在脚本中硬编码。创建一个名为 .env 的文件,内容如下:
SERPER_API_KEY=your_serper_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here
在代码中加载这些变量:
import os
from dotenv import load_dotenv
from crewai import Agent, Crew, Process, Task
from crewai_tools import SerperDevTool
from crewai import LLM
# Load environment variables from a .env file
load_dotenv()
# Set the API key for the SerperDevTool (web search tool)
os.environ['SERPER_API_KEY'] = os.getenv('SERPER_API_KEY')
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
接下来,我们初始化 Gemini 1.5 模型和网页搜索工具(SerperDevTool)。温度参数控制响应的创新性或确定性,通常 0.7 是一个平衡点。
# Initialize the tool for internet searching capabilities
tool = SerperDevTool()
llm = LLM(
model="gemini/gemini-1.5-flash", # Specify the AI model to use
temperature=0.7, # Set the creativity of the model
max_tokens=4096 # Adjust based on model limits
)
我们定义了三个协同工作的智能体来实现目标。每个智能体都拥有特定的角色、目标、背景故事以及使用的工具。
# Define the Topic Researcher agent
topic_researcher = Agent(
role='Topic Researcher',
goal='Search for only 1 relevant resource on the topic {topic} from the web',
verbose=True,
memory=True,
backstory='Expert in finding and analyzing relevant content from Web...',
tools=[tool],
llm=llm,
allow_delegation=True
)
# Define the Blog writer agent
blog_writer = Agent(
role='Blog Writer',
goal='Write a comprehensive blog post from the only 1 article provided by the Topic Researcher, covering all necessary sections',
verbose=True,
memory=True,
backstory='Experienced in creating in-depth, well-structured blog posts that explain technical concepts clearly and engage readers from introduction to conclusion.',
tools=[tool],
llm=llm,
allow_delegation=True
)
# Define the linkedin post writer agent
linkedin_post_agent = Agent(
role='LinkedIn Post Creator',
goal='Create a concise LinkedIn post summary from the transcription provided by the Topic Researcher.',
verbose=True,
memory=True,
backstory='Expert in crafting engaging LinkedIn posts that summarize complex topics and include trending hashtags for maximum visibility.',
tools=[tool],
llm=llm,
allow_delegation=True
)
关键点说明:
allow_delegation=True:允许智能体之间进行任务委托,提高协作效率。memory=True:启用记忆功能,使智能体能记住之前的交互上下文。backstory:详细的背景故事有助于大模型更好地理解角色的语气和风格。一旦智能体设置好,我们就定义每个智能体将执行的具体任务。任务描述越清晰,预期输出越具体,最终结果的质量通常越高。
# Define Tasks
research_task = Task(
description="Identify and analyze only 1 content or article on the {topic} from the web.",
expected_output="A complete word-by-word report on the most relevant post or article found on the topic {topic}.",
agent=topic_researcher,
tools=[tool]
)
blog_writing_task = Task(
description="""Write a comprehensive blog post based on the 1 article provided by the Topic Researcher.
The article must include an introduction, step-by-step guides, and conclusion.
The overall content must be about 400 words long.""",
expected_output="A markdown-formatted blog post",
agent=blog_writer,
tools=[tool],
output_file='./artifacts/blog-post.md'
)
linkedin_post_task = Task(
description="Create a LinkedIn post summarizing the key points from the transcription provided by the Topic Researcher, including relevant hashtags.",
expected_output="A markdown-formatted LinkedIn post",
agent=linkedin_post_agent,
tools=[tool],
output_file='./artifacts/linkedin-post.md'
)
一旦智能体和任务设置完成,我们就创建团队(Crew)并启动流程。CrewAI 支持多种进程模式,本例中使用顺序模式 (Process.sequential)。
# Create the Crew with defined agents and tasks
my_crew = Crew(
agents=[topic_researcher, linkedin_post_agent, blog_writer],
tasks=[research_task, linkedin_post_task, blog_writing_task],
verbose=True,
process=Process.sequential # Run tasks sequentially
)
# Input Topic
topic_of_interest = 'gemini 2.0 multimodal'
# Kick off the process with the provided topic
result = my_crew.kickoff(inputs={'topic': topic_of_interest})
print(result)
在这里,智能体以顺序过程协同工作,确保每个智能体依次执行其任务。启动方法使用提供的 topic_of_interest 运行流程。你可以将 output_file 路径指向本地目录,以便直接保存生成的文件。
这个项目最令人兴奋的方面之一是使用 Gemini 1.5 模型为 AI 智能体提供动力。Gemini 1.5 以其先进的自然语言处理能力而闻名,这使得它能够在广泛的上下文中理解和生成类似人类的文本。无论是进行研究还是撰写内容,Gemini 1.5 都能确保智能体以高准确性和流畅性有效地完成其任务。其长上下文窗口特性使得处理大量搜索结果成为可能。
构建这个多智能体系统并非没有挑战。以下是我在开发过程中遇到的一些关键障碍及解决方案:
确保智能体之间顺畅的沟通至关重要。每个智能体都有特定的角色,但它们需要无缝协作以产生期望的结果。我了解到,清晰和明确的任务管理对于系统的效率至关重要。如果任务描述模糊,智能体可能会产生幻觉或偏离目标。
主题研究智能体的任务是搜索网络上的相关资源。虽然它总体表现良好,但有时它会返回与主题不完全相关的文章。微调其搜索参数需要一些试错。建议在使用 SerperDevTool 时,在任务描述中强调'相关性'和'权威性'。
尽管 Gemini 1.5 在生成内容方面非常有效,但输出的质量取决于它接收到的输入。确保提供给博客撰写智能体和 LinkedIn 帖子创建智能体的研究是全面和清晰的,以产生连贯且结构良好的最终输出,这很重要。可以通过增加 verbose=True 来查看中间步骤,便于调试。
2025 年很可能是代理人工智能之年,我们已经初步看到了其潜力。我的多智能体系统只是 AI 如何用于自动化复杂任务和生成有意义输出的一个例子。随着技术的不断进步,我们可以期待更强大的系统出现,重塑行业,使 AI 成为我们日常生活中的关键部分。
通过合理配置 CrewAI 框架和大模型参数,开发者可以快速构建出具备自主规划能力的智能体应用。希望本文能为你开启 AI Agent 开发之旅提供帮助。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online