惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Y
Y Combinator Blog
O
OpenAI News
K
Kaspersky official blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
S
Securelist
C
CERT Recently Published Vulnerability Notes
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
Jina AI
Jina AI
大猫的无限游戏
大猫的无限游戏
V
Vulnerabilities – Threatpost
量子位
爱范儿
爱范儿
I
Intezer
博客园 - 叶小钗
The Hacker News
The Hacker News
N
News and Events Feed by Topic
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Privacy & Cybersecurity Law Blog
Google Online Security Blog
Google Online Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Heimdal Security Blog
NISL@THU
NISL@THU
V
Visual Studio Blog
The Last Watchdog
The Last Watchdog
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
小众软件
小众软件
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Schneier on Security
雷峰网
雷峰网
B
Blog RSS Feed
美团技术团队
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
N
Netflix TechBlog - Medium
月光博客
月光博客
S
Security Affairs

Liu Zijian's Blog | 一个技术博客

使用Certbot自动续签HTTPS证书 使用Filebeat采集Nginx日志到ES Python的协程 Python中的异常 Python中的类和对象 Python的函数 Python的数据结构,推导式、迭代器和生成器 Spring AI集成多模态模型 LangChain Tools工具使用 Python中的模块和包 Python全局环境和虚拟环境(venv) LangChain Prompt提示词工程 LangChain4j Tools工具使用 基于Dify搭建AI智能体应用 LangChain4j RAG检索增强生成 Spring AI实现MCP Server Spring AI集成MCP Client LangChain4j Prompt提示词工程 Spring AI使用知识库增强对话功能 Spring AI实现一个智能客服 Spring AI实现一个简单的对话机器人 实现MinIO数据的每日备份 自己实现一个DNS服务 简单理解AI智能体 大模型和大模型应用 LangChain开篇 LangChain4j开篇 一个解析Excel2007的POI工具类 DataPermissionInterceptor源码解读 TenantLineInnerInterceptor源码解读 BaseMultiTableInnerInterceptor源码解读 Spring AI开篇 SQL解析工具JSQLParser 芋道源码解读之多租户 芋道源码解读之数据权限 芋道源码解读开篇 Java实现将数据导出为Word文档 OA系统的天数该怎样计算 安装MySQL8 安装MySQL5.7 RockyLinux9环境下编译MySQL8 MySQL字符集及底层原理 Java实现LDAP登录 Docker Compose IPv4和IPv6 使用虚拟机安装一个K8s集群 使用GraalVM原生编译打包SpringBoot工程 Nginx防止目录穿越 Java线程的状态 Nginx防盗链设置 使用python将excel表格转换为SQL INSERT Redis的公共操作命令 Redis数据结构之Bitfleid Redis数据结构之Bitmap Redis数据结构之GEO Redis数据结构之Hash Redis数据结构之HyperLogLog Redis数据结构之List Redis数据结构之Set Redis数据结构之Stream Redis数据结构之String Redis数据结构之ZSet 使用python压缩图片 利用Python实现Hexo站点的持续集成 Nginx设置HTTPS监听 firewalld防火墙工具的使用 Linux信号(signal)机制 MySQL5.7x 主从复制 用IP自签发一个HTTPS证书 基于Hexo实现一个静态的个人博客 RockyLinux9环境下编译MySQL5.7 Docker离线安装 MySQL数据定义语言 Docker与联合文件系统 Docker的网络 Docker的镜像操作 MySQL存储过程 MyBatis-Plus开篇 MySQL变量 MySQL视图 MySQL事务 MySQL插入修改和删除 MySQL查询 MySQL系统命令 Docker的容器操作 Docker的安装和配置 Docker容器数据卷 浅谈OAuth2.0授权原理 JVM开篇 浅谈Linux(Unix)的I/O模型 一个通用的CloseableHttpClient工厂类 JUC可重入锁ReentrantLock JUC读写锁ReadWriteLock Java的单例 Java泛型 Java8的新特性 最近最少使用算法(LRU) MySQL函数 SpringBoot配置和启动 volatile作用分析
LangChain4j多模态
Liu Zijian · 2025-12-30 · via Liu Zijian's Blog | 一个技术博客

未完待续

1.多模态概述

模态,就是感知事物的方式,比如视觉,听觉等,对应的信息传播媒介可以是文字,图片,视频,音频等。多模态就是从多个模态表达和感知事物。

很多模型都是单模态,输入和输出都只能是文本,是语言模型,例如deepseek,即使能上传图片,也是识别图片中的文字。但是除了语言模型,还有除语言外还支持其他模态的模型,便是多模态的模型。

即便多模态模型支持很多模态,也很难像人类一样,完完全全支持全模态。

多模态模型实现形式有很多,有的能根据文字生成图片视频,有的则是根据图片生成文字;有的还能根据图片生成图片实现AI试衣,有的不仅支持图文,还支持其他的媒体,比如会议转录文字,听歌识曲等。

LangChain4j框架当然也对多模态模型接入使用提供了支持,本文以阿里巴巴qwen3-vl-plus模型为例介绍。

2.图片内容理解(图生文)

以这张图片(src/main/resources/image.png)为例

pom.xml中和简单的prompt工程需要的依赖是一样的

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.4</version>
    <relativePath/>
</parent>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-bom</artifactId>
            <version>1.8.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>


</dependencies>

<repositories>
    <repository>
        <name>Central Portal Snapshots</name>
        <id>central-portal-snapshots</id>
        <url>https://central.sonatype.com/repository/maven-snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>21</source>
                <target>21</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml配置也是和简单的prompt工程需要的依赖是一样的,阿里云百炼多模态支持模型同样适用OpenAI接口协议格式。

langchain4j:
  open-ai:
    chat-model:
      base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
      api-key: ${QWKEY}
      model-name: qwen3-vl-plus
      log-requests: true
      log-responses: true
      return-thinking: true

logging:
  level:
    dev.langchain4j: debug

编写测试类测试多模态,将图片上传给大模型,并根据图片内容提问:图片中的统计数据是谁发布的,大学学历网民占比是多少。

package org.example.test;

import dev.langchain4j.data.message.*;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.Main;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

@SpringBootTest(classes = Main.class)
@Slf4j
public class MTest {

    @Resource
    private ChatModel chatModel;

    @Value("classpath:image.png")
    private org.springframework.core.io.Resource resource;

    @Test
    public void imageToText() throws IOException {

        byte[] byteArray = resource.getContentAsByteArray();
        String base64 = Base64.getEncoder().encodeToString(byteArray);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("图片中的统计数据是谁发布的,大学学历网民占比是多少。"),
                ImageContent.from(base64, "image/png")
        );

        ChatResponse chatResponse = chatModel.chat(List.of(userMessage));
        log.info("******** chatResponse: {}", chatResponse);


    }
}

发送base64形式图片时,url参数会标记为data:image/png;base64,,并将base64图片放到url中,上传到大模型

有的大模型服务平台,图片URL除了base64外,还可以写图片的http网络地址

2026-01-10T18:42:56.278+08:00  INFO 20808 --- [           main] d.l.http.client.log.LoggingHttpClient    : HTTP request:
- method: POST
- url: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
- headers: [Authorization: Beare...48], [User-Agent: langchain4j-openai], [Content-Type: application/json]
- body: {
  "model" : "qwen3-vl-plus",
  "messages" : [ {
    "role" : "user",
    "content" : [ {
      "type" : "text",
      "text" : "图片中的统计数据是谁发布的,大学学历网民占比是多少。"
    }, {
      "type" : "image_url",
      "image_url" : {
        "url" : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA5YAAAHmCAIAAACH1IbBAAAQAElEQVR4Aez9C6BsSVUf/u/aux93BmYYQBAZGGZ4DEKEAQOaqIAkDIkIo0RgAgaBqKhAYghEjREBo1F5hGgEeUVAIwpojMA/0UCigJrkB0YGlPCSGV4SHgIDzMzp197/T+3qU71P9znnvu89fW73Xad61apVVbu+tWrV2rW7+5bN5nWIEKjrejqdjsfjm266aTKZnK2RjcbjG268cS+68cYbRyMq41ldn60rPCX9wnlra2uvYZLftLVlnLP2dUp6PNcagdyEQXubzYAJ0kRMiAwa0ptGoySUbm1tkZBv6NQi0AX5hptuqusazpwMzBPduLWVhEqTRGo6jnoZqZalNGpfsketcsgUjB1WexFT50OWYLEmYJur4E0H6s4IOAFFqIWseVSGsoVm71jqUVPrQq7cqNPV4oGzOmrDBLvSpNZNYduFkVoq1SZIu+adm9UFtW4tvFqayjoYOoQbOrUIlMXmtf4IsAkLjN/h7KwTu77YMK3PszK4Qb/f7/Wqsgyd7kNZllU1GAyGw+FgEFXK0C3vqB54FuDQ5qFE4UsXa1BlWfaqajgYIOOURUtqm+w+CIB3OpttMeXxeDqZzKbTEELV6+UqTVEweFny0MhhWwJ0ecZ8WtvjOicR5/aOlzHv7y56XVSbBviQrqoqj94s1E0TSDua2tds1ukyqcgKEhaYZy7LjFMm76qdIzxvyVGgiF/Y4RU

        ......

大模型回复如下,可以看出精准理解了图片内容,并且能进行一定的分析推理。

2026-01-10T18:43:04.808+08:00  INFO 20808 --- [           main] d.l.http.client.log.LoggingHttpClient    : HTTP response:
- status code: 200
- headers: [vary: Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Accept-Encoding], [x-request-id: 3ac74322-599b-9042-b069-0d381d984c69], [x-dashscope-call-gateway: true], [content-type: application/json], [content-length: 1276], [req-cost-time: 7143], [req-arrive-time: 1768041778270], [resp-start-time: 1768041785413], [x-envoy-upstream-service-time: 6827], [date: Sat, 10 Jan 2026 10:43:05 GMT], [server: istio-envoy]
- body: {"choices":[{"message":{"content":"根据图片信息:\n\n1. **统计数据发布方**:  \n   图片底部明确标注“来源:CNNIC 中国互联网络发展状况统计调查”,因此该数据是由 **中国互联网络信息中心(CNNIC)** 发布的。\n\n2. **大学学历网民占比**:  \n   图表中“大学本科及以上”学历对应的数据显示:\n   - **2016年12月**:占比为 **11.5%**\n   - **2017年6月**:占比为 **11.6%**\n\n✅ 因此,截至2017年6月,**大学本科及以上学历的网民占比为 11.6%**。\n\n---\n\n📌 补充说明:  \n“大学本科及以上”通常包括本科、硕士、博士等高等教育学历,是衡量网民受教育程度的重要指标。从数据看,该比例在半年内略有上升,但整体仍低于初中和高中/中专/技校学历群体。","reasoning_content":"","role":"assistant"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":457,"completion_tokens":211,"total_tokens":668,"prompt_tokens_details":{"image_tokens":437,"text_tokens":20},"completion_tokens_details":{"text_tokens":211}},"created":1768041785,"system_fingerprint":null,"model":"qwen3-vl-plus","id":"chatcmpl-3ac74322-599b-9042-b069-0d381d984c69"}

2026-01-10T18:43:04.839+08:00  INFO 20808 --- [           main] org.example.test.MTest                   : ******** chatResponse: ChatResponse { aiMessage = AiMessage { text = "根据图片信息:

1. **统计数据发布方**:  
   图片底部明确标注“来源:CNNIC 中国互联网络发展状况统计调查”,因此该数据是由 **中国互联网络信息中心(CNNIC)** 发布的。

2. **大学学历网民占比**:  
   图表中“大学本科及以上”学历对应的数据显示:
   - **2016年12月**:占比为 **11.5%**
   - **2017年6月**:占比为 **11.6%**

✅ 因此,截至2017年6月,**大学本科及以上学历的网民占比为 11.6%**。

---

📌 补充说明:  
“大学本科及以上”通常包括本科、硕士、博士等高等教育学历,是衡量网民受教育程度的重要指标。从数据看,该比例在半年内略有上升,但整体仍低于初中和高中/中专/技校学历群体。", thinking = null, toolExecutionRequests = [], attributes = {} }, metadata = OpenAiChatResponseMetadata{id='chatcmpl-3ac74322-599b-9042-b069-0d381d984c69', modelName='qwen3-vl-plus', tokenUsage=OpenAiTokenUsage { inputTokenCount = 457, inputTokensDetails = OpenAiTokenUsage.InputTokensDetails { cachedTokens = null }, outputTokenCount = 211, outputTokensDetails = OpenAiTokenUsage.OutputTokensDetails { reasoningTokens = null }, totalTokenCount = 668 }, finishReason=STOP, created=1768041785, serviceTier='null', systemFingerprint='null', rawHttpResponse=dev.langchain4j.http.client.SuccessfulHttpResponse@3fe8ad3f, rawServerSentEvents=[]} }