Invia richieste all'API Vertex AI per Gemini

Invia richieste all'API Gemini in Vertex AI utilizzando un SDK per linguaggi di programmazione o l'API REST per iniziare a creare la tua applicazione di AI generativa su Google Cloud.

Per iniziare, crea un account Google Cloud

Per provare questa guida rapida, devi creare un account Google Cloud. Con questo account, riceverai 300 $ di credito gratuito, oltre all'utilizzo gratuito di oltre 20 prodotti, senza costi aggiuntivi.

Inizia gratuitamente

Per ulteriori informazioni sulla configurazione su Google Cloud dopo la creazione dell'account, consulta Eseguire la configurazione su Google Cloud.

Configura l'ambiente

Fai clic per scoprire come configurare il tuo ambiente

Scopri come configurare il tuo ambiente selezionando una delle seguenti schede:

Python

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Installa o aggiorna l'SDK Vertex AI per Python tramite esegui questo comando:

    pip3 install --upgrade "google-cloud-aiplatform>=1.38"
        

Node.js

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Installa o aggiorna l'SDK Vertex AI per Node.js mediante il comando seguente:

    npm install @google-cloud/vertexai
        

Java

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Per aggiungere google-cloud-vertexai come dipendenza, aggiungi il metodo il codice appropriato per il tuo ambiente:

    Maven con BOM

    Aggiungi il seguente codice HTML a pom.xml:

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.32.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vertexai</artifactId>
      </dependency>
    </dependencies>
                

    Maven senza BOM

    Aggiungi il seguente codice HTML a pom.xml:

    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-vertexai</artifactId>
      <version>0.4.0</version>
    </dependency>
              

    Gradle without BOM

    Add the following to your build.gradle

    implementation 'com.google.cloud:google-cloud-vertexai:0.4.0'

Go

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Esamina i pacchetti Go dell'API Vertex AI disponibili per determinare quale soddisfa al meglio le esigenze del tuo progetto:

    • Pacchetto cloud.google.com/go/vertexai (consigliato)

      vertexai è un pacchetto creato da persone fisiche che fornisce l'accesso alle funzionalità e alle caratteristiche comuni.

      Questo pacchetto è consigliato come punto di partenza per la maggior parte degli sviluppatori che creano soluzioni con l'API Vertex AI. Per accedere a funzionalità e non ancora coperte da questo pacchetto, usa lo strumento generato automaticamente aiplatform in alternativa.

    • Pacchetto cloud.google.com/go/aiplatform

      aiplatform è un pacchetto generato automaticamente.

      Questo pacchetto è destinato ai progetti che richiedono l'accesso alle funzionalità e alle funzionalità dell'API Vertex AI non ancora fornite dal pacchetto vertexai creato da persone.

  9. Installa il pacchetto Go desiderato in base alle esigenze del tuo progetto eseguendo uno dei seguenti comandi:

    # Human authored package. Recommended for most developers.
    go get cloud.google.com/go/vertexai
        
    # Auto-generated package. go get cloud.google.com/go/aiplatform

C#

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

REST

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. Configura le variabili di ambiente inserendo quanto segue. Sostituisci PROJECT_ID con l'ID del tuo progetto Google Cloud.
    MODEL_ID="gemini-1.5-flash-001"
    PROJECT_ID="PROJECT_ID"
        
  8. Esegui il provisioning dell'endpoint:
    gcloud beta services identity create --service=aiplatform.googleapis.com --project=${PROJECT_ID}
        
  9. (Facoltativo) Se utilizzi Cloud Shell e ti viene chiesto di autorizzarlo, fai clic su Autorizza.

Invia una richiesta di solo testo

Dopo aver configurato l'ambiente locale, invia una richiesta di solo testo all'API Gemini di Vertex AI. Il seguente esempio restituisce un elenco di nomi possibili per un negozio di fiori specializzato.

Python

Per inviare una richiesta di prompt, crea un file Python (.py) e copialo il seguente codice nel file. Imposta il valore di PROJECT_ID sull'ID del tuo progetto Google Cloud. Quindi, esegui il file Python dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.

import vertexai
from vertexai.generative_models import GenerativeModel

# TODO(developer): Update & uncomment line below
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-001")

response = model.generate_content(
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?"
)

print(response.text)

Node.js

Per inviare una richiesta di prompt, crea un file Node.js (.js) e copia il codice seguente nel file. Sostituisci PROJECT_ID con l'ID del tuo progetto Google Cloud. Successivamente, esegui il file JavaScript riga di comando o in un IDE oppure incorpora il codice nell'applicazione dove appropriato.

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function generate_from_text_input(projectId = 'PROJECT_ID') {
  const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});

  const generativeModel = vertexAI.getGenerativeModel({
    model: 'gemini-1.5-flash-001',
  });

  const prompt =
    "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";

  const resp = await generativeModel.generateContent(prompt);
  const contentResponse = await resp.response;
  console.log(JSON.stringify(contentResponse));
}

Java

Per inviare una richiesta di prompt, crea un file Java (.java) e copia il seguente codice nel file. Imposta your-google-cloud-project-id sul tuo ID progetto Google Cloud. Dopodiché, esegui il file Java dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;
import java.io.IOException;

public class TextInput {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";
    String textPrompt =
        "What's a good name for a flower shop that specializes in selling bouquets of"
            + " dried flowers?";

    String output = textInput(projectId, location, modelName, textPrompt);
    System.out.println(output);
  }

  // Passes the provided text input to the Gemini model and returns the text-only response.
  // For the specified textPrompt, the model returns a list of possible store names.
  public static String textInput(
      String projectId, String location, String modelName, String textPrompt) throws IOException {
    // Initialize client that will be used to send requests. This client only needs
    // to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      GenerativeModel model = new GenerativeModel(modelName, vertexAI);

      GenerateContentResponse response = model.generateContent(textPrompt);
      String output = ResponseHandler.getText(response);
      return output;
    }
  }
}

Go

Per inviare una richiesta di prompt, crea un file Go (.go) e copia nel file. Sostituisci projectID con l'ID del tuo progetto Google Cloud. Quindi, esegui il file Go dalla riga di comando o in un IDE oppure incorporare il codice nell'applicazione, ove appropriato.

import (
	"context"
	"encoding/json"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

func generateContentFromText(w io.Writer, projectID string) error {
	location := "us-central1"
	modelName := "gemini-1.5-flash-001"

	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("error creating client: %w", err)
	}
	gemini := client.GenerativeModel(modelName)
	prompt := genai.Text(
		"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?")

	resp, err := gemini.GenerateContent(ctx, prompt)
	if err != nil {
		return fmt.Errorf("error generating content: %w", err)
	}
	// See the JSON response in
	// https://pkg.go.dev/cloud.google.com/go/vertexai/genai#GenerateContentResponse.
	rb, err := json.MarshalIndent(resp, "", "  ")
	if err != nil {
		return fmt.Errorf("json.MarshalIndent: %w", err)
	}
	fmt.Fprintln(w, string(rb))
	return nil
}

C#

Per inviare una richiesta di prompt, crea un file C# (.cs) e copia nel file. Imposta your-project-id su ID progetto Google Cloud. Quindi, esegui il file C# dalla riga di comando o in un o incorpora il codice nell'applicazione, ove appropriato.


using Google.Cloud.AIPlatform.V1;
using System;
using System.Threading.Tasks;

public class TextInputSample
{
    public async Task<string> TextInput(
        string projectId = "your-project-id",
        string location = "us-central1",
        string publisher = "google",
        string model = "gemini-1.5-flash-001")
    {

        var predictionServiceClient = new PredictionServiceClientBuilder
        {
            Endpoint = $"{location}-aiplatform.googleapis.com"
        }.Build();
        string prompt = @"What's a good name for a flower shop that specializes in selling bouquets of dried flowers?";

        var generateContentRequest = new GenerateContentRequest
        {
            Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
            Contents =
            {
                new Content
                {
                    Role = "USER",
                    Parts =
                    {
                        new Part { Text = prompt }
                    }
                }
            }
        };

        GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

        string responseText = response.Candidates[0].Content.Parts[0].Text;
        Console.WriteLine(responseText);

        return responseText;
    }
}

REST

Puoi inviare questa richiesta di prompt da Cloud Shell, dalla riga di comando o in un IDE oppure puoi incorporare la chiamata REST nell'applicazione, se opportuno.

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:streamGenerateContent -d \
$'{
  "contents": {
    "role": "user",
    "parts": [
      {
        "text": "What\'s a good name for a flower shop that specializes in selling bouquets of dried flowers?"
      }
    ]
  }
}'

Il modello restituisce una risposta. Tieni presente che la risposta viene generata in sezioni con ciascuna sezione valutata separatamente per la sicurezza.

Invia una richiesta che include un'immagine

Dopo aver configurato il tuo ambiente locale, invia un'email che include testo e un'immagine all'API Gemini di Vertex AI. La nel seguente esempio viene restituita una descrizione dell'oggetto immagine fornita (immagine per esempio Java).

Python

Per inviare una richiesta di prompt, crea un file Python (.py) e copialo il seguente codice nel file. Imposta il valore di PROJECT_ID sull'ID del tuo progetto Google Cloud. Quindi, esegui il file Python dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.

import vertexai

from vertexai.generative_models import GenerativeModel, Part

# TODO(developer): Update & uncomment line below
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-001")

response = model.generate_content(
    [
        Part.from_uri(
            "gs://cloud-samples-data/generative-ai/image/scones.jpg",
            mime_type="image/jpeg",
        ),
        "What is shown in this image?",
    ]
)

print(response.text)

Node.js

Per inviare una richiesta di prompt, crea un file Node.js (.js) e copia il codice seguente nel file. Sostituisci PROJECT_ID con l'ID del tuo progetto Google Cloud. Successivamente, esegui il file JavaScript riga di comando o in un IDE oppure incorpora il codice nell'applicazione dove appropriato.

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function createNonStreamingMultipartContent(
  projectId = 'PROJECT_ID',
  location = 'us-central1',
  model = 'gemini-1.5-flash-001',
  image = 'gs://generativeai-downloads/images/scones.jpg',
  mimeType = 'image/jpeg'
) {
  // Initialize Vertex with your Cloud project and location
  const vertexAI = new VertexAI({project: projectId, location: location});

  // Instantiate the model
  const generativeVisionModel = vertexAI.getGenerativeModel({
    model: model,
  });

  // For images, the SDK supports both Google Cloud Storage URI and base64 strings
  const filePart = {
    fileData: {
      fileUri: image,
      mimeType: mimeType,
    },
  };

  const textPart = {
    text: 'what is shown in this image?',
  };

  const request = {
    contents: [{role: 'user', parts: [filePart, textPart]}],
  };

  console.log('Prompt Text:');
  console.log(request.contents[0].parts[1].text);

  console.log('Non-Streaming Response Text:');

  // Generate a response
  const response = await generativeVisionModel.generateContent(request);

  // Select the text from the response
  const fullTextResponse =
    response.response.candidates[0].content.parts[0].text;

  console.log(fullTextResponse);
}

Java

Per inviare una richiesta di prompt, crea un file Java (.java) e copia il seguente codice nel file. Imposta your-google-cloud-project-id sul tuo ID progetto Google Cloud. Dopodiché, esegui il file Java dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.PartMaker;
import java.io.IOException;

public class Quickstart {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    String output = quickstart(projectId, location, modelName);
    System.out.println(output);
  }

  // Analyzes the provided Multimodal input.
  public static String quickstart(String projectId, String location, String modelName)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs
    // to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String imageUri = "gs://generativeai-downloads/images/scones.jpg";

      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      GenerateContentResponse response = model.generateContent(ContentMaker.fromMultiModalData(
          PartMaker.fromMimeTypeAndData("image/png", imageUri),
          "What's in this photo"
      ));

      return response.toString();
    }
  }
}

Go

Per inviare una richiesta di prompt, crea un file Go (.go) e copia nel file. Sostituisci projectID con l'ID del tuo progetto Google Cloud. Quindi, esegui il file Go dalla riga di comando o in un IDE oppure incorporare il codice nell'applicazione, ove appropriato.

import (
	"context"
	"encoding/json"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

func tryGemini(w io.Writer, projectID string, location string, modelName string) error {
	// location := "us-central1"
	// modelName := "gemini-1.5-flash-001"

	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("error creating client: %w", err)
	}
	gemini := client.GenerativeModel(modelName)

	img := genai.FileData{
		MIMEType: "image/jpeg",
		FileURI:  "gs://generativeai-downloads/images/scones.jpg",
	}
	prompt := genai.Text("What is in this image?")

	resp, err := gemini.GenerateContent(ctx, img, prompt)
	if err != nil {
		return fmt.Errorf("error generating content: %w", err)
	}
	rb, err := json.MarshalIndent(resp, "", "  ")
	if err != nil {
		return fmt.Errorf("json.MarshalIndent: %w", err)
	}
	fmt.Fprintln(w, string(rb))
	return nil
}

C#

Per inviare una richiesta di prompt, crea un file C# (.cs) e copia nel file. Imposta your-project-id su ID progetto Google Cloud. Quindi, esegui il file C# dalla riga di comando o in un o incorpora il codice nell'applicazione, ove appropriato.


using Google.Api.Gax.Grpc;
using Google.Cloud.AIPlatform.V1;
using System.Text;
using System.Threading.Tasks;

public class GeminiQuickstart
{
    public async Task<string> GenerateContent(
        string projectId = "your-project-id",
        string location = "us-central1",
        string publisher = "google",
        string model = "gemini-1.5-flash-001"
    )
    {
        // Create client
        var predictionServiceClient = new PredictionServiceClientBuilder
        {
            Endpoint = $"{location}-aiplatform.googleapis.com"
        }.Build();

        // Initialize content request
        var generateContentRequest = new GenerateContentRequest
        {
            Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
            GenerationConfig = new GenerationConfig
            {
                Temperature = 0.4f,
                TopP = 1,
                TopK = 32,
                MaxOutputTokens = 2048
            },
            Contents =
            {
                new Content
                {
                    Role = "USER",
                    Parts =
                    {
                        new Part { Text = "What's in this photo?" },
                        new Part { FileData = new() { MimeType = "image/png", FileUri = "gs://generativeai-downloads/images/scones.jpg" } }
                    }
                }
            }
        };

        // Make the request, returning a streaming response
        using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);

        StringBuilder fullText = new();

        // Read streaming responses from server until complete
        AsyncResponseStream<GenerateContentResponse> responseStream = response.GetResponseStream();
        await foreach (GenerateContentResponse responseItem in responseStream)
        {
            fullText.Append(responseItem.Candidates[0].Content.Parts[0].Text);
        }

        return fullText.ToString();
    }
}

REST

Puoi inviare questa richiesta di prompt da Cloud Shell, dalla riga di comando o in un IDE oppure puoi incorporare la chiamata REST nell'applicazione, se opportuno.

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:streamGenerateContent -d \
$'{
  "contents": {
    "role": "user",
    "parts": [
      {
      "fileData": {
        "mimeType": "image/jpeg",
        "fileUri": "gs://generativeai-downloads/images/scones.jpg"
        }
      },
      {
        "text": "Describe this picture."
      }
    ]
  }
}'

Il modello restituisce una risposta. Tieni presente che la risposta viene generata in sezioni, ciascuna delle quali viene valutata separatamente per la sicurezza.

Invia una richiesta che includa audio e video

Dopo aver configurato il tuo ambiente locale, invia un'email che include testo, audio e video all'API Gemini di Vertex AI. La nel seguente esempio viene restituita una descrizione dell'oggetto video fornito, inclusi eventuali elementi importanti della traccia audio.

Python

Per inviare una richiesta di prompt, crea un file Python (.py) e copialo il seguente codice nel file. Imposta il valore di PROJECT_ID sull'ID del tuo progetto Google Cloud. Quindi, esegui il file Python dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.


import vertexai
from vertexai.generative_models import GenerativeModel, Part

# TODO(developer): Update & uncomment line below
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel("gemini-1.5-flash-001")

prompt = """
Provide a description of the video.
The description should also contain anything important which people say in the video.
"""

video_file = Part.from_uri(
    uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
    mime_type="video/mp4",
)

contents = [video_file, prompt]

response = model.generate_content(contents)
print(response.text)

Node.js

Per inviare una richiesta di prompt, crea un file Node.js (.js) e copia il codice seguente nel file. Sostituisci PROJECT_ID con l'ID del tuo progetto Google Cloud. Successivamente, esegui il file JavaScript riga di comando o in un IDE oppure incorpora il codice nell'applicazione dove appropriato.

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function analyze_video_with_audio(projectId = 'PROJECT_ID') {
  const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});

  const generativeModel = vertexAI.getGenerativeModel({
    model: 'gemini-1.5-flash-001',
  });

  const filePart = {
    file_data: {
      file_uri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
      mime_type: 'video/mp4',
    },
  };
  const textPart = {
    text: `
    Provide a description of the video.
    The description should also contain anything important which people say in the video.`,
  };

  const request = {
    contents: [{role: 'user', parts: [filePart, textPart]}],
  };

  const resp = await generativeModel.generateContent(request);
  const contentResponse = await resp.response;
  console.log(JSON.stringify(contentResponse));
}

Java

Per inviare una richiesta di prompt, crea un file Java (.java) e copia il seguente codice nel file. Imposta your-google-cloud-project-id sul tuo ID progetto Google Cloud. Dopodiché, esegui il file Java dalla riga di comando o in un IDE oppure incorpora il codice nell'applicazione, se opportuno.


import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.PartMaker;
import com.google.cloud.vertexai.generativeai.ResponseHandler;
import java.io.IOException;

public class VideoInputWithAudio {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    videoAudioInput(projectId, location, modelName);
  }

  // Analyzes the given video input, including its audio track.
  public static String videoAudioInput(String projectId, String location, String modelName)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs
    // to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String videoUri = "gs://cloud-samples-data/generative-ai/video/pixel8.mp4";

      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      GenerateContentResponse response = model.generateContent(
          ContentMaker.fromMultiModalData(
              "Provide a description of the video.\n The description should also "
                  + "contain anything important which people say in the video.",
              PartMaker.fromMimeTypeAndData("video/mp4", videoUri)
          ));

      String output = ResponseHandler.getText(response);
      System.out.println(output);

      return output;
    }
  }
}

Go

Per inviare una richiesta di prompt, crea un file Go (.go) e copia nel file. Sostituisci projectID con l'ID del tuo progetto Google Cloud. Quindi, esegui il file Go dalla riga di comando o in un IDE oppure incorporare il codice nell'applicazione, ove appropriato.

import (
	"context"
	"errors"
	"fmt"
	"io"
	"mime"
	"path/filepath"

	"cloud.google.com/go/vertexai/genai"
)

// generateMultimodalContent shows how to send video and text prompts to a model, writing the response to
// the provided io.Writer.
func generateMultimodalContent(w io.Writer, projectID, location, modelName string) error {
	// location := "us-central1"
	// modelName := "gemini-1.5-flash-001"
	ctx := context.Background()

	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("unable to create client: %w", err)
	}
	defer client.Close()

	model := client.GenerativeModel(modelName)

	// Given a video file URL, prepare video file as genai.Part
	part := genai.FileData{
		MIMEType: mime.TypeByExtension(filepath.Ext("pixel8.mp4")),
		FileURI:  "gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
	}

	res, err := model.GenerateContent(ctx, part, genai.Text(`
			Provide a description of the video.
			The description should also contain anything important which people say in the video.
	`))
	if err != nil {
		return fmt.Errorf("unable to generate contents: %w", err)
	}

	if len(res.Candidates) == 0 ||
		len(res.Candidates[0].Content.Parts) == 0 {
		return errors.New("empty response from model")
	}

	fmt.Fprintf(w, "generated response: %s\n", res.Candidates[0].Content.Parts[0])
	return nil
}

C#

Per inviare una richiesta di prompt, crea un file C# (.cs) e copia nel file. Imposta your-project-id su ID progetto Google Cloud. Quindi, esegui il file C# dalla riga di comando o in un o incorpora il codice nell'applicazione, ove appropriato.


using Google.Cloud.AIPlatform.V1;
using System;
using System.Threading.Tasks;

public class VideoInputWithAudio
{
    public async Task<string> DescribeVideo(
        string projectId = "your-project-id",
        string location = "us-central1",
        string publisher = "google",
        string model = "gemini-1.5-flash-001")
    {

        var predictionServiceClient = new PredictionServiceClientBuilder
        {
            Endpoint = $"{location}-aiplatform.googleapis.com"
        }.Build();

        string prompt = @"Provide a description of the video.
The description should also contain anything important which people say in the video.";

        var generateContentRequest = new GenerateContentRequest
        {
            Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
            Contents =
            {
                new Content
                {
                    Role = "USER",
                    Parts =
                    {
                        new Part { Text = prompt },
                        new Part { FileData = new() { MimeType = "video/mp4", FileUri = "gs://cloud-samples-data/generative-ai/video/pixel8.mp4" }}
                    }
                }
            }
        };

        GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

        string responseText = response.Candidates[0].Content.Parts[0].Text;
        Console.WriteLine(responseText);

        return responseText;
    }
}

REST

Puoi inviare questa richiesta di prompt da Cloud Shell, dalla riga di comando o in un IDE oppure puoi incorporare la chiamata REST nell'applicazione, se opportuno.

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:streamGenerateContent -d \
$'{
  "contents": {
    "role": "user",
    "parts": [
      {
      "fileData": {
        "mimeType": "video/mp4",
        "fileUri": "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"
        }
      },
      {
        "text": "Provide a description of the video. The description should also contain anything important which people say in the video."
      }
    ]
  }
}'

Il modello restituisce una risposta. Tieni presente che la risposta viene generata sezioni, in cui ogni sezione viene valutata separatamente per verificarne la sicurezza.

Passaggi successivi