国内首个「混淆推理模型」Qwen3深夜开源,盘点它的N种对接方式! ...

打印 上一主题 下一主题

主题 1934|帖子 1934|积分 5812

今日凌晨,通义千问团队正式开源了 Qwen3 大模型,并且一口气发布了 8 个型号,其中包括 0.6B、1.7B、4B、8B、14B、32B 以及 30B-A3B 和 235B-A22B,利用者可以根据自己的业务情况,选择符合的版本进利用用。
更让人惊喜的是,最新的 Qwen3 系列模型具备双模推理能力(深入思考/快速响应)、支持 119 种语言及方言,并强化了 Agent 功能与代码实行能力,全面满足复杂问题处理与环球化应用需求。
   PS:Qwen3 也是国内首个「混淆推理模型」,「快思考」与「慢思考」集成进同一个模型,对简朴需求可低算力「秒回」答案,对复杂问题可多步调「深度思考」,大大节省算力消耗。
  Qwen3 旗舰模型 Qwen3-235B-A22B 在代码、数学、通用能力等基准测试中,与 DeepSeek-R1、o1、o3-mini、Grok-3 和 Gemini-2.5-Pro 等顶级模型相比,体现出极具竞争力的效果。此外,小型 MoE 模型 Qwen3-30B-A3B 的激活参数数量是 QwQ-32B 的 10%,体现更胜一筹,乃至像 Qwen3-4B 这样的小模型也能对抗 Qwen2.5-72B-Instruct 的性能,以下是测试报告:

对接 Qwen3

常见对接大模型的方案有以下几种:

  • 官方对接方式:例如,调用阿里百炼平台对接 Qwen3。
  • 当地模型对接方式:安装 Ollama 摆设 Qwen3,对接 Ollama 实现调用。
  • 三方平台对接方式:利用千帆或火山引擎等三方平台,对接调用 Qwen3。
但目前由于 Qwen3 刚刚发布,以是只能利用前两种对接方式,停止发稿时,三方平台还未上线 Qwen3,但也够用了。
详细实现

接下来我们就以官方的调用方式,来实现一下 Qwen3 的详细代码对接吧,这里提供 Spring AI 和 LangChain4j 两种对接实现。
Spring AI 对接 Qwen3

1.添加依赖

Spring AI 并没有内置阿里云百炼平台,但百炼平台支持 OpenAI 协议,因此我们可以利用 OpenAI 对接百炼平台,因此我们只需要添加 OpenAI 依赖即可。
  1. <dependency>
  2.   <groupId>org.springframework.ai</groupId>
  3.   <artifactId>spring-ai-starter-model-openai</artifactId>
  4. </dependency>
复制代码
2.设置设置信息

  1. spring:
  2.   ai:
  3.     openai:
  4.       base-url: https://dashscope.aliyuncs.com/compatible-mode/
  5.       api-key: ${ALIYUN-AK}
  6.       chat:
  7.         options:
  8.           model: qwen3-235b-a22b
复制代码
其中:


  • base-url 填写百炼平台地址。
  • api-key 为预备阶段在百炼平台申请的 AK 凭证。
  • model 设置为 qwen3-235b-a22b 模型。
   支持的模型列表参考官方文档:https://help.aliyun.com/zh/model-studio/models?spm=a2c4g.11186623.0.0.78d848237YTeH1#cefdf0875dorc
  3.编写调用代码

  1. import org.springframework.ai.openai.OpenAiChatModel;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @RequestMapping("/ds")
  8. public class TestController {
  9.     private final OpenAiChatModel chatModel;
  10.     @Autowired
  11.     public TestController(OpenAiChatModel chatModel) {
  12.         this.chatModel = chatModel;
  13.     }
  14.     @RequestMapping("/chat")
  15.     public String chat(@RequestParam("msg") String msg) {
  16.         String result = chatModel.call(msg);
  17.         System.out.println("返回结果:" + result);
  18.         return result;
  19.     }
  20. }
复制代码
LangChain4j 对接 Qwen3

LangChain4j 内置集成了阿里云百炼平台,以是可以直接对接。
1.添加依赖

  1. <dependency>
  2.   <groupId>dev.langchain4j</groupId>
  3.   <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
  4. </dependency>
复制代码
可以为“langchain4j-community-xxx”其添加统一版本管理:
  1. <dependencyManagement>
  2.   <dependencies>
  3.     <dependency>
  4.       <groupId>dev.langchain4j</groupId>
  5.       <artifactId>langchain4j-community-bom</artifactId>
  6.       <version>1.0.0-beta3</version>
  7.       <type>pom</type>
  8.       <scope>import</scope>
  9.     </dependency>
  10.   </dependencies>
  11. </dependencyManagement>
复制代码
2.设置设置信息

留意这里需要设置“chat-model”节点,官方文档有问题,如果不设置 chat-model 则不能主动注入百炼模型:
  1. langchain4j:
  2.   community:
  3.     dashscope:
  4.       base-url: https://dashscope.aliyuncs.com/compatible-mode/
  5.       chat-model:
  6.         api-key: ${ALIYUN-AK}
  7.         model-name: qwen-plus
复制代码
支持的模型列表:https://help.aliyun.com/zh/model-studio/models
3.编写调用代码

  1. import dev.langchain4j.model.chat.ChatLanguageModel;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @RequestMapping("/qw")
  7. public class QwenController {
  8.     @Autowired
  9.     private ChatLanguageModel qwenChatModel;
  10.     @RequestMapping("/chat")
  11.     public String chat(String question) {
  12.         return qwenChatModel.chat(question);
  13.     }
  14. }
复制代码
小结

固然,以上对接方式是全量输出(得到效果之后一次性返回),生产级别我们通常要利用流式输出,并且需要实现连续(上下文)对话,以及汗青对话信息持久化等功能,文章篇幅有限,这里就不逐一实现了,各人可以下来自己试试。
   本文已收录到我的技术小站 www.javacn.site,其中包罗的内容有:Spring AI、LangChain4j、MCP、Function Call、RAG、向量数据库、Prompt、多模态、向量数据库、嵌入模型等内容。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表