Google Gen AI SDK

新的 Google Gen AI SDK 透過 Gemini Developer API 和 Vertex AI (Gemini Enterprise API),提供統一的 Gemini 2.0 介面。除了少數例外狀況,在一個平台上執行的程式碼會在兩個平台上執行。Gen AI SDK 也支援 Gemini 1.5 模型。

Python

Python 專用的 Google Gen AI SDK 可在 PyPIGitHub 上取得。

詳情請參閱 Python SDK 參考資料

快速入門導覽課程

1. 安裝 SDK

pip install google-genai

2. 匯入程式庫

from google import genai

3. 建立用戶端

client = genai.Client(api_key='GEMINI_API_KEY')

4. 產生內容

response = client.models.generate_content(
    model='gemini-2.0-flash', contents='How does RLHF work?'
)
print(response.text)

Go

您可以在 go.devGitHub 取得 Go 適用的 Google Gen AI SDK。

快速入門導覽課程

1. 匯入程式庫

import "google.golang.org/genai"

2. 建立用戶端

client, err := genai.NewClient(ctx, &genai.ClientConfig{
    APIKey:   apiKey,
    Backend:  genai.BackendGeminiAPI,
})

3. 產生內容

// Call the GenerateContent method
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("How does RLHF work?"), nil)

Java

您可以透過 MavenGitHub 取得 Java 適用的 Google Gen AI SDK。

快速入門導覽課程

1. 匯入程式庫

如果您使用 Maven,請將下列依附元件新增至依附元件:

<dependencies>
  <dependency>
    <groupId>com.google.genai</groupId>
    <artifactId>google-genai</artifactId>
    <version>0.1.0</version>
  </dependency>
</dependencies>

2. 建立用戶端

import com.google.genai.Client;

// The client gets the API key from the environment variable `GOOGLE_API_KEY`
Client client = new Client();

// Use the builder class for instantiation.
Client client = Client.builder().apiKey("your-api-key").build();

3. 產生內容

package <your.pack.name>;

import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
import java.io.IOException;
import org.apache.http.HttpException;

public class GenerateContentWithTextInput {
  public static void main(String[] args) throws IOException, HttpException {
    Client client = new Client();

    GenerateContentResponse response =
        client.models.generateContent("gemini-2.0-flash-001", "How does RLHF work?", null);
    System.out.println("Unary response: " + response.text());
  }
}