DeepSeek API SDK specifically for .NET developers
- List models
- Chat & Chat streaming
- Completions & Completions streaming (beta)
- User balance
- Local model support
- ASP.NET Core integration support
Please go to official website, register and apply for DeepSeek's ApiKey
Supported .NET version: .NET8
dotnet add package Ater.DeepSeek.Core
Two methods are provided for instantiation:
public DeepSeekClient(string apiKey);
public DeepSeekClient(HttpClient http, string apiKey);
The first type only requires providing the 'apiKey' to create an instance;
The second method provides a HttpClient
parameter, which is suitable for maintaining the HttpClient
through the HttpClientFactory
and then instance it.
Note
The default timeout for internal HttpClient is 120 seconds, which can be set before sending the request using the 'SetTimeout()' method, or by using the 'CancellationTokeSource' to set the timeout for specific requests.
Tip
If you want to call a local model, try customizing HttpClient
and setting BaseAddress
to the local address.
DeepSeekClient
class provides six asynchronous methods to call DeepSeek's API:
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);
// Create an instance using the 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);
}
// Create an instance using the apiKey
var client = new DeepSeekClient(apiKey);
// Construct the request body
var request = new ChatRequest
{
Messages = [
Message.NewSystemMessage("You are a language translator"),
Message.NewUserMessage("Please translate 'They are scared! ' into English!")
],
// Specify the model
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);
// Create an instance using the apiKey
var client = new DeepSeekClient(apiKey);
// Construct the request body
var request = new ChatRequest
{
Messages = [
Message.NewSystemMessage("You are a language translator"),
Message.NewUserMessage("Please translate 'They are scared! ' into English!")
],
// Specify the model
Model = Constant.Model.ChatModel
};
var choices = 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();
// use local models api
var httpClient = new HttpClient
{
// set your local api address
BaseAddress = new Uri("http://localhost:5000"),
Timeout = TimeSpan.FromSeconds(300),
};
// if have api key
// httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + "your_token");
var localClient = new DeepSeekClient(httpClient);
localClient.SetChatEndpoint("/chat");
localClient.SetCompletionEndpoint("/completions");
var res = await localClient.ChatAsync(new ChatRequest
{
Messages = new List<Message>
{
Message.NewUserMessage("hello")
}
}, new CancellationToken());
return res?.Choices.First().Message?.Content;
Tip
More usage example
dotnet add package Ater.DeepSeek.AspNetCore
using DeepSeek.AspNetCore;
using DeepSeek.Core;
using DeepSeek.Core.Models;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var apiKey = builder.Configuration["DeepSeekApiKey"];
builder.Services.AddDeepSeek(option =>
{
option.BaseAddress = new Uri("https://api.deepseek.com");
option.Timeout = TimeSpan.FromSeconds(300);
option.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + apiKey);
});
var app = builder.Build();
app.MapGet("/test", async ([FromServices] DeepSeekClient client) =>
{
var res = await client.ChatAsync(new ChatRequest
{
Messages = new List<Message>
{
Message.NewUserMessage("Why dotnet is good?")
},
MaxTokens = 200
}, new CancellationToken());
return res?.Choices.First().Message?.Content;
});
app.Run();
app.MapGet("/chat", async (HttpContext context, [FromServices] DeepSeekClient client, CancellationToken token) =>
{
context.Response.ContentType = "text/text;charset=utf-8";
try
{
var choices = client.ChatStreamAsync(new ChatRequest
{
Messages = new List<Message>
{
Message.NewUserMessage("Why dotnet is good?")
},
MaxTokens = 200
}, token);
if (choices != null)
{
await foreach (var choice in choices)
{
await context.Response.WriteAsync(choice.Delta!.Content);
}
}
}
catch (Exception ex)
{
await context.Response.WriteAsync("暂时无法提供服务" + ex.Message);
}
await context.Response.CompleteAsync();
});
Tip
More usage example