Spring AI(12)——调用多模态模型识别和生成图像
识别图像内容
修改yml配置
spring:ai:zhipuai:api-key: 智谱的apikeychat:options:model: glm-4v-flashtemperature: 0.7
注意:这里使用智谱提供的识别图像的glm-4v-flash模型
测试代码
package com.renr.springainew.muliti;import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.content.Media;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.core.io.InputStreamResource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;@RestController
public class MultiController {@Resourceprivate ZhiPuAiChatModel chatModel;@Resourceprivate ChatClient client;@GetMapping("/multi/chat")public String chat() throws Exception {String destUrl = "http://www.qfedu.com/images/index2021/logo-edu.png";HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();InputStream inputStream = httpUrl.getInputStream();UserMessage message = UserMessage.builder().text("请识别图中内容").media(new Media(MimeTypeUtils.IMAGE_PNG, new InputStreamResource(inputStream))).build();String answer = this.chatModel.call(message);System.out.println(answer);return "success";}@GetMapping("/multi/chat2")public String chat2() throws Exception {String destUrl = "http://www.qfedu.com/images/index2021/logo-edu.png";HttpURLConnection httpUrl = (HttpURLConnection) new URL(destUrl).openConnection();InputStream inputStream = httpUrl.getInputStream();String answer = this.client.prompt().user(userMessage -> userMessage.text("请识别图中内容").media(new Media(MimeTypeUtils.parseMimeType("image/png"), new InputStreamResource(inputStream)))).call().content();System.out.println(answer);return "success";}}
输出结果
生成图像
直接注入ZhiPuAiImageModel对象
@Resourceprivate ZhiPuAiImageModel zhiPuAiImageModel;
调用智谱的CogView-3-Flash生成图像
@GetMapping("/multi/image")public String image() {ImageResponse response = zhiPuAiImageModel.call(new ImagePrompt("生成一个小孩儿在读书的图像",ZhiPuAiImageOptions.builder().model("CogView-3-Flash").build()));System.out.println(response.getResult().getOutput().getUrl());return "success";}
输出结果:
需要注意的地方
本例使用SpringAI的1.0.0版本,按照文档,可以通过如下配置使用智谱的图像生成模型:
spring:ai:zhipuai:api-key: 智谱的apikeyimage:options:model: CogView-3-Flash
但是实际测试时,使用该配置,针对智谱的CogView-3-Flash,没有生效(但是帮助文档中提到了支持CogView模型)。
另外,按照帮助文档,可以按照如下方式设置相关的属性,并发送消息:
但是实际使用时,ZhiPuAiImageOptions的属性和文档中也不一致。