Introduction
In this post, we’ll explore how to build a simple console application that interacts with the DeepSeek API, allowing us to test its capabilities in understanding and generating text. By the end, you’ll have a clear understanding of how to integrate this powerful tool into your projects—no complexity, just practical steps. Let’s get started.
For everyone interested, the complete project can be found here: DeepSeek-Demo
Creating the console app
First, create a new console application and install the required NuGet packages.
dotnet new console -n DeepSeekDemo
We moved to the folder for the newly created project.
cd DeepSeekDemo
We install the configuration extension packages to read settings from JSON files (appsettings.json) and the RestSharp package to handle HTTP calls.
Consuming the DeepSeek API
Enter the DeepSeek platform to create your API KEY.
Once you get it, we are going to create an appsettings.json file with these properties.
{
"DEEPSEEK_BASE_URL": "https://api.deepseek.com",
"DEEPSEEK_API_KEY": "YOUR_API_KEY_HERE"
}
Now, let's set up the project structure.
First, we will create the models in the Models folder to handle API requests and responses. We will create the file ChatRequest.cs and inside it.
namespace DeepSeekDemo.Models
{
public class ChatRequest
{
public string Model { get; set; } = "deepseek-chat";
public List<ChatMessage> Messages { get; set; }
public bool Stream { get; set; } = false;
}
public class ChatMessage
{
public string Role { get; set; } = "";
public string Content { get; set; } = "";
}
}
In the same Models folder, we will create the file ChatResponse.cs and inside it.
namespace DeepSeekDemo.Models
{
public class ChatResponse
{
public string Id { get; set; } = "";
public string Object { get; set; } = "";
public long Created { get; set; }
public string Model { get; set; } = "";
public List<ChatChoice> Choices { get; set; }
}
public class ChatChoice
{
public ChatMessage Message { get; set; }
public int Index { get; set; }
public string FinishReason { get; set; } = "";
}
}
Next, we'll create the service file to send the request in the correct format. Within the service folder, we will create the file DeepSeekService.cs and inside it.
using RestSharp;
using Microsoft.Extensions.Configuration;
using DeepSeekDemo.Models;
namespace DeepSeekDemo.Services
{
public class DeepSeekService
{
private readonly RestClient _client;
private readonly string _apiKey;
public DeepSeekService(IConfiguration configuration)
{
_apiKey = configuration["DEEPSEEK_API_KEY"] ?? "";
_client = new RestClient(configuration["DEEPSEEK_BASE_URL"] ?? "");
_client.AddDefaultHeader("Authorization", $"Bearer {_apiKey}");
_client.AddDefaultHeader("Content-Type", "application/json");
}
public async Task<string> SendMessageAsync(string userMessage)
{
var request = new RestRequest
{
Method = Method.Post
};
string systemPrompt = @"You are a debate champion and a master of argumentation.
As a debate assistant, you are here to challenge the user on complex topics and
stimulate an intense and thought-provoking conversation. Each time you initiate an interaction,
you suggest two debate topics and encourage the user to choose one.
Your attitude is challenging, designed to provoke and stimulate critical thinking, pushing the user to defend their stance rigorously.";
var chatRequest = new ChatRequest
{
Messages = new List<ChatMessage>
{
new ChatMessage { Role = "system", Content = systemPrompt },
new ChatMessage { Role = "user", Content = userMessage }
}
};
request.AddJsonBody(chatRequest);
var response = await _client.ExecuteAsync<ChatResponse>(request);
if (!response.IsSuccessful)
{
throw new Exception($"Error: {response.StatusCode} - {response.ErrorMessage}");
}
return response.Data?.Choices?.FirstOrDefault()?.Message?.Content ?? "No response from DeepSeek.";
}
}
}
In the Helpers folder, we will create a class named ConsoleHelper.cs, which will include utilities to enhance the presentation of responses.
namespace DeepSeekDemo.Helpers;
public static class ConsoleHelper
{
public static void PrintBotResponse(string response)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("Bot: ");
Console.ResetColor();
Console.WriteLine(response);
}
}
Now, we handle the user interaction by calling the DeepSeekService in the Program.cs class.
using DeepSeekDemo.Helpers;
using DeepSeekDemo.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(configuration)
.AddSingleton<DeepSeekService>()
.BuildServiceProvider();
var deepSeekService = serviceProvider.GetRequiredService<DeepSeekService>();
var response = await deepSeekService.SendMessageAsync("");
ConsoleHelper.PrintBotResponse(response);
do
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("You: ");
Console.ResetColor();
var userInput = Console.ReadLine();
if (userInput?.ToLower() == "exit")
{
Console.WriteLine("Goodbye! It was a pleasure to debate with you.");
break;
}
if (!string.IsNullOrEmpty(userInput))
{
response = await deepSeekService.SendMessageAsync(userInput);
ConsoleHelper.PrintBotResponse(response);
}
} while (true);
Make sure the appsettings.json file is being copied to the output directory. In your .csproj project file, add the following.
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
And finally, we run the project.
dotnet run
Thank you for reading this guide on DeepSeek for .NET Developers. I hope you found it helpful and inspiring. If you did, please consider following me on GitHub for more projects and updates.
Stay coding!