Skip to main content

Gemini + Moorcheh

This integration uses Google Gemini to generate embeddings and Moorcheh vector namespaces to store and search them with ITS ranking. Gemini embedding models can map text, image, video, audio, and PDFs (including interleaved combinations) into a unified vector space. This page focuses on the gemini-embedding-2-preview model with text; you can extend the same pattern to files using the Gemini Embedding API. Use this approach when you want full control over the embedding model and upload pre-computed vectors directly to Moorcheh.

Architecture

Embedding generation

Generate vectors with Gemini gemini-embedding-2-preview and task types such as RETRIEVAL_DOCUMENT / RETRIEVAL_QUERY

Vector storage

Store vectors in Moorcheh vector namespaces

Semantic retrieval

Search by vector query for high-relevance results

Model flexibility

Tune output dimensionality to balance quality and storage

Prerequisites

Install dependencies:
The PyPI package name is google-genai (with a hyphen). That provides the Python module google.genai (with a dot). If you see ModuleNotFoundError: No module named 'google.genai', run the pip install line above in the same environment you use to run the script.

.env file

Task types

The Gemini Embedding API accepts a task_type that optimizes vectors for the intended use. Common choices for retrieval: Other supported types include SEMANTIC_SIMILARITY, CLASSIFICATION, CLUSTERING, CODE_RETRIEVAL_QUERY, QUESTION_ANSWERING, and FACT_VERIFICATION. Use the same model and dimension settings for both indexing and querying.

Vector dimensions

By default, gemini-embedding-2-preview returns 3072 dimensions. You can set output_dimensionality (for example 768 or 1536) to reduce storage. The Moorcheh namespace vector_dimension must match the size you produce at index and query time.

End-to-end example

The following example loads keys from .env, embeds document chunks with RETRIEVAL_DOCUMENT, uploads them to Moorcheh, embeds a query with RETRIEVAL_QUERY, and runs vector search.

Embedding PDFs and other files

You can pass binary parts to embed_content (for example a PDF) using types.Part.from_bytes:
Chunk or split large documents as needed before upload; store each resulting vector in Moorcheh with metadata that points back to the source file.

Runnable demo script

See integrations/gemini/gemini_moorcheh_demo.py.

Important notes

The namespace vector_dimension must exactly match the length of vectors you upload. If you use output_dimensionality on the Gemini side, create the namespace with that same size.
Use RETRIEVAL_DOCUMENT (or equivalent) for indexed content and RETRIEVAL_QUERY for search queries.
Include text in each uploaded vector object so search results can return the original chunk without an extra lookup.
Use the same model, task types, and dimension settings for indexing and querying.

Troubleshooting

  • No vector namespace found: Create the namespace first with type="vector".
  • Dimension mismatch: Recreate the namespace with the correct vector_dimension or align Gemini output_dimensionality with the namespace.
  • Auth errors: Confirm GEMINI_API_KEY is set and valid for the Gemini API.