对新闻文章进行标签化,并将新闻内容和标签向量化存储。
通过用户画像,捕捉用户的阅读历史和行为习惯,如点赞、收藏、点击的文章等。
根据用户画像和行为在文章库中进行初步搜索(召回)。
对召回结果进行精排,考虑用户兴趣变化、文章来源、时效性等因素。
输出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})returntagged_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())
defgenerate_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()returnuser_profile3.2.2 应用生成用户画像函数
# 示例:生成用户画像user_history = ['N1', 'N2', 'N3']# 假设这是某个用户的点击历史user_profile = generate_user_profile(user_history, client)# 输出用户画像print(user_profile)
defrecall_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)]returncandidate_news3.3.2 应用召回候选新闻函数
将召回候选新闻函数应用于用户画像,获取候选新闻。
# 示例:召回候选新闻candidate_news = recall_candidate_news(user_profile, tagged_news_df, client)# 输出召回的候选新闻print(candidate_news)
设计一个合适的Prompt,告诉通义千问或其他大模型我们需要对候选新闻进行评分和排序。
defscore_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)returnscored_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']])
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |