Dimension für die Einbettung von multimodaler Eingabe angeben
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Codebeispiel wird gezeigt, wie Sie eine niedrigere Einbettungsdimension für Text- und Bildeingaben angeben.
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","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)."]]