Bitzsoft.Integrations.SemanticKernel
1.0.0-alpha.3
This is a prerelease version of Bitzsoft.Integrations.SemanticKernel.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Bitzsoft.Integrations.SemanticKernel --version 1.0.0-alpha.3
NuGet\Install-Package Bitzsoft.Integrations.SemanticKernel -Version 1.0.0-alpha.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" Version="1.0.0-alpha.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.SemanticKernel" Version="1.0.0-alpha.3" />
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.SemanticKernel --version 1.0.0-alpha.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Bitzsoft.Integrations.SemanticKernel, 1.0.0-alpha.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.SemanticKernel@1.0.0-alpha.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.SemanticKernel&version=1.0.0-alpha.3&prerelease
#tool nuget:?package=Bitzsoft.Integrations.SemanticKernel&version=1.0.0-alpha.3&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Bitzsoft.Integrations.SemanticKernel
Semantic Kernel 扩展 -- 基于 Microsoft Semantic Kernel 的 AI 编排服务,提供对话、流式输出和插件化能力。
功能特性
- 封装 Microsoft Semantic Kernel,开箱即用的
ISemanticKernelClient接口 - 支持一次性响应与流式(Streaming)响应两种对话模式
- 内置系统提示词(SystemPrompt)配置,支持全局默认值与单次覆盖
- 支持 Plugin 机制:通过
ImportPluginFromType<T>()注册原生插件 - 兼容所有 OpenAI Chat Completions API 协议的服务端点
- 注册为 Singleton 生命周期,Kernel 实例全局复用
安装
dotnet add package Bitzsoft.Integrations.SemanticKernel
或通过 csproj 引用:
<PackageReference Include="Bitzsoft.Integrations.SemanticKernel" Version="*" />
配置
在 appsettings.json 中添加以下配置节:
{
"SemanticKernel": {
"Enabled": true,
"ModelId": "gpt-4",
"Endpoint": "https://api.openai.com/v1",
"ApiKey": "sk-your-api-key",
"SystemPrompt": "你是一个有帮助的 AI 助手。"
}
}
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Enabled |
bool |
false |
是否启用 Semantic Kernel 服务 |
ModelId |
string |
"gpt-4" |
使用的模型 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-4";
options.Endpoint = "https://api.openai.com/v1";
options.ApiKey = "sk-your-api-key";
options.SystemPrompt = "你是一个专业的企业知识库助手。";
});
注意:
AddSemanticKernel将ISemanticKernelClient注册为 Singleton,确保 Kernel 实例全局复用,插件状态持久化。
使用示例
基础对话
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;
}
直接访问 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.4)
- Microsoft.Extensions.Options (>= 10.0.4)
- Microsoft.SemanticKernel (>= 1.74.0)
- Microsoft.SemanticKernel.Connectors.OpenAI (>= 1.74.0)
-
net8.0
- Microsoft.Extensions.Http (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.4)
- Microsoft.Extensions.Options (>= 10.0.4)
- Microsoft.SemanticKernel (>= 1.74.0)
- Microsoft.SemanticKernel.Connectors.OpenAI (>= 1.74.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 |