|
此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3! |
Google GenAI Chat
The Google GenAI API 允许开发者使用 Google 的 Gemini 模型通过 Gemini 开发者 API 或 Vertex AI 构建生成式人工智能应用。 Google GenAI API 支持多模态提示作为输入,并输出文本或代码。 一个多模态模型能够处理来自多种模态的信息,包括图像、视频和文本。例如,您可以向模型发送一张饼干盘的照片,并请它给出这些饼干的Recipes。
Gemini 是由 Google DeepMind 开发的一系列生成式 AI 模型,旨在用于多模态应用场景。Gemini API 给您访问 Gemini 2.0 Flash、Gemini 2.0 Flash-Lite、所有 Gemini Pro 模型,直至最新的 Gemini 3 Pro。
此实现提供了两种认证模式:
-
Gemini 开发者 API: 使用 API 密钥进行快速原型制作和开发
-
Vertex AI: 使用Google Cloud凭据进行生产部署,具备企业级功能
前置条件
请选择以下之一的身份验证方法:
选项 1: 吉明尼开发人员 API(API 密钥)
-
从Google AI Studio获取API密钥
-
设置API密钥作为环境变量或在您的应用程序属性中
选项 2: Vertex AI(Google Cloud)
-
安装适用于您操作系统的 gcloud CLI。
-
通过运行以下命令进行身份验证。 将
PROJECT_ID替换为您的Google Cloud项目ID,将ACCOUNT替换为您的Google Cloud用户名。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>
Auto-configuration
|
Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。 |
Spring AI 为 Google GenAI 聊天客户端提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到您的项目 Maven pom.xml 或 Gradle build.gradle 构建文件中:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-google-genai</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-google-genai'
}
| 请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。 |
聊天属性
|
现在通过带有前缀 要启用,请设置:spring.ai.model.chat=google-genai(默认已启用) 要禁用,请设置spring.ai.model.chat=none(或任何不匹配google-genai的值) 此更改是为了允许配置多个模型。 |
连接属性
前缀spring.ai.google.genai用作属性前缀,让您能够连接到Google GenAI。
| <property> </property> | <description> </description> | 默认 |
|---|---|---|
spring.ai.model.chat |
启用聊天模型客户端 |
google-genai |
spring.ai.google.genai.api-key |
Gemini开发者API的API密钥。当提供时,客户端将使用Gemini开发者API而不是Vertex AI。 |
- |
spring.ai.google.genai.project-id |
Google Cloud Platform 项目ID(在 Vertex AI 模式下必填) |
- |
spring.ai.google.genai.location |
Google Cloud 区域(对于 Vertex AI 模式是必需的) |
- |
spring.ai.google.genai.credentials-uri |
使用指向Google Cloud凭证的URI。当提供时,将用于创建一个 |
- |
聊天模型属性
spring.ai.google.genai.chat 前缀是属性前缀,用于配置与 Google GenAI Chat 的聊天模型实现相关的设置。
| <property> </property> | <description> </description> | 默认 |
|---|---|---|
spring.ai.google.genai.chat.options.model |
支持使用的Google GenAI Chat 模型包括 |
gemini-2.0-flash |
spring.ai.google.genai.chat.options.response-mime-type |
输出生成的候选文本的响应MIME类型。 |
|
spring.ai.google.genai.chat.options.google-search-retrieval |
使用Google搜索功能 |
|
spring.ai.google.genai.chat.options.temperature |
控制输出的随机性。值可以在 [0.0,1.0] 的范围内,包括边界值。接近 1.0 的值将产生更加多变的回答,而接近 0.0 的值通常会导致生成的回答不那么令人惊讶。 |
- |
spring.ai.google.genai.chat.options.top-k |
考虑在采样时使用的最大token数量。生成模型使用结合了Top-k和核截断(nucleus)的采样方法。Top-k采样会考虑最有可能的前K个token集合。 |
- |
spring.ai.google.genai.chat.options.top-p |
考虑采样时要考虑的Tokens的最大累积概率。生成模型使用结合了Top-k和核取样方法。核取样会考虑那些概率总和至少为topP的最小Tokens集合。 |
- |
spring.ai.google.genai.chat.options.candidate-count |
返回的生成响应消息的数量。此值必须在 [1, 8] 范围内,包括边界值。默认值为 1。 |
1 |
spring.ai.google.genai.chat.options.max-output-tokens |
生成的最大token数。 |
- |
spring.ai.google.genai.chat.options.frequency-penalty |
减少重复性的频率惩罚。 |
- |
spring.ai.google.genai.chat.options.presence-penalty |
减少重复的存现罚金。 |
- |
spring.ai.google.genai.chat.options.thinking-budget |
思考预算用于思考过程。请参见思考配置。 |
- |
spring.ai.google.genai.chat.options.thinking-level |
应由模型生成的思考Tokens水平。有效值: |
- |
spring.ai.google.genai.chat.options.include-thoughts |
启用函数调用的思想签名。在使用Gemini 3 Pro时必需,以避免内部工具执行循环中的验证错误。详见思想签名。 |
false |
spring.ai.google.genai.chat.options.tool-names |
使用名称标识的工具列表,以在单个提示请求中启用功能调用。具有这些名称的工具必须存在于ToolCallback注册表中。 |
- |
spring.ai.google.genai.chat.options.tool-callbacks |
使用回调注册与ChatModel相关的工具。 |
- |
spring.ai.google.genai.chat.options.internal-tool-execution-enabled |
如果为true,则应执行工具执行,否则将模型的响应返回给用户。默认值为null,但如果为null,则会考虑 |
- |
spring.ai.google.genai.chat.options.safety-settings |
列表用于控制安全过滤器的安全设置,由Google GenAI 安全设置定义。每个安全设置可以包含方法、阈值和类别。 |
- |
spring.ai.google.genai.chat.options.cached-content-name |
此请求使用的缓存内容名称。当与 |
- |
spring.ai.google.genai.chat.options.use-cached-content |
是否使用可用的缓存内容。当设置为true且 |
false |
spring.ai.google.genai.chat.options.auto-cache-threshold |
超过此token阈值的提示将自动缓存。启用时,超出该值的提示将被自动缓存以供重用。设置为null以禁用自动缓存。 |
- |
spring.ai.google.genai.chat.options.auto-cache-ttl |
时间存活期(持续时间),以ISO-8601格式表示(例如, |
PT1H |
spring.ai.google.genai.chat.enable-cached-content |
启用 |
true |
所有以spring.ai.google.genai.chat.options开头的属性可以在运行时通过向Prompt调用添加请求特定的运行时选项来覆盖。 |
运行时选项
The GoogleGenAiChatOptions.java 提供了模型配置,例如温度、topK等。
启动时,可以使用GoogleGenAiChatModel(client, options)构造函数或spring.ai.google.genai.chat.options.*属性来配置默认选项。
在运行时,您可以覆盖默认选项并通过向Prompt调用添加新的、请求特定的选项来实现。例如,要为特定请求覆盖默认温度设置:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
GoogleGenAiChatOptions.builder()
.temperature(0.4)
.build()
));
除此之外,除了特定模型的GoogleGenAiChatOptions之外,您还可以使用一个通用的ChatOptions实例,该实例是通过调用ChatOptions#builder()创建的。 |
工具调用
The Google GenAI 模型支持工具调用(函数调用)能力,允许模型在对话中使用工具。
以下是定义和使用基于@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?")
.toolNames("weatherFunction")
.inputType(Request.class)
.call()
.content();
在工具文档中查找更多内容。
思考配置
Gemini模型支持一种"思考"能力,允许模型在生成响应之前进行更深入的推理。这通过ThinkingConfig控制,包括三个相关选项:thinkingBudget、thinkingLevel和includeThoughts。
思考层级
thinkingLevel选项控制模型生成的推理Tokens深度。此功能适用于支持思考(例如,Gemini 3 Pro Preview)的模型。
| 值 | <description> </description> |
|---|---|
|
简单的思考方式。适用于简单查询,在速度优先于深入分析的情况下使用。 |
|
广泛思考。适用于需要深入分析和逐步推理的复杂问题。 |
|
该模型使用其默认行为。 |
配置通过属性
spring.ai.google.genai.chat.options.model=gemini-3-pro-preview
spring.ai.google.genai.chat.options.thinking-level=HIGH
程序化配置
import org.springframework.ai.google.genai.common.GoogleGenAiThinkingLevel;
ChatResponse response = chatModel.call(
new Prompt(
"Explain the theory of relativity in simple terms.",
GoogleGenAiChatOptions.builder()
.model("gemini-3-pro-preview")
.thinkingLevel(GoogleGenAiThinkingLevel.HIGH)
.build()
));
思考预算
The thinkingBudget 选项为思考过程设置一个Tokens预算:
-
正数: 思考时的最大Tokens数(例如,
8192) -
零 (
0): 完全禁用思考 -
未设置: 模型根据查询复杂度自动决定
ChatResponse response = chatModel.call(
new Prompt(
"Solve this complex math problem step by step.",
GoogleGenAiChatOptions.builder()
.model("gemini-2.5-pro")
.thinkingBudget(8192)
.build()
));
选项兼容性
|
|
您可以将includeThoughts与thinkingLevel或thinkingBudget结合使用(但不能同时使用两者):
// For Gemini 3 Pro: use thinkingLevel + includeThoughts
ChatResponse response = chatModel.call(
new Prompt(
"Analyze this complex scenario.",
GoogleGenAiChatOptions.builder()
.model("gemini-3-pro-preview")
.thinkingLevel(GoogleGenAiThinkingLevel.HIGH)
.includeThoughts(true)
.build()
));
// For Gemini 2.5: use thinkingBudget + includeThoughts
ChatResponse response = chatModel.call(
new Prompt(
"Analyze this complex scenario.",
GoogleGenAiChatOptions.builder()
.model("gemini-2.5-pro")
.thinkingBudget(8192)
.includeThoughts(true)
.build()
));
模型支持
thinking 配置选项是模型特定的:
| 模型 | 思考层级 | 思考预算 | 注释 |
|---|---|---|---|
Gemini 3 Pro (预览版) |
✅ 支持 |
⚠️ 只向后兼容 |
使用 |
Gemini 2.5 Pro |
❌ 不支持 |
✅ 支持 |
使用 |
Gemini 2.5 Flash |
❌ 不支持 |
✅ 支持 |
使用 |
Gemini 2.5 Flash-Lite |
❌ 不支持 |
✅ 支持 |
默认禁用思考。设为 |
Gemini 2.0 Flash |
❌ 不支持 |
❌ 不支持 |
thinking 不可用。 |
|
| 启用思考功能会增加Tokens使用量和API成本。请根据您的查询复杂性适当使用。 |
思想特征
Gemini 3 Pro 引入了思维签名,这是一种不透明的字节数组,在函数调用过程中保留模型的推理上下文。当 includeThoughts 启用时,在内部工具执行循环中,模型返回的思维签名必须在同一次回合内传递回去。
当思维签名至关重要
重要:思维签名验证仅适用于当前回合-具体而言是在模型在内部工具执行循环中进行函数调用(包括并行和顺序)时。API 不会验证对话历史中的前几回合的思维签名。
来自Google 的文档:
-
在当前回合内强制执行函数调用验证
-
前一轮签名无需保留
-
当前轮次函数调用中缺少签名会导致Gemini 3 Pro返回HTTP 400错误
-
对于并行函数调用,只有第一个
functionCall部分携带签名
对于Gemini 2.5 Pro及更早版本的模型,思维签名是可选的,并且API较为宽容。
配置
使用配置属性启用思维签名:
spring.ai.google.genai.chat.options.model=gemini-3-pro-preview
spring.ai.google.genai.chat.options.include-thoughts=true
在运行时编程设置:
ChatResponse response = chatModel.call(
new Prompt(
"Your question here",
GoogleGenAiChatOptions.builder()
.model("gemini-3-pro-preview")
.includeThoughts(true)
.toolCallbacks(callbacks)
.build()
));
自动处理
Spring AI 在内部工具执行循环中自动处理思维签名。当internalToolExecutionEnabled为真(默认值)时,Spring AI:
-
从模型响应中提取思想特征
-
强制将其附加到发送回函数响应的正确
functionCall部分 -
在单个回合内的函数调用中,正确地传播它们(无论是并行还是顺序)
无需手动管理思维签名 - Spring AI 确保它们按照API规范正确地附加到functionCall部分。
示例与函数调用
@Bean
@Description("Get the weather in a location")
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
return new WeatherService();
}
// Enable includeThoughts for Gemini 3 Pro with function calling
String response = ChatClient.create(this.chatModel)
.prompt("What's the weather like in Boston?")
.options(GoogleGenAiChatOptions.builder()
.model("gemini-3-pro-preview")
.includeThoughts(true)
.build())
.toolNames("weatherFunction")
.call()
.content();
手动工具执行模式
如果将internalToolExecutionEnabled=false设置为手动控制工具执行循环,在使用Gemini 3 Pro与includeThoughts=true时,您必须自己处理思维签名。
使用思维签名手动工具执行的要求:
-
从响应元数据中提取思想签名:
AssistantMessage assistantMessage = response.getResult().getOutput(); Map<String, Object> metadata = assistantMessage.getMetadata(); List<byte[]> thoughtSignatures = (List<byte[]>) metadata.get("thoughtSignatures"); -
在发送回函数响应时,请确保在消息历史中包含原始
AssistantMessage及其元数据。Spring AI 将会自动将思维签名附加到正确的functionCall部分。 -
Gemini 3 Pro 在当前回合未能保存思维签名将导致 API 返回 HTTP 400 错误。
| 仅当前轮次的函数调用需要思考签名。在完成一个函数调用回合后开始新的对话轮次时,不需要保留之前的轮次的签名。 |
启用includeThoughts会增加token使用量,因为响应中包含了思考过程。这会影响API成本,但能提供更好的推理透明度。 |
多模态
多模态指的是模型同时理解并处理来自各种(输入)来源的信息的能力,包括text、pdf、images、audio以及其他数据格式。
图像, 音频, 视频
Google的Gemini AI模型支持此功能,能够理解和整合文本、代码、音频、图像和视频。 如需了解详细信息,请参阅博客文章:介绍Gemini。
Spring AI的Message接口通过引入Media类型支持多模态AI模型。
这种类型包含消息中媒体附件的数据和信息,使用Spring的org.springframework.util.MimeType和一个java.lang.Object来承载原始媒体数据。
以下是一个从GoogleGenAiChatModelIT.java提取的简单代码示例,演示了用户文本与图片的结合。
byte[] data = new ClassPathResource("/vertex-test.png").getContentAsByteArray();
var userMessage = UserMessage.builder()
.text("Explain what do you see o this picture?")
.media(List.of(new Media(MimeTypeUtils.IMAGE_PNG, data)))
.build();
ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));
Google GenAI 提供了对 PDF 输入类型的支持。
使用 application/pdf 媒体类型将 PDF 文件附加到消息中:
var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");
var userMessage = UserMessage.builder()
.text("You are a very professional document summarization specialist. Please summarize the given document.")
.media(List.of(new Media(new MimeType("application", "pdf"), pdfData)))
.build();
var response = this.chatModel.call(new Prompt(List.of(userMessage)));
缓存的内容
Google GenAI的上下文缓存允许您缓存大量内容(如长文档、代码库或媒体),并在多个请求中重用这些内容。这大大减少了API成本,并提高了在相同内容上重复查询时的响应延迟。
好处
-
成本降低:缓存的Tokens按比常规输入Tokens低得多的价格计费(通常便宜75-90%)
-
性能提升:重用缓存的内容可减少大型上下文的处理时间
-
一致性: 同一缓存上下文确保多次请求返回一致的响应
使用缓存内容服务
Spring AI 提供 GoogleGenAiCachedContentService 用于程序化缓存管理。当使用 Spring Boot 自动配置时,该服务会自动配置。
创建缓存内容
@Autowired
private GoogleGenAiCachedContentService cachedContentService;
// Create cached content with a large document
String largeDocument = "... your large context here (>32k tokens) ...";
CachedContentRequest request = CachedContentRequest.builder()
.model("gemini-2.0-flash")
.contents(List.of(
Content.builder()
.role("user")
.parts(List.of(Part.fromText(largeDocument)))
.build()
))
.displayName("My Large Document Cache")
.ttl(Duration.ofHours(1))
.build();
GoogleGenAiCachedContent cachedContent = cachedContentService.create(request);
String cacheName = cachedContent.getName(); // Save this for reuse
使用缓存内容进行聊天请求
创建完缓存内容后,在您的聊天请求中引用它:
ChatResponse response = chatModel.call(
new Prompt(
"Summarize the key points from the document",
GoogleGenAiChatOptions.builder()
.useCachedContent(true)
.cachedContentName(cacheName) // Use the cached content name
.build()
));
通过配置属性:
spring.ai.google.genai.chat.options.use-cached-content=true
spring.ai.google.genai.chat.options.cached-content-name=cachedContent/your-cache-name
管理缓存内容
The GoogleGenAiCachedContentService 提供了全面的缓存管理:
// Retrieve cached content
GoogleGenAiCachedContent content = cachedContentService.get(cacheName);
// Update cache TTL
CachedContentUpdateRequest updateRequest = CachedContentUpdateRequest.builder()
.ttl(Duration.ofHours(2))
.build();
GoogleGenAiCachedContent updated = cachedContentService.update(cacheName, updateRequest);
// List all cached content
List<GoogleGenAiCachedContent> allCaches = cachedContentService.listAll();
// Delete cached content
boolean deleted = cachedContentService.delete(cacheName);
// Extend cache TTL
GoogleGenAiCachedContent extended = cachedContentService.extendTtl(cacheName, Duration.ofMinutes(30));
// Cleanup expired caches
int removedCount = cachedContentService.cleanupExpired();
异步操作
所有操作都有异步版本:
CompletableFuture<GoogleGenAiCachedContent> futureCache =
cachedContentService.createAsync(request);
CompletableFuture<GoogleGenAiCachedContent> futureGet =
cachedContentService.getAsync(cacheName);
CompletableFuture<Boolean> futureDelete =
cachedContentService.deleteAsync(cacheName);
Auto-Caching
Spring AI 可以自动缓存大型提示词,当它们超过指定的标记阈值时:
# Automatically cache prompts larger than 100,000 tokens
spring.ai.google.genai.chat.options.auto-cache-threshold=100000
# Set auto-cache TTL to 1 hour
spring.ai.google.genai.chat.options.auto-cache-ttl=PT1H
或编程方式:
ChatResponse response = chatModel.call(
new Prompt(
largePrompt,
GoogleGenAiChatOptions.builder()
.autoCacheThreshold(100000)
.autoCacheTtl(Duration.ofHours(1))
.build()
));
| 自动缓存适用于一次性处理大型上下文。对于相同上下文的重复使用,手动创建和引用缓存内容更高效。 |
监控缓存使用情况
缓存的内容包括可通过服务访问的使用元数据:
GoogleGenAiCachedContent content = cachedContentService.get(cacheName);
// Check if cache is expired
boolean expired = content.isExpired();
// Get remaining TTL
Duration remaining = content.getRemainingTtl();
// Get usage metadata
CachedContentUsageMetadata metadata = content.getUsageMetadata();
if (metadata != null) {
System.out.println("Total tokens: " + metadata.totalTokenCount().orElse(0));
}
最佳实践
-
缓存有效期: 根据您的使用场景设置适当的TTL。频繁变化的内容应设置较短的TTL,静态内容则可以设置较长的TTL。
-
强缓存命名:使用描述性的显示名称来轻松识别缓存的内容。
-
清理: 定期清除过期的缓存以保持组织有序。
-
Token阈值: 只缓存超过最小阈值(32,768个标记)的内容。
-
成本优化: 在多个请求中重用缓存的内容以最大限度地节省成本。
配置示例
完整配置示例:
# Enable cached content service (enabled by default)
spring.ai.google.genai.chat.enable-cached-content=true
# Use a specific cached content
spring.ai.google.genai.chat.options.use-cached-content=true
spring.ai.google.genai.chat.options.cached-content-name=cachedContent/my-cache-123
# Auto-caching configuration
spring.ai.google.genai.chat.options.auto-cache-threshold=50000
spring.ai.google.genai.chat.options.auto-cache-ttl=PT30M
样本控制器
创建一个新的Spring Boot项目,并在pom(或gradle)依赖中添加spring-ai-starter-model-google-genai。
在src/main/resources目录下添加一个application.properties文件,以启用并配置Google GenAI聊天模型:
使用Gemini开发者API(API密钥)
spring.ai.google.genai.api-key=YOUR_API_KEY
spring.ai.google.genai.chat.options.model=gemini-2.0-flash
spring.ai.google.genai.chat.options.temperature=0.5
使用 Vertex AI
spring.ai.google.genai.project-id=PROJECT_ID
spring.ai.google.genai.location=LOCATION
spring.ai.google.genai.chat.options.model=gemini-2.0-flash
spring.ai.google.genai.chat.options.temperature=0.5
替换project-id为您的Google云项目ID,location是Google云区域,例如us-central1、europe-west1等。 |
|
每个模型都有其支持的区域集合,在模型页面中可以找到支持的区域列表。 |
这将创建一个GoogleGenAiChatModel实现,你可以在你的类中注入。
以下是一个使用聊天模型进行文本生成的简单@Controller类示例。
@RestController
public class ChatController {
private final GoogleGenAiChatModel chatModel;
@Autowired
public ChatController(GoogleGenAiChatModel 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);
}
}
手动配置
The GoogleGenAiChatModel 实现了 ChatModel 并使用 com.google.genai.Client 连接到 Google GenAI 服务。
将如下的spring-ai-google-genai依赖添加到项目中Maven的pom.xml文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai</artifactId>
</dependency>
请将以下内容添加到您的Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-google-genai'
}
| 请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。 |
接下来,创建一个GoogleGenAiChatModel 并用于文本生成:
使用API密钥
Client genAiClient = Client.builder()
.apiKey(System.getenv("GOOGLE_API_KEY"))
.build();
var chatModel = new GoogleGenAiChatModel(genAiClient,
GoogleGenAiChatOptions.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."));
使用 Vertex AI
Client genAiClient = Client.builder()
.project(System.getenv("GOOGLE_CLOUD_PROJECT"))
.location(System.getenv("GOOGLE_CLOUD_LOCATION"))
.vertexAI(true)
.build();
var chatModel = new GoogleGenAiChatModel(genAiClient,
GoogleGenAiChatOptions.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."));
The GoogleGenAiChatOptions 提供了聊天请求的配置信息。
The GoogleGenAiChatOptions.Builder 是流畅选项构建器。
迁移至Vertex AI Gemini
如果当前您正在使用Vertex AI Gemini实现(spring-ai-vertex-ai-gemini),您可以使用最少的更改迁移到Google GenAI: