A partire dal 29 aprile 2025, i modelli Gemini 1.5 Pro e Gemini 1.5 Flash non sono disponibili nei progetti che non li hanno mai utilizzati, inclusi i nuovi progetti. Per maggiori dettagli, vedi Versioni e ciclo di vita dei modelli.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
L'ottimizzatore zero-shot ti consente di perfezionare e migliorare automaticamente
i prompt scritti dagli utenti. Spesso, un prompt potrebbe non produrre la risposta del modello che desideri a causa di un linguaggio ambiguo, della mancanza di contesto o dell'inclusione di informazioni irrilevanti. Questo ottimizzatore analizza e riscrive un prompt esistente per renderlo
più chiaro, efficace e in linea con le funzionalità del modello,
portando in definitiva a risposte di qualità superiore.
L'ottimizzatore zero-shot è particolarmente utile per:
Adattamento agli aggiornamenti del modello: quando esegui l'upgrade a una versione più recente di un modello, i prompt esistenti potrebbero non funzionare più in modo ottimale.
Miglioramento della comprensione dei prompt: quando la formulazione di un prompt è complessa
o potrebbe essere interpretata in modo errato, lo strumento può riformularla per ottenere la massima chiarezza e
precisione, riducendo la possibilità di un risultato indesiderato.
Esistono due modi per utilizzare l'ottimizzatore:
Generazione di istruzioni: anziché scrivere istruzioni di sistema complesse
da zero, puoi descrivere il tuo obiettivo o la tua attività in un linguaggio semplice. L'ottimizzatore genererà quindi un insieme completo e ben strutturato di istruzioni di sistema progettate per raggiungere il tuo obiettivo.
Raffinamento del prompt: hai un prompt funzionante, ma l'output del modello è
incoerente, leggermente fuori tema o non ha i dettagli che desideri. L'ottimizzatore
può aiutarti a migliorare il prompt per un output migliore.
L'ottimizzatore supporta l'ottimizzazione dei prompt in tutte le lingue supportate da
Gemini ed è disponibile tramite l'SDK
Vertex AI.
# Import librariesimportvertexaiimportlogging# Google Colab authenticationfromgoogle.colabimportauthPROJECT_NAME="PROJECT"auth.authenticate_user(project_id=PROJECT_NAME)# Initialize the Vertex AI clientclient=vertexai.Client(project=PROJECT_NAME,location='us-central1')# Input original prompt to optimizeprompt="""You are a professional chef. Your goal is teaching how to cook healthy cooking recipes to your apprentice.Given a question from your apprentice and some context, provide the correct answer to the question.Use the context to return a single and correct answer with some explanation."""# Optimize promptoutput=client.prompt_optimizer.optimize_prompt(prompt=prompt)# View optimized promptprint(output.model_dump_json(indent=2))
Questo oggetto output è di tipo OptimizeResponse e fornisce informazioni
sul processo di ottimizzazione. La parte più importante è il
suggested_prompt, che contiene il prompt ottimizzato che puoi utilizzare per ottenere
risultati migliori dal tuo modello. Gli altri campi, in particolare
applicable_guidelines, sono utili per capire perché e come il tuo prompt
è stato migliorato, il che può aiutarti a scrivere prompt migliori in futuro. Ecco un
esempio dell'output:
{"optimization_mode":"zero_shot","applicable_guidelines":[{"applicable_guideline":"Structure","suggested_improvement":"Add role definition.","text_before_change":"...","text_after_change":"Role: You are an AI assistant...\n\nTask Context:\n..."},{"applicable_guideline":"RedundancyInstructions","suggested_improvement":"Remove redundant explanation.","text_before_change":"...","text_after_change":""}],"original_prompt":"...","suggested_prompt":"Role: You are an AI assistant...\n\nTask Context:\n..."}
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[],[],null,["# Zero-shot optimizer\n\nThe **zero-shot optimizer** lets you automatically refine and improve\nuser-written prompts. Often, a prompt may not produce the model response you\nwant due to ambiguous language, missing context, or the inclusion of irrelevant\ninformation. This optimizer analyzes and rewrites an existing prompt to be\nclearer, more effective, and better aligned with the model's capabilities,\nultimately leading to higher-quality responses.\n\nThe zero-shot optimizer is particularly useful for:\n\n- **Adapting to Model Updates:** When you upgrade to a newer version of a\n model, your existing prompts might no longer perform optimally.\n\n- **Enhancing Prompt Comprehension:** When the phrasing of a prompt is complex\n or could be misinterpreted, the tool can rephrase it for maximum clarity and\n precision, reducing the chance of an undesirable outcome.\n\nThere are two ways to use the optimizer:\n\n- **Instruction Generation**: Instead of writing complex system instructions\n from scratch, you can describe your goal or task in plain language. The\n optimizer will then generate a complete and well-structured set of system\n instructions designed to achieve your objective.\n\n- **Prompt Refinement**: You have a working prompt, but the model's output is\n inconsistent, slightly off-topic, or lacks the detail you want. The\n optimizer can help improve the prompt for a better output.\n\nThe optimizer supports prompt optimization in all languages supported by\nGemini and is available through the [Vertex AI\nSDK](/vertex-ai/generative-ai/docs/reference/libraries)\n\nBefore you begin\n----------------\n\n\nTo ensure that the [Compute Engine default service account](/iam/docs/service-account-types#default) has the necessary\npermissions to optimize prompts,\n\nask your administrator to grant the [Compute Engine default service account](/iam/docs/service-account-types#default) the\nfollowing IAM roles on the project:\n\n| **Important:** You must grant these roles to the [Compute Engine default service account](/iam/docs/service-account-types#default), *not* to your user account. Failure to grant the roles to the correct principal might result in permission errors.\n\n- [Vertex AI User](/iam/docs/roles-permissions/aiplatform#aiplatform.user) (`roles/aiplatform.user`)\n- [Vertex AI Service Agent](/iam/docs/roles-permissions/aiplatform#aiplatform.serviceAgent) (`roles/aiplatform.serviceAgent`)\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYour administrator might also be able to give the [Compute Engine default service account](/iam/docs/service-account-types#default)\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nOptimize a prompt\n-----------------\n\n # Import libraries\n import https://cloud.google.com/python/docs/reference/vertexai/latest/\n import logging\n\n # Google Colab authentication\n from google.colab import auth\n PROJECT_NAME = \"PROJECT\"\n auth.authenticate_user(project_id=PROJECT_NAME)\n\n # Initialize the Vertex AI client\n client = https://cloud.google.com/python/docs/reference/vertexai/latest/.Client(project=PROJECT_NAME, location='us-central1')\n\n # Input original prompt to optimize\n prompt = \"\"\"You are a professional chef. Your goal is teaching how to cook healthy cooking recipes to your apprentice.\n\n Given a question from your apprentice and some context, provide the correct answer to the question.\n Use the context to return a single and correct answer with some explanation.\n \"\"\"\n\n # Optimize prompt\n output = client.prompt_optimizer.optimize_prompt(prompt=prompt)\n\n # View optimized prompt\n print(output.model_dump_json(indent=2))\n\nThis `output` object is of type `OptimizeResponse` and provides information\nabout the optimization process. The most important part is the\n`suggested_prompt` which contains the optimized prompt that you can use to get\nbetter results from your model. The other fields, especially\n`applicable_guidelines`, are useful for understanding why and how your prompt\nwas improved, which can help you write better prompts in the future. Here's an\nexample of the output: \n\n {\n \"optimization_mode\": \"zero_shot\",\n \"applicable_guidelines\": [\n {\n \"applicable_guideline\": \"Structure\",\n \"suggested_improvement\": \"Add role definition.\",\n \"text_before_change\": \"...\",\n \"text_after_change\": \"Role: You are an AI assistant...\\n\\nTask Context:\\n...\"\n },\n {\n \"applicable_guideline\": \"RedundancyInstructions\",\n \"suggested_improvement\": \"Remove redundant explanation.\",\n \"text_before_change\": \"...\",\n \"text_after_change\": \"\"\n }\n ],\n \"original_prompt\": \"...\",\n \"suggested_prompt\": \"Role: You are an AI assistant...\\n\\nTask Context:\\n...\"\n }"]]