DEV Community

Cover image for Getting Started with Google Gemini Embeddings in Python: A Hands-On Guide
Imthadh Ahamed
Imthadh Ahamed

Posted on

Getting Started with Google Gemini Embeddings in Python: A Hands-On Guide

Artificial Intelligence is evolving rapidly, and one of the most exciting areas is retrieval-augmented generation (RAG) and semantic search. At the heart of these systems lies a powerful concept: embeddings.

In this article, I'll walk you through the process of generating embeddings using Google's Gemini API in Python. Don't worry if you're a beginner, we'll break it down step by step, with code you can run today.

What Are Embeddings (with a real-world twist)?

Imagine walking into a huge supermarket. Instead of wandering, you notice how items are grouped.

  • Fruits are in one section 🍎🍌🍇

  • Vegetables in another 🥦🥕

  • Bakery items together 🥖🍩

Even though apple and banana are different, they're close together in the fruit section because they mean similar things (both are fruits).
That's exactly what embeddings do for text. They put similar concepts near each other in a mathematical supermarket.
So if you ask the system about solar energy, it doesn't just look for the exact word "solar." It also finds photovoltaics, sunlight power, or renewable energy, because those live in the same "aisle."

👉 Think of embeddings as a way to organize knowledge like a supermarket organizes groceries.

What Are Embeddings (Technical View)?

An embedding is a numerical representation of data (text, images, audio, etc.) in a continuous vector space. In NLP (natural language processing), embeddings are used to represent words, sentences, or documents as high-dimensional vectors of real numbers.

Key Properties

Dimensionality

  • Each embedding is a vector of fixed length (768 dimensions for embedding-001 in Gemini).
  • Example:
[0.12, -0.34, 0.89, ...] # length = 768 
Enter fullscreen mode Exit fullscreen mode

Semantic Proximity

  • The geometry of the vector space encodes meaning.
  • Texts with similar semantic meaning will have embeddings that are closer together (low cosine distance / high cosine similarity).
  • Example:
  • "Solar energy" and "photovoltaics" → embeddings close in space
  • "Solar energy" and "chocolate cake" → embeddings far apart

Training Basis

  • Embeddings are learned by large language models (LLMs) trained on massive corpora.
  • The model optimizes representations such that semantically related text produces vectors with high similarity.

Mathematical Use

  • You can compute distances between embeddings using:
  • Cosine similarity (most common)
  • Euclidean distance

Setting Up the Environment

pip install google-generativeai python-dotenv 
Enter fullscreen mode Exit fullscreen mode
  • google-generativeai → The engine (Gemini API)
  • python-dotenv → The cashier who checks your membership card (API key)

Inside your .env file, add:
GEMINI_API_KEY=your_api_key_here

Writing the Python Code

Here's our hands-on demo:

import os import google.generativeai as genai from dotenv import load_dotenv # Load API key os.environ.pop("GEMINI_API_KEY", None) load_dotenv(override=True) GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") if not GEMINI_API_KEY: raise ValueError("GEMINI_API_KEY is not set in the environment variables.") print("GEMINI_API_KEY is set.") # Configure Gemini genai.configure(api_key=GEMINI_API_KEY) # Text chunks = our knowledge items chunks = [ "Chunk 1: Renewable energy comes from resources that are naturally replenished (sunlight, wind, rain, tides, waves, geothermal).", "Chunk 2: Solar energy is abundant and captured using photovoltaic panels. Wind energy uses turbines to generate electricity.", "Chunk 3: Geothermal energy comes from heat inside the Earth. Biomass energy is derived from organic materials." ] # Generate embeddings for i, chunk in enumerate(chunks): response = genai.embed_content( model="models/embedding-001", content=chunk, task_type="retrieval_document" ) embedding = response["embedding"] print(f"\nEmbedding for Chunk {i + 1}:\n{embedding[:10]}...") 
Enter fullscreen mode Exit fullscreen mode

Breaking It Down

API Key Handling

  • We use .env for security. never hardcode keys.

Chunks of Text

  • Each chunk represents a small passage of knowledge. In real-world projects, chunks might be paragraphs from PDFs, product descriptions, or support docs.

embed_content Call

  • The Gemini model (embedding-001) converts text into a 768-dimensional vector. We only print the first 10 numbers to keep output readable.

Result

  • Each chunk now has a unique vector representation. These embeddings can be stored in a vector database (like Pinecone, Weaviate, or ChromaDB) for semantic search or powering RAG pipelines.

Real-World Applications

So why does this matter? Here are some examples where embeddings shine:

  • Search Engines → Find relevant docs by meaning, not just keywords.
  • Chatbots & RAG Systems → Retrieve context-aware answers.
  • Recommendation Engines → Suggest similar products or articles.
  • Clustering & Topic Modeling → Group similar content automatically.

Imagine building a renewable energy Q&A bot: the chunks above could serve as knowledge, and embeddings would help the bot fetch the right passage when a user asks, "How does geothermal energy work?"

Conclusion

Embeddings are like the hidden language that bridges human words and machine understanding. With Google's Gemini API, creating them is no longer rocket science - it's just a few lines of Python.

If you're planning to build your own AI-powered search, chatbot, or recommendation system, embeddings will be at the core of it. This hands-on example is your first step toward building those advanced systems.

Top comments (0)