使用 Swarm 构建多智能体新闻助理
本文将带你构建一个多智能体新闻助理,利用 OpenAI 的 Swarm 框架和 Llama 3.2 来自动化新闻处理工作流。在本地运行环境下,我们将实现一个多智能体系统,让不同的智能体各司其职,分步完成新闻搜索、信息综合与摘要生成等任务,而无需付费使用外部服务。
应用设计
相关技术
Swarm
OpenAI Swarm 是一个新兴的多智能体协作框架,旨在通过集成强化学习和分布式计算的优势来解决复杂问题。该框架允许多个智能体在共享环境中同时交互与学习,彼此共享信息,从而提高决策质量和学习效率。Swarm 框架采用先进的强化学习算法,使智能体能够在动态环境中持续适应和优化策略,同时通过分布式计算高效利用资源,加速训练过程。它具有很强的可扩展性,能够根据需求增加智能体数量,适应不同规模和复杂度的任务。应用场景广泛,包括智能交通系统、机器人群体和游戏开发等,OpenAI Swarm 为实现更高效的智能系统提供了灵活且强大的解决方案,推动各领域的智能化进程。
Swarm 的详细介绍可参考之前的文章:《通过 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 进行总结。
三个智能体的职责描述如下:
- Searcher:通过 DuckDuckGo 搜索与给定主题最相关且来源可靠的最新新闻,并以结构化格式返回结果。
- Synthesizer:分析提供的原始新闻报道,确定关键主题和重要信息,综合多种来源的信息,编写一份全面而简明的综述,注重事实,保持新闻的客观性,并以清晰、专业的写作风格呈现。
- 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 按钮后,会得到搜索概述结果:
参考文献
- https://mp.weixin.qq.com/s/0k43d3GNnW01zoE9WL9lLg
- https://streamlit.io/
- https://github.com/openai/swarm
- https://pypi.org/project/duckduckgo-search/
- https://ollama.com/library/llama3.2
文章来自于微信公众号“Tech For Fun”,作者“kaelzhang”
全部评论
留言在赶来的路上...
发表评论