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

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.lryc.cn/news/581753.html

相关文章:

  • MyBatis实战指南(九)MyBatis+JSP+MySQL 前端页面实现数据库的增加与删除显示数据
  • 分布式会话的演进和最佳事件,含springBoot 实现(Java版本)
  • 【网络安全】不要在 XSS 中使用 alert(1)
  • 电池预测 | 第33讲 Matlab基于CNN-LSTM-Attention的锂电池剩余寿命预测,附锂电池最新文章汇集
  • 一个简单的脚本,让pdf开启夜间模式
  • 【STM32】通用定时器PWM
  • 李宏毅NLP-8-语音模型
  • 20250706-11-Docker快速入门(下)-构建Nginx镜像和Tomcat镜像_笔记
  • Kotlin lazy 委托的底层实现原理
  • styled-components:现代React样式解决方案
  • 构建下一代智能应用:RAG系统开发深度指南
  • 基于STM32单片机的心率血氧监测系统设计(STM32代码编写+手机APP设计+PCB设计+Proteus仿真)
  • C# 接口(什么是接口)
  • 【机器学习笔记Ⅰ】1 机器学习
  • .golangci.yml文件配置
  • C语言学习(第一天)
  • 求医十年,病因不明,ChatGPT:你看起来有基因突变
  • Gin 框架中如何实现 JWT 鉴权中间件
  • PH热榜 | 2025-07-06
  • 宏定义实现自定义关系运算比较--3
  • 微服务负载均衡全解析:从原理到实践
  • 【王树森推荐系统】召回05:矩阵补充、最近邻查找
  • 操作系统【2】【内存管理】【虚拟内存】【参考小林code】
  • Linux - Linux基础知识
  • 数据挖掘:深度解析与实战应用
  • AI+Web3:从自动化工具到自主经济体的范式革命
  • 电信、移动、联通、广电跨运营商网速慢原因
  • 基于文心开源大模型ERNIE-4.5-0.3B-Paddle私有化部署并构建一个企业智能客服系统
  • SpringBoot基于Mysql的商业辅助决策系统设计与实现
  • Python实现优雅的目录结构打印工具