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 聊天客户端的 Spring Boot 自动配置。要启用此功能,请将以下依赖项添加到项目的 Maven pom.xml
或 Gradle build.gradle
构建文件中:
- 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'
}
请参考 依赖管理 部分,将 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 | Vertex AI Gemini 凭据的 URI。当提供时,用于创建一个 GoogleCredentials 实例以验证 VertexAI 。 | - |
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。 | GRPC |
前缀 spring.ai.vertex.ai.gemini.chat
是用于配置 VertexAI Gemini Chat 聊天模型实现的属性前缀。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.vertex.ai.gemini.chat.options.model | 支持的 Vertex AI Gemini Chat 模型 包括 (1.0) gemini-pro 、gemini-pro-vision (已弃用)以及新的 gemini-1.5-pro-001 、gemini-1.5-flash-001 模型。 | gemini-1.5-pro-001 |
spring.ai.vertex.ai.gemini.chat.options.responseMimeType | 生成的候选文本的输出响应 MIME 类型。 | text/plain : (默认)文本输出或 application/json : JSON 响应。 |
spring.ai.vertex.ai.gemini.chat.options.googleSearchRetrieval | 使用 Google 搜索 Grounding 功能 | true 或 false ,默认 false 。 |
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 | 采样时考虑的最大 token 数量。生成模型使用 Top-k 和核采样结合的方式。Top-k 采样考虑前 topK 个最可能的 token。 | - |
spring.ai.vertex.ai.gemini.chat.options.topP | 采样时考虑的 token 的最大累积概率。生成模型使用 Top-k 和核采样结合的方式。核采样考虑概率总和至少为 topP 的最小 token 集。 | - |
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 | 生成的最大 token 数量。 | - |
spring.ai.vertex.ai.gemini.chat.options.frequencyPenalty | - | |
spring.ai.vertex.ai.gemini.chat.options.presencePenalty | - | |
spring.ai.vertex.ai.gemini.chat.options.toolNames | 工具名称列表,用于在单个提示请求中启用函数调用。具有这些名称的工具必须存在于 ToolCallback 注册表中。 | - |
(已弃用,由 toolNames 替代)spring.ai.vertex.ai.gemini.chat.options.functions | 函数名称列表,用于在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。 | - |
spring.ai.vertex.ai.gemini.chat.options.proxy-tool-calls | 如果为 true,Spring AI 不会在内部处理函数调用,而是将其代理给客户端。然后由客户端负责处理函数调用,将其分派给适当的函数并返回结果。如果为 false(默认),Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 | false |
spring.ai.vertex.ai.gemini.chat.options.safetySettings | 控制安全过滤器的安全设置列表,如 Vertex AI 安全过滤器 所定义。每个安全设置可以包含方法、阈值和类别。 | - |
所有以 spring.ai.vertex.ai.gemini.chat.options
为前缀的属性都可以在运行时通过在 Prompt
调用中添加特定请求的运行时选项来覆盖。
运行时选项
VertexAiGeminiChatOptions.java 提供了模型配置,例如 temperature
、topK
等。
在启动时,默认选项可以通过 VertexAiGeminiChatModel(api, options)
构造函数或 spring.ai.vertex.ai.chat.options.*
属性进行配置。
在运行时,你可以通过向 Prompt
调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认 temperature
值:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
VertexAiGeminiChatOptions.builder()
.temperature(0.4)
.build()
));
除了特定于模型的 VertexAiGeminiChatOptions
外,你还可以使用一个通用的 ChatOptions 实例,该实例可以通过 ChatOptionsBuilder#builder() 创建。
工具调用
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
中的 beans 作为工具使用:
@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();
更多内容请参阅 工具 文档。
多模态
多模态(Multimodality)指的是模型能够同时理解并处理来自多种来源的信息,包括 文本
、PDF
、图像
、音频
以及其他数据格式。
图像、音频、视频
Google 的 Gemini AI 模型通过理解和整合文本、代码、音频、图像和视频来支持这一功能。更多详细信息,请参阅博客文章 Introducing Gemini。
Spring AI 的 Message
接口通过引入 Media 类型来支持多模态 AI 模型。该类型包含消息中媒体附件的数据和信息,使用 Spring 的 org.springframework.util.MimeType
和一个 java.lang.Object
来存储原始媒体数据。
以下是从 VertexAiGeminiChatModelIT.java 中提取的一个简单代码示例,展示了用户文本与图像的结合。
// 示例代码展示了如何将用户文本与图像结合
public class VertexAiGeminiChatModelIT {
@Test
public void testTextWithImage() {
// 加载图像
Image image = loadImage("path/to/image.jpg");
// 创建用户消息
UserMessage userMessage = new UserMessage("这是一张图片的描述", image);
// 发送消息并获取响应
ChatResponse response = chatModel.sendMessage(userMessage);
// 打印响应
System.out.println(response.getText());
}
private Image loadImage(String path) {
// 加载图像的逻辑
return new Image(path);
}
}
在这个示例中,代码展示了如何加载一张图片并将其与用户文本结合,然后通过 chatModel
发送消息并获取响应。
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/pdf
媒体类型将 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)));
示例控制器
创建一个新的 Spring Boot 项目,并将 spring-ai-vertex-ai-gemini-spring-boot-starter
添加到你的 pom(或 gradle)依赖中。
在 src/main/resources
目录下添加一个 application.properties
文件,以启用并配置 Vertex AI 聊天模型:
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
类示例,它使用聊天模型来生成文本。
@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
依赖项添加到您项目的 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 添加到你的构建文件中。
接下来,创建一个 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
是一个流式的选项构建器。
低级 Java 客户端
以下类图展示了 Vertex AI Gemini 原生 Java API: