1 using MarkdownSharp; 2 using Microsoft.Extensions.Hosting; 3 using Microsoft.SemanticKernel; 4 using OpenAI; 5 using OpenAI.Chat; 6 using System.ClientModel; 7 using System.Text; 8 9 var builder = Host.CreateApplicationBuilder(args); 10 //builder.Services.AddKernel(); 11 var openAIClientCredential = new ApiKeyCredential("sk-Key"); 12 var openAIClientOption = new OpenAIClientOptions 13 { 14 Endpoint = new Uri("https://api.deepseek.com/v1"), 15 16 }; 17 var openapiClient = new OpenAIClient(openAIClientCredential, openAIClientOption); 18 //这里添加对那个表示的模型,可以添加很多,可以做一个选择器 19 builder.Services.AddOpenAIChatCompletion("deepseek-chat", openapiClient, "对话serviceId"); 20 var host = builder.Build(); 21 Console.ForegroundColor = ConsoleColor.White; 22 Console.WriteLine("DeepSeek:你好,有什么是我能帮你的吗?"); 23 24 List<ChatMessage> chatHistory = new List<ChatMessage>(); 25 var systemContent = ChatMessage.CreateSystemMessage("你是很好的助手"); 26 chatHistory.Add(systemContent); 27 28 var settingOptions = new ChatCompletionOptions 29 { 30 //输出最大token 31 MaxOutputTokenCount = 4096, 32 33 #region 不能同时设置 34 //采样温度,介于 0 和 2 之间。更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。 35 //Temperature = 0, 36 //TopP = 0 37 #endregion 38 // ToolChoice = ChatToolChoice.CreateFunctionChoice("信息查询") 39 }; 40 //settingOptions.Tools.Add(ChatTool.CreateFunctionTool("信息查询", "通过这个来查询系统内容", BinaryData.Empty)); 41 42 var client = openapiClient.GetChatClient("deepseek-chat"); 43 while (true) 44 { 45 46 Console.ForegroundColor = ConsoleColor.Yellow; 47 Console.Write("你: "); 48 var userInput = Console.ReadLine(); 49 if (string.IsNullOrEmpty(userInput)) break; 50 var inputmsg = ChatMessage.CreateUserMessage(userInput); 51 chatHistory.Add(inputmsg); 52 53 #region 1、流输出 54 55 var clientResult = client.CompleteChatStreaming(chatHistory, settingOptions); 56 if (clientResult != null) 57 { 58 Console.ForegroundColor = ConsoleColor.White; 59 Console.Write("DeepSeek:"); 60 StringBuilder sb = new StringBuilder(); 61 foreach (var item in clientResult) 62 { 63 var text = item.ContentUpdate.FirstOrDefault()?.Text; 64 sb.Append(text); 65 Console.Write(text); 66 } 67 var assistant = ChatMessage.CreateAssistantMessage(sb.ToString()); 68 chatHistory.Add(assistant); 69 Console.WriteLine(); 70 71 } 72 73 74 75 #endregion 76 77 78 79 80 }
标签:Console,DeepSeek,new,chatHistory,Add,NET8,var,using,SemanticKernel From: https://www.cnblogs.com/liyouming/p/18681618