Member-only story
How to Use Geminiโs New Embedding Model in Python
In the ever-evolving world of artificial intelligence, embedding models have become a cornerstone for various applications, from natural language processing to recommendation systems. Googleโs Gemini has recently introduced a new embedding model, gemini-embedding-exp-03-07
, which promises to deliver state-of-the-art performance in generating embeddings for text data. In this article, we'll walk through how to use this new embedding model in Python to generate embeddings and compute cosine similarity between documents.

What are Embeddings?
Embeddings are numerical representations of text that capture the semantic meaning of words, sentences, or entire documents. These representations are crucial for tasks like text classification, clustering, and similarity analysis. The quality of embeddings can significantly impact the performance of machine learning models, making it essential to use robust and accurate embedding models.
Setting Up the Environment
Before we dive into the code, ensure that you have the necessary libraries installed. Youโll need the google-generativeai
library to interact with Gemini's embedding model. You can install it using pip:
pip install google-generativeai
Additionally, youโll need to set up your API key. You can store it securely using Google Colabโs userdata
or any other secure method.
from google import genai
from google.colab import userdata
# Initialize the Gemini client
client = genai.Client(api_key=userdata.get('gemini'))
Generating Embeddings
Once the environment is set up, you can start generating embeddings using the gemini-embedding-exp-03-07
model. Here's a simple function to generate embeddings for a given text:
def embed(text):
result = client.models.embed_content(
model="gemini-embedding-exp-03-07",
contents=text
)
return result.embeddings[0].values
This function takes a text input and returns a 3072-dimensional embedding vector.