当前位置: 首页 > ai >正文

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的属性和文档中也不一致。

http://www.xdnf.cn/news/14831.html

相关文章:

  • 关于笔记本充电,使用氮化镓充电器
  • Omi录屏专家 Screen Recorder by Omi 屏幕录制Mac
  • 高效处理大体积Excel文件的Java技术方案解析
  • 云原生 Serverless 架构下的智能弹性伸缩与成本优化实践
  • SNAT DNAT实验
  • 探索实现C++ STL容器适配器:优先队列priority_queue
  • MySQL CDC与Kafka整合指南:构建实时数据管道的完整方案
  • 前端常见 HTTP 状态码
  • DPDK 网卡驱动
  • WPF学习笔记(25)MVVM框架与项目实例
  • Stlink v2调试器采用SWD模式连接stm32f103c8t6核心板的接线方式
  • AI小智项目全解析:软硬件架构与开发环境配置
  • 信号与槽的总结
  • Linux内核深度解析:IPv4策略路由的核心实现与fib_rules.c源码剖析
  • bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
  • 从“电话催维修“到“手机看进度“——售后服务系统开发如何重构客户体验
  • 历史数据分析——中证医药
  • 《数据维度的视觉重构:打造交互式高维数据可视化的黄金法则》
  • 如何解决Spring Boot中@Valid对List校验失效问题
  • Python小工具之PDF合并
  • Linux应用基础
  • [netty5: HttpObjectEncoder HttpObjectDecoder]-源码解析
  • 传输层 udptcp
  • 【力扣 简单 C】746. 使用最小花费爬楼梯
  • 国产 OFD 标准公文软件数科 OFD 阅读器:OFD/PDF 双格式支持,公务办公必备
  • LongT5: 针对长序列的高效文本到文本Transformer
  • Linux NFS终极指南:安装、配置与性能优化
  • 【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
  • 【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
  • 简单 Python 爬虫程序设计