Bitzsoft.Integrations.AI
1.0.0-alpha.5
See the version list below for details.
dotnet add package Bitzsoft.Integrations.AI --version 1.0.0-alpha.5
NuGet\Install-Package Bitzsoft.Integrations.AI -Version 1.0.0-alpha.5
<PackageReference Include="Bitzsoft.Integrations.AI" Version="1.0.0-alpha.5" />
<PackageVersion Include="Bitzsoft.Integrations.AI" Version="1.0.0-alpha.5" />
<PackageReference Include="Bitzsoft.Integrations.AI" />
paket add Bitzsoft.Integrations.AI --version 1.0.0-alpha.5
#r "nuget: Bitzsoft.Integrations.AI, 1.0.0-alpha.5"
#:package Bitzsoft.Integrations.AI@1.0.0-alpha.5
#addin nuget:?package=Bitzsoft.Integrations.AI&version=1.0.0-alpha.5&prerelease
#tool nuget:?package=Bitzsoft.Integrations.AI&version=1.0.0-alpha.5&prerelease
Bitzsoft.Integrations.AI
AI 服务集成 — 基于 Microsoft.Extensions.AI 的 IChatClient 抽象,提供轻量 OpenAI 兼容客户端。
功能特性
- 基于
Microsoft.Extensions.AI.IChatClient标准抽象,Provider 无关 - 兼容所有 OpenAI Chat Completions API 协议的服务商(OpenAI、DeepSeek、通义千问等)
- 支持命名客户端(Keyed DI),满足多租户或多模型场景
- 支持结构化输出(
GetObjectAsync<T>),直接获取强类型对象 - 内置 MEAI 中间件管道:OpenTelemetry 遥测、分布式缓存、日志记录
安装
dotnet add package Bitzsoft.Integrations.AI
配置
在 appsettings.json 中添加以下配置节:
{
"AI": {
"Endpoint": "https://api.openai.com/v1",
"ApiKey": "sk-your-api-key",
"ModelId": "gpt-4o",
"SystemPrompt": "你是一个有帮助的 AI 助手。"
}
}
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Endpoint |
string |
https://api.openai.com/v1 |
OpenAI 兼容端点地址 |
ApiKey |
string |
- | API 密钥 |
ModelId |
string |
gpt-4o |
默认模型名称 |
SystemPrompt |
string |
你是一个有帮助的 AI 助手。 |
默认系统提示词 |
EnableTelemetry |
bool |
false |
是否启用 OpenTelemetry 遥测中间件 |
EnableCaching |
bool |
false |
是否启用分布式缓存中间件(需注册 IDistributedCache) |
兼容历史配置:
ApiUrl映射到Endpoint,DefaultModel映射到ModelId。
注册服务
基础注册
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.AI;
// Program.cs
services.AddBitzChatClient(options =>
{
options.Endpoint = "https://api.openai.com/v1";
options.ApiKey = "sk-your-api-key";
options.ModelId = "gpt-4o";
});
多租户 / 多模型注册
// 注册 GPT-4o 客户端
services.AddBitzChatClient("GPT4", options =>
{
options.Endpoint = "https://api.openai.com/v1";
options.ApiKey = "sk-openai-key";
options.ModelId = "gpt-4o";
});
// 注册 DeepSeek 客户端
services.AddBitzChatClient("DeepSeek", options =>
{
options.Endpoint = "https://api.deepseek.com/v1";
options.ApiKey = "sk-deepseek-key";
options.ModelId = "deepseek-chat";
});
使用示例
注入并使用 IChatClient
using Microsoft.Extensions.AI;
public class DocumentService(IChatClient chatClient)
{
/// <summary>
/// 对文档内容进行摘要提取
/// </summary>
public async Task<string> SummarizeAsync(string documentContent, CancellationToken ct = default)
{
var messages = new List<ChatMessage>
{
new(ChatRole.System, "你是一个专业的文档摘要助手。"),
new(ChatRole.User, $"请对以下文档内容进行摘要:\n\n{documentContent}")
};
var response = await chatClient.GetResponseAsync(messages, cancellationToken: ct);
return response.Text;
}
/// <summary>
/// 流式输出
/// </summary>
public async IAsyncEnumerable<string> SummarizeStreamAsync(
string documentContent,
[EnumeratorCancellation] CancellationToken ct = default)
{
var messages = new List<ChatMessage>
{
new(ChatRole.System, "你是一个专业的文档摘要助手。"),
new(ChatRole.User, documentContent)
};
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, cancellationToken: ct))
{
if (update.Text is not null)
{
yield return update.Text;
}
}
}
}
结构化输出(Structured Output)
通过 GetObjectAsync<T> 直接获取强类型对象,无需手动 JSON 反序列化:
using Bitzsoft.Integrations.AI;
// 定义返回类型
public record SentimentResult(string Sentiment, double Confidence, string Summary);
public class AnalysisService(IChatClient chatClient)
{
public async Task<SentimentResult?> AnalyzeAsync(string text, CancellationToken ct = default)
{
return await chatClient.GetObjectAsync<SentimentResult>(
message: $"请分析以下文本的情感倾向:\n{text}",
systemPrompt: "你是情感分析助手,返回 JSON 格式:sentiment(正面/负面/中性)、confidence(0-1)、summary");
}
}
使用命名客户端(多模型场景)
public class MultiModelService(
[FromKeyedServices("BitzChatClient_GPT4")] IChatClient gpt4Client,
[FromKeyedServices("BitzChatClient_DeepSeek")] IChatClient deepseekClient)
{
public Task<string> AskGptAsync(string question) =>
gpt4Client.GetResponseAsync(question);
public Task<string> AskDeepSeekAsync(string question) =>
deepseekClient.GetResponseAsync(question);
}
相关包
| 包名 | 说明 |
|---|---|
| Bitzsoft.Integrations.AI.Abstractions | AI 共享抽象层(AiOptions、DI 扩展) |
| Bitzsoft.Integrations.SemanticKernel | Semantic Kernel 编排层(插件、函数调用) |
| Bitzsoft.Integrations.RAG | 检索增强生成(Qdrant + IEmbeddingGenerator) |
| 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.AI (>= 10.7.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.7.0)
- Microsoft.Extensions.Options (>= 10.0.9)
- OpenAI (>= 2.11.0)
-
net8.0
- Bitzsoft.Integrations.AI.Abstractions (>= 1.0.0-alpha.5)
- Microsoft.Extensions.AI (>= 10.7.0)
- Microsoft.Extensions.AI.OpenAI (>= 10.7.0)
- Microsoft.Extensions.Options (>= 10.0.9)
- OpenAI (>= 2.11.0)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Bitzsoft.Integrations.AI:
| Package | Downloads |
|---|---|
|
Bitzsoft.Integrations.All
Bitzsoft 第三方集成聚合包 — net5.0 包含传统连接器,net8.0+ 额外包含受上游 TFM 限制的 AI 模块 |
|
|
Bitzsoft.Integrations.AI.Anthropic
Anthropic Claude 原生 Messages 协议与 Microsoft.Extensions.AI 集成 |
|
|
Bitzsoft.Integrations.AI.Conversation
AI 会话基础设施 — 会话/分支/回放持久化存储与全链路编排、M.E.AI IChatReducer 压缩与长响应续传、后台长任务队列、FinOps 精确核算与出站脱敏拦截、富消息分片与跨块流式投影、无锁单调主键生成器 |
|
|
Bitzsoft.Integrations.AI.Testing
Bitzsoft AI 与 MCP 测试支持包 — 无测试框架锁定的存储端口契约测试基类、进程内测试替身、受控测试时钟与 FDE 黄金评估运行器 |
|
|
Bitzsoft.Integrations.AI.All
Bitzsoft AI 套件 1.2.6 独立运行期聚合元包 — 一键引入全部 11 个活跃 Bitzsoft.Integrations.AI.* 运行期包(不含已弃用旧包、不含 Ocr、AI.Testing 与可选按需安装的 AI.Redis) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.6 | 66 | 12/3/2026 |
| 1.2.5 | 177 | 11/29/2026 |
| 1.2.4 | 175 | 11/27/2026 |
| 1.2.3 | 235 | 11/24/2026 |
| 1.2.2 | 215 | 11/23/2026 |
| 1.2.1 | 149 | 11/23/2026 |
| 1.2.0 | 243 | 11/21/2026 |
| 1.1.0 | 197 | 11/14/2026 |
| 1.0.5 | 150 | 11/10/2026 |
| 1.0.4 | 269 | 9/23/2026 |
| 1.0.3 | 124 | 9/22/2026 |
| 1.0.2 | 166 | 8/29/2026 |
| 1.0.1 | 200 | 8/3/2026 |
| 1.0.0 | 197 | 8/2/2026 |
| 1.0.0-alpha.10 | 268 | 7/26/2026 |
| 1.0.0-alpha.9 | 100 | 7/12/2026 |
| 1.0.0-alpha.8 | 97 | 7/1/2026 |
| 1.0.0-alpha.7 | 117 | 6/16/2026 |
| 1.0.0-alpha.6 | 105 | 6/16/2026 |
| 1.0.0-alpha.5 | 99 | 6/14/2026 |