dashscope-sb 阿里百炼-文生图20260413
1、pom
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.2.0</spring-boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
<version>1.0.0</version>
</dependency>
<!-- 阿里云通义千问(DashScope)starter -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 统一管理Spring AI依赖版本 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>${spring-ai-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、yml
server:
port: 8082
spring:
application:
name: deepseek
# ========== 阿里云通义千问(DashScope)配置 ==========
#spring.ai.dashscope.api-key=${ALI_AI_KEY}
# spring.ai.dashscope.chat.options.model= 可指定模型,如 qwen-max、qwen-plus 等
dashscope:
api-key: "keykey"
#chat:
# options:
# model:
注:api-key 请用自己的key
3、controller
mport com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.stringtemplate.v4.compiler.Bytecode.instructions;
@RequestMapping("/openai")
@ResponseBody
@Controller
public class QwenImgChatController {
@Autowired
DashScopeImageModel imageModel;
@GetMapping("/simple/img")
public String simpleImg () {
DashScopeImageOptions imageOptions =
DashScopeImageOptions.builder()
.withModel("wanx2.1-t2i-turbo") // 模型名称
.withN(1) // 生成1张图
.build();
// 2. 构建提示词 + 调用模型
// 你原来的语法错误我已经修复:instructions 是字符串,参数顺序正确
ImagePrompt imagePrompt = new ImagePrompt("-大美林芝", imageOptions);
ImageResponse imageResponse = imageModel.call(imagePrompt);
String imageUrl = imageResponse.getResult().getOutput().getUrl();
//图片un
// 4. 输出并返回
System.out.println("生成的图片地址:" + imageUrl);
return "生成成功!图片地址:" + imageUrl;
}
}
4、浏览器
http://localhost:8082/openai/simple/img


浙公网安备 33010602011771号