新しい Google Gen AI SDK は、Gemini Developer API と Vertex AI(Gemini Enterprise API)の両方を通じて Gemini 2.0 への統合インターフェースを提供します。いくつかの例外を除き、1 つのプラットフォームで実行されるコードは両方のプラットフォームで実行されます。Gen AI SDK は Gemini 1.5 モデルもサポートしています。
Python
Google Gen AI SDK for Python は、PyPI と GitHub で入手できます。
詳細については、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 用の Google Gen AI SDK は、go.dev と GitHub で入手できます。
クイックスタート
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
Google Gen AI SDK for Java は、Maven と GitHub から入手できます。
クイックスタート
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());
}
}