SDK Google Gen AI

Il nuovo SDK di IA generativa di Google fornisce un'interfaccia unificata a Gemini 2.0 tramite sia l'API Gemini Developer sia Vertex AI (l'API Gemini Enterprise). Con alcune eccezioni, il codice che viene eseguito su una piattaforma verrà eseguito su entrambe. L'SDK Gen AI supporta anche i modelli Gemini 1.5.

Python

L'SDK di AI generativa di Google per Python è disponibile su PyPI e GitHub.

Per scoprire di più, consulta il riferimento all'SDK Python.

Guida rapida

1. Installa l'SDK

pip install google-genai

2. Importa la libreria

from google import genai

3. Crea un client

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

4. Generare contenuti

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

Vai

L'SDK di IA generativa di Google per Go è disponibile su go.dev e GitHub.

Guida rapida

1. Importa la libreria

import "google.golang.org/genai"

2. Crea un client

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

3. Generare contenuti

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

Java

L'SDK Google Gen AI per Java è disponibile tramite Maven e GitHub.

Guida rapida

1. Importa la libreria

Se utilizzi Maven, aggiungi quanto segue alle dipendenze:

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

2. Crea un client

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. Generare contenuti

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