Location>code7788 >text

1. From DeepSeek API calls to Semantic Kernel integration: Deep analysis chatbot development full link

Popularity:53 ℃/2025-03-17 15:37:33

Introduction: The evolution of chatbot development paradigm in the AI ​​era

At the moment when generative AI technology is exploded, chat robot development based on large language model (LLM) has formed a standardized technology link. This article will combine the DeepSeek API with Microsoft's Semantic Kernel framework to demonstrate the complete development process from basic API calls to advanced framework integration in C# language.

Environment preparation and basic configuration

  • .NET 9 SDK
  • Visual Studio 2022 or VSCode
  • DeepSeek API KeyOfficial website application

DeepSeek API basic call

The Endpoint address of the DeepSeek API is:/chat/completions, relevant documents can be viewedOfficial Documentation

  1. Single-wheel dialogue implementation
    Code Example
public async Task<ResponseBody> GetChatMessageContentsAsync(CancellationToken cancellationToken = new CancellationToken())
{
    var client = new HttpClient();
    var request = new HttpRequestMessage(, _builder.Endpoint);
    ("Accept", "application/json");
    ("Authorization", $"Bearer {_builder.ApiKey}");

    _body.Stream = false;
    var content = new StringContent(_body.SerializeObject(), null, "application/json");
     = content;
    var response = await (request, cancellationToken);
    var responseBody = await (cancellationToken);
    return <ResponseBody>(responseBody) ?? new ResponseBody();
}
  1. Streaming response processing
    Code Example
public async IAsyncEnumerable<ResponseBody> GetStreamingChatMessageContentsAsync([EnumeratorCancellation] CancellationToken cancellationToken = new CancellationToken())
{
    var client = new HttpClient();
    var request = new HttpRequestMessage(, _builder.Endpoint);
    ("Accept", "application/json");
    ("Authorization", $"Bearer {_builder.ApiKey}");
    
    _body.Stream = true;
    var content = new StringContent(_body.SerializeObject(), null, "application/json");
     = content;
    var response = await (request, cancellationToken);
    var stream = await (cancellationToken);
    var reader = new StreamReader(stream);
    while (!)
    {
        var line = await (cancellationToken);
        if ((line) || (":")) continue;
        if (("data: "))
        {
            var jsonData = line["data: ".Length ..];
            if (jsonData == "[DONE]") break;
            yield return <ResponseBody>(jsonData) ?? new ResponseBody();
        }
    }
}

Semantic Kernel Framework Integration

Semantic Kernelis a lightweight open source development kit that can be used to easily generate AI agents and integrate the latest AI models into C#, Python, or Java code bases. It acts as an efficient middleware for rapid delivery of enterprise-level solutions.
The DeepSeek API integrates with the Semantic Kernel framework to quickly realize chatbot development based on large language models. Due to the compatibility of the DeepSeek API with the OpenAI API, the integration of the DeepSeek API with the Semantic Kernel framework is very simple. Quickly implement the integration of the DeepSeek API with the Semantic Kernel framework by simply using OpenAI's connector.

  1. NuGet package installation
dotnet add package 
  1. Semantic Kernel Initialization
var openAIClientCredential = new ApiKeyCredential(apiKey);
var openAIClientOption = new OpenAIClientOptions
{
    Endpoint = new Uri(""),

};
var builder = ()
    .AddOpenAIChatCompletion(modelId, new OpenAIClient(openAIClientCredential, openAIClientOption));

var kernel = ();
  1. Chatbot Development
var chatCompletionService = <IChatCompletionService>();

("😀User >> "+ ().Content);
var result = (
    chatHistory
);
("👨Assistant >> ");
await foreach (var item in result)
{
    (200);
    ();
}

Code Example