Optimiser les requêtes pour la génération de texte avec Vertex AI
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cet exemple montre comment utiliser l'optimiseur de prompts Vertex AI pour optimiser les prompts d'un modèle de génération de texte.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez la page suivante :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["# Optimize prompts for text generation with Vertex AI\n\nThis sample demonstrates how to use the Vertex AI Prompt Optimizer to optimize prompts for a text generation model.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Optimize prompts](/vertex-ai/generative-ai/docs/learn/prompts/prompt-optimizer)\n\nCode sample\n-----------\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Python API\nreference documentation](/python/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import aiplatform\n\n # Initialize Vertex AI platform\n aiplatform.https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.html(project=PROJECT_ID, location=\"us-central1\")\n\n # TODO(Developer): Check and update lines below\n # cloud_bucket = \"gs://cloud-samples-data\"\n # config_path = f\"{cloud_bucket}/instructions/sample_configuration.json\"\n # output_path = \"custom_job/output/\"\n\n custom_job = aiplatform.https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.types.CustomJob.html(\n display_name=\"Prompt Optimizer example\",\n worker_pool_specs=[\n {\n \"replica_count\": 1,\n \"container_spec\": {\n \"image_uri\": \"us-docker.pkg.dev/vertex-ai-restricted/builtin-algorithm/apd:preview_v1_0\",\n \"args\": [f\"--config={cloud_bucket}/{config_path}\"],\n },\n \"machine_spec\": {\n \"machine_type\": \"n1-standard-4\",\n },\n }\n ],\n staging_bucket=cloud_bucket,\n base_output_dir=f\"{cloud_bucket}/{output_path}\",\n )\n\n custom_job.submit()\n print(f\"Job resource name: {custom_job.resource_name}\")\n # Example response:\n # 'projects/123412341234/locations/us-central1/customJobs/12341234123412341234'\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=generativeaionvertexai)."]]