DbGpt.Client 1.0.1

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

DbGpt.Client

DB-GPT .NET SDK — 基于 DB-GPT REST API 封装的 .NET 客户端库,支持流式对话、App、Flow(AWEL DAG)、知识库管理。

安装

dotnet add package DbGpt.Client

如果只需要接口和模型定义(不引入 HTTP 实现):

dotnet add package DbGpt.Client.Abstractions

快速开始

1. 注册服务

// Program.cs 或 ABP Module 的 ConfigureServices
builder.Services.AddDbGptClient(options =>
{
    options.BaseUrl = "http://localhost:5670";
    options.ApiKey  = "dbgpt";
});

2. 普通对话(流式)

public class ChatService
{
    private readonly IChatClient _chat;

    public ChatService(IChatClient chat) => _chat = chat;

    public async Task<string> AskAsync(string question)
    {
        var sb = new StringBuilder();

        await foreach (var chunk in _chat.ChatNormalAsync(question, "gpt-4o"))
        {
            sb.Append(chunk.Content);
        }

        return sb.ToString();
    }
}

3. 多轮对话

// 第一轮,保存 conv_uid
string? convUid = null;

await foreach (var chunk in _chat.ChatNormalAsync("你好", "gpt-4o"))
{
    convUid ??= chunk.Id;  // 首帧 id 即为 conv_uid
    Console.Write(chunk.Content);
}

// 第二轮,传入 convUid 续接上下文
await foreach (var chunk in _chat.ChatNormalAsync("继续", "gpt-4o", convUid))
{
    Console.Write(chunk.Content);
}

4. App 对话

await foreach (var chunk in _chat.ChatAppAsync("帮我分析数据", "gpt-4o", "your-app-code"))
{
    Console.Write(chunk.Content);
}

5. Flow 对话(AWEL DAG)

await foreach (var chunk in _chat.ChatFlowAsync("触发工作流", "gpt-4o", "your-flow-uid"))
{
    Console.Write(chunk.Content);
}

6. 知识库对话

// 注意:chat_param 传知识库的 name,不是 id
await foreach (var chunk in _chat.ChatKnowledgeAsync("什么是 AWEL?", "gpt-4o", "your-space-name"))
{
    Console.Write(chunk.Content);
}

7. 非流式对话

var response = await _chat.ChatAsync(new ChatRequest
{
    Messages = "用一句话介绍 DB-GPT",
    Model    = "gpt-4o",
    Stream   = false
});

Console.WriteLine(response.Choices[0].Message?.Content);

8. Flow 管理

public class FlowService
{
    private readonly IFlowClient _flow;

    public FlowService(IFlowClient flow) => _flow = flow;

    // 列出所有 Flow
    public Task<List<FlowModel>> ListAsync() => _flow.ListAsync();

    // 创建 Flow
    public Task<FlowModel> CreateAsync(FlowModel flow) => _flow.CreateAsync(flow);

    // 删除 Flow
    public Task<FlowModel> DeleteAsync(string flowUid) => _flow.DeleteAsync(flowUid);
}

9. 知识库管理

public class KnowledgeService
{
    private readonly IKnowledgeClient _knowledge;

    public KnowledgeService(IKnowledgeClient knowledge) => _knowledge = knowledge;

    // 创建知识库
    public Task<SpaceModel> CreateAsync(string name) =>
        _knowledge.CreateSpaceAsync(new SpaceModel
        {
            Name       = name,
            VectorType = "Chroma"
        });

    // 删除知识库
    public Task<SpaceModel> DeleteAsync(string spaceId) =>
        _knowledge.DeleteSpaceAsync(spaceId);
}

配置项

属性 类型 默认值 说明
BaseUrl string http://localhost:5670 DB-GPT 服务地址
ApiKey string dbgpt API Key,对应 DB-GPT 的 API_KEYS 环境变量
Timeout TimeSpan 30 秒 普通请求超时
StreamTimeout TimeSpan 5 分钟 流式请求超时

对话模式说明

chat_mode 快捷方法 chat_param
chat_normal ChatNormalAsync 无
chat_app ChatAppAsync app_code
chat_flow ChatFlowAsync flow uid
chat_knowledge ChatKnowledgeAsync space name(非 id)

异常处理

try
{
    await foreach (var chunk in _chat.ChatNormalAsync("Hello", "gpt-4o"))
    {
        Console.Write(chunk.Content);
    }
}
catch (DbGptAuthException ex)
{
    // API Key 无效或未授权(HTTP 401)
    Console.WriteLine($"认证失败:{ex.Message}");
}
catch (DbGptApiException ex)
{
    // 服务端返回 4xx / 5xx
    Console.WriteLine($"API 错误 [{ex.StatusCode}]:{ex.ResponseBody}");
}
catch (DbGptStreamException ex)
{
    // SSE 流式传输中断或解析失败
    Console.WriteLine($"流式错误:{ex.Message}");
}

异常类型

异常类 触发场景
DbGptAuthException HTTP 401,API Key 无效
DbGptApiException HTTP 4xx / 5xx,携带 StatusCode 和 ResponseBody
DbGptStreamException SSE 流连接中断或帧解析失败

项目结构

DbGpt.Client.Abstractions/   # 接口和模型,可独立引用
  Exceptions/                # 异常类型定义
  Chat/                      # IChatClient、ChatRequest、ChatResponse、ChatStreamChunk
  Apps/                      # IAppClient、AppModel
  Flows/                     # IFlowClient、FlowModel
  Knowledge/                 # IKnowledgeClient、SpaceModel

DbGpt.Client/                # SDK 核心实现
  Core/                      # DbGptHttpClient、SseStreamReader、DbGptClientOptions
  Chat/                      # ChatClient
  Apps/                      # AppClient
  Flows/                     # FlowClient
  Knowledge/                 # KnowledgeClient
  Extensions/                # ServiceCollectionExtensions(AddDbGptClient)

DbGpt.Client.Tests/          # 单元测试(xUnit v3 + Moq)
  Helpers/                   # MockHttpHelper
  Chat/                      # ChatClientTests(11 个测试)
  Flows/                     # FlowClientTests(6 个测试)
  Knowledge/                 # KnowledgeClientTests(7 个测试)

环境要求

  • .NET 8.0 或更高版本
  • DB-GPT v0.7.4 或更高版本

License

MIT

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

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
1.0.1 98 3/11/2026
1.0.0 78 3/11/2026