Skip to content

Update model name to gemini-embedding-001 in code snipets #13388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generative_ai/embeddings/batch_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def embed_text_batch() -> BatchPredictionJob:
output_uri = OUTPUT_URI

textembedding_model = language_models.TextEmbeddingModel.from_pretrained(
"textembedding-gecko@003"
"gemini-embedding-001"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model is updated to gemini-embedding-001. This is a key change. Could you please confirm if this new model has been verified for full compatibility with the existing usage patterns in this and other updated examples?

Specifically for the examples touched in this PR:

  • Are the task types used in code_retrieval_example.py (CODE_RETRIEVAL_QUERY, RETRIEVAL_DOCUMENT) and document_retrieval_example.py (RETRIEVAL_DOCUMENT) supported and appropriate for gemini-embedding-001?
  • Is the output_dimensionality=256 (used in code_retrieval_example.py and document_retrieval_example.py) a valid, supported, and optimal setting for gemini-embedding-001?
  • Does gemini-embedding-001 (when used via TextEmbeddingModel.from_pretrained) behave as expected with the batch_predict method and the input/output formats used in this specific batch_example.py file?

Ensuring these compatibilities are verified is important for the correctness and reliability of these samples. If the new model has different characteristics or requirements, further code adjustments might be needed.

)

batch_prediction_job = textembedding_model.batch_predict(
Expand Down
25 changes: 16 additions & 9 deletions generative_ai/embeddings/code_retrieval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,31 @@
# [START generativeaionvertexai_embedding_code_retrieval]
from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel

MODEL_NAME = "text-embedding-005"
DIMENSIONALITY = 256
MODEL_NAME = "gemini-embedding-001"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

With MODEL_NAME now set to gemini-embedding-001, it's important to ensure this model works correctly with the parameters used in this example. Specifically, have the task types (CODE_RETRIEVAL_QUERY and RETRIEVAL_DOCUMENT, as used in the if __name__ == "__main__" block) and the default dimensionality=256 been verified for compatibility and optimal performance with gemini-embedding-001?

Different models can have varying support for task types or optimal dimensionalities, so confirming this would help ensure the example remains accurate and effective.

DIMENSIONALITY = 3072


def embed_text(
texts: list[str] = ["Retrieve a function that adds two numbers"],
task: str = "CODE_RETRIEVAL_QUERY",
model_name: str = "text-embedding-005",
dimensionality: int | None = 256,
model_name: str = "gemini-embedding-001",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The default model_name in the embed_text function signature is also updated to gemini-embedding-001. This is consistent with the MODEL_NAME constant change. The same verification question applies here: has this default been confirmed to work well with the function's other default parameters (like task and dimensionality) when using gemini-embedding-001?

dimensionality: int | None = 3072,
) -> list[list[float]]:
"""Embeds texts with a pre-trained, foundational model."""
model = TextEmbeddingModel.from_pretrained(model_name)
inputs = [TextEmbeddingInput(text, task) for text in texts]
kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {}
embeddings = model.get_embeddings(inputs, **kwargs)
# Example response:
# [[0.025890009477734566, -0.05553026497364044, 0.006374752148985863,...],
return [embedding.values for embedding in embeddings]

embeddings = []
# gemini-embedding-001 takes one input at a time
for text in texts:
text_input = TextEmbeddingInput(text, task)
embedding = model.get_embeddings([text_input], **kwargs)
print(embedding)
# Example response:
# [[0.006135190837085247, -0.01462465338408947, 0.004978656303137541, ...]]
embeddings.append(embedding[0].values)

return embeddings


if __name__ == "__main__":
Expand Down
21 changes: 13 additions & 8 deletions generative_ai/embeddings/document_retrieval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ def embed_text() -> list[list[float]]:
# A list of texts to be embedded.
texts = ["banana muffins? ", "banana bread? banana muffins?"]
# The dimensionality of the output embeddings.
dimensionality = 256
dimensionality = 3072
# The task type for embedding. Check the available tasks in the model's documentation.
task = "RETRIEVAL_DOCUMENT"

model = TextEmbeddingModel.from_pretrained("text-embedding-005")
inputs = [TextEmbeddingInput(text, task) for text in texts]
model = TextEmbeddingModel.from_pretrained("gemini-embedding-001")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Updating the model to gemini-embedding-001 here is consistent with the other changes. As with the other files, could you confirm if the task = "RETRIEVAL_DOCUMENT" and dimensionality = 256 settings have been verified as compatible and appropriate for gemini-embedding-001? This verification is key to ensuring the example functions correctly and demonstrates best practices with the new model.

kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {}
embeddings = model.get_embeddings(inputs, **kwargs)

print(embeddings)
# Example response:
# [[0.006135190837085247, -0.01462465338408947, 0.004978656303137541, ...], [0.1234434666, ...]],
return [embedding.values for embedding in embeddings]
embeddings = []
# gemini-embedding-001 takes one input at a time
for text in texts:
text_input = TextEmbeddingInput(text, task)
embedding = model.get_embeddings([text_input], **kwargs)
print(embedding)
# Example response:
# [[0.006135190837085247, -0.01462465338408947, 0.004978656303137541, ...]]
embeddings.append(embedding[0].values)

return embeddings


# [END generativeaionvertexai_embedding]
Expand Down
2 changes: 1 addition & 1 deletion generative_ai/embeddings/test_embeddings_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_generate_embeddings_with_lower_dimension() -> None:
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_text_embed_text() -> None:
embeddings = document_retrieval_example.embed_text()
assert [len(e) for e in embeddings] == [256, 256]
assert [len(e) for e in embeddings] == [3072, 3072]


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
Expand Down