Gemini Enterprise Agent Platform in modalità express ti consente di provare le funzionalità di AI generativa di base disponibili su Agent Platform. Questo tutorial mostra come completare le seguenti attività utilizzando l'API Agent Platform in modalità express:
- Installa e inizializza l'SDK Google Gen AI per la modalità express.
Invia una richiesta all'API Gemini for Google Cloud, inclusi i seguenti elementi:
- Richiesta di streaming
- Richiesta non di streaming
- Richiesta di chiamata di funzione
Prima di iniziare
Prima di eseguire le attività descritte in questo documento, registrati alla modalità express.
Installa e inizializza l'SDK per la modalità Express
L'SDK Google Gen AI ti consente di utilizzare i modelli e le funzionalità di AI generativa di Google per creare applicazioni basate sull'AI. Quando utilizzi Agent Platform in modalità express, installa e inizializza il pacchetto google-genai per l'autenticazione utilizzando la chiave API generata.
Installa
Per installare l'SDK Google Gen AI per la modalità express, esegui i seguenti comandi:
# Developer TODO: If you're using Colab, uncomment the following lines:
# from google.colab import auth
# auth.authenticate_user()
!pip install --upgrade google-genai
Se utilizzi Colaboratory, riavvia il runtime dopo l'installazione, se richiesto.
Inizializzazione
Configura la chiave API per la modalità express e inizializza il client con
enterprise=True. Per informazioni dettagliate su come ottenere una chiave API, consulta
Panoramica di Agent Platform in modalità express.
from google import genai
from google.genai import types
# Developer TODO: Replace YOUR_API_KEY with your API key.
API_KEY = "YOUR_API_KEY"
client = genai.Client(
enterprise=True, api_key=API_KEY
)
Invia una richiesta all'API Gemini for Google Cloud
Puoi inviare richieste di streaming o non di streaming all'API Gemini for Google Cloud. Le richieste di streaming restituiscono la risposta in blocchi man mano che la richiesta viene elaborata. Le richieste non di streaming restituiscono la risposta completa al termine dell'elaborazione.
Richiesta di streaming
Per inviare una richiesta di streaming, chiama client.models.generate_content_stream() e
stampa ogni blocco man mano che arriva:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
for chunk in client.models.generate_content_stream(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
):
print(chunk.text, end="")
generate()
Richiesta non di streaming
Il seguente esempio di codice definisce una funzione che invia una richiesta non in streaming
a Gemini 3.8 Flash (gemini-3.8-flash). Mostra come configurare il livello di pensiero, i parametri di output e le impostazioni di sicurezza:
from google import genai
from google.genai import types
def generate():
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="MEDIUM",
),
seed=5,
max_output_tokens=1024,
stop_sequences=["STOP!"],
safety_settings=[
types.SafetySetting(
category="HARM_CATEGORY_HATE_SPEECH",
threshold="BLOCK_ONLY_HIGH",
)
],
)
response = client.models.generate_content(
model="gemini-3.8-flash",
contents="Explain bubble sort to me",
config=config,
)
print(response.text)
generate()
Richiesta di chiamata di funzione
Il seguente esempio di codice dichiara uno strumento di funzione, invia un prompt iniziale a
3.8 Flash, riceve una parte di chiamata di funzione nella risposta
e poi invia il FunctionResponse corrispondente al modello per generare una
risposta finale:
from google import genai
from google.genai import types
client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")
get_weather_declaration = types.FunctionDeclaration(
name="get_current_weather",
description="Gets the current weather in a given city.",
parameters={
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "The city and state, such as Boston, MA.",
},
"unit": {
"type": "STRING",
"enum": ["C", "F"],
},
},
"required": ["location"],
},
)
tools = [types.Tool(function_declarations=[get_weather_declaration])]
prompt = "What is the weather in Boston?"
first_response = client.models.generate_content(
model="gemini-3.8-flash",
contents=prompt,
config=types.GenerateContentConfig(tools=tools),
)
function_call = first_response.function_calls[0]
contents = [
types.Content(role="user", parts=[types.Part(text=prompt)]),
first_response.candidates[0].content,
types.Content(
role="user",
parts=[
types.Part(
function_response=types.FunctionResponse(
id=function_call.id,
name=function_call.name,
response={"weather": "sunny", "temperature": "72F"},
)
)
],
),
]
response = client.models.generate_content(
model="gemini-3.8-flash",
contents=contents,
config=types.GenerateContentConfig(tools=tools),
)
print(response.text)
Esegui la pulizia
Questo tutorial non crea risorse Google Cloud , quindi non è necessaria alcuna pulizia per evitare addebiti.
Passaggi successivi
- Prova il tutorial di Agent Studio per Agent Platform in modalità express.
- Consulta il Riferimento API completo per Agent Platform in modalità express.