๐€๐ˆ ๐ฆ๐จ๐ง๐ค๐ฌ.๐ข๐จ

AImonks (https://medium.com/aimonks) is an AI-Educational Publication.

Follow publication

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.

Example: Embedding Documents

--

--

KoshurAI
KoshurAI

Written by KoshurAI

Passionate about Data Science? I offer personalized data science training and mentorship. Join my course today to unlock your true potential in Data Science.

Responses (2)

Write a response