支持私有化部署
AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


Spring-ai-Alibaba整合QwQ_32b

发布日期:2025-04-28 15:22:30 浏览次数: 1528 作者:程序员徐庶
推荐语

紧跟AI技术潮流,快速适配阿里新模型QwQ-32b。

核心内容:
1. 阿里新模型QwQ-32b的介绍
2. Spring-ai与Spring Cloud Alibaba AI的整合方法
3. 创建SCA AI应用的详细配置步骤

杨芳贤
53A创始人/腾讯云(TVP)最具价值专家


 DeepSeek国内大模型一哥的屁股都没坐热, 阿里又来了个qwq-32b。但是没关系, 如果你用spring-ai(或者Spring Cloud Alibaba AI)改个配置即可适配(温馨提醒:qwq-32b和deepseek-r1一样不支持function-call)

Spring Cloud Alibaba AI

Spring Cloud Alibaba AI是阿里专门用于给java程序员接入百炼平台大模型的springboot-stater。 基于 Spring AI 而来, 和SpringAi同步更新。

 

快速体验

创建 SCA AI 应用

在 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>

api-key 配置

获取API Key

  1. 鼠标悬停于页面右上角的
    图标,在下拉菜单中单击API-KEY
  1. 在左侧导航栏,选择全部API-KEY我的API-KEY,然后创建(图中位置①)或查看(图中位置②)API Key。

当然也可以通过 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+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

添加专属顾问

回到顶部

加载中...

扫码咨询