Bitzsoft.Integrations.SemanticKernel
1.0.0-alpha.5
See the version list below for details.
dotnet add package Bitzsoft.Integrations.SemanticKernel --version 1.0.0-alpha.5
NuGet\Install-Package Bitzsoft.Integrations.SemanticKernel -Version 1.0.0-alpha.5
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" Version="1.0.0-alpha.5" />
<PackageVersion Include="Bitzsoft.Integrations.SemanticKernel" Version="1.0.0-alpha.5" />
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" />
paket add Bitzsoft.Integrations.SemanticKernel --version 1.0.0-alpha.5
#r "nuget: Bitzsoft.Integrations.SemanticKernel, 1.0.0-alpha.5"
#:package Bitzsoft.Integrations.SemanticKernel@1.0.0-alpha.5
#addin nuget:?package=Bitzsoft.Integrations.SemanticKernel&version=1.0.0-alpha.5&prerelease
#tool nuget:?package=Bitzsoft.Integrations.SemanticKernel&version=1.0.0-alpha.5&prerelease
Bitzsoft.Integrations.SemanticKernel
Semantic Kernel 编排层 -- 基于 Microsoft Semantic Kernel 的 AI 编排服务,提供对话、流式输出、插件注册与函数调用(Function Calling)能力。
功能特性
- 封装 Microsoft Semantic Kernel,Kernel 通过 DI 容器注册(微软最佳实践)
- 支持一次性响应与流式(Streaming)响应两种对话模式
- 内置系统提示词(SystemPrompt)配置,支持全局默认值与单次覆盖
- 支持 Plugin 机制:通过
ImportPluginFromType<T>()注册原生插件 - Function Calling:
SendMessageWithFunctionCallingAsync自动识别并调用注册的[KernelFunction]方法 - 内置
DateTimePlugin日期时间插件示例 - 兼容所有 OpenAI Chat Completions API 协议的服务端点
安装
dotnet add package Bitzsoft.Integrations.SemanticKernel
或通过 csproj 引用:
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" Version="*" />
配置
在 appsettings.json 中添加以下配置节:
{
"SemanticKernel": {
"Enabled": true,
"ModelId": "gpt-4o",
"Endpoint": "https://api.openai.com/v1",
"ApiKey": "sk-your-api-key",
"SystemPrompt": "你是一个有帮助的 AI 助手。"
}
}
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Enabled |
bool |
false |
是否启用 Semantic Kernel 服务 |
ModelId |
string |
"gpt-4o" |
使用的模型 ID |
Endpoint |
string |
"https://api.openai.com/v1" |
OpenAI 兼容 API 端点地址 |
ApiKey |
string |
- | API 密钥 |
SystemPrompt |
string |
"你是一个有帮助的 AI 助手。" |
全局默认系统提示词 |
注册服务
using Bitzsoft.Integrations.SemanticKernel;
using Bitzsoft.Integrations.SemanticKernel.Dto;
// Program.cs / Startup.cs
services.AddSemanticKernel(options =>
{
options.Enabled = true;
options.ModelId = "gpt-4o";
options.Endpoint = "https://api.openai.com/v1";
options.ApiKey = "sk-your-api-key";
options.SystemPrompt = "你是一个专业的企业知识库助手。";
});
架构说明:
AddSemanticKernel通过 DI 工厂注册Kernel(Singleton),遵循微软最佳实践——消费方可直接注入Kernel、ISemanticKernelClient或 SK 的IChatCompletionService。
使用示例
基础对话
using Bitzsoft.Integrations.SemanticKernel;
public class ChatService
{
private readonly ISemanticKernelClient _kernelClient;
public ChatService(ISemanticKernelClient kernelClient)
{
_kernelClient = kernelClient;
}
/// <summary>
/// 回答用户问题
/// </summary>
public async Task<string> AnswerQuestionAsync(string question, CancellationToken ct = default)
{
// 使用全局默认 SystemPrompt
return await _kernelClient.SendMessageAsync(question, cancellationToken: ct);
}
/// <summary>
/// 带自定义角色的对话
/// </summary>
public async Task<string> ChatAsExpertAsync(string userMessage, CancellationToken ct = default)
{
return await _kernelClient.SendMessageAsync(
message: userMessage,
systemPrompt: "你是一位资深 .NET 架构师,擅长系统设计与性能优化。",
cancellationToken: ct);
}
}
流式响应(Streaming)
using System.Runtime.CompilerServices;
using Bitzsoft.Integrations.SemanticKernel;
public class StreamingChatService
{
private readonly ISemanticKernelClient _kernelClient;
public StreamingChatService(ISemanticKernelClient kernelClient)
{
_kernelClient = kernelClient;
}
/// <summary>
/// 流式输出 AI 响应,适合实时展示场景
/// </summary>
public async IAsyncEnumerable<string> StreamReplyAsync(
string userMessage,
[EnumeratorCancellation] CancellationToken ct = default)
{
await foreach (var chunk in _kernelClient.SendMessageStreamAsync(
message: userMessage,
systemPrompt: "请用简洁的方式回答问题。",
cancellationToken: ct))
{
yield return chunk;
}
}
}
注册并使用 Plugin
using System.ComponentModel;
using Bitzsoft.Integrations.SemanticKernel;
using Microsoft.SemanticKernel;
// 定义一个业务插件
public class OrderQueryPlugin
{
[KernelFunction("query_order")]
[Description("根据订单号查询订单状态")]
public string QueryOrder(string orderId)
{
// 实际业务中此处调用订单服务
return $"订单 {orderId} 状态:已发货";
}
}
// 在应用启动时注册插件
public class PluginSetupService(IServiceProvider provider) : IHostedService
{
public Task StartAsync(CancellationToken ct)
{
var client = provider.GetRequiredService<ISemanticKernelClient>();
// 注册类型插件
client.ImportPluginFromType<OrderQueryPlugin>("OrderQuery");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken ct) => Task.CompletedTask;
}
Function Calling(自动函数调用)
注册插件后,通过 SendMessageWithFunctionCallingAsync 让 AI 自动识别并调用已注册的工具方法:
using Bitzsoft.Integrations.SemanticKernel;
using Bitzsoft.Integrations.SemanticKernel.Plugins;
// 1. 注册内置日期时间插件(GetCurrentDateTime / GetDaysBetween 等)
client.ImportPluginFromType<DateTimePlugin>("DateTime");
// 2. AI 将自动调用 DateTimePlugin 获取当前时间来回答
var answer = await client.SendMessageWithFunctionCallingAsync(
message: "今天是几号?距离 2026-12-31 还有多少天?",
systemPrompt: "你是一个智能助手");
内置
DateTimePlugin提供:GetCurrentDateTime、GetCurrentDate、GetDaysBetween三个工具方法。
直接访问 Kernel 实例
using Bitzsoft.Integrations.SemanticKernel;
using Microsoft.SemanticKernel;
public class AdvancedKernelService
{
private readonly ISemanticKernelClient _kernelClient;
public AdvancedKernelService(ISemanticKernelClient kernelClient)
{
_kernelClient = kernelClient;
}
public async Task<string> ExecutePromptAsync(string promptTemplate, CancellationToken ct = default)
{
// 通过 Kernel 属性直接使用 Semantic Kernel 原生 API
var result = await _kernelClient.Kernel.InvokePromptAsync(promptTemplate, cancellationToken: ct);
return result.ToString();
}
}
相关包
| 包名 | 说明 |
|---|---|
| Bitzsoft.Integrations.AI | 轻量级 AI 客户端,兼容 OpenAI API |
| Bitzsoft.Integrations.RAG | 检索增强生成(Qdrant + Ollama 嵌入集成) |
| Bitzsoft.Integrations.AgentFramework | AI Agent 框架 |
| Bitzsoft.Integrations.McpServer | MCP Server 集成 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Bitzsoft.Integrations.AI.Abstractions (>= 1.0.0-alpha.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.9)
- Microsoft.Extensions.Options (>= 10.0.9)
- Microsoft.SemanticKernel (>= 1.77.0)
- Microsoft.SemanticKernel.Connectors.OpenAI (>= 1.77.0)
-
net8.0
- Bitzsoft.Integrations.AI.Abstractions (>= 1.0.0-alpha.5)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.9)
- Microsoft.Extensions.Options (>= 10.0.9)
- Microsoft.SemanticKernel (>= 1.77.0)
- Microsoft.SemanticKernel.Connectors.OpenAI (>= 1.77.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.SemanticKernel:
| Package | Downloads |
|---|---|
|
Bitzsoft.Integrations.All
Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-alpha.7 | 57 | 6/16/2026 |
| 1.0.0-alpha.6 | 66 | 6/16/2026 |
| 1.0.0-alpha.5 | 59 | 6/14/2026 |
| 1.0.0-alpha.3 | 63 | 6/7/2026 |
| 1.0.0-alpha.2 | 65 | 5/29/2026 |
| 1.0.0-alpha.1 | 57 | 5/28/2026 |