VertexAI Gemini Chat

Vertex AI Gemini API允许开发人员使用 Gemini 模型构建生成式 AI 应用程序。Vertex AI Gemini API 支持多模态提示作为输入和输出文本或代码。多模态模型是一个模型,能够处理来自多个模态的信息,包括图像、视频和文本。例如,您可以向模型发送一盘饼干的照片,并要求它为您提供这些饼干的食谱。 Gemini 是由 Google DeepMind 开发的一系列生成式 AI 模型,专为多模态用例而设计。Gemini API 使您能够访问 Gemini 1.0 Pro Vision 和 Gemini 1.0 Pro 模型。有关 Vertex AI Gemini API 模型的规范,请参阅 Model informationGemini API Reference

Prerequisites

设置 Java 开发环境。通过运行以下命令进行身份验证。将 PROJECT_ID 替换为 Google Cloud 项目 ID,将 ACCOUNT 替换为 Google Cloud 用户名。

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

Auto-configuration

Spring AI 为 VertexAI Gemini 聊天客户端提供了 Spring Boot 自动配置。要启用它,请向项目 Maven pom.xml 文件添加以下依赖项:

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

或添加到 Gradle build.gradle 构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini-spring-boot-starter'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Chat Properties

前缀 spring.ai.vertex.ai.gemini 用作允许您连接到 VertexAI 的属性前缀。

Property Description Default

spring.ai.vertex.ai.gemini.projectId

Google Cloud Platform 项目 ID

-

spring.ai.vertex.ai.gemini.location

Region

-

spring.ai.vertex.ai.gemini.credentialsUri

指向 Vertex AI Gemini 凭据的 URI。提供后,它将用于创建 GoogleCredentials 实例以对 VertexAI 进行身份验证。

-

前缀 spring.ai.vertex.ai.gemini.chat 是允许您配置用于 VertexAI Gemini 聊天的聊天客户端实现的属性前缀。

Property Description Default

spring.ai.vertex.ai.gemini.chat.options.model

这是要使用的 Vertex AI Gemini Chat model

gemini-pro-vision

spring.ai.vertex.ai.gemini.chat.options.temperature

控制输出的随机性。值介于 [0.0,1.0],包括在内。接近 1.0 的值将产生差异更大的响应,而接近 0.0 的值通常会产生来自生成器的较不意外的响应。此值指定在向生成器发出调用时后端使用的默认值。

0.8

spring.ai.vertex.ai.gemini.chat.options.topK

在采样时要考虑的最大令牌数。生成式模型使用组合 Top-k 和核采样。Top-k 采样考虑的是最可能的 topK 令牌集合。

-

spring.ai.vertex.ai.gemini.chat.options.topP

在采样时要考虑的令牌的最大累积概率。生成式模型使用组合 Top-k 和核采样。核采样考虑的令牌集合最少,但不低于 topP 的概率和。

-

spring.ai.vertex.ai.gemini.chat.options.candidateCount

要返回的生成响应消息数。此值必须介于 [1, 8],包括在内。默认为 1。

-

spring.ai.vertex.ai.gemini.chat.options.candidateCount

要返回的生成响应消息数。此值必须介于 [1, 8],包括在内。默认为 1。

-

spring.ai.vertex.ai.gemini.chat.options.maxOutputTokens

要生成的令牌数最大值。

-

spring.ai.vertex.ai.gemini.chat.options.frequencyPenalty

-

spring.ai.vertex.ai.gemini.chat.options.presencePenalty

-

spring.ai.vertex.ai.gemini.chat.options.functions

按其名称标识的函数列表,以在单提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。

-

带有 spring.ai.vertex.ai.gemini.chat.options 前缀的所有属性都可以通过将特定请求 Runtime options 添加到 Prompt 调用在运行时进行覆盖。

Runtime options

VertexAiGeminiChatOptions.java提供模型配置,例如温度、前 K 个等。

在启动时,可以使用 VertexAiGeminiChatClient(api, options) 构造函数或 spring.ai.vertex.ai.chat.options.* 属性配置默认选项。

在运行时,可以通过向 Prompt 调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认温度:

ChatResponse response = chatClient.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        VertexAiPaLm2ChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));

除了特定 VertexAiChatPaLm2Options 的模型之外,您还可以使用使用 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。

Function Calling

你可以使用 VertexAiGeminiChatClient 注册自定义 Java 函数,并让 Gemini Pro 模型智能地选择输出一个 JSON 对象,该对象包含用于调用一个或多个已注册函数的参数。这是一种将 LLM 功能与外部工具和 API 结合起来的强大技术。阅读更多有关 Vertex AI Gemini Function Calling 的信息。

Multimodal

多模态是指模型同时理解和处理来自各种来源(包括文本、图像、音频和其他数据格式)的信息的能力。此范例表示 AI 模型的重大进步。

Google 的 Gemini AI 模型通过理解和集成文本、代码、音频、图像和视频来支持此功能。有关更多详细信息,请参阅博客文章 Introducing Gemini

Spring AI 的 Message 接口通过引入媒体类型来支持多模态 AI 模型。此类型包含消息中媒体附件的数据和信息,使用 Spring 的 org.springframework.util.MimeTypejava.lang.Object 作为原始媒体数据。

下面是从 VertexAiGeminiChatClientIT.java中提取的一个简单的代码示例,展示了用户文本与图像的组合。

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

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

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

Sample Controller

Create 一个新的 Spring Boot 项目,并将 spring-ai-vertex-ai-palm2-spring-boot-starter 添加到你的 pom(或 gradle)依赖项。

src/main/resources 目录下添加一个 application.properties 文件,以启用并配置 VertexAi Chat 客户端:

spring.ai.vertex.ai.gemini.project-id=PROJECT_ID
spring.ai.vertex.ai.gemini.location=LOCATION
spring.ai.vertex.ai.gemini.chat.options.model=vertex-pro-vision
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5

api-key 替换为你的 VertexAI 凭据。

这将创建一个 VertexAiGeminiChatClient 实现,您可以将其注入您的类中。这里有一个简单的 @Controller 类的示例,它使用聊天客户端进行文本生成。

@RestController
public class ChatController {

    private final VertexAiGeminiChatClient chatClient;

    @Autowired
    public ChatController(VertexAiGeminiChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatClient.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 chatClient.stream(prompt);
    }
}

Manual Configuration

VertexAiGeminiChatClient实现了 `ChatClient`并使用 `VertexAI`连接到 Vertex AI Gemini 服务。

向项目 Maven pom.xml 文件中添加 spring-ai-vertex-ai-gemini 依赖项:

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

或添加到 Gradle build.gradle 构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

接下来,创建一个 VertexAiGeminiChatClient 并使用它进行文本生成:

VertexAI vertexApi =  new VertexAI(projectId, location);

var chatClient = new VertexAiGeminiChatClient(vertexApi,
    VertexAiGeminiChatOptions.builder()
        .withTemperature(0.4)
    .build());

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

VertexAiGeminiChatOptions 提供了聊天请求的配置信息。VertexAiGeminiChatOptions.Builder 是流畅的选项构建器。