Skip to content

ayende/samples.imgs-embeddings

Repository files navigation

Category Image App

A .NET web application that integrates with RavenDB and OpenAI's CLIP model for category management with image embeddings.

Features

  • Category Management: Create, view, and search categories
  • Image Support: Upload and display images for categories stored as RavenDB attachments
  • Text Search: Traditional text-based search through category names and descriptions
  • Semantic Search: AI-powered search using OpenAI embeddings
  • Embedding Generation: Generate embeddings for categories using OpenAI's CLIP model
  • Modern UI: Bootstrap-based responsive interface

Prerequisites

  1. RavenDB: Install and run RavenDB locally on port 8080
  2. OpenAI API Key: Get an API key from OpenAI
  3. .NET 8 SDK: Install .NET 8 SDK

Setup Instructions

1. RavenDB Setup

2. OpenAI Configuration

{
  "OpenAI": {
    "ApiKey": "your-actual-openai-api-key-here"
  }
}

3. Running the Application

# Restore packages
dotnet restore

# Run the application
dotnet run

The application will be available at https://localhost:5001 or http://localhost:5000.

Usage

Initial Setup

  1. Create Sample Data: Click "Create Sample Data" to generate some example categories
  2. Initialize System: Click "Initialize System" to generate embeddings for all categories using OpenAI's CLIP model

Features

  • Browse Categories: View all categories with their images
  • Text Search: Use the search bar for traditional keyword search
  • Semantic Search: Check "Use AI Semantic Search" for AI-powered search
  • Add Categories: Click "Add Category" to create new categories with optional images

Architecture

Models

  • Category: Main entity with name, description, image support, and embeddings
  • SearchRequest: DTO for search operations

Services

  • CategoryService: Handles CRUD operations and similarity search
  • OpenAIService: Integrates with OpenAI API for embeddings
  • EmbeddingService: Orchestrates embedding generation

Database

  • RavenDB: Document database with attachment support
  • Indexes: Custom indexes for text search and embedding operations

API Endpoints

  • GET / - Main categories page with search
  • GET /Home/CreateCategory - Category creation form
  • POST /Home/CreateCategory - Create new category
  • POST /Home/InitializeSystem - Generate embeddings for all categories
  • POST /Home/CreateSampleData - Create sample categories
  • GET /Home/GetCategoryImage/{id} - Retrieve category image

Technical Details

Embedding Process

  1. For categories with images: Generate embeddings from the image using OpenAI's CLIP model
  2. For categories without images: Generate embeddings from text (name + description)
  3. Store embeddings in RavenDB for similarity search

Search Types

  1. Text Search: Uses RavenDB's full-text search capabilities
  2. Semantic Search: Uses cosine similarity with OpenAI embeddings

Image Storage

  • Images are stored as RavenDB attachments
  • Supports various image formats (JPEG, PNG, etc.)
  • Images are displayed directly from RavenDB

Configuration

Key configuration options in appsettings.json:

{
  "OpenAI": {
    "ApiKey": "your-openai-api-key"
  },
  "RavenDB": {
    "Urls": ["http://localhost:8080"],
    "Database": "CategoryImageDB"
  }
}

Dependencies

  • RavenDB.Client: Document database client
  • Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation: For development-time view compilation
  • Newtonsoft.Json: JSON serialization
  • Bootstrap 5: UI framework
  • Font Awesome: Icons

Notes

  • The application uses OpenAI's text-embedding-3-large model
  • Rate limiting is implemented to avoid OpenAI API limits
  • Embeddings are cached in RavenDB to avoid regeneration
  • The UI is responsive and works on mobile devices

About

Using RavenDB to do multi modal searches (images & text)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published