Skip to content

Latest commit

 

History

History
139 lines (103 loc) · 3.51 KB

README_cn.md

File metadata and controls

139 lines (103 loc) · 3.51 KB

DeepSeekSDK-NET

NuGet Version

专门为.NET开发者提供的 DeepSeek API SDK.

English Docs

🚀 功能特性

  • 列出模型
  • 对话补全(包含流式)
  • FIM实例(包含流式)
  • 查询余额

使用

请到官方网站,注册并申请DeepSeek的ApiKey.

.NET版本:.NET8

安装Nugget包

Ater.DeepSeek.Core

dotnet add package Ater.DeepSeek.Core

实例化DeepSeekClient

提供了两种方式进行实例化:

public DeepSeekClient(string apiKey)public DeepSeekClient(HttpClient http, string apiKey);

第一种只需要提供apiKey即可创建实例;

第二种提供了HttpClient参数,适合通过HttpClientFactory来维护HttpClient,然后进行实例化。

Note

内部HttpClient的超时时间默认为120秒,可通过SetTimeout()方法在发送请求前设置,或通过CancellationTokenSource设置具体请求的超时时间。

Tip

如果你想调用本地模型,可尝试自定义HttpClient,并设置BaseAddress为本地地址。

调用方法

一共提供了三个异步方法:

Task<ModelResponse?> ListModelsAsync(CancellationToken cancellationToken);

Task<ChatResponse?> ChatAsync(ChatRequest request, CancellationToken cancellationToken);

Task<IAsyncEnumerable<Choice>?> ChatStreamAsync(ChatRequest request, CancellationToken cancellationToken);

Task<ChatResponse?> CompletionsAsync(CompletionRequest request, CancellationToken cancellationToken);

Task<IAsyncEnumerable<Choice>?> CompletionsStreamAsync(CompletionRequest request, CancellationToken cancellationToken);

Task<UserResponse?> GetUserBalanceAsync(CancellationToken cancellationToken);

获取模型列表示例

// 通过apiKey创建实例
var client = new DeepSeekClient(apiKey);

var modelResponse = await client.ListModelsAsync(new CancellationToken());
if (modelResponse is null)
{
    Console.WriteLine(client.ErrorMsg);
    return;
}
foreach (var model in modelResponse.Data)
{
    Console.WriteLine(model);
}

获取对话示例

// 通过apiKey创建实例
var client = new DeepSeekClient(apiKey);
// 构造请求体
var request = new ChatRequest
{
    Messages = [
        Message.NewSystemMessage("你是一个语言翻译家"),
        Message.NewUserMessage("请翻译'它们害怕极了!'为英语!")
    ],
    // 指定模型
    Model = Constant.Model.ChatModel
};

var chatResponse = await client.ChatAsync(request, new CancellationToken());
if (chatResponse is null)
{
    Console.WriteLine(client.ErrorMsg);
}
Console.WriteLine(chatResponse?.Choices.First().Message?.Content);

获取对话(Stream)

// 通过apiKey创建实例
var client = new DeepSeekClient(apiKey);
// 构造请求体
var request = new ChatRequest
{
    Messages = [
        Message.NewSystemMessage("你是一个语言翻译家"),
        Message.NewUserMessage("请翻译'它们害怕极了!'为英语!")
    ],
    // 指定模型
    Model = Constant.Model.ChatModel
};

var choices = await client.ChatStreamAsync(request, new CancellationToken());
if (choices is null)
{
    Console.WriteLine(client.ErrorMsg);
    return;
}
await foreach (var choice in choices)
{
    Console.Write(choice.Delta?.Content);
}
Console.WriteLine();

Tip

更多使用示例.