Spring AI(12)——调用多模态模型识别和生成图像

识别图像内容

修改yml配置

spring:
  ai:
    zhipuai:
      api-key: 智谱的apikey
      chat:
        options:
          model: glm-4v-flash
          temperature: 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 {

    @Resource
    private ZhiPuAiChatModel chatModel;


    @Resource
    private 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对象

    @Resource
    private 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: 智谱的apikey
      image:
        options:
          model: CogView-3-Flash

但是实际测试时,使用该配置,针对智谱的CogView-3-Flash,没有生效(但是帮助文档中提到了支持CogView模型)。

另外,按照帮助文档,可以按照如下方式设置相关的属性,并发送消息:

但是实际使用时,ZhiPuAiImageOptions的属性和文档中也不一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值