文章目錄
- 使用 Semantic Kernel 快速對接國產大模型實戰指南(DeepSeek/Qwen/GLM)
- 一、引言
- 二、環境準備
- 2.1 開發環境
- 2.2 模型服務配置
- 三、核心代碼實現
- 3.1 會話代碼封裝
- 3.2 CurModelContext封裝
- 3.3 DeepSeek對接示例
- 3.4 Qwen對接示例
- 3.5 GLM對接示例
- 四、常見問題排查
- 五、總結
使用 Semantic Kernel 快速對接國產大模型實戰指南(DeepSeek/Qwen/GLM)
一、引言
在人工智能應用開發中,統一對接不同大模型的能力至關重要。微軟推出的 Semantic Kernel 作為優秀的 AI 編排框架,能夠幫助我們快速對接各類大模型。本文將手把手教你如何使用 Semantic Kernel 對接國內三大主流模型:DeepSeek、通義千問(Qwen)和智譜AI(GLM),并提供可運行的代碼示例。文末提供完整代碼示例和注意事項。
二、環境準備
2.1 開發環境
? .NET 6+ SDK
? Visual Studio 2022
? NuGet包:
```bashdotnet add package Microsoft.SemanticKernel```
2.2 模型服務配置
模型 | 接口地址示例 | API Key獲取方式 |
---|---|---|
DeepSeek | https://api.deepseek.com/v1 | DeepSeek平臺申請(需充值) |
Qwen | https://api.siliconflow.cn/ | SiliconCloud平臺申請(有免費額度) |
GLM | https://open.bigmodel.cn/api/paas/v4/chat/completions | 智譜AI開放平臺申請(有免費模型) |
三、核心代碼實現
下述內容封裝兩種對話交互模式,采用統一的接口設計:
-
非流式輸出:完整獲取響應后一次性輸出
-
流式輸出:實時輸出響應片段,提升交互體驗
注:兩種模式都提供了對話歷史管理機制,確保多輪對話上下文連貫性。
3.1 會話代碼封裝
/// <summary>
/// 統一對話管理(非流式)
/// </summary>
/// <param name="kernel"></param>
/// <returns></returns>
private async Task StartChatSession(Kernel kernel)
{var chatService = kernel.GetRequiredService<IChatCompletionService>();var history = new ChatHistory();while (true){Console.Write("用戶 > ");var input = Console.ReadLine();history.AddUserMessage(input);var response = await chatService.GetChatMessageContentAsync(history);Console.WriteLine($"助手 > {response.Content}");history.AddAssistantMessage(response.Content);}
}/// <summary>
/// 統一對話管理(流式輸出)
/// </summary>
/// <param name="kernel"></param>
/// <returns></returns>
private async Task StartStreamingChatSession(Kernel kernel)
{var chatService = kernel.GetRequiredService<IChatCompletionService>();var history = new ChatHistory();while (true){///獲取用戶輸入Console.Write("用戶 > ");var input = Console.ReadLine();//將用戶輸入添加到歷史記錄history.AddUserMessage(input);//獲取流式響應var response = chatService.GetStreamingChatMessageContentsAsync(chatHistory: history,kernel: kernel);Console.WriteLine($"助手 > ");string resStr = "";//輸出流式響應await foreach (var chunk in response){resStr += chunk;//拼接聊天記錄Console.Write(chunk);}//將完整的響應添加到歷史記錄history.AddAssistantMessage(resStr);//輸出換行Console.WriteLine();}
}
3.2 CurModelContext封裝
將要獲取的模型,按如下方式封裝,也可直接寫死在代碼中,其中“sk-xx”和“Model”按需替換實際使用的key和模型。
/// <summary>
/// 全局參數
/// </summary>
public class Global
{/// <summary>/// 獲取模型配置/// </summary>public static ModelConfig CurModelContext(string model){switch (model){case "glm-4-flash":return new ModelConfig{Model = "glm-4-flash",EndpointKey = "https://open.bigmodel.cn/api/paas/v4",ApiKey = "sk-xxx"};case "glm-z1-flash":return new ModelConfig{Model = "glm-z1-flash",EndpointKey = "https://open.bigmodel.cn/api/paas/v4",ApiKey = "sk-xxx"};case "Qwen/Qwen2.5-72B-Instruct":return new ModelConfig{Model = "Qwen/Qwen2.5-72B-Instruct",EndpointKey = "https://api.siliconflow.cn",ApiKey = "sk-xxx"};case "deepseek-chat":return new ModelConfig{Model = "deepseek-chat",EndpointKey = "https://api.deepseek.com/v1",ApiKey = "sk-xxx"};case "deepseek-reasoner":return new ModelConfig{Model = "deepseek-reasoner",EndpointKey = "https://api.deepseek.com/v1",ApiKey = "sk-xxx"};default:break;}return null;}
}
3.3 DeepSeek對接示例
var modelConfig = Global.CurModelContext("deepseek-chat");// 1. 填充OpenAI格式LLM調用參數值
var modelId = modelConfig.Model;
var endpoint = modelConfig.EndpointKey;
var apiKey = modelConfig.ApiKey;// 2. 創建一個OpenAI聊天完成的內核
var builder = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId,new Uri(endpoint),apiKey);// 4.構建內核
Kernel kernel = builder.Build();//5. 對話功能(流式)
await StartStreamingChatSession(kernel);//6. 對話功能(非流式)
//await StartChatSession(kernel);
3.4 Qwen對接示例
var modelConfig = Global.CurModelContext("Qwen/Qwen2.5-72B-Instruct");
// 1. 填充OpenAI格式LLM調用參數值
var modelId = modelConfig.Model;
var endpoint = modelConfig.EndpointKey;
var apiKey = modelConfig.ApiKey;// 2. 創建一個OpenAI聊天完成的內核
var builder = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId,new Uri(endpoint),apiKey);// 4.構建內核
Kernel kernel = builder.Build();//5. 對話功能(流式)
await StartStreamingChatSession(kernel);//6. 對話功能(非流式)
//await StartChatSession(kernel);
3.5 GLM對接示例
var modelConfig = Global.CurModelContext("glm-4-flash");// 1. 填充OpenAI格式LLM調用參數值
var modelId = modelConfig.Model;
var endpoint = modelConfig.EndpointKey;
var apiKey = modelConfig.ApiKey;// 2. 創建一個OpenAI聊天完成的內核
var builder = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId,new Uri(endpoint),apiKey);// 4.構建內核
Kernel kernel = builder.Build();//5. 對話功能(流式)
await StartStreamingChatSession(kernel);
//6. 對話功能(非流式)
//await StartChatSession(kernel);
四、常見問題排查
-
401 鑒權失敗
- 檢查 API Key 有效性
- 確認密鑰傳遞方式符合平臺要求
-
模型響應超時
-
檢查網絡連通性
-
確認 endpoint 配置正確
-
-
輸出格式異常
-
調整 temperature 參數
-
檢查 max_tokens 限制
-
五、總結
通過 Semantic Kernel 的統一接口,開發者可以快速實現國內主流大模型的集成。建議根據實際需求選擇模型,并充分利用SK的插件系統、記憶機制和工具調用特性構建企業級AI應用。
- 優先選用兼容 OpenAI 格式的模型
- 對于特殊接口的模型需實現自定義OpenAI 格式封裝