A demonstration of semantic search capabilities using local embeddings with Google's Gemini models and Aiven for AlloyDB Omni.
This application demonstrates how to implement semantic search for product data stored in AlloyDB. Instead of relying on AlloyDB's built-in integration with Google's Vertex AI, this application generates embeddings locally using the Gemini API and stores them in the database.
- Semantic Product Search: Search for products using natural language queries
- Add Products: Add new products to the database with automatically generated embeddings
- Update Embeddings: Generate or update embeddings for existing products
- Python 3.8+
- An AlloyDB Omni instance (e.g., through Aiven)
- A Google API key with access to the Gemini API
- Clone this repository
- Install the required packages:
pip install -r requirements.txt
-
Create a
.envfile in the root directory with the following variables:ALLOY_DB_USER=your_db_user ALLOY_DB_PASSWORD=your_db_password ALLOY_DB_HOST=your_db_host ALLOY_DB_PORT=your_db_port GEMINI_API_KEY=your_gemini_api_key -
Create a
.streamlitdirectory and inside it create asecrets.tomlfile with the following content:[connections.postgresql] dialect = "postgresql" host = "your_db_host" port = 12345 # Your DB port database = "defaultdb" username = "your_db_user" password = "your_db_password"
Launch the Streamlit application:
streamlit run modified_search.py
If you encounter any issues:
- Enable Debug Mode in the sidebar to see detailed error messages
- Run the
simple_test.pyscript to verify your database connection:python simple_test.py - Make sure all environment variables are correctly set
The application expects a product table with the following schema:
CREATE TABLE product (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
price NUMERIC(10, 2),
images TEXT,
emb REAL[]
);- Embeddings are generated using Google's Gemini
models/embedding-001model - Embeddings are stored in the
embcolumn as arrays of floating-point numbers - Similarity is calculated using cosine similarity between the query embedding and product embeddings
This application uses st.experimental_connection() which is compatible with Streamlit version 1.27.2. If you have a newer version of Streamlit that supports st.connection(), you may need to update the code accordingly.