Ben.SemanticKernel
5.2.16
dotnet add package Ben.SemanticKernel --version 5.2.16
NuGet\Install-Package Ben.SemanticKernel -Version 5.2.16
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="Ben.SemanticKernel" Version="5.2.16" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ben.SemanticKernel" Version="5.2.16" />
<PackageReference Include="Ben.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 Ben.SemanticKernel --version 5.2.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Ben.SemanticKernel, 5.2.16"
#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 Ben.SemanticKernel@5.2.16
#: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=Ben.SemanticKernel&version=5.2.16
#tool nuget:?package=Ben.SemanticKernel&version=5.2.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Ben.SemanticKernel
一个基于 Microsoft Semantic Kernel 的强大 .NET 库,旨在简化 AI 能力的集成和使用,为企业级应用提供完整的 AI 解决方案。
✨ 核心特性
🤖 多模型支持
- OpenAI 兼容服务: OpenAI、DeepSeek、Grok、SiliconCloud
- 本地模型: Ollama 支持
- 云服务: Azure OpenAI、阿里云百炼
💬 智能对话系统
- 流式和非流式对话支持
- 对话缓存机制,提升响应速度
- 支持推理模型(o1、o3)的思维链展示
- 联网搜索增强(支持多搜索引擎)
📚 知识管理
- 向量数据库: Qdrant、PostgreSQL (pgvector)、SQLite
- 文档处理: 支持多种格式文档解析和分块
- 语义搜索: 高效的向量检索和重排序
🕸️ 知识图谱
- Neo4j 集成
- 社区检索和关系检索
- 图谱生成和查询优化
🔧 开发友好
- 完整的依赖注入支持
- 弹性策略(重试、超时、熔断)
- 后台任务队列
- Text2SQL 工具
📦 安装
NuGet 包管理器
Install-Package Ben.SemanticKernel
.NET CLI
dotnet add package Ben.SemanticKernel
PackageReference
<PackageReference Include="Ben.SemanticKernel" Version="5.2.11" />
🚀 快速开始
1. 配置文件设置
在 appsettings.json
中添加配置:
{
"BEN_SK": {
"AIConfig": {
"TextInfo": {
"ApiKey": "your-api-key",
"Model": "gpt-4",
"BaseUri": "https://api.openai.com/v1",
"Provider": "openai"
},
"EmbeddingInfo": {
"ApiKey": "your-api-key",
"Model": "text-embedding-3-small",
"BaseUri": "https://api.openai.com/v1",
"Provider": "openai",
"KmsPoint": "http://localhost:6333",
"VertorType": "qdrant",
"VectorSize": 1536,
"Index": "default"
},
"CachDays": 7
},
"BackgroundQueue": {
"MaxConcurrentTasks": 5,
"RetryCount": 3,
"TaskTimeout": "00:10:00"
}
}
}
2. 注册服务
在 Program.cs
中注册服务:
using Ben.SemanticKernel.Services;
var builder = WebApplication.CreateBuilder(args);
// 注册所有 Ben.SemanticKernel 服务
builder.Services.AddBenSKAllService(builder.Configuration);
// 或者选择性注册特定服务
// builder.Services.AddBenSKGlobalService(builder.Configuration);
// builder.Services.AddChatService(builder.Configuration);
var app = builder.Build();
app.Run();
3. 使用聊天服务
using Ben.SemanticKernel.Services.ChatService;
using Microsoft.SemanticKernel.ChatCompletion;
public class ChatController : ControllerBase
{
private readonly ChatService _chatService;
private readonly IOptions<AIOptions> _aiOptions;
public ChatController(ChatService chatService, IOptions<AIOptions> aiOptions)
{
_chatService = chatService;
_aiOptions = aiOptions;
}
// 流式对话
[HttpPost("stream")]
public async IAsyncEnumerable<string> StreamChat([FromBody] string message)
{
var history = new List<ChatMessageContent>();
await foreach (var chunk in _chatService.ChatAsync(
message,
"你是一个友好的AI助手",
_aiOptions.Value,
history))
{
yield return chunk;
}
}
// 非流式对话
[HttpPost("chat")]
public async Task<string> Chat([FromBody] string message)
{
var history = new List<ChatMessageContent>();
var result = await _chatService.Chat(
message,
"你是一个友好的AI助手",
_aiOptions.Value,
history);
return result;
}
}
4. 知识库对话
using Ben.SemanticKernel.Services.TextMemory;
using Microsoft.SemanticKernel;
public class KnowledgeController : ControllerBase
{
private readonly ImportKmsService _importService;
private readonly ChatService _chatService;
private readonly IOptions<AIOptions> _aiOptions;
// 导入文档到知识库
[HttpPost("import")]
public async Task<IActionResult> ImportDocument(IFormFile file)
{
var tempPath = Path.GetTempFileName();
await using (var stream = new FileStream(tempPath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
await _importService.ImportFromFileAsync(
tempPath,
_aiOptions.Value,
file.FileName);
return Ok("导入成功");
}
// 基于知识库的对话
[HttpPost("ask")]
public async IAsyncEnumerable<string> AskWithKnowledge([FromBody] AskRequest request)
{
// 从知识库检索相关文档
var documents = await _chatService.getMemoryDocument(
request.Question,
_aiOptions.Value,
index: "my-index");
// 构建提示词
var prompt = $@"基于以下文档回答问题:
文档内容:
{documents}
问题:{{{{$input}}}}
请基于文档内容给出准确的回答。如果文档中没有相关信息,请明确说明。";
var arguments = new KernelArguments
{
["input"] = request.Question
};
// 流式返回答案
await foreach (var chunk in _chatService.KmsChatCompletionAsync(
request.Question,
_aiOptions.Value,
prompt,
arguments))
{
yield return chunk;
}
}
}
public record AskRequest(string Question);
5. 联网搜索增强对话
// 启用联网搜索的对话
[HttpPost("search-chat")]
public async IAsyncEnumerable<string> SearchChat([FromBody] string message)
{
var history = new List<ChatMessageContent>();
await foreach (var chunk in _chatService.ChatAsync(
message,
"你是一个能够搜索互联网的AI助手",
_aiOptions.Value,
history,
netWork: true, // 启用联网搜索
engine: "smart")) // 使用智能多引擎搜索
{
yield return chunk;
}
}
📖 高级用法
配置多种 AI 提供商
DeepSeek
{
"TextInfo": {
"ApiKey": "sk-xxx",
"Model": "deepseek-chat",
"BaseUri": "https://api.deepseek.com",
"Provider": "deepseek"
}
}
Ollama(本地模型)
{
"TextInfo": {
"ApiKey": "not-needed",
"Model": "qwen2.5:7b",
"BaseUri": "http://localhost:11434",
"Provider": "ollama"
},
"EmbeddingInfo": {
"ApiKey": "not-needed",
"Model": "nomic-embed-text",
"BaseUri": "http://localhost:11434",
"Provider": "ollama"
}
}
Azure OpenAI
{
"TextInfo": {
"ApiKey": "your-azure-key",
"Model": "gpt-4",
"BaseUri": "https://your-resource.openai.azure.com",
"Provider": "azure"
}
}
使用 PostgreSQL 作为向量数据库
{
"EmbeddingInfo": {
"KmsPoint": "Host=localhost;Port=5432;Database=vectors;Username=postgres;Password=password",
"VertorType": "pgsql",
"VectorSize": 1536
}
}
对话缓存
// 使用缓存提升响应速度
var cacheList = new List<ChatCacheDto>
{
new ChatCacheDto
{
Question = "什么是AI?",
Answer = "人工智能(AI)是...",
QuestionEmbedding = "...",
ExpiresAt = DateTime.UtcNow.AddDays(7)
}
};
await foreach (var chunk in _chatService.KmsChatCompletionAsync(
input: "什么是人工智能?",
aiOptions: _aiOptions.Value,
prompt: myPrompt,
arguments: args,
EnableCache: true, // 启用缓存
SimilarityThreshold: 0.9f, // 相似度阈值
chatCacheDtos: cacheList))
{
// 如果缓存命中,将直接返回缓存的答案
yield return chunk;
}
🏗️ 项目架构
Ben.SemanticKernel/
├── AI/ # AI 核心功能
│ ├── KernelFactory.cs # Kernel 工厂
│ ├── MemoryKernelFactory.cs # Memory Kernel 工厂
│ └── HttpClientHandlerFactory.cs
├── Models/ # 数据模型
│ ├── AIOptions.cs # AI 配置
│ ├── ChatCache.cs # 缓存模型
│ └── Tools/ # 工具模型
├── Services/ # 业务服务
│ ├── ChatService/ # 聊天服务
│ ├── TextMemory/ # 文本记忆
│ ├── KnowledgeGraph/ # 知识图谱
│ ├── DocumentParsing/ # 文档解析
│ └── Tools/ # 工具服务
├── Plugins/ # SK 插件
│ ├── graph/ # 图谱插件
│ └── tools/ # 工具插件
└── Utils/ # 工具类
🔌 主要依赖
- Microsoft.SemanticKernel (1.54.0) - 核心框架
- Microsoft.KernelMemory.Core (0.98.x) - 内存管理
- Microsoft.SemanticKernel.Connectors.Qdrant - Qdrant 连接器
- Microsoft.SemanticKernel.Connectors.PgVector - PostgreSQL 连接器
- Microsoft.Playwright - Web 自动化(联网搜索)
- Polly - 弹性和瞬态故障处理
📝 配置说明
文本模型配置 (TextInfo)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
ApiKey | string | 是 | API 密钥 |
Model | string | 是 | 模型名称(如 gpt-4) |
BaseUri | string | 是 | API 基础地址 |
Provider | string | 否 | 提供商类型(默认 openai) |
嵌入模型配置 (EmbeddingInfo)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
ApiKey | string | 是 | API 密钥 |
Model | string | 是 | 模型名称 |
BaseUri | string | 是 | API 基础地址 |
Provider | string | 否 | 提供商类型 |
KmsPoint | string | 否 | 向量数据库地址 |
VertorType | string | 否 | 向量库类型(qdrant/pgsql) |
VectorSize | int | 否 | 向量维度(默认 768) |
Index | string | 否 | 索引名称 |
🤝 贡献指南
欢迎提交 Pull Request 或提出 Issue!
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature
) - 提交更改 (
git commit -m 'Add some AmazingFeature'
) - 推送到分支 (
git push origin feature/AmazingFeature
) - 开启 Pull Request
📄 许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件
🔗 相关资源
📧 联系方式
- 作者:benbena
- 邮箱:[您的邮箱]
- 项目主页:[GitHub 链接]
🌟 Star History
如果这个项目对你有帮助,请给一个 ⭐️ Star!
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 was computed. 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.
-
net8.0
- Google.Protobuf (>= 3.30.0)
- HtmlAgilityPack (>= 1.12.1)
- itext7 (>= 9.1.0)
- Microsoft.AspNetCore.Http (>= 2.3.0)
- Microsoft.Data.SqlClient (>= 6.1.0-preview1.25120.4)
- Microsoft.Extensions.Http.Polly (>= 9.0.4)
- Microsoft.KernelMemory.AI.Ollama (>= 0.98.250508.3)
- Microsoft.KernelMemory.AI.OpenAI (>= 0.98.250508.3)
- Microsoft.KernelMemory.Core (>= 0.98.250508.3)
- Microsoft.KernelMemory.MemoryDb.Postgres (>= 0.98.250508.3)
- Microsoft.KernelMemory.MemoryDb.Qdrant (>= 0.98.250508.3)
- Microsoft.Playwright (>= 1.52.0)
- Microsoft.Playwright.NUnit (>= 1.52.0)
- Microsoft.SemanticKernel (>= 1.54.0)
- Microsoft.SemanticKernel.Connectors.Ollama (>= 1.54.0-alpha)
- Microsoft.SemanticKernel.Connectors.PgVector (>= 1.54.0-preview)
- Microsoft.SemanticKernel.Connectors.Qdrant (>= 1.54.0-preview)
- Microsoft.SemanticKernel.Connectors.SqliteVec (>= 1.54.0-preview)
- MySql.Data (>= 9.3.0)
- Polly (>= 8.5.2)
- Polly.Extensions.Http (>= 3.0.0)
- pythonnet (>= 3.1.0-preview2024-09-06)
- Selenium.Support (>= 4.31.0)
- Selenium.WebDriver (>= 4.31.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated | |
---|---|---|---|
5.2.16 | 0 | 10/13/2025 | |
5.2.15 | 0 | 10/13/2025 | |
5.2.13 | 0 | 10/13/2025 | |
5.2.12 | 0 | 10/13/2025 | |
5.2.11 | 0 | 10/13/2025 | |
5.2.10 | 32 | 10/11/2025 | |
5.2.9 | 108 | 10/7/2025 | |
5.2.8 | 105 | 10/7/2025 | |
5.2.7 | 109 | 10/7/2025 | |
5.2.6 | 298 | 6/10/2025 | |
5.2.5 | 263 | 6/10/2025 | |
5.2.4 | 257 | 6/10/2025 | |
5.2.3 | 217 | 6/9/2025 | |
5.2.2 | 209 | 6/9/2025 | |
5.2.1 | 211 | 6/9/2025 | |
5.2.0 | 215 | 6/9/2025 | |
5.1.9 | 184 | 6/9/2025 | |
5.1.8 | 74 | 6/7/2025 | |
5.1.7 | 61 | 6/7/2025 | |
5.1.6 | 110 | 6/6/2025 | |
5.1.5 | 132 | 6/5/2025 | |
5.1.4 | 137 | 6/5/2025 | |
5.1.3 | 130 | 6/4/2025 | |
5.1.2 | 125 | 6/4/2025 | |
5.1.1 | 135 | 6/2/2025 | |
5.0.1 | 157 | 5/28/2025 | |
4.10.3 | 92 | 5/24/2025 | |
4.10.2 | 101 | 5/24/2025 | |
4.10.1 | 114 | 5/24/2025 | |
4.9.15 | 183 | 5/22/2025 | |
4.9.14 | 170 | 5/21/2025 | |
4.9.13 | 174 | 5/21/2025 | |
4.9.12 | 183 | 5/19/2025 | |
4.9.11 | 250 | 5/15/2025 | |
4.9.10 | 256 | 5/15/2025 | |
4.9.9 | 250 | 5/15/2025 | |
4.9.8 | 257 | 5/15/2025 | |
4.9.7 | 263 | 5/15/2025 | |
4.9.6 | 267 | 5/15/2025 | |
4.9.5 | 244 | 5/12/2025 | |
4.9.4 | 218 | 5/12/2025 | |
4.9.3 | 150 | 5/9/2025 | |
4.9.2 | 191 | 5/9/2025 | |
4.9.1 | 172 | 5/9/2025 | |
4.8.5 | 452 | 4/25/2025 | |
4.8.4 | 257 | 4/25/2025 | |
4.8.3 | 270 | 4/24/2025 | |
4.8.2 | 280 | 4/24/2025 | |
4.8.1 | 208 | 4/24/2025 | |
4.7.10 | 194 | 4/23/2025 | |
4.7.9 | 219 | 4/15/2025 | |
4.7.8 | 211 | 4/14/2025 | |
4.7.7 | 224 | 4/14/2025 | |
4.7.6 | 235 | 4/14/2025 | |
4.7.5 | 210 | 4/14/2025 | |
4.7.4 | 217 | 4/14/2025 | |
4.7.3 | 221 | 4/14/2025 | |
4.7.2 | 211 | 4/14/2025 | |
4.7.1 | 219 | 4/14/2025 | |
4.6.1 | 189 | 4/9/2025 | |
4.6.0 | 195 | 4/9/2025 | |
4.5.9 | 187 | 4/8/2025 | |
4.5.8 | 186 | 4/8/2025 | |
4.5.7 | 190 | 4/7/2025 | |
4.5.6 | 223 | 4/7/2025 | |
4.5.5 | 201 | 4/7/2025 | |
4.5.4 | 187 | 4/7/2025 | |
4.5.3 | 216 | 4/7/2025 | |
4.5.2 | 178 | 4/7/2025 | |
4.5.1 | 192 | 4/7/2025 | |
4.5.0 | 197 | 4/7/2025 | |
4.3.0 | 198 | 4/2/2025 | |
4.2.1 | 179 | 4/2/2025 | |
4.2.0 | 194 | 4/2/2025 | |
4.1.0 | 189 | 4/1/2025 | |
4.0.0 | 208 | 4/1/2025 | |
3.2.1 | 515 | 3/25/2025 | |
3.2.0 | 512 | 3/25/2025 | |
3.1.0 | 110 | 3/22/2025 | |
3.0.0 | 147 | 3/21/2025 | |
2.0.0 | 189 | 3/20/2025 | |
1.5.0 | 157 | 3/20/2025 | |
1.4.0 | 175 | 3/20/2025 | |
1.3.0 | 184 | 3/20/2025 | |
1.2.0 | 110 | 3/15/2025 | |
1.1.0 | 110 | 3/15/2025 | |
1.0.0 | 103 | 3/15/2025 |