VertexAI Gemini 聊天

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 模型的规格,请参阅 模型信息

先决条件

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

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

自动配置

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'
}
请参阅依赖管理部分,将 Spring AI BOM 添加到您的构建文件中。

聊天属性

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

属性 描述 默认

spring.ai.vertex.ai.gemini.projectId

Google Cloud平台项目ID

-

spring.ai.vertex.ai.gemini.location

区域

-

spring.ai.vertex.ai.gemini.credentialsUri

到Vertex AI Gemini凭证的URI。提供时,用于创建GoogleCredentials实例以对VertexAI进行身份验证。

-

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

属性 描述 默认值

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

这是要使用的Vertex AI Gemini聊天模型

gemini-pro-vision

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

控制输出的随机性。值可在[0.0,1.0]范围内,包括0.0和1.0。接近1.0的值将产生更多变化的响应,而接近0.0的值通常会导致生成的响应不太令人惊讶。该值指定后端在调用生成器时使用的默认值。

0.8

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

在采样时考虑的最大标记数。生成器使用了综合的Top-k和核心采样。 Top-k采样考虑了最可能的前k个标记集合。

-

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

在采样时考虑的标记的最大累积概率。生成器使用了综合的Top-k和核心采样。 核心采样考虑了概率和至少为topP的最小标记集合的概率和。

-

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

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

-

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

要返回的生成响应消息的数量。此值必须在[1, 8]范围内,包括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 开头的属性都可以通过在调用 Prompt 时添加特定于请求的 运行时选项 来在运行时进行覆盖。

运行时选项

VertexAiGeminiChatOptions.java 提供了模型配置,如温度、topK等。

在启动时,可以通过 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,您还可以使用可移植的 ChatOptions 实例,使用 ChatOptionsBuilder#builder() 创建。

函数调用

您可以向 VertexAiGeminiChatClient 注册自定义的 Java 函数,并让 Gemini Pro 模型智能选择输出一个包含调用注册函数之一或多个的参数的 JSON 对象。这是一种将 LLM 能力与外部工具和 API 连接的强大技术。详细了解有关 Vertex AI Gemini 函数调用 的信息。

多模态

多模态指的是模型同时理解和处理来自各种来源的信息,包括文本、图像、音频和其他数据格式。这种范式代表了人工智能模型的重大进步。

Google的Gemini人工智能模型通过理解和整合文本、代码、音频、图像和视频来支持这一能力。更多详情,请参阅博客文章介绍Gemini

Spring AI的Message接口通过引入媒体类型来支持多模态人工智能模型。该类型包含有关消息中媒体附件的数据和信息,使用了Spring的org.springframework.util.MimeTypejava.lang.Object来存储原始媒体数据。

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

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

var userMessage = new UserMessage("解释一下你在这张图片上看到了什么?",
        List.of(new Media(MimeTypeUtils.IMAGE_PNG, data)));

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

示例控制器

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

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

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
用您的 VertexAI 凭据替换 api-key

这将创建一个 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);
    }
}

手动配置

VertexAiGeminiChatClient 实现了 ChatClient 接口,并使用 VertexAI 来连接 Vertex AI Gemini 服务。

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

<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'
}
请参阅 依赖管理 部分,将 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("生成 5 个著名海盗的名字。"));

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