专门为.NET开发者提供的 DeepSeek API SDK.
- 列出模型
- 对话补全(包含流式)
- FIM实例(包含流式)
- 查询余额
请到官方网站,注册并申请DeepSeek的ApiKey
.
.NET版本:.NET8
dotnet add package Ater.DeepSeek.Core
提供了两种方式进行实例化:
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);
// 通过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
更多使用示例.