Bitzsoft.Integrations.AI
1.0.0-alpha.3
This is a prerelease version of Bitzsoft.Integrations.AI.
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.AI --version 1.0.0-alpha.3
NuGet\Install-Package Bitzsoft.Integrations.AI -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.AI" 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.AI" Version="1.0.0-alpha.3" />
<PackageReference Include="Bitzsoft.Integrations.AI" />
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.AI --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.AI, 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.AI@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.AI&version=1.0.0-alpha.3&prerelease
#tool nuget:?package=Bitzsoft.Integrations.AI&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.AI
AI 服务集成 -- 兼容 OpenAI API 的统一客户端,支持 OpenAI、DeepSeek、通义千问等主流大模型服务商。
功能特性
- 兼容所有 OpenAI Chat Completions API 协议的服务商(OpenAI、DeepSeek、通义千问等)
- 内置自动重试机制,可配置最大重试次数与超时时间
- 支持单轮对话、多轮对话及原始请求三种调用方式
- 支持命名客户端(Named Client),满足多租户或多模型场景
- 基于
IHttpClientFactory实现,生命周期管理安全可靠 - 完整的 Token 用量追踪(PromptTokens / CompletionTokens / TotalTokens)
安装
dotnet add package Bitzsoft.Integrations.AI
或通过 csproj 引用:
<PackageReference Include="Bitzsoft.Integrations.AI" Version="*" />
配置
在 appsettings.json 中添加以下配置节:
{
"AI": {
"ApiUrl": "https://api.openai.com/v1/chat/completions",
"ApiKey": "sk-your-api-key",
"DefaultModel": "gpt-4",
"TimeoutSeconds": 60,
"MaxRetries": 3
}
}
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
ApiUrl |
string |
- | AI 服务 Chat Completions 端点地址 |
ApiKey |
string |
- | API 密钥,支持 Bearer sk-xxx 或纯密钥格式 |
DefaultModel |
string |
- | 默认使用的模型名称,如 gpt-4、deepseek-chat |
TimeoutSeconds |
int |
60 |
单次请求超时时间(秒) |
MaxRetries |
int |
3 |
请求失败后的最大重试次数 |
注册服务
基础注册
using Bitzsoft.Integrations.AI;
using Bitzsoft.Integrations.AI.Dto;
// Program.cs / Startup.cs
services.AddAIClient(options =>
{
options.ApiUrl = "https://api.openai.com/v1/chat/completions";
options.ApiKey = "sk-your-api-key";
options.DefaultModel = "gpt-4";
options.TimeoutSeconds = 60;
options.MaxRetries = 3;
});
多租户 / 多模型注册
using Bitzsoft.Integrations.AI;
using Bitzsoft.Integrations.AI.Dto;
// 注册 GPT-4 客户端
services.AddAIClient("GPT4", options =>
{
options.ApiUrl = "https://api.openai.com/v1/chat/completions";
options.ApiKey = "sk-your-openai-key";
options.DefaultModel = "gpt-4";
});
// 注册 DeepSeek 客户端
services.AddAIClient("DeepSeek", options =>
{
options.ApiUrl = "https://api.deepseek.com/v1/chat/completions";
options.ApiKey = "sk-your-deepseek-key";
options.DefaultModel = "deepseek-chat";
});
使用示例
注入并使用 IAIClient
using Bitzsoft.Integrations.AI;
using Bitzsoft.Integrations.AI.Dto;
public class DocumentService
{
private readonly IAIClient _aiClient;
public DocumentService(IAIClient aiClient)
{
_aiClient = aiClient;
}
/// <summary>
/// 对文档内容进行摘要提取
/// </summary>
public async Task<string> SummarizeAsync(string documentContent, CancellationToken ct = default)
{
var result = await _aiClient.SendAsync(
message: $"请对以下文档内容进行摘要:\n\n{documentContent}",
systemPrompt: "你是一个专业的文档摘要助手,请用简洁的语言概括文档核心内容。",
model: null,
cancellationToken: ct);
if (result.Error is not null)
{
throw new InvalidOperationException($"AI 调用失败:{result.Error.Message}");
}
return result.GetContent() ?? string.Empty;
}
/// <summary>
/// 多轮对话场景:结合上下文回答问题
/// </summary>
public async Task<string> ChatWithHistoryAsync(
string userQuestion,
List<AIMessageItem> history,
CancellationToken ct = default)
{
var messages = new List<AIMessageItem>(history)
{
new() { Role = "user", Content = userQuestion }
};
var result = await _aiClient.SendMessagesAsync(messages, cancellationToken: ct);
return result.GetContent() ?? string.Empty;
}
}
使用原始请求精细控制参数
using Bitzsoft.Integrations.AI;
using Bitzsoft.Integrations.AI.Dto;
public class CreativeWritingService
{
private readonly IAIClient _aiClient;
public CreativeWritingService(IAIClient aiClient)
{
_aiClient = aiClient;
}
public async Task<string> GenerateCreativeContentAsync(string topic, CancellationToken ct = default)
{
var request = new AISendDto
{
Model = "gpt-4",
Messages = new List<AIMessageItem>
{
new() { Role = "system", Content = "你是一位创意写作专家" },
new() { Role = "user", Content = $"请围绕「{topic}」写一段创意短文" }
},
Temperature = 1.2,
MaxTokens = 2000,
EnableSearch = false,
Stream = false
};
var result = await _aiClient.SendRequestAsync(request, ct);
// 检查 Token 消耗
if (result.Usage is not null)
{
Console.WriteLine($"Token 消耗 - Prompt: {result.Usage.PromptTokens}, " +
$"Completion: {result.Usage.CompletionTokens}, " +
$"Total: {result.Usage.TotalTokens}");
}
return result.GetContent() ?? string.Empty;
}
}
相关包
| 包名 | 说明 |
|---|---|
| Bitzsoft.Integrations.SemanticKernel | 基于 Microsoft Semantic Kernel 的 AI 编排服务 |
| Bitzsoft.Integrations.RAG | 检索增强生成(Qdrant + Ollama 嵌入集成) |
| Bitzsoft.Integrations.AgentFramework | AI Agent 框架 |
| Bitzsoft.Integrations.McpServer | MCP Server 集成 |
| Bitzsoft.Integrations.Ocr | OCR 文字识别服务 |
| 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.Http.Resilience (>= 9.6.0)
- Microsoft.Extensions.Options (>= 10.0.4)
-
net8.0
- Microsoft.Extensions.Http (>= 10.0.0)
- Microsoft.Extensions.Http.Resilience (>= 9.6.0)
- Microsoft.Extensions.Options (>= 10.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.AI:
| 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 | 60 | 6/16/2026 |
| 1.0.0-alpha.5 | 56 | 6/14/2026 |
| 1.0.0-alpha.4 | 62 | 6/14/2026 |
| 1.0.0-alpha.3 | 60 | 6/7/2026 |
| 1.0.0-alpha.2 | 61 | 5/29/2026 |
| 1.0.0-alpha.1 | 57 | 5/28/2026 |