返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Spring-ai-Alibaba整合QwQ_32b

[复制链接]
链载Ai 显示全部楼层 发表于 昨天 18:09 |阅读模式 打印 上一主题 下一主题


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


回复

使用道具 举报

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

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ