前言
首先感謝 Google Developer Expert program 的贊助,以及 Google Developer Relations 的大力支持,讓我能夠前往現場,參與 Google IO 2025,並與產品開發團隊進行深入交流,了解更多更新與產品回饋。
在 Google IO 2025 大會上,Google 宣布了 GenAI SDK 的更新,包括:
新增 Java 官方支援
另外稍微研究一下,也將 Golang/JavaScript SDK 呼叫 Gemini-2.5-flash-preview-05-20 說明一下。
At Google IO 2025, Google announced updates to the GenAI SDK, including:
New Java support
I also tried and show how to call the latest Gemini 2.5 Flash model with Golang and JavaScript SDK.
主要更新
以下是本次 GenAI SDK 的重點更新內容:
新增 Java 支援
透過 Maven 新增 artifact id: `google-genai`
Add the following dependency to your Maven project:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Related Repository: java-genai
Example: (參考自 GenerateContent.java)
使用 Gemini API
使用 Gemini API
export GOOGLE_API_KEY=YOUR_API_KEY
使用 Vertex AI API / Using Vertex AI API
export GOOGLE_CLOUD_PROJECT=${PROJECT_NAME}
export GOOGLE_CLOUD_LOCATION=${LOCATION}
export GOOGLE_GENAI_USE_VERTEXAI=true
範例:
package com.google.genai.examples;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
/** An example of using the Unified Gen AI Java SDK to generate content. */
public final class GenerateContent {
public static void main(String[] args) {
try {
Client client = new Client();
String backend = client.vertexAI() ? "Vertex AI" : "Gemini Developer API";
System.out.println("Using " + backend);
String modelId = "gemini-2.5-flash-preview-05-20";
System.out.println("Sending request to " + modelId + "...");
GenerateContentResponse response = client.models.generateContent(
modelId,
"What is your name?",
null
);
if (response == null || response.text() == null) {
throw new IllegalStateException("Received invalid response from the API");
}
System.out.println("Response: " + response.text());
System.out.println("Full response: " + response);
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
throw e;
}
}
private GenerateContent() {}
}
JavaScript → Gemini-2.5-flash-preview-05-20
Repo: js-genai
新版 `@google/genai` 是 Google DeepMind 為生成式 AI 功能開發的官方 JavaScript SDK。
This SDK (@google/genai) is Google Deepmind’s "vanilla" SDK for its generative AI offerings, and is where Google Deepmind adds new AI features.
JavaScript 函式庫: `@google/genai`, 舊版本 `@google/generative-ai` 的支援只到 2025 年 8 月.
安裝方法
npm install @google/genai
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const response = await ai.models.generateContent({
// model: "gemini-2.0-flash",
model: "gemini-2.5-flash-preview-05-20",
contents: "How does AI work?",
});
console.log(response.text);
}
await main();
Golang → Gemini-2.5-flash-preview-05-20
與 Python, JavaScript SDK 一樣,原本的 SDK library 支援只到 2025 年 8 月。
Repo: go-genai
安裝
go get google.golang.org/genai
Import
import "google.golang.org/genai"
Gemini API Client
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})
Vertex AI Client
client, err := genai.NewClient(ctx, &genai.ClientConfig{
Project: project,
Location: location,
Backend: genai.BackendVertexAI,
})
完整範例參考自 thinking_generation
import (
"context"
"fmt"
"log"
"os"
"strings"
"google.golang.org/genai"
)
// Define the thinking model centrally
const modelID = "gemini-2.5-flash-preview-05-20"
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.Model(modelID)
response, err := model.GenerateContent(ctx, "How does AI work?")
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Text)
}
更多 範例
References
Vertex AI SDK: https://cloud.google.com/vertex-ai/generative-ai/docs/sdks/overview?hl=zh-tw
Gemini API SDK: https://ai.google.dev/gemini-api/docs/libraries
Golang Examples: https://github.com/google-gemini/api-examples/tree/main/go