feat: ai完成stock模块搭建
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user