DeepSeek AI Integration with .NET MAUI Smart Components
19 Dec 20256 minutes to read
The Syncfusion .NET MAUI AI-powered components can enhance applications with intelligent capabilities. You can integrate DeepSeek using the IChatInferenceService interface, which standardizes communication between the editor and your custom AI service.
Setting Up DeepSeek
-
Obtain a DeepSeek API Key
Create an account at DeepSeek Platform, sign in, and navigate to API Keys to generate an API key. -
Review Model Specifications
Refer to DeepSeek Models Documentation for details on available models (e.g.,deepseek-chat).
Define Request and Response Models
Create a file named DeepSeekModels.cs in the Services folder and add:
public class DeepSeekMessage
{
public string? Role { get; set; }
public string? Content { get; set; }
}
public class DeepSeekChatRequest
{
public string? Model { get; set; }
public float Temperature { get; set; }
public List<DeepSeekMessage>? Messages { get; set; }
}
public class DeepSeekChatResponse
{
public List<DeepSeekChoice>? Choices { get; set; }
}
public class DeepSeekChoice
{
public DeepSeekMessage? Message { get; set; }
}Create a DeepSeek AI Service
This service manages requests to the DeepSeek Chat Completions endpoint and returns the generated text.
- Create a
Servicesfolder in your project. - Add a new file named
DeepSeekAIService.csin theServicesfolder. - Implement the service as shown below:
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using System.Net;
using System.Text;
using System.Text.Json;
public class DeepSeekAIService
{
private readonly string _apiKey = ""; // API key
private readonly string _modelName = "deepseek-chat"; // Example model
private readonly string _endpoint = "https://api.deepseek.com/v1/chat/completions";
private static readonly HttpClient HttpClient = new(new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(30),
EnableMultipleHttp2Connections = true
})
{
DefaultRequestVersion = HttpVersion.Version20 // Fallback to HTTP/2 for compatibility
};
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public DeepSeekAIService()
{
if (!HttpClient.DefaultRequestHeaders.Contains("Authorization"))
{
HttpClient.DefaultRequestHeaders.Clear();
HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}");
}
}
public async Task<string> CompleteAsync(List<ChatMessage> chatMessages)
{
var requestBody = new DeepSeekChatRequest
{
Model = _modelName,
Temperature = 0.7f, // Controls response randomness (0.0 to 1.0)
Messages = chatMessages.Select(m => new DeepSeekMessage
{
Role = m.Role == ChatRole.User ? "user" : "system", // Align with DeepSeek API roles
Content = m.Text
}).ToList()
};
var content = new StringContent(JsonSerializer.Serialize(requestBody, JsonOptions), Encoding.UTF8, "application/json");
try
{
var response = await HttpClient.PostAsync(_endpoint, content);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var responseObject = JsonSerializer.Deserialize<DeepSeekChatResponse>(responseString, JsonOptions);
return responseObject?.Choices?.FirstOrDefault()?.Message?.Content ?? "No response from DeepSeek.";
}
catch (Exception ex) when (ex is HttpRequestException || ex is JsonException)
{
throw new InvalidOperationException("Failed to communicate with DeepSeek API.", ex);
}
}
}Implement IChatInferenceService
Create DeepSeekInferenceService.cs:
using Syncfusion.Maui.SmartComponents;
public class DeepSeekInferenceService : IChatInferenceService
{
private readonly DeepSeekAIService _deepSeekService;
public DeepSeekInferenceService(DeepSeekAIService deepSeekService)
{
_deepSeekService = deepSeekService;
}
public async Task<string> GenerateResponseAsync(List<ChatMessage> chatMessages)
{
return await _deepSeekService.CompleteAsync(chatMessages);
}
}Register Services in MAUI
Update MauiProgram.cs:
using Syncfusion.Maui.Core.Hosting;
using Syncfusion.Maui.SmartComponents;
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore();
builder.Services.AddSingleton<DeepSeekAIService>();
builder.Services.AddSingleton<IChatInferenceService, DeepSeekInferenceService>();