微信扫码
添加专属顾问
我要投稿
对新闻文章进行标签化,并将新闻内容和标签向量化存储。
通过用户画像,捕捉用户的阅读历史和行为习惯,如点赞、收藏、点击的文章等。
根据用户画像和行为在文章库中进行初步搜索(召回)。
对召回结果进行精排,考虑用户兴趣变化、文章来源、时效性等因素。
输出Top 10的个性化推荐文章。
接下来,我们将详细说明各个步骤的技术细节及实现方案。
首先,我们需要读取新闻数据文件 news.tsv,并将其转换为Pandas DataFrame格式。
import pandas as pd# 读取新闻数据news_df = pd.read_csv('news.tsv', sep='\t', header=None, names=['news_id', 'category', 'subcategory', 'title', 'abstract', 'url', 'title_entities', 'abstract_entities'])# 查看前几行数据print(news_df.head())
接下来,我们需要读取用户行为日志文件 behaviors.tsv,并将其转换为Pandas DataFrame格式。
# 读取用户行为日志behavior_df = pd.read_csv('behaviors.tsv', sep='\t', header=None, names=['impression_id', 'user_id', 'time', 'history', 'impressions'])# 查看前几行数据print(behavior_df.head())
from qwen import QwenClient# 初始化通义千问客户端client = QwenClient(api_key='YOUR_API_KEY')def tag_articles(articles, client):tagged_articles = []for article in articles:prompt = f"为以下文章生成标签:\n类别: {article['category']}, 子类别: {article['subcategory']}, 标题: {article['title']}, 摘要: {article['abstract']}\n请生成相关的标签。"# 调用通义千问生成标签response = client.generate(prompt)tags = response.strip().split(',')tagged_articles.append({**article, 'tags': tags})return tagged_articles
3.1.2 应用打标签函数
# 示例:为新闻文章打标签tagged_news_df = tag_articles(news_df.to_dict('records'), client)tagged_news_df = pd.DataFrame(tagged_news_df)# 查看带有标签的新闻数据print(tagged_news_df.head())
def generate_user_profile(user_history, client):# 将用户点击过的新闻详细信息拼接成一段长文本history_news = tagged_news_df[tagged_news_df['news_id'].isin(user_history)]history_text = ' '.join(history_news['title'] + ' ' + history_news['abstract'].fillna(''))# 设计promptprompt = f"基于以下用户点击历史,生成一个用户画像,描述用户感兴趣的新闻主题和地区。\n点击历史:\n{history_text}\n请描述用户的兴趣主题和关注地区。"# 调用通义千问生成用户画像response = client.generate(prompt)user_profile = response.strip() return user_profile3.2.2 应用生成用户画像函数
# 示例:生成用户画像user_history = ['N1', 'N2', 'N3']# 假设这是某个用户的点击历史user_profile = generate_user_profile(user_history, client)# 输出用户画像print(user_profile)
def recall_candidate_news(user_profile, tagged_news_df, client):# 设计promptprompt = f"基于以下用户画像,从新闻库中召回一批与用户兴趣匹配的新闻。\n用户画像:\n{user_profile}\n新闻库:\n{tagged_news_df.to_json(orient='records')}\n请从新闻库中选出与用户兴趣匹配的新闻。"# 调用通义千问召回候选新闻response = client.generate(prompt)candidate_news_ids = response.strip().split('\n')# 获取召回的候选新闻candidate_news = tagged_news_df[tagged_news_df['news_id'].isin(candidate_news_ids)] return candidate_news3.3.2 应用召回候选新闻函数
将召回候选新闻函数应用于用户画像,获取候选新闻。
# 示例:召回候选新闻candidate_news = recall_candidate_news(user_profile, tagged_news_df, client)# 输出召回的候选新闻print(candidate_news)
设计一个合适的Prompt,告诉通义千问或其他大模型我们需要对候选新闻进行评分和排序。
def score_and_rank_news(user_profile, candidate_news, client):# 设计promptcandidate_news_json = candidate_news.to_json(orient='records')prompt = f"基于以下用户画像,对以下候选新闻进行评分,并按评分从高到低排序。\n用户画像:\n{user_profile}\n候选新闻:\n{candidate_news_json}\n请对这些候选新闻进行评分,并按评分从高到低排序。"# 调用通义千问进行评分和排序response = client.generate(prompt)scored_news = json.loads(response.strip())# 将评分结果转换为DataFramescored_news_df = pd.DataFrame(scored_news)scored_news_df = scored_news_df.sort_values(by='score', ascending=False) return scored_news_df3.4.2 应用评分和排序函数
# 示例:评分和排序scored_news = score_and_rank_news(user_profile, candidate_news, client)# 输出评分和排序后的新闻列表print(scored_news)
最后,我们将排序后的新闻列表展示给用户,输出Top 10的新闻。
# 输出推荐结果top_n = 10recommended_news = scored_news.head(top_n)# 输出推荐的Top 10新闻print(recommended_news[['news_id', 'title', 'score']])
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-30
Cursor 2.0的一些有趣的新特性
2025-10-30
Anthropic 发布最新研究:LLM 展现初步自省迹象
2025-10-30
让Agent系统更聪明之前,先让它能被信任
2025-10-30
Rag不行?谷歌DeepMind同款,文档阅读新助手:ReadAgent
2025-10-29
4大阶段,10个步骤,助你高效构建企业级智能体(Agent)
2025-10-29
DocReward:让智能体“写得更专业”的文档奖励模型
2025-10-29
沃尔沃RAG实战:企业级知识库,早就该放弃小分块策略
2025-10-29
大模型的Funcation Calling是什么?
2025-08-21
2025-08-21
2025-08-19
2025-09-16
2025-10-02
2025-09-08
2025-09-17
2025-08-19
2025-09-29
2025-08-20
2025-10-29
2025-10-29
2025-10-28
2025-10-28
2025-10-27
2025-10-26
2025-10-25
2025-10-23