This is a submission for the Open Source AI Challenge with pgai and Ollama
This is an AI based research assistant with a semantic document search system for smart document storage and retrieval using natural language queries. Ollama is integrated into the assistant to summarise, and generate sentiment analysis, key points, related topics for provided content. Streamlit is used to provide a minimalistic user interface.
You can use natural language to search data stored in the PostgreSQL database. Uses pgvector for vector similarity search, pgai through TimescaleDB for search AI features. It is very helpful in cases where you have to manage and search through large collections of documents based on meaning rather than just keywords.
Key Features:
- Uses Ollama to summarise docs, and generate sentiment analysis, key points, and related topics
- Semantic search capability using document embeddings, powered by pgai
- Batch document processing (directly upload CSV files)
- User-friendly interface built with Streamlit
- Document addition and indexing from GUI
- Rich metadata support for categorization
- Simple table view and a detailed view for data
- Scalable vector search using both pgvector's IVFFlat indexing and the pgvectorscale extension
Although initially the idea was to develop a semantic document search system, later on I decided to extend this to an AI research assistant featuring the same document search system along with Ollama integration.
Because of problems with hosting Ollama along with the assistant app, only the semantic document search tool demo is hosted.
- Thanks to Streamlit community cloud, visit the demo ⭐
- Ollama is integrated into the assistant to summarise, and generate sentiment analysis, key points, related topics for provided content.
- TimescaleDB (PostgreSQL) for primary database (can be configured for self hosted psql as well)
- pgvector for efficient vector similarity search
- pgai through TimescaleDB for AI
- Streamlit for the web interface
-
Database Layer
- pgvector extension for vector operations
- pgai extension for AI features
- IVFFlat indexing for efficient similarity search
- JSONB data type for flexible metadata storage
-
Machine Learning
- Sentence-Transformers (
all-MiniLM-L6-v2 model
) - 384-dimensional embeddings for semantic representation
- Sentence-Transformers (
-
Backend
- Python 3.12+
- psycopg2 for PostgreSQL interaction
- Vector similarity calculations using cosine distance
-
Frontend
- Streamlit for the web interface
- Pandas for data display
- Download data as CSV files
-
Create a Timescale Service
- Open Timescale Cloud Console and create a service
- In the AI tab, enable
ai
,vector
extensions - Pick Python app and copy the database connection URL
-
Configure Environment Edit the
src/.env
file with the copied URLPSQL_URL=postgres://username:password@hostname:port/dbname?sslmode=require
-
Install Ollama and any model (make sure its added to script) for assistant
curl -fsSL https://ollama.com/install.sh | sh ollama pull mistral ollama serve
-
Install Requirements
pip install -r requirements.txt # or if you have poetry poetry install && poetry shell
-
Run the Assistant
cd src streamlit run assistant.py
Run the Document Search Tool
cd src streamlit run main.py
-
Install PostgreSQL and Extensions
# Install PostgreSQL sudo apt-get install postgresql postgresql-common # Install pgvector sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh sudo apt install postgresql-12-pgvector # Install pgai # https://github.com/timescale/pgai/tree/main?tab=readme-ov-file#install-from-source
-
Configure Database
CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS ai CASCADE;
-
Configure Environment
PSQL_URL=postgresql://user:password@localhost:5432/dbname
or configure within script
db_params = { 'dbname': 'dbname', 'user': 'postgres', 'password': 'your_password', 'host': 'localhost', 'port': '5432' }
This project is about integrating AI vector search features with traditional databases (which are hard to get used to). The same tool is used to create an AI research assistant with Ollama integration. This is a very helpful tool for content management systems where you need to manage and search through large collections of documents. Integration of pgvector and pgai provides a strong solution.
- Better visualization of results using charts and stuff
- Batch document processing (import CSV)
- Delete, update documents functionality
- Filtering based on metadata as well
- More use cases of pgai