Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ai/select-algorithm-typescript/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ========================================
# Azure OpenAI Embedding Settings
# ========================================
AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small
AZURE_OPENAI_EMBEDDING_API_VERSION=2023-05-15
AZURE_OPENAI_EMBEDDING_ENDPOINT=https://<RESOURCE-NAME>.openai.azure.com

# ========================================
# Data File Paths and Vector Configuration
# ========================================
DATA_FILE_WITH_VECTORS=../data/Hotels_Vector.json
EMBEDDED_FIELD=DescriptionVector
EMBEDDING_DIMENSIONS=1536
LOAD_SIZE_BATCH=100

# ========================================
# MongoDB/DocumentDB Connection Settings
# ========================================
MONGO_CLUSTER_NAME=<CLUSTER-NAME>

# ========================================
# Algorithm Selection (used by select-algorithm.ts)
# ========================================
# ALGORITHM: "all" | "diskann" | "hnsw" | "ivf"
ALGORITHM=all
# SIMILARITY: "all" | "COS" | "L2" | "IP"
SIMILARITY=COS
61 changes: 61 additions & 0 deletions ai/select-algorithm-typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Select Algorithm - TypeScript

Compare DiskANN, HNSW, and IVF vector index algorithms across COS, L2, and IP similarity metrics using Azure DocumentDB.

## Prerequisites

- Node.js 20+
- Azure DocumentDB cluster
- Azure OpenAI resource with `text-embedding-3-small` deployment

## Setup

1. Copy `.env.example` to `../../.env` (repo root) and fill in your values.
2. Install dependencies:

```bash
npm install
```

## Usage

### Compare all algorithms (default: COS similarity)

```bash
npm start
```

Set `ALGORITHM` and `SIMILARITY` env vars in `.env` to control which collections are queried:

| ALGORITHM | SIMILARITY | Collections queried |
|-----------|------------|---------------------|
| `all` | `COS` | 3 (one per algorithm, COS) |
| `all` | `all` | 9 (all combinations) |
| `diskann` | `COS` | 1 (hotels_diskann_cos) |
| `diskann` | `all` | 3 (diskann × all similarities) |

### Run single algorithm

```bash
npm run start:diskann
npm run start:hnsw
npm run start:ivf
```

### Verify indexes

```bash
npm run start:show-indexes
```

## Architecture

Creates 9 collections (3 algorithms × 3 distance metrics):

| Algorithm | COS | L2 | IP |
|-----------|-----|----|----|
| DiskANN | `hotels_diskann_cos` | `hotels_diskann_l2` | `hotels_diskann_ip` |
| HNSW | `hotels_hnsw_cos` | `hotels_hnsw_l2` | `hotels_hnsw_ip` |
| IVF | `hotels_ivf_cos` | `hotels_ivf_l2` | `hotels_ivf_ip` |

Each collection gets its own vector index created via `db.command()` and data inserted via `insertMany()`. The main script runs `$search` aggregation queries and prints a comparison table.
Loading