In today's fast-paced world, keeping up with the latest news in a particular field can be challenging. What if we could harness the power of generative AI and agents to create a personalized news aggregator that runs entirely on local machines? In this article, we'll explore how to useOllamaof the Llama 3.2 model,SwarmPerform proxy scheduling, andDuckDuckGoConduct a web search to build such a system.
The Power of Local AI
With the rise of large-scale language models, we are now able to run complex AI systems on our personal computers. This opens up endless possibilities for creating tools that are customized for our specific needs. Our news aggregator is a perfect example of this potential.
Components of our system
- Ollama with Llama 3.2 : This is the heart of our system, powering our AI agents.
- Swarm : An agent orchestration framework that allows us to create and manage multiple AI agents.
- DuckDuckGo Search :: Provide up-to-date web search results without tracking user data.
Working Principle
Our news aggregator consists of two main AI agents:
- News Assistant : Use DuckDuckGo search to get the latest news articles on specific topics.
- Editorial Assistant : Review and refine the news collected for final presentation.
Let's break down the workflow:
1. Setting up the environment
ollama pull llama3.2
export OPENAI_MODEL_NAME=llama3.2
export OPENAI_BASE_URL=http://localhost:11434/v1
export OPENAI_API_KEY=any
pip install git+https://github.com/openai/swarm.git duckduckgo-search
We start by importing the necessary libraries and initializing our Swarm Client:
from duckduckgo_search import DDGS
from swarm import Swarm, Agent
from datetime import datetime
current_date = datetime.now().strftime("%Y-%m")
client = Swarm()
2. Creation of a news search function
We define a function to search for news using DuckDuckGo:
pythondef get_news_articles(topic):
ddg_api = DDGS()
results = ddg_api.text(f"{topic} {current_date}", max_results=5)
if results:
news_results = "\n\n".join([f"Title: {result['title']}\nURL: {result['href']}\nDescription: {result['body']}" for result in results])
return news_results
else:
return f"Could not find news results for {topic}."
3. Defining our AI agents
We create two agents using Ollama's Llama 3.2 model:
news_agent = Agent(
model="llama3.2",
name="News Assistant",
instructions="You provide the latest news articles for a given topic using DuckDuckGo search.",
functions=[get_news_articles],
)
editor_agent = Agent(
model="llama3.2",
name="Editor Assistant",
instructions="You review and finalise the news article for publishing.",
)
4. Harmonization of workflows
We define a function to run our news aggregation workflow:
def run_news_workflow(topic):
# Fetch news
news_response = client.run(
agent=news_agent,
messages=[{"role": "user", "content": f"Get me the news about {topic} on {current_date}"}],
)
raw_news = news_response.messages[-1]["content"]
# Pass news to editor for final review
edited_news_response = client.run(
agent=editor_agent,
messages=[{"role": "system", "content": raw_news}],
)
print(f"{edited_news_response.messages[-1]['content']}")
5. Operational systems
Finally, we can run our news aggregator for any topic of interest:
run_news_workflow("AI in Drug Discovery")
Full code : app.py
from duckduckgo_search import DDGS
from swarm import Swarm, Agent
from datetime import datetime
current_date = datetime.now().strftime("%Y-%m")
# 初始化 Swarm 客户端
client = Swarm()
# 1. 创建互联网搜索工具
def get_news_articles(topic):
print(f"正在为 {topic} 进行 DuckDuckGo 新闻搜索...")
# DuckDuckGo 搜索
ddg_api = DDGS()
results = ddg_api.text(f"{topic} {current_date}", max_results=5)
if results:
news_results = "\n\n".join([f"标题: {result['title']}\n网址: {result['href']}\n描述: {result['body']}" for result in results])
return news_results
else:
return f"未能找到关于 {topic} 的新闻结果。"
# 2. 创建 AI 代理
def transfer_to_editor_assistant(raw_news):
print("将文章传递给编辑助手...")
return editor_agent.run({"role": "system", "content": raw_news})
# 新闻代理以获取新闻
news_agent = Agent(
model="llama3.2",
name="新闻助手",
instructions="您提供有关给定主题的最新新闻文章,使用 DuckDuckGo 搜索。",
functions=[get_news_articles],
)
# 编辑代理以编辑新闻
editor_agent = Agent(
model="llama3.2",
name="编辑助手",
instructions="您审阅并最终确定新闻文章以供发布。",
)
# 3. 创建工作流程
def run_news_workflow(topic):
print("运行新闻代理工作流程...")
# 第一步: 获取新闻
news_response = client.run(
agent=news_agent,
messages=[{"role": "user", "content": f"获取关于 {topic} 在 {current_date} 的新闻"}],
)
raw_news = news_response.messages[-1]["content"]
print(f"获取的新闻: {raw_news}")
# 第二步: 将新闻传递给编辑进行最终审查
edited_news_response = client.run(
agent=editor_agent,
messages=[{"role": "system", "content": raw_news}],
)
print(f"{edited_news_response.messages[-1]['content']}")
# 运行给定主题的新闻工作流程示例
run_news_workflow("药物发现中的 AI")
sample output
Running news Agent workflow...
Running DuckDuckGo news search for AI in Drug Discovery...
Fetched news: Here's a formatted answer based on the news articles:
**药物发现中的人工智能:革命性的转变**
人工智能(AI)在药物发现中的作用标志着制药领域的革命性转变。AI利用复杂的算法进行自主决策,从数据分析中增强人类能力,而不是取代它们。
**挑战与局限性**
尽管有着令人期待的进展,但在该领域中仍然存在挑战和局限性。论文《AI在药物发现中的作用》探讨了这些问题,强调了高质量数据的必要性、伦理问题的解决以及对基于AI的方法局限性的认识。
**AI在药物发现中的应用**
AI有潜力在药物发现、设计和研究药物间相互作用中发挥关键作用。AI在药物发现中的应用包括:
* 多靶点药理学:AI可以预测化合物对多种疾病的有效性。
* 化学合成:AI可以优化化学合成过程,以实现更快和更高效的生产。
* 药物重定位:AI可以识别现有药物的新用途。
* 预测药物特性:AI可以预测化合物的效力、毒性和物理化学特性。
**药物发现中AI的未来**
随着AI的不断发展,预计将对制药行业产生重大影响。AI的成功应用将依赖于高质量数据的可用性、伦理问题的解决以及对基于AI的方法局限性的认识。
Benefits of Local AI News Syndication
- private business : All processing takes place on your local machine, ensuring that your data stays in your own hands.
- customization : You can easily modify an agent's instructions or add a new agent to meet your specific needs.
- Latest Information : By searching with DuckDuckGo, you always get the latest news on the topic of your choice.
- AI-driven curation : The editorial assistant helps refine and organize the collected news to provide a more polished final output.
reach a verdict
This local AI-powered news aggregator demonstrates the potential of combining large-scale language models with web search capabilities. By utilizing Ollama's Llama 3.2 model, Swarm for agent orchestration, and DuckDuckGo for search, we have created a powerful tool that allows us to stay informed on any topic of interest while maintaining our privacy and running entirely on our local computers.
As AI continues to evolve, the possibilities for creating personalized, AI-powered tools will only continue to expand. This news aggregator is just the beginning - imagine what other innovative applications you could build with these technologies!