feat: 完成提示词工程

This commit is contained in:
橙子
2025-03-09 23:51:30 +08:00
parent afa5fad8c6
commit c782246a1d
2 changed files with 158 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.Stock.Domain.Entities;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
using System.Text;
namespace Yi.Framework.Stock.Domain.Managers;
@@ -16,17 +17,72 @@ public class NewsManager:DomainService
_newsRepository = newsRepository;
}
/// <summary>
/// 获取最近的新闻
/// </summary>
/// <param name="count">获取数量</param>
/// <returns>最近的新闻列表</returns>
public async Task<List<StockNewsAggregateRoot>> GetRecentNewsAsync(int count = 10)
{
return await _newsRepository._DbQueryable
.OrderByDescending(n => n.CreationTime)
.Take(count)
.Select(n => new StockNewsAggregateRoot
{
Title = n.Title,
Summary = n.Summary,
Source = n.Source,
CreationTime = n.CreationTime
})
.ToListAsync();
}
/// <summary>
/// 生成一个新闻
/// </summary>
/// <returns></returns>
public async Task GenerateNewsAsync()
{ var question = """
{
// 获取最近10条新闻
var recentNews = await GetRecentNewsAsync(10);
// 构建新闻背景上下文
var newsContext = new StringBuilder();
if (recentNews.Any())
{
newsContext.AppendLine("以下是最近的新闻简介:");
foreach (var news in recentNews)
{
newsContext.AppendLine($"- {news.CreationTime:yyyy-MM-dd} 来源:{news.Source}");
newsContext.AppendLine($" 标题:{news.Title}");
newsContext.AppendLine($" 简介:{news.Summary}");
newsContext.AppendLine();
}
}
else
{
newsContext.AppendLine("目前没有最近的新闻记录。");
}
var question = $$"""
基于以下最近的新闻背景,生成一条新的股市相关新闻。
{{newsContext}}
请生成一条有关联性的新闻,包含以下要素:
1. 新闻标题:吸引人且与股市、金融或经济相关
2. 新闻内容:详细且符合逻辑的报道,篇幅适中
3. 新闻简介:简明扼要的总结
4. 新闻来源:提供一个可信的媒体或机构名称
注意:
- 新闻内容应当与前面的新闻有一定关联性
- 内容应当暗示可能对股市产生某种影响(积极或消极)
- 行业焦点可以包括娱乐、科技、能源、金融等多个领域
- 只需生成一次即可
""";
await _skClient.ChatCompletionAsync(question, ("NewsPlugins","save_news"));
await _skClient.ChatCompletionAsync(question, ("NewsPlugins","save_news"));
}
public async Task SaveNewsAsync(NewsModel news)