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 gratuitamentePer 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
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
-
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.
Installa o aggiorna l'SDK Vertex AI per Python tramite esegui questo comando:
pip3 install --upgrade "google-cloud-aiplatform>=1.38"
Node.js
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
-
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.
Installa o aggiorna l'SDK Vertex AI per Node.js mediante il comando seguente:
npm install @google-cloud/vertexai
Java
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
-
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.
-
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
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
-
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.
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.
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#
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
-
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
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the Vertex AI and Cloud Storage APIs.
-
In the Google Cloud console, 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.
- 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"
- Esegui il provisioning dell'endpoint:
gcloud beta services identity create --service=aiplatform.googleapis.com --project=${PROJECT_ID}
-
(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.
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.
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.
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.
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.
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?" } ] } }'
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
- Scopri di più sull'API Gemini in Vertex AI.
- Esplora il riferimento dell'SDK dell'API Vertex AI Gemini per Python, Node.js Java, Vai oppure C#.
- Consulta l'API Model for Gemini in Vertex AI.
- Scopri come chiamare i modelli Vertex AI utilizzando la libreria OpenAI.