feat: ai完成stock模块搭建
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using Yi.Framework.Stock.Domain.Managers.Plugins;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Volo.Abp.Domain.Services;
|
||||
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;
|
||||
|
||||
namespace Yi.Framework.Stock.Domain.Managers;
|
||||
|
||||
public class NewsManager:DomainService
|
||||
@@ -19,9 +21,12 @@ public class NewsManager:DomainService
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task GenerateNewsAsync()
|
||||
{
|
||||
_skClient.RegisterPlugins<NewsPlugins>("news");
|
||||
await _skClient.ChatCompletionAsync("帮我生成一个新闻");
|
||||
{ var question = """
|
||||
生成并保存一个新闻,包含新闻标题、新闻内容、新闻简介、新闻来源
|
||||
内容关于娱乐圈
|
||||
只用生成一次即可
|
||||
""";
|
||||
await _skClient.ChatCompletionAsync(question, ("NewsPlugins","save_news"));
|
||||
}
|
||||
|
||||
public async Task SaveNewsAsync(NewsModel news)
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Yi.Framework.Stock.Domain.Managers;
|
||||
|
||||
namespace Yi.Framework.Stock.Domain.Managers.Plugins;
|
||||
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
|
||||
|
||||
public class NewsPlugins
|
||||
public class NewsPlugins
|
||||
{
|
||||
private readonly NewsManager _newsManager;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public NewsPlugins(NewsManager newsManager)
|
||||
public NewsPlugins(IServiceProvider serviceProvider)
|
||||
{
|
||||
_newsManager = newsManager;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
[KernelFunction("save_news"), Description("生成并且保存一个新闻")]
|
||||
[KernelFunction("save_news"), Description("生成并保存一个新闻")]
|
||||
public async Task SaveAsync(NewsModel news)
|
||||
{
|
||||
await _newsManager.SaveNewsAsync(news);
|
||||
var newsManager = _serviceProvider.GetRequiredService<NewsManager>();
|
||||
await newsManager.SaveNewsAsync(news);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,15 +30,13 @@ public class NewsModel
|
||||
|
||||
[JsonPropertyName("content")]
|
||||
[DisplayName("新闻内容")]
|
||||
public string? Content { get; set; }
|
||||
|
||||
//新闻简介
|
||||
public string Content { get; set; }
|
||||
|
||||
[JsonPropertyName("summary")]
|
||||
[DisplayName("新闻简介")]
|
||||
public string? Summary { get; set; }
|
||||
|
||||
//新闻来源
|
||||
public string Summary { get; set; }
|
||||
|
||||
[JsonPropertyName("source")]
|
||||
[DisplayName("新闻来源")]
|
||||
public string? Source { get; set; }
|
||||
public string Source { get; set; }
|
||||
}
|
||||
@@ -1,26 +1,34 @@
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
|
||||
namespace Yi.Framework.Stock.Domain.Managers.Plugins;
|
||||
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
|
||||
|
||||
public class StockPlugins
|
||||
{
|
||||
[KernelFunction("save_stocks"), Description("生成并且保存多个股票记录")]
|
||||
public async Task<string> SaveAsync(List<StockModel> stockModels)
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public StockPlugins(IServiceProvider serviceProvider)
|
||||
{
|
||||
return "成功";
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
[KernelFunction("save_stocks"), Description("生成并且保存多个股票记录")]
|
||||
public async Task SaveAsync(List<StockModel> stockModels)
|
||||
{
|
||||
var stockMarketManager= _serviceProvider.GetRequiredService<StockMarketManager>();
|
||||
await stockMarketManager.SaveStockAsync(stockModels);
|
||||
}
|
||||
}
|
||||
|
||||
public class StockModel
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public int Id { get; set; }
|
||||
[DisplayName("股票id")]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonPropertyName("is_on")]
|
||||
public bool? IsOn { get; set; }
|
||||
[JsonPropertyName("values")]
|
||||
[DisplayName("股票未来24小时价格")]
|
||||
public decimal[] Values { get; set; }
|
||||
}
|
||||
@@ -1,57 +1,18 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Yi.Framework.Stock.Domain.Managers;
|
||||
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel;
|
||||
|
||||
public class SemanticKernelClient:ITransientDependency
|
||||
{
|
||||
private Kernel Kernel { get; }
|
||||
private readonly IKernelBuilder _kernelBuilder;
|
||||
private SemanticKernelOptions Options { get; }
|
||||
public Kernel Kernel { get;}
|
||||
|
||||
public SemanticKernelClient(IOptions<SemanticKernelOptions> semanticKernelOption)
|
||||
public SemanticKernelClient(Kernel kernel)
|
||||
{
|
||||
Options = semanticKernelOption.Value;
|
||||
_kernelBuilder = Kernel.CreateBuilder();
|
||||
RegisterChatCompletion();
|
||||
Kernel = _kernelBuilder.Build();
|
||||
RegisterDefautlPlugins();
|
||||
this.Kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册
|
||||
/// </summary>
|
||||
private void RegisterChatCompletion()
|
||||
{
|
||||
_kernelBuilder.AddOpenAIChatCompletion(
|
||||
modelId: Options.ModelId,
|
||||
apiKey: Options.ApiKey,
|
||||
httpClient: new HttpClient() { BaseAddress = new Uri(Options.Endpoint) });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插件注册
|
||||
/// </summary>
|
||||
private void RegisterDefautlPlugins()
|
||||
{
|
||||
//动态导入插件
|
||||
// this.Kernel.ImportPluginFromPromptDirectory(System.IO.Path.Combine("wwwroot", "plugin","stock"),"stock");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义插件
|
||||
/// </summary>
|
||||
/// <param name="pluginName"></param>
|
||||
/// <typeparam name="TPlugin"></typeparam>
|
||||
public void RegisterPlugins<TPlugin>(string pluginName)
|
||||
{
|
||||
this.Kernel.Plugins.AddFromType<TPlugin>(pluginName);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 执行插件
|
||||
/// </summary>
|
||||
@@ -70,19 +31,24 @@ public class SemanticKernelClient:ITransientDependency
|
||||
/// 聊天对话,调用方法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IReadOnlyList<ChatMessageContent>> ChatCompletionAsync(string question)
|
||||
public async Task<IReadOnlyList<ChatMessageContent>> ChatCompletionAsync(string question,params (string,string)[] functions)
|
||||
{
|
||||
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
|
||||
if (functions is null)
|
||||
{
|
||||
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
|
||||
MaxTokens = Options.MaxTokens
|
||||
throw new Exception("请选择插件");
|
||||
}
|
||||
var openSettings = new OpenAIPromptExecutionSettings()
|
||||
{
|
||||
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(functions.Select(x=>this.Kernel.Plugins.GetFunction(x.Item1, x.Item2)).ToList(),true),
|
||||
// ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
|
||||
MaxTokens =1000
|
||||
};
|
||||
|
||||
var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>();
|
||||
|
||||
var results =await chatCompletionService.GetChatMessageContentsAsync(
|
||||
question,
|
||||
executionSettings: openAIPromptExecutionSettings,
|
||||
executionSettings: openSettings,
|
||||
kernel: Kernel);
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,9 @@
|
||||
namespace Yi.Framework.Stock.Domain.Managers;
|
||||
|
||||
public class SemanticKernelOptions
|
||||
namespace Yi.Framework.Stock.Domain.Managers.SemanticKernel
|
||||
{
|
||||
/// <summary>
|
||||
/// OpenAI 模型 ID
|
||||
/// </summary>
|
||||
public string ModelId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// OpenAI API 密钥
|
||||
/// </summary>
|
||||
public string ApiKey { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// API 端点地址
|
||||
/// </summary>
|
||||
public string Endpoint { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 最大生成令牌数
|
||||
/// </summary>
|
||||
public int MaxTokens { get; set; } = 1000;
|
||||
|
||||
/// <summary>
|
||||
/// 插件目录路径
|
||||
/// </summary>
|
||||
public string PluginsDirectoryPath { get; set; } = string.Empty;
|
||||
}
|
||||
public class SemanticKernelOptions
|
||||
{
|
||||
public string ModelId { get; set; }
|
||||
public string Endpoint { get; set; }
|
||||
public string ApiKey { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,8 @@ using Yi.Framework.Stock.Domain.Entities;
|
||||
using Yi.Framework.Stock.Domain.Shared;
|
||||
using Yi.Framework.Stock.Domain.Shared.Etos;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using Yi.Framework.Stock.Domain.Managers.Plugins;
|
||||
using Yi.Framework.Stock.Domain.Managers.SemanticKernel;
|
||||
using Yi.Framework.Stock.Domain.Managers.SemanticKernel.Plugins;
|
||||
|
||||
namespace Yi.Framework.Stock.Domain.Managers
|
||||
{
|
||||
@@ -277,27 +278,25 @@ namespace Yi.Framework.Stock.Domain.Managers
|
||||
/// </summary>
|
||||
/// <param name="priceRecords">价格记录列表</param>
|
||||
/// <returns>保存的记录数量</returns>
|
||||
public async Task<int> BatchSaveStockPriceRecordsAsync(List<StockPriceRecordEntity> priceRecords)
|
||||
public async Task BatchSaveStockPriceRecordsAsync(List<StockPriceRecordEntity> priceRecords)
|
||||
{
|
||||
if (priceRecords == null || !priceRecords.Any())
|
||||
{
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证数据
|
||||
foreach (var record in priceRecords)
|
||||
for (int i = 0; i < priceRecords.Count; i++)
|
||||
{
|
||||
var record = priceRecords[i];
|
||||
if (record.CurrentPrice <= 0)
|
||||
{
|
||||
throw new UserFriendlyException($"股票ID {record.StockId} 的价格必须大于0");
|
||||
}
|
||||
|
||||
// 设置创建时间为当前时间(如果未设置)
|
||||
if (record.CreationTime == default)
|
||||
{
|
||||
record.CreationTime = DateTime.Now;
|
||||
}
|
||||
|
||||
// 设置记录时间(当前时间加上i个小时),只记录到年月日小时
|
||||
record.RecordTime = new DateTime(DateTime.Now.AddHours(i).Year, DateTime.Now.AddHours(i).Month, DateTime.Now.AddHours(i).Day, DateTime.Now.AddHours(i).Hour, 0, 0);
|
||||
|
||||
// 计算交易额(如果未设置)
|
||||
if (record.Turnover == 0 && record.Volume > 0)
|
||||
{
|
||||
@@ -306,18 +305,85 @@ namespace Yi.Framework.Stock.Domain.Managers
|
||||
}
|
||||
|
||||
await _stockPriceRecordRepository.InsertManyAsync(priceRecords);
|
||||
return priceRecords.Count;
|
||||
}
|
||||
|
||||
|
||||
public async Task SaveStockAsync(List<StockModel> stockModels)
|
||||
{
|
||||
if (stockModels == null || !stockModels.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 收集所有股票ID
|
||||
var stockIds = stockModels.Select(m => m.Id).ToList();
|
||||
|
||||
// 一次性查询所有相关股票信息
|
||||
var stockMarkets = await _stockMarketRepository.GetListAsync(s => stockIds.Contains(s.Id));
|
||||
|
||||
// 构建字典以便快速查找
|
||||
var stockMarketsDict = stockMarkets.ToDictionary(s => s.Id);
|
||||
|
||||
// 将StockModel转换为StockPriceRecordEntity
|
||||
var priceRecords = new List<StockPriceRecordEntity>();
|
||||
|
||||
foreach (var stockModel in stockModels)
|
||||
{
|
||||
if (stockModel.Values == null || !stockModel.Values.Any())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 从字典中查找股票信息,而不是每次查询数据库
|
||||
if (!stockMarketsDict.TryGetValue(stockModel.Id, out var stockMarket))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 为每个价格点创建一个记录
|
||||
foreach (var priceValue in stockModel.Values)
|
||||
{
|
||||
var priceRecord = new StockPriceRecordEntity
|
||||
{
|
||||
StockId = stockMarket.Id,
|
||||
CurrentPrice = priceValue,
|
||||
Volume = 0, // 可以根据实际情况设置
|
||||
Turnover = 0, // 可以根据实际情况设置
|
||||
PeriodType = PeriodTypeEnum.Hour
|
||||
};
|
||||
|
||||
priceRecords.Add(priceRecord);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量保存价格记录
|
||||
if (priceRecords.Any())
|
||||
{
|
||||
await BatchSaveStockPriceRecordsAsync(priceRecords);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 生成最新股票记录
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task GenerateStocksAsync()
|
||||
{
|
||||
_skClient.RegisterPlugins<StockPlugins>("stock");
|
||||
await _skClient.ChatCompletionAsync("帮我生成多个股市内容");
|
||||
var question = """
|
||||
根据给出的模拟新闻,模拟生成多家股票未来价格
|
||||
|
||||
以下是近期新闻:
|
||||
ai势力逐步崛起
|
||||
大量网红下岗
|
||||
近期群星演唱会即将开幕
|
||||
|
||||
以下是多个股票:
|
||||
伦敦股票:id:3a1886d5-1479-5402-3bcb-063e989898d1 最后一次价格:188.7
|
||||
上海股票:id:3a1886d5-4393-606c-b040-52f1ef7d2158 最后一次价格:125.2
|
||||
纽约股票:id:3a1886d5-6906-8f30-d955-198fbcfe4026 最后一次价格:185.5
|
||||
|
||||
根据上面信息,给每个股票返回未来24小时的股票价格,每个小时,整点为单位一条记录,一家共有24条,返回股票id 与 长度为24的价格列表
|
||||
只用生成一次即可
|
||||
""";
|
||||
await _skClient.ChatCompletionAsync(question,("StockPlugins","save_stocks"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user