Skip to content

Commit 991a352

Browse files
authored
Merge pull request #32 from vcon-dev/CON-43-litellm-embeddings
Enhance embedding functionality with LiteLLM support
2 parents 4cc1dd3 + 0ba1f42 commit 991a352

15 files changed

Lines changed: 332 additions & 311 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ SUPABASE_ANON_KEY=your-anon-key-here
88
# Optional: For service role operations (use carefully!)
99
# SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
1010

11+
# LiteLLM proxy (optional; used for embeddings when set)
12+
# LITELLM_PROXY_URL=http://localhost:4000
13+
# LITELLM_MASTER_KEY=sk-your-master-key
14+
1115
# ============================================================================
1216
# Plugin Configuration
1317
# ============================================================================

docs/api/tools.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ Create a new vCon (Virtual Conversation) record.
7272
```typescript
7373
{
7474
vcon_data: {
75-
vcon: "0.3.0", // vCon version
75+
vcon: "0.4.0", // vCon version
7676
uuid?: string, // Auto-generated if not provided
7777
subject?: string, // Conversation subject
7878
parties: Party[], // At least one party required
7979
dialog?: Dialog[], // Optional conversation content
8080
analysis?: Analysis[], // Optional AI analysis
8181
attachments?: Attachment[], // Optional files
8282
extensions?: string[], // Optional extensions
83-
must_support?: string[] // Optional requirements
83+
critical?: string[] // Optional: extensions that must be supported (v0.4.0)
8484
},
8585
metadata?: {
8686
basename?: string,
@@ -106,7 +106,7 @@ Create a new vCon (Virtual Conversation) record.
106106
```typescript
107107
{
108108
"vcon_data": {
109-
"vcon": "0.3.0",
109+
"vcon": "0.4.0",
110110
"subject": "Customer Support Call",
111111
"parties": [
112112
{
@@ -146,7 +146,7 @@ Retrieve a vCon by UUID.
146146
{
147147
success: boolean,
148148
vcon: {
149-
vcon: "0.3.0",
149+
vcon: "0.4.0",
150150
uuid: string,
151151
created_at: string,
152152
updated_at?: string,
@@ -181,7 +181,7 @@ Update vCon metadata and top-level fields.
181181
updates: {
182182
subject?: string,
183183
extensions?: string[],
184-
must_support?: string[],
184+
critical?: string[],
185185
[key: string]: any
186186
},
187187
merge_strategy?: "replace" | "merge" | "append", // Default: "merge"

docs/development/INGEST_AND_EMBEDDINGS.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npm install
1919
- `SUPABASE_URL`
2020
- `SUPABASE_SERVICE_ROLE_KEY`
2121
- `VCON_S3_BUCKET` (optional, for S3 loading)
22-
- `OPENAI_API_KEY` or `HF_API_TOKEN` (for embeddings)
22+
- One embedding provider (see priority order below): `LITELLM_PROXY_URL`+`LITELLM_MASTER_KEY`, `OPENAI_API_KEY`, Azure vars, or `HF_API_TOKEN`
2323

2424
---
2525

@@ -56,7 +56,7 @@ npm run sync:vcons -- /absolute/path/to/vcons
5656
Notes:
5757
- Use absolute directory paths. Files must end with `.vcon` extension.
5858
- The script is idempotent and skips vCons already in the database.
59-
- Handles both legacy (0.0.1-0.2.0) and current (0.3.0) vCon specs.
59+
- Handles both legacy (0.0.1-0.2.0) and current (0.4.0) vCon specs.
6060

6161
---
6262

@@ -76,12 +76,11 @@ Analysis elements with `encoding='none'` are prioritized because they contain pl
7676
Environment variables (set in `.env` file or exported):
7777
- `SUPABASE_URL`
7878
- `SUPABASE_SERVICE_ROLE_KEY`
79-
- One provider (choose one):
80-
- `OPENAI_API_KEY` (uses `text-embedding-3-small` with `dimensions=384`)
81-
- or Azure OpenAI:
82-
- `AZURE_OPENAI_EMBEDDING_ENDPOINT` (e.g., `https://your-resource.openai.azure.com`)
83-
- `AZURE_OPENAI_EMBEDDING_API_KEY`
84-
- or `HF_API_TOKEN` (Hugging Face Inference API with `sentence-transformers/all-MiniLM-L6-v2`)
79+
- One provider (auto-detected in priority order — first match wins):
80+
1. **LiteLLM** (highest priority): `LITELLM_PROXY_URL` + `LITELLM_MASTER_KEY` (or `LITELLM_API_KEY`)
81+
2. **Azure OpenAI**: `AZURE_OPENAI_EMBEDDING_ENDPOINT` + `AZURE_OPENAI_EMBEDDING_API_KEY`
82+
3. **OpenAI**: `OPENAI_API_KEY` (uses `text-embedding-3-small` with `dimensions=384`)
83+
4. **Hugging Face**: `HF_API_TOKEN` (uses `sentence-transformers/all-MiniLM-L6-v2`)
8584

8685
#### Generate Embeddings
8786

@@ -101,6 +100,9 @@ For more control, use the script directly:
101100
# Process 100 units (default)
102101
npx tsx scripts/embed-vcons.ts
103102

103+
# Process with LiteLLM proxy (set LITELLM_PROXY_URL + LITELLM_MASTER_KEY in .env)
104+
npx tsx scripts/embed-vcons.ts --limit=500 --provider=litellm
105+
104106
# Process 500 units with OpenAI
105107
npx tsx scripts/embed-vcons.ts --limit=500 --provider=openai
106108

docs/development/embeddings.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Supabase Semantic Search Implementation Guide
22

3+
> **⚠️ Historical reference document.** The SQL examples in Steps 1–2 below reflect an early 1536-dim / `text-embedding-ada-002` design. The current implementation uses **`text-embedding-3-small` at 384 dimensions**. For the authoritative setup, see [`docs/development/INGEST_AND_EMBEDDINGS.md`](./INGEST_AND_EMBEDDINGS.md).
4+
35
## Overview
46

57
This guide explains how to implement semantic search for vCon conversation content in Supabase PostgreSQL using the **pgvector** extension for vector similarity search.
@@ -48,18 +50,18 @@ SELECT * FROM pg_extension WHERE extname = 'vector';
4850
```sql
4951
-- Add vector columns to existing tables
5052
ALTER TABLE vcons
51-
ADD COLUMN subject_embedding vector(1536), -- OpenAI ada-002 dimension
52-
ADD COLUMN subject_embedding_model TEXT DEFAULT 'text-embedding-ada-002',
53+
ADD COLUMN subject_embedding vector(384), -- text-embedding-3-small dimension
54+
ADD COLUMN subject_embedding_model TEXT DEFAULT 'text-embedding-3-small',
5355
ADD COLUMN subject_embedding_updated_at TIMESTAMPTZ;
5456

5557
ALTER TABLE dialog
56-
ADD COLUMN content_embedding vector(1536),
57-
ADD COLUMN content_embedding_model TEXT DEFAULT 'text-embedding-ada-002',
58+
ADD COLUMN content_embedding vector(384),
59+
ADD COLUMN content_embedding_model TEXT DEFAULT 'text-embedding-3-small',
5860
ADD COLUMN content_embedding_updated_at TIMESTAMPTZ;
5961

6062
ALTER TABLE analysis
61-
ADD COLUMN summary_embedding vector(1536),
62-
ADD COLUMN summary_embedding_model TEXT DEFAULT 'text-embedding-ada-002',
63+
ADD COLUMN summary_embedding vector(384),
64+
ADD COLUMN summary_embedding_model TEXT DEFAULT 'text-embedding-3-small',
6365
ADD COLUMN summary_embedding_updated_at TIMESTAMPTZ;
6466
```
6567

@@ -79,9 +81,9 @@ CREATE TABLE vcon_embeddings (
7981
content_text TEXT NOT NULL, -- Original text that was embedded
8082

8183
-- The embedding
82-
embedding vector(1536) NOT NULL,
83-
embedding_model TEXT NOT NULL DEFAULT 'text-embedding-ada-002',
84-
embedding_dimension INTEGER NOT NULL DEFAULT 1536,
84+
embedding vector(384) NOT NULL,
85+
embedding_model TEXT NOT NULL DEFAULT 'text-embedding-3-small',
86+
embedding_dimension INTEGER NOT NULL DEFAULT 384,
8587

8688
-- Metadata
8789
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
@@ -157,7 +159,7 @@ from supabase import create_client
157159
openai.api_key = "your-openai-key"
158160
supabase = create_client("your-supabase-url", "your-supabase-key")
159161

160-
def generate_embedding(text: str, model: str = "text-embedding-ada-002") -> list[float]:
162+
def generate_embedding(text: str, model: str = "text-embedding-3-small") -> list[float]:
161163
"""Generate embedding using OpenAI API."""
162164
response = openai.embeddings.create(
163165
input=text,
@@ -177,8 +179,8 @@ def embed_vcon_content(vcon_id: str, subject: str, dialog_texts: list[str]):
177179
'content_reference': None,
178180
'content_text': subject,
179181
'embedding': subject_embedding,
180-
'embedding_model': 'text-embedding-ada-002',
181-
'embedding_dimension': 1536
182+
'embedding_model': 'text-embedding-3-small',
183+
'embedding_dimension': 384
182184
}).execute()
183185

184186
# Embed dialogs
@@ -264,7 +266,7 @@ serve(async (req) => {
264266
})
265267
```
266268

267-
See `docs/INGEST_AND_EMBEDDINGS.md` for the production-ready function (`supabase/functions/embed-vcons/index.ts`), environment variables, and Cron scheduling. This repository standardizes on 384‑dim embeddings to match the migrations and HNSW index.
269+
See [`docs/development/INGEST_AND_EMBEDDINGS.md`](./INGEST_AND_EMBEDDINGS.md) for the production-ready function (`supabase/functions/embed-vcons/index.ts`), environment variables, and Cron scheduling. This repository standardizes on 384‑dim embeddings to match the migrations and HNSW index.
268270

269271
---
270272

@@ -275,7 +277,7 @@ See `docs/INGEST_AND_EMBEDDINGS.md` for the production-ready function (`supabase
275277
```sql
276278
-- Function to search by semantic similarity
277279
CREATE OR REPLACE FUNCTION search_vcons_semantic(
278-
query_embedding vector(1536),
280+
query_embedding vector(384),
279281
match_threshold float DEFAULT 0.7,
280282
match_count int DEFAULT 20
281283
)

docs/guide/installation.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,19 @@ curl "https://your-project.supabase.co/rest/v1/" \
532532

533533
Complete list of supported environment variables:
534534

535+
Embedding provider priority: **LiteLLM → Azure OpenAI → OpenAI → Hugging Face** (first configured wins).
536+
535537
| Variable | Required | Description | Default |
536538
|----------|----------|-------------|---------|
537539
| `SUPABASE_URL` | ✅ Yes | Your Supabase project URL | - |
538540
| `SUPABASE_ANON_KEY` | ✅ Yes | Supabase anon public key | - |
539541
| `SUPABASE_SERVICE_ROLE_KEY` | ❌ No | Service role key (admin operations) | - |
540-
| `OPENAI_API_KEY` | ❌ No | OpenAI API key for embeddings | - |
542+
| `LITELLM_PROXY_URL` | ❌ No | LiteLLM proxy base URL — takes priority for embeddings | - |
543+
| `LITELLM_MASTER_KEY` | ❌ No | LiteLLM proxy API key (also accepted as `LITELLM_API_KEY`) | - |
544+
| `OPENAI_API_KEY` | ❌ No | OpenAI API key for embeddings (if LiteLLM not set) | - |
541545
| `AZURE_OPENAI_EMBEDDING_ENDPOINT` | ❌ No | Azure OpenAI base endpoint (e.g., https://your-resource.openai.azure.com) | - |
542546
| `AZURE_OPENAI_EMBEDDING_API_KEY` | ❌ No | Azure OpenAI API key | - |
547+
| `HF_API_TOKEN` | ❌ No | Hugging Face API token for embeddings (lowest priority fallback) | - |
543548
| `VCON_PLUGINS_PATH` | ❌ No | Comma-separated plugin paths | - |
544549
| `VCON_LICENSE_KEY` | ❌ No | Enterprise license key | - |
545550
| `MCP_SERVER_NAME` | ❌ No | Server name for MCP | `vcon-mcp-server` |

docs/guide/search.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ The vCon MCP server provides four search tools with different capabilities, from
114114
}
115115
```
116116

117-
**Note:** Automatic embedding generation from query text is not yet implemented. Use `search_vcons_content` for keyword-based search without embeddings.
118-
119117
**Returns:** Similar conversations ranked by semantic similarity
120118

121119
---
@@ -287,8 +285,8 @@ Analysis with `encoding='json'` or `encoding='base64url'` typically contains:
287285
For semantic and hybrid search to work effectively, you need to generate embeddings for your vCons.
288286

289287
See the following guides:
290-
- [INGEST_AND_EMBEDDINGS.md](./INGEST_AND_EMBEDDINGS.md) - Complete guide to embedding generation
291-
- [EMBEDDING_STRATEGY_UPGRADE.md](./EMBEDDING_STRATEGY_UPGRADE.md) - Details on which content is embedded
288+
- [INGEST_AND_EMBEDDINGS.md](../development/INGEST_AND_EMBEDDINGS.md) - Complete guide to embedding generation
289+
- [EMBEDDING_STRATEGY_UPGRADE.md](../development/EMBEDDING_STRATEGY_UPGRADE.md) - Details on which content is embedded
292290

293291
**Quick start:**
294292
```bash
@@ -387,6 +385,6 @@ npm run embeddings:check
387385
## Related Documentation
388386

389387
- [QUICK_START.md](../QUICK_START.md) - Getting started with vCon MCP
390-
- [INGEST_AND_EMBEDDINGS.md](./INGEST_AND_EMBEDDINGS.md) - Embedding generation
388+
- [INGEST_AND_EMBEDDINGS.md](../development/INGEST_AND_EMBEDDINGS.md) - Embedding generation
391389
- [SUPABASE_SEMANTIC_SEARCH_GUIDE.md](../SUPABASE_SEMANTIC_SEARCH_GUIDE.md) - Database search implementation
392390

docs/reference/CORRECTED_SCHEMA.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ CREATE TABLE attachments (
128128
dialog INTEGER, -- CORRECTED: Added dialog reference per spec Section 4.4.4
129129

130130
-- Content fields
131-
mimetype TEXT,
131+
mediatype TEXT, -- CORRECTED: v0.0.2+ renamed mimetype→mediatype
132132
filename TEXT,
133133
body TEXT,
134134
encoding TEXT CHECK (encoding IS NULL OR encoding IN ('base64url', 'json', 'none')), -- CORRECTED: Removed default, added constraint
@@ -265,9 +265,9 @@ CREATE INDEX idx_vcons_subject_trgm ON vcons USING gin (subject gin_trgm_ops);
265265
CREATE INDEX idx_parties_name_trgm ON parties USING gin (name gin_trgm_ops);
266266

267267
-- Comments for documentation
268-
COMMENT ON TABLE vcons IS 'Main vCon container table - compliant with draft-ietf-vcon-vcon-core-00';
268+
COMMENT ON TABLE vcons IS 'Main vCon container table - compliant with draft-ietf-vcon-vcon-core-02 (v0.4.0)';
269269
COMMENT ON COLUMN vcons.extensions IS 'List of vCon extensions used (Section 4.1.3)';
270-
COMMENT ON COLUMN vcons.must_support IS 'List of incompatible extensions that must be supported (Section 4.1.4)';
270+
COMMENT ON COLUMN vcons.critical IS 'List of incompatible extensions that must be supported (Section 4.1.4); renamed from must_support in v0.4.0';
271271

272272
COMMENT ON TABLE parties IS 'Party objects from vCon parties array (Section 4.2)';
273273
COMMENT ON COLUMN parties.uuid IS 'Unique identifier for participant across vCons (Section 4.2.12)';
@@ -322,9 +322,9 @@ BEGIN;
322322

323323
-- 1. Add new required columns
324324
ALTER TABLE vcons ADD COLUMN IF NOT EXISTS extensions TEXT[];
325-
ALTER TABLE vcons ADD COLUMN IF NOT EXISTS must_support TEXT[];
326-
ALTER TABLE vcons ADD COLUMN IF NOT EXISTS appended JSONB DEFAULT '{}';
327-
ALTER TABLE vcons ALTER COLUMN vcon_version SET DEFAULT '0.3.0';
325+
ALTER TABLE vcons ADD COLUMN IF NOT EXISTS critical TEXT[];
326+
ALTER TABLE vcons ADD COLUMN IF NOT EXISTS amended JSONB DEFAULT '{}';
327+
ALTER TABLE vcons ALTER COLUMN vcon_version SET DEFAULT '0.4.0';
328328

329329
ALTER TABLE parties ADD COLUMN IF NOT EXISTS did TEXT;
330330
ALTER TABLE parties ADD COLUMN IF NOT EXISTS uuid UUID;

0 commit comments

Comments
 (0)