feat: 补充部分功能

This commit is contained in:
橙子
2025-03-07 00:35:32 +08:00
parent c092ee46e9
commit 337088c908
6 changed files with 93 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ namespace Yi.Framework.Stock.Domain.Entities
/// <summary> /// <summary>
/// 创建时间 /// 创建时间
/// </summary> /// </summary>
public DateTime CreationTime { get; set; } = DateTime.Now; public DateTime CreationTime { get; set; }
/// <summary> /// <summary>
/// 创建者ID /// 创建者ID
@@ -57,13 +57,18 @@ namespace Yi.Framework.Stock.Domain.Entities
/// <summary> /// <summary>
/// 发布时间 /// 发布时间
/// </summary> /// </summary>
public DateTime PublishTime { get; set; } = DateTime.Now; public DateTime PublishTime { get; set; }
/// <summary> /// <summary>
/// 新闻来源 /// 新闻来源
/// </summary> /// </summary>
public string Source { get; set; } = string.Empty; public string Source { get; set; } = string.Empty;
/// <summary>
/// 新闻摘要
/// </summary>
public string Summary { get; set; } = string.Empty;
public StockNewsAggregateRoot() { } public StockNewsAggregateRoot() { }
public StockNewsAggregateRoot( public StockNewsAggregateRoot(

View File

@@ -1,14 +1,17 @@
using Yi.Framework.Stock.Domain.Managers.Plugins; using Yi.Framework.Stock.Domain.Managers.Plugins;
using Volo.Abp.Domain.Services;
using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.Stock.Domain.Entities;
namespace Yi.Framework.Stock.Domain.Managers; namespace Yi.Framework.Stock.Domain.Managers;
public class NewsManager public class NewsManager:DomainService
{ {
private SemanticKernelClient _skClient; private SemanticKernelClient _skClient;
private ISqlSugarRepository<StockNewsAggregateRoot> _newsRepository;
public NewsManager(SemanticKernelClient skClient) public NewsManager(SemanticKernelClient skClient,ISqlSugarRepository<StockNewsAggregateRoot> newsRepository)
{ {
_skClient = skClient; _skClient = skClient;
_newsRepository = newsRepository;
} }
/// <summary> /// <summary>
@@ -20,4 +23,21 @@ public class NewsManager
_skClient.RegisterPlugins<NewsPlugins>("news"); _skClient.RegisterPlugins<NewsPlugins>("news");
await _skClient.ChatCompletionAsync("帮我生成一个新闻"); await _skClient.ChatCompletionAsync("帮我生成一个新闻");
} }
public async Task SaveNewsAsync(NewsModel news)
{
var newsEntity = new StockNewsAggregateRoot(
title: news.Title,
content: news.Content,
source: news.Source
)
{
Summary = news.Summary,
CreationTime = DateTime.Now,
IsDeleted = false,
OrderNum = 0
};
await _newsRepository.InsertAsync(newsEntity);
}
} }

View File

@@ -1,19 +1,27 @@
using System.ComponentModel; using System.ComponentModel;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers;
namespace Yi.Framework.Stock.Domain.Managers.Plugins; namespace Yi.Framework.Stock.Domain.Managers.Plugins;
public class NewsPlugins public class NewsPlugins
{ {
[KernelFunction("save_news"), Description("生成并且保存一个新闻")] private readonly NewsManager _newsManager;
public async Task<string> SaveAsync(NewModel news)
public NewsPlugins(NewsManager newsManager)
{ {
return "成功"; _newsManager = newsManager;
}
[KernelFunction("save_news"), Description("生成并且保存一个新闻")]
public async Task SaveAsync(NewsModel news)
{
await _newsManager.SaveNewsAsync(news);
} }
} }
public class NewModel public class NewsModel
{ {
[JsonPropertyName("title")] [JsonPropertyName("title")]
[DisplayName("新闻标题")] [DisplayName("新闻标题")]
@@ -22,4 +30,14 @@ public class NewModel
[JsonPropertyName("content")] [JsonPropertyName("content")]
[DisplayName("新闻内容")] [DisplayName("新闻内容")]
public string? Content { get; set; } public string? Content { get; set; }
//新闻简介
[JsonPropertyName("summary")]
[DisplayName("新闻简介")]
public string? Summary { get; set; }
//新闻来源
[JsonPropertyName("source")]
[DisplayName("新闻来源")]
public string? Source { get; set; }
} }

View File

@@ -2,10 +2,11 @@
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI; using Microsoft.SemanticKernel.Connectors.OpenAI;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Stock.Domain.Managers; namespace Yi.Framework.Stock.Domain.Managers;
public class SemanticKernelClient public class SemanticKernelClient:ITransientDependency
{ {
private Kernel Kernel { get; } private Kernel Kernel { get; }
private readonly IKernelBuilder _kernelBuilder; private readonly IKernelBuilder _kernelBuilder;
@@ -26,9 +27,9 @@ public class SemanticKernelClient
private void RegisterChatCompletion() private void RegisterChatCompletion()
{ {
_kernelBuilder.AddOpenAIChatCompletion( _kernelBuilder.AddOpenAIChatCompletion(
modelId: "", modelId: Options.ModelId,
apiKey: "", apiKey: Options.ApiKey,
httpClient: new HttpClient() { BaseAddress = new Uri("") }); httpClient: new HttpClient() { BaseAddress = new Uri(Options.Endpoint) });
} }
/// <summary> /// <summary>
@@ -74,7 +75,7 @@ public class SemanticKernelClient
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{ {
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(), FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
MaxTokens = 1000 MaxTokens = Options.MaxTokens
}; };
var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>(); var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>();

View File

@@ -2,5 +2,28 @@
public class SemanticKernelOptions public class SemanticKernelOptions
{ {
/// <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;
} }

View File

@@ -2,6 +2,7 @@ using Volo.Abp.Caching;
using Volo.Abp.Domain; using Volo.Abp.Domain;
using Yi.Framework.Stock.Domain.Shared; using Yi.Framework.Stock.Domain.Shared;
using Yi.Framework.Mapster; using Yi.Framework.Mapster;
using Yi.Framework.Stock.Domain.Managers;
namespace Yi.Framework.Stock.Domain namespace Yi.Framework.Stock.Domain
{ {
@@ -14,6 +15,15 @@ namespace Yi.Framework.Stock.Domain
)] )]
public class YiFrameworkStockDomainModule : AbpModule public class YiFrameworkStockDomainModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<SemanticKernelOptions>((options)=>{
options.Endpoint = "https://api.token-ai.cn/v1";
options.ApiKey = "sk-V6OqmrloXDAiTM2FWoisGgaop72Ngr0fXAnXL8";
options.ModelId = "gpt-4o-mini";
options.MaxTokens = 1000;
options.PluginsDirectoryPath = "plugins";
});
}
} }
} }