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

View File

@@ -1,14 +1,17 @@
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;
public class NewsManager
public class NewsManager:DomainService
{
private SemanticKernelClient _skClient;
public NewsManager(SemanticKernelClient skClient)
private ISqlSugarRepository<StockNewsAggregateRoot> _newsRepository;
public NewsManager(SemanticKernelClient skClient,ISqlSugarRepository<StockNewsAggregateRoot> newsRepository)
{
_skClient = skClient;
_newsRepository = newsRepository;
}
/// <summary>
@@ -20,4 +23,21 @@ public class NewsManager
_skClient.RegisterPlugins<NewsPlugins>("news");
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.Text.Json.Serialization;
using Microsoft.SemanticKernel;
using Yi.Framework.Stock.Domain.Managers;
namespace Yi.Framework.Stock.Domain.Managers.Plugins;
public class NewsPlugins
{
[KernelFunction("save_news"), Description("生成并且保存一个新闻")]
public async Task<string> SaveAsync(NewModel news)
private readonly NewsManager _newsManager;
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")]
[DisplayName("新闻标题")]
@@ -22,4 +30,14 @@ public class NewModel
[JsonPropertyName("content")]
[DisplayName("新闻内容")]
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.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Stock.Domain.Managers;
public class SemanticKernelClient
public class SemanticKernelClient:ITransientDependency
{
private Kernel Kernel { get; }
private readonly IKernelBuilder _kernelBuilder;
@@ -26,9 +27,9 @@ public class SemanticKernelClient
private void RegisterChatCompletion()
{
_kernelBuilder.AddOpenAIChatCompletion(
modelId: "",
apiKey: "",
httpClient: new HttpClient() { BaseAddress = new Uri("") });
modelId: Options.ModelId,
apiKey: Options.ApiKey,
httpClient: new HttpClient() { BaseAddress = new Uri(Options.Endpoint) });
}
/// <summary>
@@ -74,7 +75,7 @@ public class SemanticKernelClient
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
MaxTokens = 1000
MaxTokens = Options.MaxTokens
};
var chatCompletionService = this.Kernel.GetRequiredService<IChatCompletionService>();

View File

@@ -2,5 +2,28 @@
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 Yi.Framework.Stock.Domain.Shared;
using Yi.Framework.Mapster;
using Yi.Framework.Stock.Domain.Managers;
namespace Yi.Framework.Stock.Domain
{
@@ -14,6 +15,15 @@ namespace Yi.Framework.Stock.Domain
)]
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";
});
}
}
}