引言:AI期间下的谈天机器人开发范式演进
在生成式AI技术发作的当下,基于大语言模子(LLM)的谈天机器人开发已形成尺度化技术链路。本文将结合DeepSeek API与微软Semantic Kernel框架,以C#语言实战演示从底子API调用到高级框架集成的完整开发流程。
环境准备与底子配置
- .NET 9 SDK
- Visual Studio 2022或VSCode
- DeepSeek API密钥 官网申请
DeepSeek API底子调用
DeepSeek API的Endpoint地址为:https://api.deepseek.com/chat/completions,相关文档可查看官方文档
- public async Task<ResponseBody> 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(), null, "application/json");
- request.Content = content;
- var response = await client.SendAsync(request, cancellationToken);
- var responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
- return JsonConvert.DeserializeObject<ResponseBody>(responseBody) ?? new ResponseBody();
- }
复制代码- public async IAsyncEnumerable<ResponseBody> 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(), null, "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.IsNullOrEmpty(line) || line.StartsWith(":")) continue;
- if (line.StartsWith("data: "))
- {
- var jsonData = line["data: ".Length ..];
- if (jsonData == "[DONE]") break;
- yield return JsonConvert.DeserializeObject<ResponseBody>(jsonData) ?? new ResponseBody();
- }
- }
- }
复制代码 Semantic Kernel框架集成
Semantic Kernel是一种轻型开源开发工具包,可用于轻松生成 AI 代理并将最新的 AI 模子集成到 C#、Python 或 Java 代码库中。 它充当一个高效的中心件,可实现企业级解决方案的快速交付。
DeepSeek API与Semantic Kernel框架集成,可快速实现基于大语言模子的谈天机器人开发。由于DeepSeek API与OpenAI API的兼容性,因此DeepSeek API与Semantic Kernel框架的集成非常简单。只需使用OpenAI的连接器,即可快速实现DeepSeek API与Semantic Kernel框架的集成。
- dotnet add package Microsoft.SemanticKernel
复制代码- 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 = builder.Build();
复制代码 [code]var chatCompletionService = kernel.GetRequiredService();Console.WriteLine("
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |