最新快照版本请使用Spring AI 1.1.0spring-doc.cadn.net.cn

VertexAI 双子座聊天

Vertex AI Gemini API 允许开发者使用 Gemini 模型构建生成式 AI 应用。 Vertex AI Gemini API 支持多模态提示作为输入和输出文本或代码。 多模态模型是一种能够处理来自多种模态信息的模型,包括图像、视频和文本。例如,你可以给模特发一张饼干盘的照片,让它给你一份饼干的Recipes。spring-doc.cadn.net.cn

Gemini 是由 Google DeepMind 开发的一系列生成式人工智能模型,专为多模态应用场景设计。Gemini API 让你访问 Gemini 2.0 闪存Gemini 2.0 Flash-Lite。 有关顶点AI Gemini API模型的规格,请参见模型信息spring-doc.cadn.net.cn

前提条件

gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>

自动配置

春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明spring-doc.cadn.net.cn

Spring AI 为 VertexAI Gemini 聊天客户端提供 Spring Boot 自动配置。 要启用它,请在项目的Maven中添加以下依赖pom.xml或者Gradlebuild.gradle构建文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-vertex-ai-gemini</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-gemini'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

聊天属性

现在,启用和禁用聊天自动配置通过带有前缀的顶层属性进行配置spring.ai.model.chat.spring-doc.cadn.net.cn

启用时,spring.ai.model.chat=vertexai(默认启用)spring-doc.cadn.net.cn

要禁用,请使用spring.ai.model.chat=none(或任何不匹配vertexai的值)spring-doc.cadn.net.cn

此改动旨在允许配置多个模型。spring-doc.cadn.net.cn

前缀spring.ai.vertex.ai.gemini作为属性前缀,允许你连接到VertexAI。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.model.chatspring-doc.cadn.net.cn

启用聊天模型客户端spring-doc.cadn.net.cn

顶点spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.project-idspring-doc.cadn.net.cn

Google Cloud Platform 项目标识spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.locationspring-doc.cadn.net.cn

地区spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.credentials-urispring-doc.cadn.net.cn

URI到顶点AI的Gemini凭证。提供后,用来生成a谷歌凭证实例以认证VertexAI.spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.api-endpointspring-doc.cadn.net.cn

Vertex AI Gemini API 端点。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.scopesspring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.transportspring-doc.cadn.net.cn

API传输。GRPC还是REST。spring-doc.cadn.net.cn

GRPCspring-doc.cadn.net.cn

前缀spring.ai.vertex.ai.gemini.chat是属性前缀,允许你配置VertexAI Gemini Chat的聊天模型实现。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vertex.ai.gemini.chat.options.modelspring-doc.cadn.net.cn

支持的VertexAI双子座聊天模型包括双子座2.0闪光灯,Gemini-2.0-Flash-Lite以及新的Gemini-2.5-pro-preview-03-25,Gemini-2.5-flash-preview-04-17模型。spring-doc.cadn.net.cn

双子座2.0闪光灯spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.response-mime-typespring-doc.cadn.net.cn

输出响应模拟生成候选文本的类型。spring-doc.cadn.net.cn

文本/纯文字:(默认)文本输出或application/json: JSON 回复。spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.google-search-retrievalspring-doc.cadn.net.cn

使用谷歌搜索接地功能spring-doc.cadn.net.cn

truefalse默认值false.spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.temperature。spring-doc.cadn.net.cn

控制输出的随机性。数值范围可超过[0.0,1.0],包括。接近1.0的值会产生更多样化的响应,而接近0.0的值通常导致生成反应的意外性降低。该值指定了后端在调用生成程序时使用的默认值。spring-doc.cadn.net.cn

0.7spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.top-kspring-doc.cadn.net.cn

抽样时应考虑的最大Tokens数量。生成式结合了Top-k和核采样。顶K抽样考虑topK最可能的符号集合。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.top-pspring-doc.cadn.net.cn

抽样时需要考虑的Tokens的最大累计概率。生成式结合了Top-k和核采样。核抽样考虑概率和至少为topP的最小标记集。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.candidate-countspring-doc.cadn.net.cn

需要返回的生成响应消息数量。该值必须介于[1, 8]之间,包含。默认为1。spring-doc.cadn.net.cn

1spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.max-输出-标记spring-doc.cadn.net.cn

最多可生成的Tokens数。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.ai.gemini.chat.options.tool-namesspring-doc.cadn.net.cn

列出了工具,按名称标识,用于在一次提示请求中实现函数调用。带有这些名称的工具必须存在于 ToolCallback 注册表中。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

(被淘汰工具名称) spring.ai.vertex.ai.gemini.chat.options.functionsspring-doc.cadn.net.cn

函数列表,按名称标识,以支持函数调用一次提示请求。带有这些名称的函数必须存在于函数回调注册表中。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.internal-tool-execution-enabledspring-doc.cadn.net.cn

如果为真,则应执行工具执行,否则模型的响应将返回给用户。默认是空,但如果是空,ToolCallingChatOptions.DEFAULT_TOOL_EXECUTION_ENABLED这点是真的,将考虑在内spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

(被淘汰内部工具执行启用) spring.ai.vertex.ai.gemini.chat.options.proxy-tool-callsspring-doc.cadn.net.cn

如果属实,Spring AI不会在内部处理函数调用,而是会代理给客户端。那么,客户端负责处理函数调用,将其派遣到相应的函数,并返回结果。如果 false(默认),Spring AI 会在内部处理函数调用。仅适用于支持函数调用的聊天模型spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.chat.options.safety-settingsspring-doc.cadn.net.cn

控制安全过滤器的安全设置列表,均由Vertex AI安全过滤器定义。每个安全设置可以有方法、阈值和类别。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

所有以spring.ai.vertex.ai.gemini.chat.options可以通过在运行时为 请求添加特定的运行时选项来覆盖提示叫。

运行时选项

VertexAiGeminiChatOptions.java提供模型配置,如温度、topK等。spring-doc.cadn.net.cn

启动时,默认选项可以配置为VertexAiGeminiChatModel(api, options)构造者或spring.ai.vertex.ai.chat.options.*性能。spring-doc.cadn.net.cn

运行时,你可以通过添加新的、针对请求的选项来覆盖默认选项提示叫。 例如,要覆盖特定请求的默认温度:spring-doc.cadn.net.cn

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        VertexAiGeminiChatOptions.builder()
            .temperature(0.4)
        .build()
    ));
除了型号本身VertexAiGeminiChatOptions你可以使用一个用ChatOptions#builder()创建的便携式ChatOptions实例。

工具调用

Vertex AI Gemini 模型支持工具调用(在 Google Gemini 语境中称为函数调用)功能,使模型在对话中能够使用工具。 这里有一个定义和使用的示例@Tool基于工具:spring-doc.cadn.net.cn

public class WeatherService {

    @Tool(description = "Get the weather in location")
    public String weatherByLocation(@ToolParam(description= "City or state name") String location) {
        ...
    }
}

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .tools(new WeatherService())
        .call()
        .content();

你也可以用java.util.function 豆子作为工具:spring-doc.cadn.net.cn

@Bean
@Description("Get the weather in location. Return temperature in 36°F or 36°C format.")
public Function<Request, Response> weatherFunction() {
    return new MockWeatherService();
}

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .toolNames("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();

更多信息请参见工具文档。spring-doc.cadn.net.cn

模 态

多模态指的是模型能够同时理解和处理来自多个(输入)来源的信息,包括文本,PDF格式,图像,音频以及其他数据格式。spring-doc.cadn.net.cn

图像、音频、视频

谷歌的Gemini AI模型支持这一能力,能够理解并整合文本、代码、音频、图像和视频。 更多细节请参见博客文章《介绍双子座》。spring-doc.cadn.net.cn

春季 AI消息界面通过引入媒体类型支持多模态 AI 模型。 这种类型包含关于消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType以及一个java.lang.Object对于原始媒体数据。spring-doc.cadn.net.cn

以下是从 VertexAiGeminiChatModelIT#multiModalityTest() 提取的一个简单代码示例,演示了用户文本与图片的组合。spring-doc.cadn.net.cn

byte[] data = new ClassPathResource("/vertex-test.png").getContentAsByteArray();

var userMessage = new UserMessage("Explain what do you see on this picture?",
        List.of(new Media(MimeTypeUtils.IMAGE_PNG, this.data)));

ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));

PDF格式

最新 Vertex Gemini 支持PDF输入类型。 使用该申请表/PDF将PDF文件附加到消息后,媒体类型:spring-doc.cadn.net.cn

var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");

var userMessage = new UserMessage(
        "You are a very professional document summarization specialist. Please summarize the given document.",
        List.of(new Media(new MimeType("application", "pdf"), pdfData)));

var response = this.chatModel.call(new Prompt(List.of(userMessage)));

采样控制器

创建一个新的 Spring Boot 项目并添加Spring-AI-starter-model-vertex-ai-gemini对你的POM(或Gradle)依赖。spring-doc.cadn.net.cn

添加一个application.properties文件,在src/主/资源目录,用于启用和配置VertexAi聊天模型:spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.project-id=PROJECT_ID
spring.ai.vertex.ai.gemini.location=LOCATION
spring.ai.vertex.ai.gemini.chat.options.model=gemini-2.0-flash
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
替换项目标识附带您的 Google Cloud Project ID 和位置是谷歌云区域 喜欢美国中部1,欧洲-西方1等。。。

每个模型都有自己的支持区域集合,你可以在模型页面找到支持区域列表。spring-doc.cadn.net.cn

例如,模型=双子座2.5闪光目前可在美国中部1仅限区域,必须设置位置=美国中部1, 在型号页面下,Gemini 2.5闪存——支持的区域spring-doc.cadn.net.cn

这将产生VertexAiGeminiChatModel你可以把这些实现注入到你的类里。 这里有一个简单的例子@Controller使用聊天模式生成文本的课程。spring-doc.cadn.net.cn

@RestController
public class ChatController {

    private final VertexAiGeminiChatModel chatModel;

    @Autowired
    public ChatController(VertexAiGeminiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }
}

手动配置

VertexAiGeminiChatModel 实现了聊天模型并且使用VertexAI连接到 Vertex AI Gemini 服务。spring-doc.cadn.net.cn

添加春-艾-顶点-艾-双子对你项目Maven的依赖pom.xml文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-vertex-ai-gemini</artifactId>
</dependency>

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

接下来,创建一个VertexAiGeminiChatModel并用于文本生成:spring-doc.cadn.net.cn

VertexAI vertexApi =  new VertexAI(projectId, location);

var chatModel = new VertexAiGeminiChatModel(this.vertexApi,
    VertexAiGeminiChatOptions.builder()
        .model(ChatModel.GEMINI_2_0_FLASH)
        .temperature(0.4)
    .build());

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

VertexAiGeminiChatOptions提供聊天请求的配置信息。 这VertexAiGeminiChatOptions.Builder是流利期权构建器。spring-doc.cadn.net.cn

低级 Java 客户端

以下类图展示了 Vertex AI Gemini 原生 Java API:spring-doc.cadn.net.cn

顶点AI Gemini原生API