Bitzsoft.Integrations.Translation.AI.OpenAI 1.0.0

dotnet add package Bitzsoft.Integrations.Translation.AI.OpenAI --version 1.0.0
                    
NuGet\Install-Package Bitzsoft.Integrations.Translation.AI.OpenAI -Version 1.0.0
                    
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.Translation.AI.OpenAI" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.Translation.AI.OpenAI" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.Translation.AI.OpenAI" />
                    
Project file
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.Translation.AI.OpenAI --version 1.0.0
                    
#r "nuget: Bitzsoft.Integrations.Translation.AI.OpenAI, 1.0.0"
                    
#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.Translation.AI.OpenAI@1.0.0
                    
#: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.Translation.AI.OpenAI&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.Translation.AI.OpenAI&version=1.0.0
                    
Install as a Cake Tool

Bitzsoft.Integrations.Translation.AI.OpenAI

OpenAI 兼容网关大模型翻译实现包 -- 通吃 OpenAI / 智谱 GLM / DeepSeek / Kimi / 通义千问等 compatible 端点(一个 chat/completions 走天下),复用既有 Bitzsoft.Integrations.AI.Abstractions 的 IChatClient 体系(端点 / 模型 / 密钥租户感知,密钥进凭据存储),实现 IAiTranslationProvider 契约。

功能特性

  • 一套代码通吃兼容网关:端点差异由 AI 域内置 Profile(openai / zhipu / kimi / qwen / deepseek)吸收
  • 法律术语约束提示词:内置法律与商务文书级系统提示词模板,Terms 逐行注入强约束译法
  • 批量分段策略:长文本按 SegmentCharLimit(段落 → 句子 → 硬截断三级边界)切分、译文按原始分隔符回拼
  • JSON 模式输出 + 失败回退:单译文 {"translation":...} / 批量 {"translations":[...]},response_format=json_object 下发;解析失败自动回退纯文本重译,批量解析失败回退逐条
  • 风格 / 温度 / 提示词模板:Style / Instructions / SystemPromptTemplate(占位符可自定义)/ Temperature 全量支持
  • 语言检测:大模型 JSON 输出识别语种并归一化统一编码
  • 健康探测:单次最小补全(限定 8 token)

与 AI 域抽象的关系(评审结论)

模型调用复用 AI.Abstractions 的 IAIChatClientFactory / IChatClient(密钥不落配置、租户凭据解析、审计遥测开箱即用);翻译特有语义(术语注入、统一语言编码、分段回拼、JSON 回退、领域错误码)全部落在 Translation.Abstractions 与本包,两个抽象互不依赖。

安装

dotnet add package Bitzsoft.Integrations.Translation.AI.OpenAI

配置

appsettings.json(翻译编排参数)

{
  "Translation": {
    "OpenAI": {
      "ClientName": "translation",
      "ModelId": "glm-4.6",
      "SegmentCharLimit": 6000,
      "MaxBatchItems": 20,
      "EnableJsonMode": true,
      "DefaultTemperature": 0.2,
      "DefaultDomainHint": "法律合同"
    }
  }
}
配置项 说明 必填 默认值
ClientName AI 命名客户端(与 AddBitzChatClient 注册名一致) 否 translation
ModelId 模型覆盖(gpt-4o / glm-4.6 / deepseek-chat ...) 否 命名 AiOptions 默认模型
SegmentCharLimit 单分段字符上限 否 6000
MaxBatchItems 单请求批量条数上限 否 20
EnableJsonMode JSON 模式优先(失败自动纯文本回退) 否 true
DefaultTemperature 默认采样温度 否 0.2
DefaultSystemPromptTemplate / DefaultStyle / DefaultDomainHint / DefaultInstructions 默认提示词参数 否 内置法律文书级模板

注册服务

using Bitzsoft.Integrations.AI;
using Microsoft.Extensions.DependencyInjection;

// 1. 注册 AI 命名客户端(端点 + 模型 + 凭据存储,密钥不落配置)
builder.Services.AddInMemoryIntegrationCredentials();
builder.Services.AddBitzChatClient("translation", options =>
{
    options.ProviderId = "zhipu";
    options.ProfileId = "zhipu";
    options.ModelId = "glm-4.6";
    options.Endpoint = "https://open.bigmodel.cn/api/paas/v4";
});

// 2. 注册大模型翻译(引用上一步的命名客户端)
builder.Services.AddBitzsoftOpenAiTranslation(builder.Configuration);

DeepSeek / Kimi / Qwen / OpenAI 官方等兼容端点只需换 ProfileId + Endpoint + ModelId,翻译包零改动。

使用示例

// AI 契约:术语 + 风格 + 温度
var provider = factory.GetProvider(TranslationProviderCode.OpenAI);
var result = await ((IAiTranslationProvider)provider).TranslateAsync(new AiTranslationRequest
{
    Text = longContractText,          // 超长自动分段回拼
    SourceLanguage = "auto",
    TargetLanguage = "zh",
    Terms = new[]
    {
        new TranslationTerm("force majeure", "不可抗力"),
        new TranslationTerm("consideration", "对价"),
    },
    Style = "正式法律文书",
    Instructions = "保留条款编号不译",
    Temperature = 0.1
});

能力矩阵

能力 支持 说明
单文本翻译 ✅ 超长文本分段回拼
批量翻译 ✅ JSON 数组单请求,失败逐条回退,超 MaxBatchItems 自动分块
语言检测 ✅ 大模型 JSON 输出识别并归一化
领域词汇表(Terms) ✅ 提示词强约束 法律文书级系统提示词注入
风格 / 指令 / 提示词模板 / 温度 ✅ 全量支持
HTML 格式 ⚠️ 以纯文本传输,标签保留依赖模型行为
健康探测 ✅ 最小补全(8 token)

错误码收敛

信号 统一错误码
网络异常 / DNS 解析失败 NetworkUnreachable
超时 Timeout
模型侧失败 / 空译文 OperationFailed / InvalidResponse
JSON 解析失败 自动纯文本回退(不报错)

鉴权 / 配额类错误由 AI 域 IChatClient 底层抛出,经 OperationFailed 包装;需要细分时建议在 AI 域凭据与网关层排障。

相关包

Product 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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.Translation.AI.OpenAI:

Package Downloads
Bitzsoft.Integrations.Translation.All

翻译服务聚合包 — 传统机翻(阿里云 / 百度 / 腾讯云 / 有道 / DeepL / Google 官方)+ AI 翻译(通义 Qwen-MT / 百度大模型 / 火山引擎大模型,OpenAI 兼容 / Anthropic net8+),配置存在性驱动注册

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 69 10/21/2026