| 此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT! | 
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 模型的规格,请参阅模型信息。
先决条件
- 
安装 gcloud CLI,适合您的作系统。 
- 
通过运行以下命令进行身份验证。 取代 PROJECT_ID替换为您的 Google Cloud 项目 ID,并将ACCOUNT替换为您的 Google Cloud 用户名。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>自动配置
Spring AI 为 VertexAI Gemini Chat 客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml或 Gradlebuild.gradlebuild 文件:
- 
Maven 
- 
Gradle 
<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.ai.vertex.ai.gemini.projectId | Google Cloud Platform 项目 ID | - | 
| spring.ai.vertex.ai.gemini.location | 地区 | - | 
| spring.ai.vertex.ai.gemini.credentialsUri | URI 的 Vertex AI Gemini 凭证。当提供时,它用于创建 a | - | 
| spring.ai.vertex.ai.gemini.apiEndpoint | Vertex AI Gemini API 端点。 | - | 
| spring.ai.vertex.ai.gemini.scopes | - | |
| spring.ai.vertex.ai.gemini.transport | API 传输。GRPC 或 REST。 | 自粘橡胶 | 
前缀spring.ai.vertex.ai.gemini.chat是允许您为 VertexAI Gemini Chat 配置聊天模型实现的属性前缀。
| 财产 | 描述 | 违约 | 
|---|---|---|
| spring.ai.vertex.ai.gemini.chat.options.model | 支持的 Vertex AI Gemini Chat 模型包括 (1.0 ) | 双子座-1.5-PRO-001 | 
| spring.ai.vertex.ai.gemini.chat.options.responseMimeType | 生成的候选文本的输出响应 mimetype。 | 
 | 
| spring.ai.vertex.ai.gemini.chat.options.googleSearchRetrieval | 使用 Google 搜索接地功能 | 
 | 
| spring.ai.vertex.ai.gemini.chat.options.temperature | 控制输出的随机性。值的范围可以超过 [0.0,1.0] 和 [0.0,1.0]。接近 1.0 的值将产生更多变化的响应,而接近 0.0 的值通常会导致来自生成项的响应不那么令人惊讶。此值指定 default 在调用 generate 时由后端使用。 | 0.8 | 
| spring.ai.vertex.ai.gemini.chat.options.topK | 采样时要考虑的最大令牌数。生成使用结合了 Top-k 和 nucleus 采样。Top-k 采样会考虑 topK 最可能的代币集。 | - | 
| spring.ai.vertex.ai.gemini.chat.options.topP | 采样时要考虑的 token 的最大累积概率。生成使用结合了 Top-k 和 nucleus 采样。Nucleus 采样考虑概率和至少为 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.tool名称 | 工具列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的工具必须存在于 ToolCallback 注册表中。 | - | 
| (废弃者 | 函数列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。 | - | 
| spring.ai.vertex.ai.gemini.chat.options.proxy-tool-calls | 如果为true,则 Spring AI 将不会在内部处理函数调用,而是将它们代理给客户端。然后,客户端负责处理函数调用,将它们分派给适当的函数,并返回结果。如果为 false (默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 | 假 | 
| spring.ai.vertex.ai.gemini.chat.options.safetySettings | 用于控制安全过滤器的安全设置列表,由 Vertex AI 安全过滤器定义。每个安全设置都可以有方法、阈值和类别。 | - | 
运行时选项
VertexAiGeminiChatOptions.java 提供模型配置,例如温度、topK 等。
启动时,可以使用VertexAiGeminiChatModel(api, options)constructor 或spring.ai.vertex.ai.chat.options.*性能。
在运行时,您可以通过向Prompt叫。
例如,要覆盖特定请求的默认温度:
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基于的工具:
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 用作工具:
@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();在工具文档中查找更多信息。
模 态
多模态是指模型同时理解和处理来自各种来源的信息的能力,包括text,pdf,images,audio和其他数据格式。
图像、音频、视频
Google 的 Gemini AI 模型通过理解和集成文本、代码、音频、图像和视频来支持此功能。 有关更多详细信息,请参阅博客文章 Gemini 简介。
Spring AI 的Message界面通过引入 Media 类型来支持多模态 AI 模型。
此类型包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType以及java.lang.Object对于原始媒体数据。
下面是从 VertexAiGeminiChatModelIT.java 中提取的简单代码示例,演示了用户文本与图像的组合。
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 文件附加到消息中:
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)依赖项中。
添加application.properties文件中的src/main/resources目录中,以启用和配置 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| 将 project-id替换为您的 Google Cloud 项目 ID,并将location与 Gemini 位置。 | 
这将创建一个VertexAiGeminiChatModel实现,您可以将其注入到您的类中。
下面是一个简单的示例@Controller使用 Chat 模型生成文本的类。
@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-ai-vertex-ai-gemini依赖项添加到项目的 Mavenpom.xml文件:
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-vertex-ai-gemini</artifactId>
</dependency>或发送到您的 Gradlebuild.gradlebuild 文件。
dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini'
}| 请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 | 
接下来,创建一个VertexAiGeminiChatModel并将其用于文本生成:
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。
