Skip to content

niltor/DeepSeekSDK-NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeepSeekSDK-NET

NuGet Version

DeepSeek API SDK specifically for .NET developers

中文文档

🚀 Features

  • List models
  • Chat & Chat streaming
  • Completions & Completions streaming (beta)
  • User balance

Usage Requirements

Usage

Please go to official website, register and apply for DeepSeek's ApiKey

Supported .NET version: .NET8

Install Nuget package

Ater.DeepSeek.Core

dotnet add package Ater.DeepSeek.Core

Instantiate DeepSeekClient

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.

Calling method

Three asynchronous methods are provided:

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);

List Models Sample

// 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);
}

Chat Examples

// 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);

Chat Examples (Stream)

// 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 = 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

More usage example

About

DeepSeek API SDK for .NET developer

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages