VertexAI Gemini 聊天
Vertex AI Gemini API 允许开发人员使用 Gemini 模型构建生成式 AI 应用程序。 Vertex AI Gemini API 支持将多模态提示作为输入和输出文本或代码。 多模态模型是一种能够处理来自多种模态的信息的模型,包括图像、视频和文本。例如,您可以向模特发送一盘饼干的照片,并要求它为您提供这些饼干的Recipes。
Gemini 是由 Google DeepMind 开发的一系列生成式 AI 模型,专为多模态用例而设计。通过 Gemini API,您可以访问 Gemini 2.0 Flash 和 Gemini 2.0 Flash-Lite。 如需了解 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 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明。 |
Spring AI 为 VertexAI Gemini 聊天客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Mavenpom.xml
或 Gradlebuild.gradle
构建文件:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-vertex-ai-gemini</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-gemini'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。 |
聊天属性
现在,通过前缀为 要启用,spring.ai.model.chat=vertexai(默认启用) 要禁用,spring.ai.model.chat=none(或任何与vertexai不匹配的值) 进行此更改是为了允许配置多个模型。 |
前缀spring.ai.vertex.ai.gemini
用作允许您连接到 VertexAI 的属性前缀。
属性 | 描述 | 默认值 |
---|---|---|
spring.ai.model.聊天 |
启用聊天模型客户端 |
顶点赛 |
spring.ai.vertex.ai.gemini.project-id |
Google Cloud Platform 项目 ID |
- |
spring.ai.vertex.ai.gemini.location |
地区 |
- |
spring.ai.vertex.ai.gemini.credentials-uri |
URI 到 Vertex AI Gemini 凭据。提供时,它用于创建 |
- |
spring.ai.vertex.ai.gemini.api-endpoint |
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 模型包括 |
双子座-2.0-闪存 |
spring.ai.vertex.ai.gemini.chat.options.response-mime-type |
输出生成的候选文本的响应 mimetype。 |
|
spring.ai.vertex.ai.gemini.chat.options.google-search-retrieval |
使用 Google 搜索接地功能 |
|
spring.ai.vertex.ai.gemini.chat.options.temperature |
控制输出的随机性。值的范围可以超过 [0.0,1.0](含)。值接近 1.0 将产生更多样化的响应,而值接近 0.0 通常会导致生成响应不那么令人惊讶。此值指定后端在调用生成式时要使用的默认值。 |
0.7 |
spring.ai.vertex.ai.gemini.chat.options.top-k |
采样时要考虑的最大标记数。生成式结合使用 Top-k 和 nucleus 采样。Top-k 采样考虑 topK 最可能的标记集。 |
- |
spring.ai.vertex.ai.gemini.chat.options.top-p |
采样时要考虑的标记的最大累积概率。生成式结合使用 Top-k 和 nucleus 采样。Nucleus 采样考虑概率总和至少为 topP 的最小标记集。 |
- |
spring.ai.vertex.ai.gemini.chat.options.candidate-count |
要返回的生成响应消息数。此值必须介于 [1, 8] 之间,包括 [1, 8]。默认为 1。 |
1 |
spring.ai.vertex.ai.gemini.chat.options.max输出Tokens |
要生成的最大Tokens数。 |
- |
spring.ai.vertex.ai.gemini.chat.options.tool-names |
工具列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的工具必须存在于 ToolCallback 注册表中。 |
- |
(弃用者 |
函数列表,由其名称标识,以便在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。 |
- |
spring.ai.vertex.ai.gemini.chat.options.internal-tool-execution-enabled |
如果为 true,则应执行工具执行,否则将将来自模型的响应返回给用户。默认值为 null,但如果为 null, |
- |
(弃用者 |
如果为 true,则 Spring AI 不会在内部处理函数调用,而是将它们代理到客户端。然后,客户端负责处理函数调用,将它们分派到适当的函数,并返回结果。如果为 false(默认值),则 Spring AI 将在内部处理函数调用。仅适用于支持函数调用的聊天模型 |
false |
spring.ai.vertex.ai.gemini.chat.options.safety-settings |
用于控制安全过滤器的安全设置列表,如 Vertex AI 安全过滤器所定义。每个安全设置都可以有一个方法、阈值和类别。 |
- |
所有以spring.ai.vertex.ai.gemini.chat.options 可以通过将特定于请求的运行时选项添加到Prompt 叫。 |
运行时选项
VertexAiGeminiChatOptions.java提供模型配置,如温度、topK等。
启动时,可以使用VertexAiGeminiChatModel(api, options)
构造函数或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 您可以使用使用 ChatOptions#builder() 创建的可移植 ChatOptions 实例。 |
工具调用
Vertex AI Gemini 模型支持工具调用(在 Google Gemini 上下文中,它称为function calling
) 功能,允许模型在对话期间使用工具。
下面是一个如何定义和使用@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?")
.toolNames("weatherFunction")
.inputType(Request.class)
.call()
.content();
在工具文档中查找更多信息。
模 态
多模态是指模型同时理解和处理来自各种(输入)来源的信息的能力,包括text
,pdf
,images
,audio
和其他数据格式。
图像、音频、视频
谷歌的 Gemini AI 模型通过理解和集成文本、代码、音频、图像和视频来支持此功能。 有关更多详细信息,请参阅博客文章 Gemini 简介。
Spring AI 的Message
界面通过引入媒体类型支持多模态 AI 模型。
此类型包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType
和java.lang.Object
用于原始媒体数据。
下面是从 VertexAiGeminiChatModelIT#multiModalityTest() 中提取的简单代码示例,演示了用户文本与图像的组合。
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)));
Samples控制器
创建一个新的 Spring Boot 项目,并将spring-ai-starter-model-vertex-ai-gemini
到你的 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=gemini-2.0-flash
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
将project-id 替换为您的 Google Cloud 项目 ID 和location 是 Google Cloud 区域
喜欢us-central1 ,europe-west1 等。。。 |
每个模型都有自己的一组支持区域,您可以在模型页面中找到支持的区域列表。 例如,model= |
这将创建一个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>
或 Gradlebuild.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_2_0_FLASH)
.temperature(0.4)
.build());
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
这VertexAiGeminiChatOptions
提供聊天请求的配置信息。
这VertexAiGeminiChatOptions.Builder
是流畅的选项生成器。