
隨著人工智慧的發展及其與社交媒體的融合,它無疑有助於創造有價值的內容。由於人工智慧減少了人際互動,這種融合的後果是人們的注意力持續時間縮短。因此,問題在於,如何在創作能夠提升參與度的內容的同時,吸引讀者的全神貫注?
答案是新聞通訊。
在本文中,我將向您展示如何建立您自己的新聞通訊 AI 代理。
為什麼要傳送簡報?
簡報是一種定期分發的出版物。它通常透過電子郵件傳送,用於與特定受眾分享最新動態、見解或定製內容。簡報的主要目的是:
- 分享資訊:企業或組織使用簡報來發布公司最新動態和發展動態(例如產品釋出、部落格更新、活動)。
- 推廣:公司向目標受眾傳送簡報,以一種微妙的、建立關係的方式推廣產品、服務或課程。
- 互動:包含推廣內容的社羣首先會透過簡報分享相關內容來與受眾互動,因此,一旦受眾分享了推廣內容,他們很可能會忽略它。
轉變:從手動到自主的內容創作
自誕生以來,新聞通訊一直遵循著相同的模式,那就是有人會花費數小時收集連結並總結內容。這些郵件除了收件人姓名之外,幾乎沒有個性化內容,而且難以針對小眾受眾進行擴充套件。但情況正在迅速改變。
自從我們採用 Agentic 工作流程以來,我們不僅朝著生成個性化內容的目標邁進了一步,而且還實現了自動化。透過結合 LLM 和 Agentic 工作流程,我們不僅可以制定內容策略,還可以做出決策並執行任務,而無需任何常規輸入。
構建新聞通訊AI代理的專案計劃
讓我們嘗試直觀地瞭解一下 AI 驅動的新聞通訊代理的工作原理:

- 提示:第一步,您將透過建立每週 AI 綜述來展現您的意圖。
- 目標設定:下一步,我們將透過定義我們想要的新聞簡報型別來設定預期。
- 執行計劃:接下來是代理的角色,它會搜尋資訊來源、總結見解並進行格式化。
- 輸出:最後,它會整理最終的新聞簡報,準備傳送。
開始構建:您的首個新聞通訊AI代理
現在您已經瞭解了代理工作流對於制定新聞通訊策略和自動化的重要性,讓我們繼續探索“如何”實現。在本文中,我們將構建一個簡單的 AI 驅動工作流,該工作流可自動從新聞文章的 CSV 資料集建立新聞通訊。讓我們開始吧。
步驟 1:提取包含多條新聞條目的CSV檔案
首先,我們需要讀取包含新聞文章的 CSV 檔案。CSV 檔案的結構應為每行包含新聞文章的詳細資訊,例如標題、內容和其他後設資料。我們可以使用 Pandas 庫來提取和操作這些資料。
提取 CSV 的程式碼:
# Load the CSV file containing news articles
def load_news_csv(file_path: str):
df = pd.read_csv(file_path)
news_data = load_news_csv("news_articles.csv")
print(news_data.head()) # Display first few rows of the dataset
import pandas as pd
# Load the CSV file containing news articles
def load_news_csv(file_path: str):
df = pd.read_csv(file_path)
return df
# Example usage
news_data = load_news_csv("news_articles.csv")
print(news_data.head()) # Display first few rows of the dataset
import pandas as pd
# Load the CSV file containing news articles
def load_news_csv(file_path: str):
df = pd.read_csv(file_path)
return df
# Example usage
news_data = load_news_csv("news_articles.csv")
print(news_data.head()) # Display first few rows of the dataset
在此步驟中,我們讀取 CSV 檔案並將其儲存在 DataFrame 中。現在可以處理資料集以提取新聞稿的相關文章。
步驟 2:根據AI相關關鍵詞或主題篩選和評分每篇文章
現在我們有了文章,我們需要分析其中與 AI 相關的關鍵詞或主題。這些關鍵詞可能包括“機器學習”、“人工智慧”、“神經網路”等。程式碼中的 AIContentFilter 類就是用於此目的的。它會分析每篇文章的內容,以確定其是否與 AI/ML/資料科學相關。
"""AI Content Filter with multiple compatibility modes"""
def __init__(self, openai_api_key: str):
self.openai_api_key = openai_api_key
# Setup for LangChain or keyword-based filtering
'ai', 'artificial intelligence', 'machine learning', 'deep learning',
'neural network', 'chatgpt', 'claude', 'gemini', 'openai', 'anthropic'
def keyword_analysis(self, content: str) -> bool:
"""Keyword-based analysis for AI-related topics"""
content_lower = content.lower()
return any(keyword in content_lower for keyword in self.ai_keywords)
def filter_articles(self, df: pd.DataFrame) -> pd.DataFrame:
"""Filter articles based on AI-related keywords"""
return df[df['content'].apply(self.keyword_analysis)]
# Filter the articles in the dataset
ai_filter = AIContentFilter(openai_api_key="your-openai-api-key")
filtered_articles = ai_filter.filter_articles(news_data)
print(filtered_articles.head()) # Show filtered AI-related articles
class AIContentFilter:
"""AI Content Filter with multiple compatibility modes"""
def __init__(self, openai_api_key: str):
self.openai_api_key = openai_api_key
# Setup for LangChain or keyword-based filtering
self.mode = "keyword"
self.ai_keywords = [
'ai', 'artificial intelligence', 'machine learning', 'deep learning',
'neural network', 'chatgpt', 'claude', 'gemini', 'openai', 'anthropic'
]
def keyword_analysis(self, content: str) -> bool:
"""Keyword-based analysis for AI-related topics"""
content_lower = content.lower()
return any(keyword in content_lower for keyword in self.ai_keywords)
def filter_articles(self, df: pd.DataFrame) -> pd.DataFrame:
"""Filter articles based on AI-related keywords"""
return df[df['content'].apply(self.keyword_analysis)]
# Filter the articles in the dataset
ai_filter = AIContentFilter(openai_api_key="your-openai-api-key")
filtered_articles = ai_filter.filter_articles(news_data)
print(filtered_articles.head()) # Show filtered AI-related articles
class AIContentFilter:
"""AI Content Filter with multiple compatibility modes"""
def __init__(self, openai_api_key: str):
self.openai_api_key = openai_api_key
# Setup for LangChain or keyword-based filtering
self.mode = "keyword"
self.ai_keywords = [
'ai', 'artificial intelligence', 'machine learning', 'deep learning',
'neural network', 'chatgpt', 'claude', 'gemini', 'openai', 'anthropic'
]
def keyword_analysis(self, content: str) -> bool:
"""Keyword-based analysis for AI-related topics"""
content_lower = content.lower()
return any(keyword in content_lower for keyword in self.ai_keywords)
def filter_articles(self, df: pd.DataFrame) -> pd.DataFrame:
"""Filter articles based on AI-related keywords"""
return df[df['content'].apply(self.keyword_analysis)]
# Filter the articles in the dataset
ai_filter = AIContentFilter(openai_api_key="your-openai-api-key")
filtered_articles = ai_filter.filter_articles(news_data)
print(filtered_articles.head()) # Show filtered AI-related articles
步驟 3:應用閾值以識別最相關的文章
篩選文章後,我們可能需要應用閾值,以僅包含高度相關的文章。例如,我們可以根據文章中與 AI 相關的關鍵詞的數量來設定置信度分數。分數越高,文章的相關性就越高。
應用閾值的程式碼:
def apply_relevance_threshold(df: pd.DataFrame, threshold: int = 3) -> pd.DataFrame:
"""Apply threshold to select only the most relevant articles"""
df['relevance_score'] = df['content'].apply(lambda x: sum(keyword in x.lower() for keyword in ai_filter.ai_keywords))
return df[df['relevance_score'] >= threshold]
# Apply threshold to filtered articles
relevant_articles = apply_relevance_threshold(filtered_articles, threshold=3)
print(relevant_articles.head()) # Display most relevant articles
def apply_relevance_threshold(df: pd.DataFrame, threshold: int = 3) -> pd.DataFrame:
"""Apply threshold to select only the most relevant articles"""
df['relevance_score'] = df['content'].apply(lambda x: sum(keyword in x.lower() for keyword in ai_filter.ai_keywords))
return df[df['relevance_score'] >= threshold]
# Apply threshold to filtered articles
relevant_articles = apply_relevance_threshold(filtered_articles, threshold=3)
print(relevant_articles.head()) # Display most relevant articles
def apply_relevance_threshold(df: pd.DataFrame, threshold: int = 3) -> pd.DataFrame:
"""Apply threshold to select only the most relevant articles"""
df['relevance_score'] = df['content'].apply(lambda x: sum(keyword in x.lower() for keyword in ai_filter.ai_keywords))
return df[df['relevance_score'] >= threshold]
# Apply threshold to filtered articles
relevant_articles = apply_relevance_threshold(filtered_articles, threshold=3)
print(relevant_articles.head()) # Display most relevant articles
步驟 4:使用語言學習模型 (LLM) 生成篩選後文章的摘要
現在我們已經獲得了最相關的文章,下一步就是生成這些文章的摘要。我們將使用語言學習模型 (LLM) 來實現此目的。在提供的程式碼中,我們使用 LangChain 包中的 ChatOpenAI 類與 OpenAI 的模型進行互動,以對文章進行摘要。
生成文章摘要的程式碼:
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
def generate_summary(content: str, openai_api_key: str) -> str:
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1, api_key=openai_api_key)
prompt = f"Summarize the following article:\n\n{content}"
return response['choices'][0]['text'].strip()
# Generate summaries for the filtered articles
relevant_articles['summary'] = relevant_articles['content'].apply(lambda x: generate_summary(x, openai_api_key="your-openai-api-key"))
print(relevant_articles[['title', 'summary']].head()) # Display article summaries
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
def generate_summary(content: str, openai_api_key: str) -> str:
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1, api_key=openai_api_key)
prompt = f"Summarize the following article:\n\n{content}"
response = llm(prompt)
return response['choices'][0]['text'].strip()
# Generate summaries for the filtered articles
relevant_articles['summary'] = relevant_articles['content'].apply(lambda x: generate_summary(x, openai_api_key="your-openai-api-key"))
print(relevant_articles[['title', 'summary']].head()) # Display article summaries
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
def generate_summary(content: str, openai_api_key: str) -> str:
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1, api_key=openai_api_key)
prompt = f"Summarize the following article:\n\n{content}"
response = llm(prompt)
return response['choices'][0]['text'].strip()
# Generate summaries for the filtered articles
relevant_articles['summary'] = relevant_articles['content'].apply(lambda x: generate_summary(x, openai_api_key="your-openai-api-key"))
print(relevant_articles[['title', 'summary']].head()) # Display article summaries
步驟 5:將選定內容格式化為可傳送的新聞稿佈局
最後,我們需要將選定內容格式化為適合作為新聞稿傳送的佈局。這可以使用 Markdown 或 HTML 格式,具體取決於您的偏好。以下示例展示瞭如何將選定文章及其摘要格式化為 Markdown 格式。
內容格式化程式碼:
def format_newsletter(articles_df: pd.DataFrame) -> str:
"""Format the selected articles into a newsletter (Markdown)"""
newsletter_content = "# AI News Newsletter\n\n"
for _, row in articles_df.iterrows():
newsletter_content += f"## {row['title']}\n\n"
newsletter_content += f"**Summary**: {row['summary']}\n\n"
newsletter_content += "----\n"
return newsletter_content
# Format the relevant articles into a newsletter
newsletter = format_newsletter(relevant_articles)
print(newsletter) # Display the formatted newsletter
def format_newsletter(articles_df: pd.DataFrame) -> str:
"""Format the selected articles into a newsletter (Markdown)"""
newsletter_content = "# AI News Newsletter\n\n"
for _, row in articles_df.iterrows():
newsletter_content += f"## {row['title']}\n\n"
newsletter_content += f"**Summary**: {row['summary']}\n\n"
newsletter_content += "----\n"
return newsletter_content
# Format the relevant articles into a newsletter
newsletter = format_newsletter(relevant_articles)
print(newsletter) # Display the formatted newsletter
def format_newsletter(articles_df: pd.DataFrame) -> str:
"""Format the selected articles into a newsletter (Markdown)"""
newsletter_content = "# AI News Newsletter\n\n"
for _, row in articles_df.iterrows():
newsletter_content += f"## {row['title']}\n\n"
newsletter_content += f"**Summary**: {row['summary']}\n\n"
newsletter_content += "----\n"
return newsletter_content
# Format the relevant articles into a newsletter
newsletter = format_newsletter(relevant_articles)
print(newsletter) # Display the formatted newsletter
透過這些步驟,我們實現了 AI 驅動的新聞通訊內容的自動化建立。
輸出:
以下是篩選所有新聞後建立的新聞通訊:

小結
在這個內容氾濫的世界裡,新聞通訊仍然是策劃有意義內容的重要來源。我們在本文章中介紹的內容只是冰山一角;如何利用 LLM 和 Agentic 工作框架將手動任務轉變為可擴充套件的智慧系統。隨著這些工具越來越普及,使其更具可擴充套件性和個性化的能力不再侷限於大型團隊或開發人員。您可以根據自己的需求進行嘗試和定製。
評論留言