当前位置: 首页 > news >正文

使用 Semantic Kernel 快速对接国产大模型实战指南(DeepSeek/Qwen/GLM)

文章目录

  • 使用 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获取方式
DeepSeekhttps://api.deepseek.com/v1DeepSeek平台申请(需充值)
Qwenhttps://api.siliconflow.cn/SiliconCloud平台申请(有免费额度)
GLMhttps://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 格式封装

相关文章:

  • 家政平台派单系统设计与实现详解
  • Unity-Shader详解-其四
  • BUUCTF——Mark loves cat
  • CloudCompare 中 ccDrawableObject
  • 健康养生:从微小改变开始
  • 2025系统架构师---论软件可靠性设计范文
  • yolo 用roboflow标注的数据集本地训练 kaggle训练 comet使用 训练笔记5
  • 从零开始学Python:开启编程新世界的大门
  • C++ 适配器模式详解
  • uniapp 云开发全集 云数据库
  • 11.施工监测
  • 【项目】基于ArkTS的网吧会员应用开发(1)
  • NHANES指标推荐:ZJU index
  • 作者新游戏1.1
  • 什么是外联模板(extern template)?
  • 解锁现代健康密码:科学养生新主张
  • Qt中的UIC
  • 【机器学习-线性回归-5】多元线性回归:概念、原理与实现详解
  • 数据库的二级索引
  • 2022年全国青少年信息素养大赛 Python编程挑战赛 小学/初中组 初赛真题答案详细解析
  • 多省份晒出“五一”旅游“成绩单”:北京游客接待量、旅游消费创历史新高
  • 许昌市场监管部门对胖东来玉石开展日常检查:平均毛利率不超20%
  • 美国内政部长:今年夏天美国可能发生西班牙式大停电,全怪拜登
  • 今晚上海地铁1、2、10、17号线加开定点班车至次日0点
  • 经济日报:合力推进民企与毕业生双向奔赴
  • 美国加州州长:加州继续对中国“敞开贸易大门”