微信扫码
添加专属顾问
我要投稿
紧跟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-04-28
程序员狂喜!DevDocs:开源AI文档神器,自动抓取+整理+喂给AI (MCP加持)
2025-04-28
DeepWiki深度探索:驾驭代码复杂性的AI百科全书
2025-04-28
Agent S2:像人一样操作 GUI,开源 AI 智能体新突破
2025-04-28
杨植麟再掀 AI 风暴!Kimi-Audio 如何让机器听懂人类 “弦外之音”?
2025-04-27
一文了解Text Embedding模型:从text2vec、openai-text embedding到m3e、bge(下)
2025-04-27
精选MCP服务器列表
2025-04-27
Google ADK框架:打造多智能体系统的开源利器
2025-04-27
DeepWiki 比较适合用来生成开发者手册,而不是用户手册
2024-07-25
2025-01-01
2025-01-21
2024-05-06
2024-09-20
2024-07-20
2024-07-11
2024-06-12
2024-08-13
2024-12-26
2025-04-28
2025-04-21
2025-04-19
2025-04-17
2025-04-15
2025-04-13
2025-04-10
2025-04-07