使用 Haystack & OPEA 总结 Hacker News 博文
构建一个 RAG 管道,以获取实时的 Hacker News 博文并使用本地 LLM 端点进行总结
2025 年 6 月 10 日欢迎阅读本分步教程,我们将使用 Haystack 和 OPEA 构建一个简单的检索增强生成 (RAG) 管道。我们将获取最新的 Hacker News 博文,将它们输入到一个轻量级的 LLM 端点 (OPEAGenerator),并生成简洁的单句摘要(基于此 笔记本)。让我们开始吧!🎉
1. 引言与动机
在现代生成式 AI 应用中,拥有一个灵活、高性能且可扩展的平台至关重要。OPEA (Open Platform for Enterprise AI,企业 AI 开放平台) 是一个开放的、模型无关的框架,用于构建和操作可组合的生成式 AI 解决方案。它提供了
- 一系列微服务(LLM、数据存储、提示词引擎)和高阶宏服务,支持端到端工作流
- 基于 HTTP 的推理,支持多模型(开源和闭源)
- 高级功能,如批处理、流式传输、自动扩展、通过网关路由以及统一的可观测性
在此演示中,我们将在 Haystack 管道中使用 OPEA LLM 端点,为您提供
- 即时 HTTP 访问任何托管模型。
- 从小型原型到生产级 RAG 解决方案的无缝切换。
在本教程中,我们将构建一个简单的 RAG 管道,该管道将获取最新的 Hacker News 博文,将其发送到一个本地 OPEA 端点,该端点运行 Qwen/Qwen2.5-7B-Instruct 演示模型,并生成简洁的单句摘要。当然,您可以将我们的示例模型替换为任何其他由 OPEA 服务的模型,使此模式既适用于原型设计的轻量级,又适用于实际部署的强大功能。让我们开始吧!🚀
2. 先决条件
请确保您已安装
- Python 3.9+
- 安装依赖项:
pip install haystack-ai haystack-opea newspaper3k lxml[html_clean] - 一个运行中的 OPEA 端点,地址为 https://:9000/v1 (或您自己的端点)
注意: 作为参考,这里有一个 Docker Compose 示例,可供您开始。OPEA LLM 服务可以配置为使用各种模型服务后端,如 TGI、vLLM、ollama、OVMS……并提供经过验证的运行时设置,以在包括 Intel Gaudi 在内的各种硬件上获得良好的性能。在此示例中,它会创建一个带有 TGI 后端的 OPEA LLM 服务。有关 LLM 生成 的文档,请参阅。该代码基于 OPEA LLM 示例 和 OPEA TGI 示例。
要运行,请调用
LLM_MODEL_ID=Qwen/Qwen2.5-7B-Instruct docker compose up。
3. 构建新闻获取器
我们将创建一个自定义的 Haystack 组件 HackernewsNewestFetcher,它将
- 调用 Hacker News API 获取最新的故事 ID
- 过滤出包含 URL 的帖子
- 使用
newspaper3k下载和解析每个文章。 - 将结果包装成 Haystack
Document对象
from typing import List
from haystack import component, Document
from newspaper import Article
import requests
@component
class HackernewsNewestFetcher():
@component.output_types(documents=List[Document])
def run(self, last_k: int):
# Fetch the IDs of the newest stories
newest_list = requests.get(
url='https://hacker-news.firebaseio.com/v0/newstories.json?print=pretty'
)
url_list = []
# Keep only the first `last_k` IDs with URLs
for id in newest_list.json()[0:last_k]:
article = requests.get(
url=f"https://hacker-news.firebaseio.com/v0/item/{id}.json?print=pretty"
)
if 'url' in article.json():
url_list.append(article.json()['url'])
docs = []
# Download and parse each article
for url in url_list:
try:
article = Article(url)
article.download()
article.parse()
docs.append(
Document(
content=article.text,
meta={'title': article.title, 'url': url}
)
)
except Exception:
print(f"Couldn't download {url}, skipped")
return {"documents": docs}
4. 集成 LLM (OPEAGenerator)
我们使用 OPEAGenerator 通过 HTTP 调用我们的 LLM。在这里,我们指向一个本地端点,该端点服务于 Qwen/Qwen2.5-7B-Instruct 模型
from haystack_opea import OPEAGenerator
llm = OPEAGenerator(
"https://:9000/v1", # Your OPEA endpoint
"Qwen/Qwen2.5-7B-Instruct", # Model name
model_arguments={"max_tokens": 2000} # Generation settings
)
5. 编写提示词
使用 PromptBuilder,我们定义了一个 Jinja 风格的模板,它将
- 列出每篇文章的标题、内容和 URL。
- 要求模型提供单句摘要以及 URL。
from haystack.components.builders import PromptBuilder
prompt_template = """
You will be provided a few of the latest posts in HackerNews, followed by their URL.
For each post, provide a one sentence summary, followed by the original post URL.
Posts:
{% for doc in documents %}
{{doc.meta['title']}}:
{{doc.content}}
URL: {{doc.meta['url']}}
{% endfor %}
"""
prompt_builder = PromptBuilder(template=prompt_template)
6. 构建管道
我们将组件连接到一个 Pipeline 中
from haystack import Pipeline
# Instantiate components
fetcher = HackernewsNewestFetcher()
# Build and connect
pipe = Pipeline()
pipe.add_component("hackernews_fetcher", fetcher)
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)
# Define data flow
pipe.connect("hackernews_fetcher.documents", "prompt_builder.documents")
pipe.connect("prompt_builder.prompt", "llm.prompt")
# Visualize the pipeline
pipe.show() # Interactive usage
pipe.draw(pipeline.png) # Plotting to file
7. 运行管道
获取并总结最新的 2 篇 Hacker News 博文
result = pipe.run(data={"hackernews_fetcher": {"last_k": 2}})
print(result['llm']['replies'][0])
8. 结果
A course on using Large Language Models (LLMs) to understand and structure search queries without relying on external services is being offered, demonstrating how LLMs can significantly improve and automate search capabilities.
[URL: https://softwaredoug.com/blog/2025/04/08/llm-query-understand]
The European Commission's new ProtectEU security strategy proposes enhanced tools for law enforcement, including methods to access encrypted data, raising significant concerns about potential violations of privacy and civil liberties.
[URL: https://www.cloudwards.net/news/protecteu-security-strategy-raises-encryption-concerns/]
优美、简洁的摘要,只需几秒钟!✨
9. 结论
在本教程中,我们构建了一个完整的 RAG 管道,其中包括
- 用于 Hacker News 的自定义新闻获取器。
- 通过
OPEAGenerator实现轻量级 LLM 集成。 - 用于结构化输入的 Jinja 模板化提示词。
- Haystack 管道编排。
随意扩展此设置,添加更高级的检索、缓存或不同的 LLM 后端。编码愉快!🛠️🔥
