Google Embedding Models
Note that Google has to AI API products:
- Google Cloud AI, also known as Vertex AI.
- Gemini API, also known as Generative Language API.
The NodeJS SDK for Gemini API is significantly nicer, so the example below will use it. However, Vertex AI has more embedding models - multi-lingual, multi-modal, images and video.
Availble models
The example below is based on the new text-embedding-preview-0409
model, also called text-embedding-0004
in Gemini APIs.
It is the best text embedding model available from Google and ranks well in the MTEB benchmark.
Model | Dimensions | Max Tokens | Cost | MTEB Avg Score | Similarity Metric |
---|---|---|---|---|---|
text-embedding-preview-0409 / text-embedding-0004 | 768 (scales down) | 2048 | $0.025/1M tokens in Vertex, free in Gemini | 66.31 | cosine, L2 |
Usage
To use Google’s embedding models, you need a Google Cloud project. The example below uses Gemini, so you will need to have Gemini Generative Language APIs enabled. and you will also need an API key with permissions to access the Generative Language API. You can get one by going to APIs & Services -> Credentials in your Google Cloud Console. (You can also use Google’s AI Studio to get an API key).
Vertex has separate API to enable, separate key permissions, separate pricing and a different SDK (which we don’t document here).
Installing dependencies
Generating embeddings with Google
Storing and retrieving the embeddings
Additional notes
Scale down
Google’s text-embedding-0004
model has 768 dimensions, but you can scale it down to lower dimensions.
The older model, text-embedding-0001
does not support scaling down.
Task types
Google’s documentation about taskTypes is a bit confusing. Some documents say that taskType
is only supported
by text-embedding-0001
model, and other say that it works with 0004
as well. My experiments showed that
taskType
works with 0004
, so I have included it in the example above. I assume there are typos in the docs.
Distance metrics
Google documentation doesn’t mention the distance metric used for similarity search and doesn’t mention anything about normalization either. However, the MTEB benchmark records for Google’s model show use of Cosine and L2 distance.