An AI-powered document management and chat system built with Rails 8, featuring intelligent document processing, vector embeddings, and conversational AI.
- 📄 Document Management: Upload, process, and organize documents (PDF, DOCX, TXT)
- 🤖 AI-Powered Chat: Ask questions about your documents using OpenAI's GPT models
- 🔍 Vector Search: Semantic similarity search using embeddings (with optional pgvector support)
- 👥 Multi-tenant: Support for multiple customers with isolated document collections
- 🔐 Authentication: User roles (admin, regular, read_only) with Devise
- ⚡ Background Jobs: Async document processing with Sidekiq
- 💾 Caching: Redis-based caching for improved performance
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Document │ │ Text │ │ Embedding │
│ Upload │───▶│ Extraction │───▶│ Generation │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Chat │ │ Document │ │ Vector │
│ Interface │◀───│ Retrieval │◀───│ Storage │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Response │ │ Caching │
│ Generation │◀───│ Layer │
└─────────────────┘ └─────────────────┘
- Ruby 3.4+ (managed via asdf)
- PostgreSQL 16+ (via Docker)
- Redis 7+ (via Docker)
- Docker and Docker Compose
- OpenAI API key
# Install Ruby dependencies
bundle install
# Install Node.js dependencies (if using any JS packages)
# npm install # or yarn installCopy the example environment file and update with your values:
cp .env.example .envThen edit .env and update the following required values:
OPENAI_API_KEY: Your OpenAI API key (required for AI features)- Database credentials if different from defaults
- Redis URL if different from default
The .env.example file contains all available configuration options with sensible defaults.
# Start PostgreSQL and Redis containers
docker compose up -d postgres redis
# Verify containers are running
docker compose ps# Create the database
rails db:create
# Run migrations
rails db:migrate
# (Optional) Load seed data with sample users and documents
rails db:seed# Start Rails server
rails s
# In a separate terminal, start Sidekiq for background jobs
bundle exec sidekiqThe application will be available at http://localhost:3000
For enhanced vector similarity search performance, you can set up pgvector:
The docker-compose.yml already uses the pgvector/pgvector:pg16 image which includes pgvector.
Add to your Gemfile:
gem "pgvector", "~> 0.2"Then run:
bundle installCreate a migration:
rails generate migration EnablePgvectorExtensionEdit the migration:
class EnablePgvectorExtension < ActiveRecord::Migration[8.0]
def change
enable_extension "vector"
end
endCreate a migration to change the column type:
rails generate migration ConvertEmbeddingVectorToPgvectorclass ConvertEmbeddingVectorToPgvector < ActiveRecord::Migration[8.0]
def up
# Convert text column to vector type
execute <<-SQL
ALTER TABLE document_embeddings
ALTER COLUMN embedding_vector TYPE vector(1536)
USING embedding_vector::vector;
SQL
# Add vector similarity index
execute <<-SQL
CREATE INDEX index_embeddings_on_vector_cosine
ON document_embeddings
USING ivfflat (embedding_vector vector_cosine_ops)
WITH (lists = 100);
SQL
end
def down
execute <<-SQL
DROP INDEX IF EXISTS index_embeddings_on_vector_cosine;
ALTER TABLE document_embeddings
ALTER COLUMN embedding_vector TYPE text;
SQL
end
endrails db:migrateIn config/initializers/docusense_config.rb, set:
FEATURE_FLAGS = {
enable_pgvector: true, # Change from false to true
# ...
}The seed file creates sample data including:
- 2 Customers: Acme Corporation and Smith & Associates Law Firm
- 4 Users: Admin, regular user, read-only user, and lawyer
- 3 Document Collections: HR Policies, Technical Documentation, Legal Documents
- 3 Sample Documents: Employee Handbook, API Documentation, Service Agreement
- 2 Chat Sessions: With sample conversations
After running rails db:seed, you'll see login credentials printed to the console.
After running rails db:seed, the following users are created:
-
Admin:
[email protected]/password123- Role: Admin
- Full access to all features
-
Regular User:
[email protected]/password123- Role: Regular
- Can upload documents and use chat
-
Read-Only User:
[email protected]/password123- Role: Read Only
- Can view documents and chat, but cannot upload
- Lawyer:
[email protected]/password123- Role: Admin
- Full access for the law firm
# Run all tests
rails test
# Run specific test file
rails test test/models/user_test.rb# Run RuboCop
bundle exec rubocop
# Run Brakeman (security scanner)
bundle exec brakemanrails dbconsolerails console- Set production environment variables
- Configure production database credentials
- Set up SSL/TLS certificates
- Configure Redis for production
- Set up Sidekiq workers
- Enable pgvector if using vector search
If you get connection errors:
- Ensure Docker containers are running:
docker compose ps - Check
.envfile has correctDATABASE_URLpointing tolocalhost - Verify PostgreSQL is accessible:
docker compose logs postgres
- Check Redis is running:
docker compose ps redis - Verify
REDIS_URLin.envuseslocalhostfor local development - Test connection:
redis-cli -h localhost ping
- If you get errors about missing tables, ensure you've run
rails db:createfirst - For pgvector errors, ensure the extension is enabled:
rails dbconsolethenCREATE EXTENSION IF NOT EXISTS vector;