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

Google Gen AI SDK for Python 可在 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)

JavaScript(预览版)

适用于 TypeScript 和 JavaScript 的 Google Gen AI SDK 预览版已在 GitHub 上发布。

如需了解详情,请参阅 JavaScript SDK 参考文档

快速入门

1. 安装 SDK

npm install @google/genai

2. 导入库

import {GoogleGenAI} from '@google/genai';

3. 创建客户端

const ai = new GoogleGenAI({
  apiKey: "GEMINI_API_KEY",
});

4. 生成内容

async function main() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash-001',
    contents: 'How does RLHF work?',
  });
  console.log(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());
  }
}