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 的 Google Gen AI SDK 可在 go.devGitHub 上获取。

快速入门

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());
  }
}