This code sample demonstrates how to fine-tune an embedding model using Vertex AI. The sample uses a pre-trained model and tunes it on a specific dataset.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Tune text embeddings](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/tuning/embeddings)

## Code sample

### Python


Before trying this sample, follow the Python setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/aiplatform/latest).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import re

    from google.cloud.aiplatform import initializer as aiplatform_init
    from vertexai.language_models import TextEmbeddingModel


    def tune_embedding_model(
        api_endpoint: str,
        base_model_name: str = "text-embedding-005",
        corpus_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/corpus.jsonl",
        queries_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/queries.jsonl",
        train_label_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/train.tsv",
        test_label_path: str = "gs://cloud-samples-data/ai-platform/embedding/goog-10k-2024/r11/test.tsv",
    ):  # noqa: ANN201
        """Tune an embedding model using the specified parameters.
        Args:
            api_endpoint (str): The API endpoint for the Vertex AI service.
            base_model_name (str): The name of the base model to use for tuning.
            corpus_path (str): GCS URI of the JSONL file containing the corpus data.
            queries_path (str): GCS URI of the JSONL file containing the queries data.
            train_label_path (str): GCS URI of the TSV file containing the training labels.
            test_label_path (str): GCS URI of the TSV file containing the test labels.
        """
        match = re.search(r"^(\w+-\w+)", api_endpoint)
        location = match.group(1) if match else "us-central1"
        base_model = TextEmbeddingModel.from_pretrained(base_model_name)
        tuning_job = base_model.https://docs.cloud.google.com/python/docs/reference/agentplatform/latest/vertexai.language_models._language_models._TunableModelMixin.html#vertexai_language_models__language_models__TunableModelMixin_tune_model(
            task_type="DEFAULT",
            corpus_data=corpus_path,
            queries_data=queries_path,
            training_data=train_label_path,
            test_data=test_label_path,
            batch_size=128,  # The batch size to use for training.
            train_steps=1000,  # The number of training steps.
            tuned_model_location=location,
            output_dimensionality=768,  # The dimensionality of the output embeddings.
            learning_rate_multiplier=1.0,  # The multiplier for the learning rate.
        )
        return tuning_job

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).