Skip to navigation

EmbeddingGemma 2

View as Markdown

EmbeddingGemma 2 is an open-weight embedding model. It turns text into a 768-dimension vector with length 1, so the dot product of two vectors is their cosine similarity. Use it for semantic search, retrieval for RAG, clustering, classification, and deduplication.

It is served on POST /v2/embeddings, which follows the OpenAI embeddings API, so the OpenAI SDKs work with only the base URL changed.

EmbeddingGemma 2 is available on Provisioned Throughput: capacity is set up for your workload and enabled for your organisation. Without access, requests return 404 not_found_error and the model isn’t listed in GET /v2/models. Contact us to request access. See Provisioned Throughput.

At a glance

Model IDembeddinggemma-2
APIPOST https://api.sarvam.ai/v2/embeddings (OpenAI-compatible)
InputText: a string, or a list of strings in one request
OutputOne 768-dimension, unit-length float vector per input, in input order
Max input8,192 tokens per string, checked against an estimate (in practice, about 6,500 English words). Details
Encodingsfloat (default) or base64
Pricing₹2 per 1M input tokens; no output tokens are billed (Pricing)

Quickstart

Embed a query and a document, then compare them:

from openai import OpenAI
client = OpenAI(
api_key="YOUR_SARVAM_API_KEY",
base_url="https://api.sarvam.ai/v2",
)
response = client.embeddings.create(
model="embeddinggemma-2",
input=[
"task: search result | query: how do I reset my password",
"title: none | text: To reset your password, open Settings > Security.",
],
)
query, document = (item.embedding for item in response.data)
similarity = sum(q * d for q, d in zip(query, document))
print(f"cosine similarity: {similarity:.3f}")

Response (vectors shortened to their first values):

{
"object": "list",
"model": "embeddinggemma-2",
"data": [
{"object": "embedding", "index": 0, "embedding": [0.0061, 0.0338, 0.0432, ...]},
{"object": "embedding", "index": 1, "embedding": [0.0139, 0.0393, 0.0321, ...]}
],
"usage": {"prompt_tokens": 33, "total_tokens": 33, "completion_tokens": 0}
}

data[i].index matches the position of the string in input.

Task prefixes

EmbeddingGemma 2 is trained with a prefix that says what each text is for. The API does not add it, so put it at the start of every string. Without one, retrieval quality drops.

UsePrefix
Search or retrieval querytask: search result | query:
Document or passage being indexedtitle: none | text: (or title: {title} | text: )
Question answeringtask: question answering | query:
Fact checkingtask: fact checking | query:
Classificationtask: classification | query:
Clusteringtask: clustering | query:
Semantic similaritytask: sentence similarity | query:
Code retrievaltask: code retrieval | query:

For search, embed queries with the query prefix and documents with the document prefix.

Smaller vectors

dimensions is not supported and returns 400. To store 512, 256, or 128 values per vector, keep the first N values of the 768-dimension vector and scale it back to length 1:

import math
def shorten(vector, n):
head = vector[:n]
norm = math.sqrt(sum(x * x for x in head))
return [x / norm for x in head]

Parameters

ParameterRequiredDescription
modelYesembeddinggemma-2.
inputYesA string, or a list of strings. Each string can be up to 8,192 tokens. An empty list returns 400.
encoding_formatNofloat (default) or base64, a base64 string of 768 little-endian float32 values.
dimensionsNoNot supported; any value returns 400. See Smaller vectors.

Known limitations

  • Text only. Image embeddings are not available on the API.
  • The task prefix is not added for you (see Task prefixes).
  • Any string over 8,192 tokens fails the whole request. Split long documents into chunks before embedding them.
  • The 8,192-token limit is checked against an estimate made before the text is tokenised, and the estimate runs high, at about 1.25 tokens per English word. A 7,000-word string (about 7,000 real tokens) is refused with “Estimated tokens (8750) exceed the context window of 8192”. Keep each string under about 6,500 words.

Limits and errors

AreaLimit or conditionAPI responseRecommended action
AccessYour organisation does not have Provisioned Throughput for this model404 not_found_error; the model isn’t listed in GET /v2/modelsContact us to request access.
Input lengthA string’s estimated token count is over 8,192 (about 6,500 English words)400 invalid_request_error (“Estimated tokens (…) exceed the context window of 8192”)Chunk the text.
Empty inputinput is an empty list400 invalid_request_errorSend at least one string.
Dimensionsdimensions is set400 invalid_request_errorRemove it and shorten vectors on your side.
Endpointembeddinggemma-2 sent to another endpoint, or a non-embedding model sent to /v2/embeddings400 invalid_request_error or 404 not_found_error, naming the endpointSend embeddinggemma-2 to POST /v2/embeddings only.
AuthenticationAPI key is missing or invalid403 invalid_api_key_errorCheck the api-subscription-key header, or the API key passed to the OpenAI SDK.
Rate limitRequest or concurrency limit is exceeded429 rate_limit_exceeded_errorBack off and retry.