微信扫码
添加专属顾问
我要投稿
紧跟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-06-13
MinerU-利用专用LLM模型提取PDF内容的工具实测
2025-06-13
官宣|Milvus 2.6正式开源:内存减少 72%,速度比ES快4倍
2025-06-13
CoCo:全球首个“有记忆”的企业级AI Agent
2025-06-13
OpenAI 三连更:Team 上线 o3-pro、o3 额度翻倍、Projects 大升级!
2025-06-13
Manus免费版来了!两大核心更新功能实测
2025-06-13
[开源]RAGFlow: 基于深度文档理解的开源RAG引擎
2025-06-12
干货:手把手教你搭建自己的MCP Server
2025-06-12
AI实现智能客服第4节:开源N8N编排采集同城旅行网数据知识
2025-03-19
2025-03-19
2025-03-17
2025-04-01
2025-04-01
2025-04-13
2025-04-29
2025-03-19
2025-03-23
2025-03-22
2025-06-10
2025-06-08
2025-05-28
2025-05-28
2025-05-26
2025-05-25
2025-05-23
2025-05-17