Google Gen AI SDK

Google Gen AI SDK は、Gemini Developer API と Vertex AI の Gemini API の両方を介して Gemini 2.0 への統合インターフェースを提供します。いくつかの例外を除き、1 つのプラットフォームで実行されるコードは両方のプラットフォームで実行されます。つまり、Developer API を使用してアプリケーションのプロトタイプを作成してから、コードを書き換えることなくアプリケーションを Vertex AI に移行できます。

Google Gen AI SDK は Gemini 1.5 モデルもサポートしています。

Python

Google Gen AI SDK for Python は、PyPI と GitHub で入手できます。

詳細については、Python SDK リファレンス(新しいタブで開く)をご覧ください。

クイックスタート

Vertex AI をエクスプレス モードで使用しているかどうかに応じて、次のいずれかのオプションを選択します。

  • Vertex AI(すべての機能とサービス)を使用する Google Cloud
from google import genai
from google.genai.types import HttpOptions

client = genai.Client(http_options=HttpOptions(api_version="v1"))
response = client.models.generate_content(
    model="gemini-2.0-flash-001",
    contents="How does AI work?",
)
print(response.text)
# Example response:
# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
#
# Here's a simplified overview:
# ...
  • エクスプレス モードで Vertex AI を使用する
from google import genai

# TODO(developer): Update below line
API_KEY = "YOUR_API_KEY"

client = genai.Client(vertexai=True, api_key=API_KEY)

response = client.models.generate_content(
    model="gemini-2.0-flash-001",
    contents="""Explain bubble sort to me.""",
)

print(response.text)
# Example response:
# Bubble Sort is a simple sorting algorithm that repeatedly steps through the list

Go

Go 用の Google Gen AI SDK は、go.dev と GitHub で入手できます。

クイックスタート

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/genai"
)

// generateWithText shows how to generate text using a text prompt.
func generateWithText(w io.Writer) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	resp, err := client.Models.GenerateContent(ctx,
		"gemini-2.0-flash-001",
		genai.Text("How does AI work?"),
		nil,
	)
	if err != nil {
		return fmt.Errorf("failed to generate content: %w", err)
	}

	respText, err := resp.Text()
	if err != nil {
		return fmt.Errorf("failed to convert model response to text: %w", err)
	}
	fmt.Fprintln(w, respText)
	// Example response:
	// That's a great question! Understanding how AI works can feel like ...
	// ...
	// **1. The Foundation: Data and Algorithms**
	// ...

	return nil
}

Java

Google Gen AI SDK for Java は、Maven Central と GitHub で入手できます。