Type something to search...
CrewAI 示例项目 - AI 技术博客

CrewAI 示例项目 - AI 技术博客

在本教程中,基于 AI 的博客写作助手为内容创作者开发,使用 CrewAI 平台。该项目的结构旨在自动提供和建议用户在创建技术博客文章时可能需要的基本和高级信息。这种方法旨在减轻内容创作者的工作负担,提高质量,并确保一致性。

目标

  • 开发一个人工智能驱动的系统,以加快用户创建技术博客文章的过程。
  • 通过扫描谷歌上最受欢迎和最新的发展来创建博客文章。
  • 通过提高内容质量来帮助用户更高效地工作。

技术结构

本项目使用了两个必需的API和密钥:

API密钥通过将其添加到.env文件中作为环境变量分配:

GEMINI_API_KEY="AI…………"
SERPER_API_KEY="b……………"

所需库

在开始项目之前,需要安装必要的 Python 库:

pip install crewai crewai-tools
pip install load_dotenv
pip install python-dotenv
pip install google-generativeai

这些库是运行 AI 模型、与 API 进行通信以及在项目中管理环境变量所必需的。

定义 Serper API 工具

在这个项目中,Serper API 被用来执行搜索。这个 API 是一个服务,允许以编程方式访问搜索引擎功能。通过以 JSON 格式返回结果,它使开发者能够利用搜索数据。

首先,需要从 .env 文件中检索 Serper API 密钥并将其分配给一个环境变量:

import os
from dotenv import load_dotenv
from crewai_tools.tools.serper_dev_tool.serper_dev_tool import SerperDevTool

load_dotenv()
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")

serper_tool = SerperDevTool()

在这里,使用 crew-ai-tools 包中的 SerperDevTool 类创建了一个工具,并将其分配给 serper_tool 变量。这个工具将处理用于博客文章生成的数据收集过程。

代理

项目中定义了两个独立的代理:研究员和作者。这些代理旨在执行特定任务。每个代理都有特定的角色和目标。

  • 研究员: 对相关技术主题进行研究并提供分析。
  • 作者: 使用研究员收集的信息撰写特定主题的博客文章。

如果您希望代理的工作更加全面,建议编写一个详细的背景故事。为了表明研究员代理将向其他代理提供信息,allow_delegation 布尔值被设置为 True。此外,由于我希望两个代理都使用 Gemini 模型,因此只需在 llm 部分写下模型的名称,并分配环境变量 GEMINI_API_KEY。我们创建的 Serper API 也通过 tool 参数分配。

在编写提示时,如果我们想要分配变量值,我们在文本中用大括号写出输入值的名称,如 {topic}

import os

from crewai import Agent
from dotenv import load_dotenv

from tools import serper_tool

load_dotenv()

os.environ['GEMINI_API_KEY'] = os.getenv("GEMINI_API_KEY")

researcher = Agent(
    role= "Senior Researcher",
    goal="Uncover groundbreaking technologies in {topic}",
    verbose= True,
    memory=True,
    backstory=("""
        Driven by curiosity and a relentless pursuit of knowledge, you're at the forefront of innovation. Your role
        is to explore and discover emerging technologies within the {topic} space. You gather insights from multiple 
        sources, analyze them meticulously, and compile them into actionable reports. You are well-versed in understanding 
        market trends, technological shifts, and their potential impacts on various industries.
    """),
    tools= [serper_tool],
    llm="gemini/gemini-1.5-flash",
    allow_delegation=True
)

writer = Agent(
    role= "Writer",
    goal="Narrate compelling tech stories about {topic}",
    verbose= True,
    memory=True,
    backstory=("""
        With a passion for structuring complex topics into cohesive narratives, you bring clarity to groundbreaking technologies.
        Your writing bridges the gap between technical depth and readability, engaging readers by providing clear introductions, 
        detailed explanations, and insightful conclusions. You are known for creating a flow that draws readers in and leaves 
        them with a deeper understanding of the subject matter and its future implications.
    """),
    tools= [serper_tool],
    llm="gemini/gemini-1.5-flash"
)
  • 研究员代理: 对主题进行深入研究,分析获得的信息,并创建报告。
  • 作者代理: 使用研究员提供的信息撰写易读且流畅的文章。

任务

分配给代理的任务使用 Task 类定义。每个任务包括描述、预期输出、使用的工具以及执行任务的代理。

  • 研究员任务: 准备关于该主题的深入报告。
  • 写作任务: 使用该报告撰写博客文章。

如您所见,由于任务需要顺序完成,因此该过程必须作为 Sequential Process 进行。因此,async_execution 值必须设置为 False。这样,写作者在执行其任务之前会等待研究员。

from crewai import Task

from agents import researcher, writer
from tools import serper_tool

researcher_task = Task(
    description=("""
        Conduct an in-depth exploration of the latest trends in {topic} and identify key technologies that have the potential 
        to disrupt the market in the near future. Your analysis should include the strengths, weaknesses, and market 
        implications of these technologies. Focus on identifying major opportunities and risks, providing a clear picture 
        of how these innovations could shape the industry landscape. Pay special attention to scalability, adoption barriers, 
        and competitive dynamics.
    """),
    expected_output="A comprehensive 3-paragraph report analyzing emerging technologies, market potential, and risks in {topic}.",
    tools=[serper_tool],
    agent=researcher
)

writer_task = Task(
    description=("""
        Write the article in language {language}.
        Structure the article into three sections: Introduction, Development, and Conclusion. 
        
        In the **Introduction**, provide an overview of {topic}, its current relevance, and why it matters today. 
        
        In the **Development** section, write min 2 max 4 paragraphs that analyze key trends, technological advancements, 
        and their implications for the industry. Include both the opportunities and challenges these technologies present.
        
        In the **Conclusion**, summarize the overall impact of these technologies and offer predictions about how 
        they may evolve in the future. Make sure the article is cohesive, flows naturally from one section to the next, 
        and ends with a forward-looking perspective.
        
        The article should not have headings labeled as Introduction, Development, or Conclusion. 
        All paragraphs should be between 500 and 850 words in length.
        If the selected language is not English, I would like you to write English technical terms in parentheses next to the translated term. 
        For example, in the Turkish translation, I want it to say 'Üretken Yapay Zeka (Generative AI)'.
    """),
    expected_output="A well-structured article (4 paragraphs) in markdown with a clear Introduction, Development (2-3 paragraphs), and Conclusion.",
    async_execution= False,
    tools=[serper_tool],
    agent=writer,
    output_file= "new-blog-post.md"
)

团队与流程

一旦代理和任务被定义,流程就使用 Crew 对象进行定义。在这个流程中,任务是顺序执行的。最后,使用 kickoff 方法来运行项目。在这个方法中,必要的参数被输入,流程开始。

from crewai import Crew, Process

from agents import researcher, writer
from tasks import researcher_task, writer_task

crew = Crew(
    agents=[researcher, writer],
    tasks=[researcher_task, writer_task],
    process=Process.sequential,
    verbose=True
)

result = crew.kickoff(inputs={
    'topic': 'AI in industry',
    'language': 'english'})
print(result)

这段代码接受要研究的主题和语言参数,并启动代理与任务之间的互动。作为输出,生成一篇博客文章。

示例输出

帖子的输出保存到指定的文件路径。

一个示例输出:

The AI industry is undergoing a dramatic transformation, fueled by emerging technologies with the potential to reshape the market. Generative AI, specifically large language models (LLMs) and image-generating AI, stands at the forefront of this revolution. Its ability to create novel content, automate tasks, and personalize experiences across various industries, from healthcare and manufacturing to finance and marketing, is driving widespread adoption. This transformative potential has positioned generative AI as a vital tool for businesses seeking to optimize processes, enhance products, and create new revenue streams.  

The rise of Multimodal AI (Multimodal AI), which integrates different data modalities like text, image, and audio, represents another crucial trend reshaping the industry. This technology offers a more comprehensive understanding of complex data, enabling more sophisticated applications in areas such as natural language processing, computer vision, and robotics.  The increasing accessibility of open-source AI tools and models is democratizing access to AI technology, fostering innovation and empowering smaller players to participate in the market.  Furthermore, cloud-based AI platforms provide scalable solutions, making AI accessible to a wider range of businesses. 

While these developments present exciting opportunities, they also introduce risks. Ethical considerations are paramount, requiring careful attention to bias, fairness, and transparency in AI systems.  The rapid pace of development and the evolving nature of AI technologies also pose challenges, requiring continuous adaptation and learning.  Moreover, the high computational demands of generative AI models and the requirement for specialized expertise pose significant adoption barriers, particularly for smaller businesses.  

Despite these complexities, the future of AI in industry appears promising, with these emerging technologies poised to revolutionize industries and create new opportunities for innovation and growth.  As AI technologies continue to advance, the industry will likely see further consolidation and the emergence of new use cases across various sectors.  The focus will likely shift towards developing more robust and ethical AI systems, ensuring responsible and equitable implementation.  The integration of AI with other technologies like the Internet of Things (IoT) and blockchain will also create exciting new possibilities. Ultimately, the success of AI in industry will depend on collaboration between businesses, researchers, and policymakers to address ethical and practical challenges and harness the transformative potential of these technologies for the benefit of society.

通过这个项目,用户可以自动生成技术博客文章,并显著加快内容创作过程。

如果您希望我继续进行与AI相关的系列,我感谢您的支持🙂此外,请别忘了关注😉

Related Posts

使用 ChatGPT 搜索网络功能的 10 种创意方法

使用 ChatGPT 搜索网络功能的 10 种创意方法

例如,提示和输出 你知道可以使用 ChatGPT 的“搜索网络”功能来完成许多任务,而不仅仅是基本的网络搜索吗? 对于那些不知道的人,ChatGPT 新的“搜索网络”功能提供实时信息。 截至撰写此帖时,该功能仅对使用 ChatGPT 4o 和 4o-mini 的付费会员开放。 ![](https://images.weserv.nl/?url=https://cdn-im

阅读更多
在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和技术领域保持领先地位的 10 项必学技能 📚

在人工智能和科技这样一个动态的行业中,保持领先意味着不断提升你的技能。无论你是希望深入了解人工智能模型性能、掌握数据分析,还是希望通过人工智能转变传统领域如法律,这些课程都是你成功的捷径。以下是一个精心策划的高价值课程列表,可以助力你的职业发展,并让你始终处于创新的前沿。 1. 生成性人工智能简介课程: [生成性人工智能简介](https://genai.works

阅读更多
10 个强大的 Perplexity AI 提示,让您的营销任务自动化

10 个强大的 Perplexity AI 提示,让您的营销任务自动化

在当今快速变化的数字世界中,营销人员总是在寻找更智能的方法来简化他们的工作。想象一下,有一个个人助理可以为您创建受众档案,建议营销策略,甚至为您撰写广告文案。这听起来像是一个梦想? 多亏了像 Perplexity 这样的 AI 工具,这个梦想现在成为现实。通过正确的提示,您可以将 AI 转变为您的 个人营销助理。在本文中,我将分享 10 个强大的提示,帮助您自动

阅读更多
10+ 面向 UI/UX 设计师的顶级 ChatGPT 提示

10+ 面向 UI/UX 设计师的顶级 ChatGPT 提示

人工智能技术,如机器学习、自然语言处理和数据分析,正在重新定义传统设计方法。从自动化重复任务到实现个性化用户体验,人工智能使设计师能够更加专注于战略思维和创造力。随着这一趋势的不断增长,UI/UX 设计师越来越多地采用 AI 驱动的工具来促进他们的工作。利用人工智能不仅能提供基于数据的洞察,还为满足多样化用户需求的创新设计解决方案开辟了机会。 1. 用户角色开发 目的

阅读更多
在几分钟内完成数月工作的 100 种人工智能工具

在几分钟内完成数月工作的 100 种人工智能工具

人工智能(AI)的快速发展改变了企业的运作方式,使人们能够在短短几分钟内完成曾经需要几周或几个月的任务。从内容创作到网站设计,AI工具帮助专业人士节省时间,提高生产力,专注于创造力。以下是按功能分类的100个AI工具的全面列表,以及它们在现实世界中的使用实例。 1. 研究工具 研究可能耗时,但人工智能工具使查找、分析和组织数据变得更加容易。**ChatGPT, Cop

阅读更多
你从未知道的 17 个令人惊叹的 GitHub 仓库

你从未知道的 17 个令人惊叹的 GitHub 仓库

Github 隐藏的宝石!! 立即收藏的代码库 学习编程相对简单,但掌握编写更好代码的艺术要困难得多。GitHub 是开发者的宝藏,那里“金子”是其他人分享的精心编写的代码。通过探索 GitHub,您可以发现如何编写更清晰的代码,理解高质量代码的样子,并学习成为更熟练开发者的基本步骤。 1. notwaldorf/emoji-translate *谁需

阅读更多