从DeepSeek API调用到Semantic Kernel集成:深度解析聊天机器人开发全链路

B站影视 电影资讯 2025-03-18 17:10 1

摘要:public async TaskGetChatMessageContentsAsync(CancellationToken cancellationToken = new CancellationToken){var client

DeepSeek API的Endpoint地址为:,相关文档可查看官方文档

1. 单轮对话实现

代码示例

public async Task GetChatMessageContentsAsync(CancellationToken cancellationToken = new CancellationToken)
{
var client = new HttpClient;
var request = new HttpRequestMessage(HttpMethod.Post, _builder.Endpoint);
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", $"Bearer {_builder.ApiKey}");

_body.Stream =false;
var content = new StringContent(_body.SerializeObject, , "application/json");
request.Content = content;
var response = await client.SendAsync(request, cancellationToken);
var responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
return JsonConvert.DeserializeObject(responseBody) ?? new ResponseBody;
}

2. 流式响应处理

代码示例

public async IAsyncEnumerable GetStreamingChatMessageContentsAsync([EnumeratorCancellation] CancellationToken cancellationToken = new CancellationToken)
{
var client = new HttpClient;
var request = new HttpRequestMessage(HttpMethod.Post, _builder.Endpoint);
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", $"Bearer {_builder.ApiKey}");

_body.Stream =true;
var content = new StringContent(_body.SerializeObject, , "application/json");
request.Content = content;
var response = await client.SendAsync(request, cancellationToken);
var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
var reader = new StreamReader(stream);
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync(cancellationToken);
if (string.IsOrEmpty(line) || line.StartsWith(":")) continue;
if (line.StartsWith("data: "))
{
var jsonData = line["data: ".Length ..];
if (jsonData == "[DONE]") break;
yieldreturn JsonConvert.DeserializeObject(jsonData) ?? new ResponseBody;
}
}
}

Semantic Kernel是一种轻型开源开发工具包,可用于轻松生成 AI 代理并将最新的 AI 模型集成到 C#、Python 或 Java 代码库中。 它充当一个高效的中间件,可实现企业级解决方案的快速交付。

DeepSeek API与Semantic Kernel框架集成,可快速实现基于大语言模型的聊天机器人开发。由于DeepSeek API与OpenAI API的兼容性,因此DeepSeek API与Semantic Kernel框架的集成非常简单。只需使用OpenAI的连接器,即可快速实现DeepSeek API与Semantic Kernel框架的集成。

1. NuGet包安装

dotnet add package Microsoft.SemanticKernel

2. Semantic Kernel初始化

var openAIClientCredential = new ApiKeyCredential(apiKey);
var openAIClientOption = new OpenAIClientOptions
{
Endpoint = new Uri("https://api.deepseek.com"),

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

var kernel = builder1.Build;

3. 聊天机器人开发

var chatCompletionService = kernel1.GetRequiredService;

Console.WriteLine("😀User >> "+ chatHistory.Last.Content);
var result = chatCompletionService.GetStreamingChatMessageContentsAsync(
chatHistory
);
Console.Write("👨Assistant >> ");
await foreach (var item in result)
{
Thread.Sleep(200);
Console.Write(item.Content);
}

代码示例

来源:opendotnet

相关推荐