Spécifier la dimension d'embedding pour l'entrée multimodale
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cet exemple de code montre comment spécifier une dimension d'embedding inférieure pour les entrées de texte et d'image.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les pages suivantes :
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,["# Specify Embedding dimension for multimodal input\n\nThis code sample shows how to specify a lower embedding dimension for text and image inputs.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Get multimodal embeddings](/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go 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 Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).\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 import (\n \t\"context\"\n \t\"encoding/json\"\n \t\"fmt\"\n \t\"io\"\n\n \taiplatform \"cloud.google.com/go/aiplatform/apiv1beta1\"\n \taiplatformpb \"cloud.google.com/go/aiplatform/apiv1beta1/aiplatformpb\"\n \t\"google.golang.org/api/option\"\n \t\"google.golang.org/protobuf/encoding/protojson\"\n \t\"google.golang.org/protobuf/types/known/structpb\"\n )\n\n // generateWithLowerDimension shows how to generate lower-dimensional embeddings for text and image inputs.\n func generateWithLowerDimension(w io.Writer, project, location string) error {\n \t// location = \"us-central1\"\n \tctx := context.Background()\n \tapiEndpoint := fmt.Sprintf(\"%s-aiplatform.googleapis.com:443\", location)\n \tclient, err := aiplatform.https://cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1beta1.html#cloud_google_com_go_aiplatform_apiv1beta1_PredictionClient_NewPredictionClient(ctx, option.WithEndpoint(apiEndpoint))\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to construct API client: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tmodel := \"multimodalembedding@001\"\n \tendpoint := fmt.Sprintf(\"projects/%s/locations/%s/publishers/google/models/%s\", project, location, model)\n\n \t// This is the input to the model's prediction call. For schema, see:\n \t// https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/multimodal-embeddings-api#request_body\n \tinstance, err := structpb.NewValue(map[string]any{\n \t\t\"image\": map[string]any{\n \t\t\t// Image input can be provided either as a Google Cloud Storage URI or as\n \t\t\t// base64-encoded bytes using the \"bytesBase64Encoded\" field.\n \t\t\t\"gcsUri\": \"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png\",\n \t\t},\n \t\t\"text\": \"Colosseum\",\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to construct request payload: %w\", err)\n \t}\n\n \t// TODO(developer): Try different dimenions: 128, 256, 512, 1408\n \toutputDimensionality := 128\n \tparams, err := structpb.NewValue(map[string]any{\n \t\t\"dimension\": outputDimensionality,\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to construct request params: %w\", err)\n \t}\n\n \treq := &aiplatformpb.PredictRequest{\n \t\tEndpoint: endpoint,\n \t\t// The model supports only 1 instance per request.\n \t\tInstances: []*structpb.Value{instance},\n \t\tParameters: params,\n \t}\n\n \tresp, err := client.Predict(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate embeddings: %w\", err)\n \t}\n\n \tinstanceEmbeddingsJson, err := protojson.Marshal(resp.GetPredictions()[0])\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to convert protobuf value to JSON: %w\", err)\n \t}\n \t// For response schema, see:\n \t// https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/multimodal-embeddings-api#response-body\n \tvar instanceEmbeddings struct {\n \t\tImageEmbeddings []float32 `json:\"imageEmbedding\"`\n \t\tTextEmbeddings []float32 `json:\"textEmbedding\"`\n \t}\n \tif err := json.Unmarshal(instanceEmbeddingsJson, &instanceEmbeddings); err != nil {\n \t\treturn fmt.Errorf(\"failed to unmarshal JSON: %w\", err)\n \t}\n\n \timageEmbedding := instanceEmbeddings.ImageEmbeddings\n \ttextEmbedding := instanceEmbeddings.TextEmbeddings\n\n \tfmt.Fprintf(w, \"Text embedding (length=%d): %v\\n\", len(textEmbedding), textEmbedding)\n \tfmt.Fprintf(w, \"Image embedding (length=%d): %v\\n\", len(imageEmbedding), imageEmbedding)\n \t// Example response:\n \t// Text Embedding (length=128): [0.27469793 -0.14625867 0.022280363 ... ]\n \t// Image Embedding (length=128): [0.06225733 -0.040650766 0.02604402 ... ]\n\n \treturn nil\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 import https://cloud.google.com/python/docs/reference/vertexai/latest/\n\n from vertexai.vision_models import https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.generative_models.Image.html, https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.vision_models.MultiModalEmbeddingModel.html\n\n # TODO(developer): Update & uncomment line below\n # PROJECT_ID = \"your-project-id\"\n https://cloud.google.com/python/docs/reference/vertexai/latest/.init(project=PROJECT_ID, location=\"us-central1\")\n\n # TODO(developer): Try different dimenions: 128, 256, 512, 1408\n embedding_dimension = 128\n\n model = https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.vision_models.MultiModalEmbeddingModel.html.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.vision_models.MultiModalEmbeddingModel.html#vertexai_preview_vision_models_MultiModalEmbeddingModel_from_pretrained(\"multimodalembedding@001\")\n image = https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.generative_models.Image.html.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.generative_models.Image.html#vertexai_preview_generative_models_Image_load_from_file(\n \"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png\"\n )\n\n embeddings = model.https://cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.vision_models.MultiModalEmbeddingModel.html#vertexai_preview_vision_models_MultiModalEmbeddingModel_get_embeddings(\n image=image,\n contextual_text=\"Colosseum\",\n dimension=embedding_dimension,\n )\n\n print(f\"Image Embedding: {embeddings.image_embedding}\")\n print(f\"Text Embedding: {embeddings.text_embedding}\")\n\n # Example response:\n # Image Embedding: [0.0622573346, -0.0406507477, 0.0260440577, ...]\n # Text Embedding: [0.27469793, -0.146258667, 0.0222803634, ...]\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)."]]