Genera incorporaciones para entradas multimodales
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En este muestra de código, se muestra cómo usar el modelo multimodal para generar embeddings para entradas de texto y de imágenes.
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["# Generate embeddings for multimodal input\n\nThis code sample shows how to use the multimodal model to generate embeddings 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- [Multimodal embeddings API](/vertex-ai/generative-ai/docs/model-reference/multimodal-embeddings-api)\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 // generateForTextAndImage shows how to use the multimodal model to generate embeddings for\n // text and image inputs.\n func generateForTextAndImage(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 \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}\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=1408): [0.0023026613 0.027898183 -0.011858357 ... ]\n \t// Image embedding (length=1408): [-0.012314269 0.07271844 0.00020170923 ... ]\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 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 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=1408,\n )\n print(f\"Image Embedding: {embeddings.image_embedding}\")\n print(f\"Text Embedding: {embeddings.text_embedding}\")\n # Example response:\n # Image Embedding: [-0.0123147098, 0.0727171078, ...]\n # Text Embedding: [0.00230263756, 0.0278981831, ...]\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)."]]