微信扫码
添加专属顾问
 
                        我要投稿
紧跟AI技术潮流,快速适配阿里新模型QwQ-32b。 核心内容: 1. 阿里新模型QwQ-32b的介绍 2. Spring-ai与Spring Cloud Alibaba AI的整合方法 3. 创建SCA AI应用的详细配置步骤
 
                                DeepSeek国内大模型一哥的屁股都没坐热, 阿里又来了个qwq-32b。但是没关系, 如果你用spring-ai(或者Spring Cloud Alibaba AI)改个配置即可适配(温馨提醒:qwq-32b和deepseek-r1一样不支持function-call)
Spring Cloud Alibaba AI是阿里专门用于给java程序员接入百炼平台大模型的springboot-stater。 基于 Spring AI 而来, 和SpringAi同步更新。
在 pom.xml 中引入如下依赖配置:
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-ai</artifactId>
</dependency>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring.cloud.alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<!-- 因为 Spring AI 还没有正式发布到 maven 仓库,所以需要添加此配置项 目前 maven 仓库为假的。
issue:https://github.com/spring-projects/spring-ai/issues/537
-->
<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>当然也可以通过 application.yml配置项配置:
为了把api-key不暴露在代码中,${ALI_AI_KEY} 读取的环境变量:
spring:
  ai:
    dashscope:
      api-key: ${ALI_AI_KEY}
      model: qwq-32b
public class ChatService {
    // 聊天客户端
    private final ChatClient chatClient;
    // stream 流式客户端
    private final StreamingChatClient streamingChatClient;
    @Autowired
    public ChatService(ChatClient chatClient, StreamingChatClient streamingChatClient) {
        this.chatClient = chatClient;
        this.streamingChatClient = streamingChatClient;
    }
    @Override
    public String normalCompletion(String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }
    @Override
    public Map<String, String> streamCompletion(String message) {
        StringBuilder fullContent = new StringBuilder();
        streamingChatClient.stream(new Prompt(message))
        .flatMap(chatResponse -> Flux.fromIterable(chatResponse.getResults()))
        .map(content -> content.getOutput().getContent())
        .doOnNext(fullContent::append)
        .last()
        .map(lastContent -> Map.of(message, fullContent.toString()))
        .block();
        return Map.of(message, fullContent.toString());
    }
}之后,创建 controller 接口调用 service 服务:
@Autowired
private ChatService chatService;
@GetMapping("/example")
public String completion(
    @RequestParam(value = "message", defaultValue = "Tell me a joke")
    String message
) {
    return chatService.completion(message);
}
@GetMapping("/stream")
public Map<String, String> streamCompletion(
    @RequestParam(value = "message", defaultValue = "请告诉我西红柿炖牛腩怎么做?")
    String message
) {
    return chatService.streamCompletion(message);
}下面进行接口测试:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.cloud.ai.example.model;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImageOptionsBuilder;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ai")
public class ImageModelController {
 private final ImageModel imageModel;
 ImageModelController(ImageModel imageModel) {
 this.imageModel = imageModel;
 }
 @GetMapping("/image/{input}")
 public void image(@PathVariable("input") String input, HttpServletResponse response) {
 ImageOptions options = ImageOptionsBuilder.builder()
 .model("wanx-v1") 
 .build();
 ImagePrompt imagePrompt = new ImagePrompt(input, options);
 ImageResponse imageResponse = imageModel.call(imagePrompt);
 String imageUrl = imageResponse.getResult().getOutput().getUrl();
 try {
 URL url = new URL(imageUrl);
 InputStream in = url.openStream();
 response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);
 response.getOutputStream().write(in.readAllBytes());
 response.getOutputStream().flush();
 } catch (IOException e) {
 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
 }
 }
}
接口调用体验:
Terminal window
http://localhost:8080/ai/image/美女
点击地址我们可以看到如下生成的美女图片:
更多配置项可以参考:https://help.aliyun.com/zh/dashscope/developer-reference/api-details。
示例代码:https://gitee.com/xscodeit/spring-cloud-alibaba-ai-example
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-31
Google DeepMind揭秘:开源AI模型如何泄露训练秘方
2025-10-31
有人问我会不会用 AI,我直接拿出这个 Ollama + FastGPT 项目给他看
2025-10-30
开源可信MCP,AICC机密计算新升级!
2025-10-30
OpenAI 开源了推理安全模型-gpt-oss-safeguard-120b 和 gpt-oss-safeguard-20b
2025-10-29
刚刚,OpenAI 再次开源!安全分类模型 gpt-oss-safeguard 准确率超越 GPT-5
2025-10-29
AI本地知识库+智能体系列:手把手教你本地部署 n8n,一键实现自动采集+智能处理!
2025-10-29
n8n如何调用最近爆火的deepseek OCR?
2025-10-29
OpenAI终于快要上市了,也直面了这23个灵魂拷问。
 
            2025-08-20
2025-09-07
2025-08-05
2025-08-20
2025-08-26
2025-08-22
2025-09-06
2025-08-06
2025-10-20
2025-08-22
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17
2025-09-09
2025-09-08
2025-09-07