VertexAI Gemini Chat
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 Client 提供 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。提供時,它會用於建立 |
- |
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-1.5-pro-001 |
spring.ai.vertex.ai.gemini.chat.options.responseMimeType |
產生的候選文字的輸出回應 mimetype。 |
|
spring.ai.vertex.ai.gemini.chat.options.googleSearchRetrieval |
使用 Google 搜尋 Grounding 功能 |
|
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.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 為字首的屬性都可以在執行時期透過將請求特定的 執行時期選項 新增至 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()
.withTemperature(0.4)
.build()
));
除了模型特定的 VertexAiGeminiChatOptions 之外,您還可以使用的可攜式 ChatOptions 實例,使用 ChatOptionsBuilder#builder() 建立。 |
函式呼叫
截至 2023 年 4 月 30 日,Vertex AI Gemini Pro 模型已顯著降低對函式呼叫的支援!雖然此功能仍然可用,但不建議用於生產環境。顯然,Gemini Pro 無法再正確處理函式名稱。平行函式呼叫也已消失。 |
您可以向 VertexAiGeminiChatModel 註冊自訂 Java 函式,並讓 Gemini Pro 模型智慧地選擇輸出 JSON 物件,其中包含呼叫一個或多個已註冊函式的引數。這是一種將 LLM 功能與外部工具和 API 連接的強大技術。閱讀更多關於Vertex AI Gemini 函式呼叫的資訊。
多模態
多模態指的是模型同時理解和處理來自各種來源資訊的能力,包括文字、影像、音訊和其他資料格式。這種範例代表了 AI 模型方面的重大進展。
Google 的 Gemini AI 模型透過理解和整合文字、程式碼、音訊、影像和影片來支援此功能。如需更多詳細資訊,請參閱部落格文章Gemini 介紹。
Spring AI 的 Message
介面透過引入媒體類型來支援多模態 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)));
範例控制器
建立新的 Spring Boot 專案,並將 spring-ai-vertex-ai-gemini-spring-boot-starter
新增至您的 pom (或 gradle) 依賴項。
在 src/main/resources
目錄下新增 application.properties
檔案,以啟用和設定 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
類別範例,該類別使用聊天模型進行文字產生。
@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()
.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
是流暢的選項建構器。