赞
踩
- 注意:修改pom文件,重新下载spring ai依赖需要科学上网,请确保网络连接没有问题
<properties> <java.version>21</java.version> <spring-ai.version>0.8.1</spring-ai.version> </properties> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </pluginRepository> </pluginRepositories>
spring:
application:
name: ChatImage
ai:
openai:
api-key: hk-xxx
base-url: https://api.openai-hk.com #请根据自己的api-key自定义配置
server:
port: 8081
import jakarta.annotation.Resource; import org.springframework.ai.image.ImageOptionsBuilder; import org.springframework.ai.image.ImagePrompt; import org.springframework.ai.image.ImageResponse; import org.springframework.ai.openai.OpenAiImageClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ImageController { @Resource private OpenAiImageClient openAiImageClient; @GetMapping("/ai/draw") public String drawImage(@RequestParam(value = "msg") String msg){ ImageResponse response = openAiImageClient.call(new ImagePrompt(msg, ImageOptionsBuilder .builder() .withModel("dall-e-3") //绘画模型 .withN(1) //生成图像的个数 .withWidth(1024) //图像宽度 默认值 .withHeight(1024) //图像高度 默认值 .build() ) ); //返回结果图片的地址 return response.getResult().getOutput().getUrl(); } }
http://localhost:8081/ai/draw?msg=请画一幅顶级程序员的日常开发场景
http://localhost:8081/ai/draw?msg=请画一幅中国考研大学生的精神状态
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。