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.gradle
build 文件:
-
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.functions |
函数列表,由其名称标识,用于在单个提示请求中启用函数调用。具有这些名称的函数必须存在于 functionCallbacks 注册表中。 |
- |
spring.ai.vertex.ai.gemini.chat.options.proxy-tool-calls |
如果为true,则 Spring AI 将不会在内部处理函数调用,而是将它们代理给客户端。然后,客户端负责处理函数调用,将它们分派给适当的函数,并返回结果。如果为 false (默认值),则 Spring 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()
.withTemperature(0.4)
.build()
));
除了特定于模型的VertexAiGeminiChatOptions 您可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。 |
函数调用
截至 2023 年 4 月 30 日,Vertex AIGemini Pro model 显著降低了对函数调用的支持!虽然该功能仍然可用,但不建议将其用于生产用途。
显然 Gemini Pro 无法再正确处理函数名称。
并行函数调用也消失了。 |
您可以使用 VertexAiGeminiChatModel 注册自定义 Java 函数,并让 Gemini Pro 模型智能地选择输出包含参数的 JSON 对象,以调用一个或多个已注册的函数。 这是一种将 LLM 功能与外部工具和 API 连接起来的强大技术。 阅读有关 Vertex AI Gemini 函数调用的更多信息。
模 态
多模态是指模型同时理解和处理来自各种来源的信息的能力,包括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/pdf
media 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.gradle
build 文件。
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()
.withModel(ChatModel.GEMINI_PRO_1_5_PRO)
.withTemperature(0.4)
.build());
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
这VertexAiGeminiChatOptions
提供聊天请求的配置信息。
这VertexAiGeminiChatOptions.Builder
是 Fluent Options Builder。