
























统一封装任意二进制媒体数据,解决不同文件类型、本地 / 网络资源适配问题,双核心属性:
image/png / image/jpegaudio/wav / audio/mp3video/mp4(取决于模型支持)Resource资源对象,兼容 Spring 全套资源体系:
ClassPathResource:classpath 内静态图片FileSystemResource:服务器本地磁盘文件UrlResource:网络图片 URLByteArrayResource:内存二进制字节流(前端上传图片)
Spring AI 2.0 主推的业务层工具,基于建造者模式封装多模态请求全流程,优势:
UserMessage/Prompt底层对象1、pom
<properties>
<java.version>17</java.version>
<spring-ai.version>2.0.0-M5</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
<!-- Redis 向量库-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>
<!--Redis 客户端-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- Tika 通用文档读取器 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-tika-document-reader</artifactId>
</dependency>
<!-- Markdown 文档读取器 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-markdown-document-reader</artifactId>
</dependency>
<!-- 问答顾问器 advisor-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-advisors-vector-store</artifactId>
</dependency>
<!-- MCP 客户端:通过 SSE 连接本机 mcp-server(默认端口 18089) -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
</dependencies><dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、application.yml
2.1
${OPENAI_API_KEY}

spring:
autoconfigure:
exclude: #禁用 Ollama Embedding(如果你用 OpenAI),不然会冲突
- org.springframework.ai.model.ollama.autoconfigure.OllamaEmbeddingAutoConfiguration
application:
name: dashscope18088
data:
redis:
url: redis://192.168.91.165:6379
client-type: jedis
ai:
openai:
api-key: ${OPENAI_API_KEY}
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
model: qwen3.6-plus
embedding:
options:
model: text-embedding-v4
dimensions: 2048 # 输出向量维度2048
# chat:
# model: qwen3.6-plus
mcp:
client:
enabled: true
type: SYNC
# 与 mcp-server 一致;须先启动 mcp-server(默认 http://localhost:18089)
sse:
connections:
mcp-weather-server:
url: http://localhost:18089
vectorstore:
redis:
initialize-schema: true #是否在启动时创建/初始化向量索引等结构
index-name: sf2026_ai_vector_index # RediSearch 里的索引名
prefix: sf2026 #存向量文档用的Redis键前级
ollama:
base-url: http://192.168.91.164:11434
chat:
model: qwen2.5:0.5b# options:
# temperature: 0.7
# max-tokens: 2000
server:
port: 18088
logging:
level:
org.springframework.ai: debug
org.springframework.ai.mcp: debug
3、controller

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyMultimodalityController {@Autowired
private ChatClient chatClient;@GetMapping("/aiimg")
public String analyzeImage(){
String result= chatClient.prompt()
.user(u -> u
.text("请详细描述这张图片的内容,包括物体、颜色、场景和可能的用途")
.media(MediaType.IMAGE_PNG, new ClassPathResource("apple.png"))
)
.call()
.content();
System.out.println("【aiimg】"+result);
return "【aiimg】"+result;
}
}
4、http://localhost:18088/aiimg

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。