Google Gen AI SDK

Das neue Google Gen AI SDK bietet eine einheitliche Benutzeroberfläche für Gemini 2.0 über die Gemini Developer API und Vertex AI (die Gemini Enterprise API). Mit wenigen Ausnahmen kann Code, der auf einer Plattform ausgeführt wird, auf beiden Plattformen ausgeführt werden. Das Gen AI SDK unterstützt auch die Gemini 1.5-Modelle.

Python

Das Google Gen AI SDK für Python ist auf PyPI und GitHub verfügbar.

Weitere Informationen finden Sie in der Python SDK-Referenz.

Kurzanleitung

1. SDK installieren

pip install google-genai

2. Bibliothek importieren

from google import genai

3. Client erstellen

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

4. Inhalte erstellen

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

JavaScript (Vorabversion)

Die Vorabversion des Google Gen AI SDK für TypeScript und JavaScript ist auf GitHub verfügbar.

Weitere Informationen finden Sie in der JavaScript SDK-Referenz.

Kurzanleitung

1. SDK installieren

npm install @google/genai

2. Bibliothek importieren

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

3. Client erstellen

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

4. Inhalte erstellen

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

Ok

Das Google Gen AI SDK für Go ist auf go.dev und GitHub verfügbar.

Kurzanleitung

1. Bibliothek importieren

import "google.golang.org/genai"

2. Client erstellen

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

3. Inhalte erstellen

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

Java

Das Google Gen AI SDK für Java ist über Maven und GitHub verfügbar.

Kurzanleitung

1. Bibliothek importieren

Wenn Sie Maven verwenden, fügen Sie Ihren Abhängigkeiten Folgendes hinzu:

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

2. Client erstellen

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. Inhalte erstellen

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