Google GenAI API允许开发者通过Gemini Developer API或Vertex AI,利用Google的Gemini模型构建生成式人工智能应用。Google GenAI API支持多模态提示作为输入,并输出文本或代码。多模态模型能够处理来自多种模态的信息,包括图像、视频和文本。例如,你可以向模型发送一盘饼干的照片,并要求它给出这些饼干的食谱。
Gemini是由谷歌DeepMind开发的一系列生成式人工智能模型,专为多模态用例设计。Gemini API可让您访问Gemini 2.0 Flash、Gemini 2.0 Flash-Lite和Gemini Pro模型。
此实现提供了两种身份验证模式:
前提条件
选择以下任一身份验证方法:
选项1:Gemini开发者API(API密钥)
从Google AI Studio获取API密钥
将API密钥设置为环境变量或应用程序属性
选项2:Vertex AI(谷歌云)
gcloud config set project <PROJECT_ID> &&gcloud auth application-default login <ACCOUNT>
自动配置
Spring AI 为 Google GenAI 聊天客户端提供了 Spring Boot 自动配置功能。要启用该功能,请将以下依赖项添加到项目的 Maven pom.xml 或 Gradle build.gradle 构建文件中:
Maven
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-google-genai</artifactId></dependency>
Gradle
dependencies { implementation 'org.springframework.ai:spring-ai-starter-model-google-genai'}
聊天属性
聊天自动配置的启用和禁用可通过带有前缀spring.ai.model.chat的顶级属性进行配置。
要启用,请将 spring.ai.model.chat 设置为 google-genai(默认情况下已启用)
要禁用,请将 spring.ai.model.chat 设置为 none(或任何与 google-genai 不匹配的值)
这一更改是为了允许配置多个模型。
连接属性
前缀“spring.ai.google.genai”用作属性前缀,可让您连接到Google GenAI。
| 属性 | 描述 | 默认值 |
|---|
spring.ai.model.chat | 启用聊天模型客户端 | google-genai |
spring.ai.google.genai.api-key | Gemini Developer API的API密钥。当提供时,客户端使用Gemini Developer 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。当提供时,它用于创建用于身份验证的 GoogleCredentials实例。
| - |
聊天模型属性
前缀“spring.ai.google.genai.chat”是属性前缀,用于配置Google GenAI Chat的聊天模型实现。
| 属性 | 描述 | 默认值 |
|---|
spring.ai.google.genai.chat.options.model | 支持使用的Google GenAI Chat模型包括 gemini-2.0-flash、gemini-2.0-flash-lite、gemini-pro和gemini-1.5-flash。
| gemini-2.0-flash |
spring.ai.google.genai.chat.options.response-mime-type | 输出生成的候选文本的响应mimetype。 | text/plain:(默认)文本输出或application/json:JSON响应。
|
spring.ai.google.genai.chat.options.google-search-retrieval | 使用谷歌搜索接地功能 | |
spring.ai.google.genai.chat.options.temperature | 控制输出的随机性。值的范围可以大于[0.0,1.0],包括[0.0,1.0]。接近1.0的值将产生变化更大的响应,而接近0.0的值通常会导致生成的响应不那么令人惊讶。 | 0.7 |
spring.ai.google.genai.chat.options.top-k | 采样时要考虑的最大标记数。生成器使用组合的Top-k和核采样。Top-k采样考虑topK个最可能的令牌的集合。 | - |
spring.ai.google.genai.chat.options.top-p | 采样时要考虑的令牌的最大累积概率。生成器使用组合的Top-k和核采样。核心采样考虑概率总和至少为topP的最小标记集。 | - |
spring.ai.google.genai.chat.options.candidate-count | 要返回的生成的响应消息数。此值必须介于[1,8]之间(包括[1,8])。1. | 1 |
spring.ai.google.genai.chat.options.max-output-tokens | 要生成的最大令牌数。 | - |
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.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,则为true的 ToolCallingChatOptions.DEFAULT_TOOL_EXECUTION_ENABLED将考虑
| - |
spring.ai.google.genai.chat.options.safety-settings | 用于控制安全过滤器的安全设置列表,由Google GenAI安全设置定义。每个安全设置都可以有一个方法、阈值和类别。 | - |
所有以“spring.ai.google.genai.chat.options”为前缀的属性,都可以通过在Prompt调用中添加特定于请求的运行时选项,在运行时进行覆盖。