pythongoogle-cloud-platformlarge-language-modelgoogle-cloud-vertex-airag

Vertex AI Text Embedding model forbidden


I am trying to run the tutorial here about text embedding on GCP : https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-text-embeddings?hl=fr

However, the code snippet below does not work (the error message indicates a disabled service while Vertex AI API is actually enabled on my GCP project):

from vertexai.language_models import TextEmbeddingModel
import vertexai

vertexai.init(project="MY_PROJET_ID", location="europe-west1")

texts = ["banana muffins? ", "banana bread? banana muffins?"]
dimensionality = 256
task = "RETRIEVAL_DOCUMENT"


model = TextEmbeddingModel.from_pretrained("text-multilingual-embedding-002")

Version: google-cloud-aiplatform = "1.74.0"

on Python 3.11 enter image description here

Error output (=> DISABLED_SERVICE):

PermissionDenied: 403 Your application is authenticating by using local Application Default Credentials. The aiplatform.googleapis.com API requires a quota project, which is not set by default. To learn how to set your quota project, see https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds . [reason: "SERVICE_DISABLED"
domain: "googleapis.com"
metadata {
  key: "service"
  value: "aiplatform.googleapis.com"
}
metadata {
  key: "consumer"
  value: "projects/764086051850"
}

Still, I can use other VertexAI services such as GenerativeModels.

I suspect that a wrong project 764086051850 is used instead of mine. I don't see where I can change that. Does anyone kown how to solve this issue ?


Solution

  • Nevermind, I've solved it:

    from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel
    import vertexai
    import google.auth
    
    creds, _ = google.auth.default(quota_project_id="MY_PROJECT")
    vertexai.init(project="MY_PROJECT", location="europe-west1", credentials=creds)
    
    texts = ["banana muffins? ", "banana bread? banana muffins?"]
    dimensionality = 256
    task = "RETRIEVAL_DOCUMENT"
    
    
    model = TextEmbeddingModel.from_pretrained("text-multilingual-embedding-002")
    inputs = [TextEmbeddingInput(text, task) for text in texts]
    kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {}
    embeddings = model.get_embeddings(inputs, **kwargs)
    

    AND had to get Service Usage Consumer role.