使用 Swarm 构建多智能体新闻助理

本文将带你构建一个多智能体新闻助理,利用 OpenAI 的 Swarm 框架和 Llama 3.2 来自动化新闻处理工作流。在本地运行环境下,我们将实现一个多智能体系统,让不同的智能体各司其职,分步完成新闻搜索、信息综合与摘要生成等任务,而无需付费使用外部服务。

应用设计

相关技术

Swarm

OpenAI Swarm 是一个新兴的多智能体协作框架,旨在通过集成强化学习和分布式计算的优势来解决复杂问题。该框架允许多个智能体在共享环境中同时交互与学习,彼此共享信息,从而提高决策质量和学习效率。Swarm 框架采用先进的强化学习算法,使智能体能够在动态环境中持续适应和优化策略,同时通过分布式计算高效利用资源,加速训练过程。它具有很强的可扩展性,能够根据需求增加智能体数量,适应不同规模和复杂度的任务。应用场景广泛,包括智能交通系统、机器人群体和游戏开发等,OpenAI Swarm 为实现更高效的智能系统提供了灵活且强大的解决方案,推动各领域的智能化进程。

本文我们将使用 Swarm 框架来构建智能体。

DuckDuckGo

DuckDuckGo 是一家以隐私保护为核心的互联网搜索引擎公司。与其他主流搜索引擎不同,DuckDuckGo承诺不追踪用户的搜索历史,也不收集个人数据,从而提供更高的隐私保护。该搜索引擎通过结合多种来源的数据,包括自有的网络爬虫和第三方API,提供快速且相关的搜索结果。

DuckDuckGo 的主要特点包括:

  • 隐私保护:不记录用户的IP地址,不存储搜索历史,不使用个人数据进行广告定向。

  • 简洁界面:提供简洁、无广告干扰的用户界面,提升用户体验。

  • 即时答案:通过“零点击信息”功能,直接在搜索结果页面提供答案,减少用户点击次数。

  • 开源社区:鼓励开发者参与改进和扩展其服务,部分代码和数据源是开源的。

我们将使用 DuckDuckGo 进行实时新闻搜索,获取最新信息。将会有一个专门的智能体将负责向 DuckDuckGo 发送搜索请求并处理返回结果。

Llama

通过 Ollama 应用在本地运行 meta 公司的大模型 Llama 3.2。使之成为智能体的模型基座,用来处理与总结新闻内容。Llama 3.2 将作为专用的摘要生成智能体,处理从搜索结果中获取的文本信息,并生成精炼、易读的新闻摘要。

Streamlit

Streamlit 是一个开源的 Python 库,专为数据科学家和开发者设计,用于快速创建和分享美观的交互式数据应用。它通过简洁的 API,允许用户以极少的代码实现复杂的应用功能,无需前端开发知识。Streamlit 支持热重载,即代码一保存,应用即更新,极大地加快了开发和迭代过程。此外,它提供了丰富的内置组件和易于部署的特性,使得从数据可视化到机器学习模型演示都变得简单快捷,非常适合快速原型开发和结果展示。

工作流设计

整个流程将由三个智能体分工协作完成:

从 Searcher进行搜索 到 Synthesizer 进行合成,最后由 Summarizer 进行总结。

三个智能体的职责描述如下:

  1. Searcher:通过 DuckDuckGo 搜索与给定主题最相关且来源可靠的最新新闻,并以结构化格式返回结果。

  2. Synthesizer:分析提供的原始新闻报道,确定关键主题和重要信息,综合多种来源的信息,编写一份全面而简明的综述,注重事实,保持新闻的客观性,并以清晰、专业的写作风格呈现。

  3. Summarizer:以简洁明了的方式总结新闻,突出最重要的发展,包含关键利益相关者及其行动,添加相关数据,解释事件的重要性和直接影响,使用有力的动词和具体的描述,保持客观性,并在250-400字的段落中提供信息和吸引读者。

准备工作

下载 Llama3.2

通过 Ollama 下载 Llama3.2:

运行 Llama3.2 看看是否正常:

安装相关依赖项

创建如下 requirements.txt 文件:

git+https://github.com/openai/swarm.git  
streamlit   
duckduckgo-search  

在命令行执行命令 pip install -r requirements.txt,安装相关依赖项。

代码实现

智能体实现

Searcher
search_agent = Agent(  
    name="News Searcher",  
    instructions="""  
    You are a news search specialist. Your task is to:  
    1. Search for the most relevant and recent news on the given topic  
    2. Ensure the results are from reputable sources  
    3. Return the raw search results in a structured format  
    """,  
    functions=[search_news],  
    model=MODEL  
)  

创建新闻搜索智能体:

  • 专门用于新闻搜索

  • 专注于信誉良好的来源

  • 返回格式化的结果

Synthesizer
synthesis_agent = Agent(  
    name="News Synthesizer",  
    instructions="""  
    You are a news synthesis expert. Your task is to:  
    1. Analyze the raw news articles provided  
    2. Identify the key themes and important information  
    3. Combine information from multiple sources  
    4. Create a comprehensive but concise synthesis  
    5. Focus on facts and maintain journalistic objectivity  
    6. Write in a clear, professional style  
    Provide a 2-3 paragraph synthesis of the main points.  
    """,  
    model=MODEL  
)  

创建新闻合成智能体:

  • 分析多种来源

  • 确定关键主题

  • 创建连贯的叙述

Summarizer
summary_agent = Agent(  
    name="News Summarizer",  
    instructions="""  
    You are an expert news summarizer combining AP and Reuters style clarity with digital-age brevity.  
  
    Your task:  
    1. Core Information:  
       - Lead with the most newsworthy development  
       - Include key stakeholders and their actions  
       - Add critical numbers/data if relevant  
       - Explain why this matters now  
       - Mention immediate implications  
  
    2. Style Guidelines:  
       - Use strong, active verbs  
       - Be specific, not general  
       - Maintain journalistic objectivity  
       - Make every word count  
       - Explain technical terms if necessary  
  
    Format: Create a single paragraph of 250-400 words that informs and engages.  
    Pattern: [Major News] + [Key Details/Data] + [Why It Matters/What's Next]  
  
    Focus on answering: What happened? Why is it significant? What's the impact?  
  
    IMPORTANT: Provide ONLY the summary paragraph. Do not include any introductory phrases,   
    labels, or meta-text like "Here's a summary" or "In AP/Reuters style."  
    Start directly with the news content.  
    """,  
    model=MODEL  
)  

创建新闻摘要代理:

  • 美联社/路透社风格写作

  • 专业格式

  • 简明摘要

完整实现

import streamlit as st  
from duckduckgo_search import DDGS  
from swarm import Swarm, Agent  
from datetime import datetime  
from dotenv import load_dotenv  
  
# 加载环境变量  
load_dotenv()  
# 定义模型  
MODEL = "llama3.2:latest"  
# 初始化 Swarm 客户端  
client = Swarm()  
  
# 通过 Streamlit 创建用户界面,为页面和应用程序添加一个标题  
st.set_page_config(page_title="AI News Processor", page_icon="📰")  
st.title("📰 News Inshorts Agent")  
  
# 定义新闻搜索 Function,使用 DuckDuckGo 搜索 API,获取当月新闻,并返回结构化结果  
def search_news(topic):  
    """Search for news articles using DuckDuckGo"""  
    with DDGS() as ddg:  
        results = ddg.text(f"{topic} news {datetime.now().strftime('%Y-%m')}", max_results=3)  
        if results:  
            news_results = "\n\n".join([  
                f"Title: {result['title']}\nURL: {result['href']}\nSummary: {result['body']}"   
                for result in results  
            ])  
            return news_results  
        return f"No news found for {topic}."  
  
# 创建智能体  
search_agent = Agent(  
    name="News Searcher",  
    instructions="""  
    You are a news search specialist. Your task is to:  
    1. Search for the most relevant and recent news on the given topic  
    2. Ensure the results are from reputable sources  
    3. Return the raw search results in a structured format  
    """,  
    functions=[search_news],  
    model=MODEL  
)  
  
synthesis_agent = Agent(  
    name="News Synthesizer",  
    instructions="""  
    You are a news synthesis expert. Your task is to:  
    1. Analyze the raw news articles provided  
    2. Identify the key themes and important information  
    3. Combine information from multiple sources  
    4. Create a comprehensive but concise synthesis  
    5. Focus on facts and maintain journalistic objectivity  
    6. Write in a clear, professional style  
    Provide a 2-3 paragraph synthesis of the main points.  
    """,  
    model=MODEL  
)  
  
summary_agent = Agent(  
    name="News Summarizer",  
    instructions="""  
    You are an expert news summarizer combining AP and Reuters style clarity with digital-age brevity.  
  
    Your task:  
    1. Core Information:  
       - Lead with the most newsworthy development  
       - Include key stakeholders and their actions  
       - Add critical numbers/data if relevant  
       - Explain why this matters now  
       - Mention immediate implications  
  
    2. Style Guidelines:  
       - Use strong, active verbs  
       - Be specific, not general  
       - Maintain journalistic objectivity  
       - Make every word count  
       - Explain technical terms if necessary  
  
    Format: Create a single paragraph of 250-400 words that informs and engages.  
    Pattern: [Major News] + [Key Details/Data] + [Why It Matters/What's Next]  
  
    Focus on answering: What happened? Why is it significant? What's the impact?  
  
    IMPORTANT: Provide ONLY the summary paragraph. Do not include any introductory phrases,   
    labels, or meta-text like "Here's a summary" or "In AP/Reuters style."  
    Start directly with the news content.  
    """,  
    model=MODEL  
)  
  
# 实施新闻处理工作流程,按顺序处理,显示进度指标  
def process_news(topic):  
    """Run the news processing workflow"""  
    with st.status("Processing news...", expanded=True) as status:  
        # Search  
        status.write("🔍 Searching for news...")  
        search_response = client.run(  
            agent=search_agent,  
            messages=[{"role": "user", "content": f"Find recent news about {topic}"}]  
        )  
        raw_news = search_response.messages[-1]["content"]  
          
        # Synthesize  
        status.write("🔄 Synthesizing information...")  
        synthesis_response = client.run(  
            agent=synthesis_agent,  
            messages=[{"role": "user", "content": f"Synthesize these news articles:\n{raw_news}"}]  
        )  
        synthesized_news = synthesis_response.messages[-1]["content"]  
          
        # Summarize  
        status.write("📝 Creating summary...")  
        summary_response = client.run(  
            agent=summary_agent,  
            messages=[{"role": "user", "content": f"Summarize this synthesis:\n{synthesized_news}"}]  
        )  
        return raw_news, synthesized_news, summary_response.messages[-1]["content"]  
  
# 用户交互界面  
topic = st.text_input("Enter news topic:", value="artificial intelligence")  
if st.button("Process News", type="primary"):  
    if topic:  
        try:  
            raw_news, synthesized_news, final_summary = process_news(topic)  
            st.header(f"📝 News Summary: {topic}")  
            st.markdown(final_summary)  
        except Exception as e:  
            st.error(f"An error occurred: {str(e)}")  
    else:  
        st.error("Please enter a topic!")  

运行效果

在命令行运行 streamlit run news_agent.py

Streamlit 应用网址 http://localhost:8501 会自动打开:

输入主题,并点击处理 Process News 按钮后,会得到搜索概述结果:

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值