googleapis / python-aiplatform

A Python SDK for Vertex AI, a fully managed, end-to-end platform for data science and machine learning.
Apache License 2.0
629 stars 341 forks source link

count_token for embedding model is failing for 'rest' transport with a 400 BadRequest error. #4513

Open vi3k6i5 opened 5 days ago

vi3k6i5 commented 5 days ago

Thanks for stopping by to let us know something could be better!

Environment details

Steps to reproduce

  1. Initialise the env over "Rest" transport.
  2. Get pretrained embedding model
  3. call count_token mothod on the model object.

Code example


vertexai.init(
    project='project',
    api_endpoint='<api_endpoint>',
    transport='rest',
    credentials=get_credentials(),
)
embedding_model = TextEmbeddingModel.from_pretrained(model_name='text-embedding-004')
resp = embedding_model.count_token(['Hello world'])

Stack trace

...
raise core_exception.from_http_response(response)
google.api_core.exception.BadRequest: 400 POST https://<api_endpoint>/v1beta1/projects/<project>/locations/us-central1/publishers/google/models/test-embedding-004@default:countTokens

Thanks!

jaycee-li commented 5 days ago

Hi @vi3k6i5 , REST transport is working on my side (see the following code). Maybe it's an issue with your api endpoint?

import vertexai
from vertexai.language_models import TextEmbeddingModel

vertexai.init(
   project='my-project',
   api_transport='rest',
)
embedding_model = TextEmbeddingModel.from_pretrained(model_name='text-embedding-004')
resp = embedding_model.count_tokens(['Hello world'])