VertexAI Gemini 聊天

Vertex AI Gemini API 允许开发人员使用 Gemini 模型构建生成式 AI 应用程序。 Vertex AI Gemini API 支持将多模态提示作为输入和输出文本或代码。 多模态模型是一种能够处理来自多种模态的信息(包括图像、视频和文本)的模型。例如,您可以向模型发送一盘饼干的照片,并要求它为您提供这些饼干的配方。spring-doc.cadn.net.cn

Gemini 是由 Google DeepMind 开发的一系列生成式 AI 模型,专为多模式用例而设计。Gemini API 可让您访问 Gemini 1.0 Pro Vision 和 Gemini 1.0 Pro 型号。有关 Vertex AI Gemini API 模型的规格,请参阅模型信息spring-doc.cadn.net.cn

先决条件

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

自动配置

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

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-vertex-ai-gemini-spring-boot-starter</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

聊天属性

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

财产 描述 违约

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

Google Cloud Platform 项目 IDspring-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.credentialsUrispring-doc.cadn.net.cn

URI 的 Vertex AI Gemini 凭证。当提供时,它用于创建 aGoogleCredentials实例对VertexAI.spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.gemini.apiEndpointspring-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

自粘橡胶spring-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

支持的 Vertex AI Gemini Chat 模型包括 (1.0 )gemini-pro,gemini-pro-vision(已弃用)和新的gemini-1.5-pro-001,gemini-1.5-flash-001模型。spring-doc.cadn.net.cn

双子座-1.5-PRO-001spring-doc.cadn.net.cn

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

生成的候选文本的输出响应 mimetype。spring-doc.cadn.net.cn

text/plain:(默认)文本输出或application/json:JSON 响应。spring-doc.cadn.net.cn

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

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

truefalse违约false.spring-doc.cadn.net.cn

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

控制输出的随机性。值的范围可以超过 [0.0,1.0] 和 [0.0,1.0]。接近 1.0 的值将产生更多变化的响应,而接近 0.0 的值通常会导致来自生成项的响应不那么令人惊讶。此值指定 default 在调用 generate 时由后端使用。spring-doc.cadn.net.cn

0.8spring-doc.cadn.net.cn

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

采样时要考虑的最大令牌数。生成使用结合了 Top-k 和 nucleus 采样。Top-k 采样会考虑 topK 最可能的代币集。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

采样时要考虑的 token 的最大累积概率。生成使用结合了 Top-k 和 nucleus 采样。Nucleus 采样考虑概率和至少为 topP 的最小标记集。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

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

-spring-doc.cadn.net.cn

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

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

-spring-doc.cadn.net.cn

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

要生成的最大令牌数。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

-spring-doc.cadn.net.cn

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

-spring-doc.cadn.net.cn

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

工具列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的工具必须存在于 ToolCallback 注册表中。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

(废弃toolNames) spring.ai.vertex.ai.gemini.chat.options.functionsspring-doc.cadn.net.cn

函数列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

如果为true,则 Spring AI 将不会在内部处理函数调用,而是将它们代理给客户端。然后,客户端负责处理函数调用,将它们分派给适当的函数,并返回结果。如果为 false (默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

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

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

-spring-doc.cadn.net.cn

所有前缀为spring.ai.vertex.ai.gemini.chat.options可以通过向Prompt叫。

运行时选项

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

启动时,可以使用VertexAiGeminiChatModel(api, options)constructor 或spring.ai.vertex.ai.chat.options.*性能。spring-doc.cadn.net.cn

在运行时,您可以通过向Prompt叫。 例如,要覆盖特定请求的默认温度: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您可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。

工具调用

Vertex AI 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 bean 用作工具: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?")
        .tools("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();

工具文档中查找更多信息。spring-doc.cadn.net.cn

模 态

多模态是指模型同时理解和处理来自各种来源的信息的能力,包括text,pdf,images,audio和其他数据格式。spring-doc.cadn.net.cn

图像、音频、视频

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

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

下面是从 VertexAiGeminiChatModelIT.java 中提取的简单代码示例,演示了用户文本与图像的组合。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 输入类型的支持.. 使用application/pdfmedia type 将 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)));

Samples控制器

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

添加application.properties文件中的src/main/resources目录中,以启用和配置 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=vertex-pro-vision
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
project-id替换为您的 Google Cloud 项目 ID,并将locationGemini 位置

这将创建一个VertexAiGeminiChatModel实现,您可以将其注入到您的类中。 下面是一个简单的示例@Controller使用 Chat 模型生成文本的类。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 实现了ChatModel并使用VertexAI连接到 Vertex AI Gemini 服务。spring-doc.cadn.net.cn

添加spring-ai-vertex-ai-gemini依赖项添加到项目的 Mavenpom.xml文件:spring-doc.cadn.net.cn

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

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

VertexAI vertexApi =  new VertexAI(projectId, location);

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

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

VertexAiGeminiChatOptions提供聊天请求的配置信息。 这VertexAiGeminiChatOptions.Builder是 Fluent Options Builder。spring-doc.cadn.net.cn

低级 Java 客户端

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

Vertex AI Gemini 原生 API