-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
421 lines (393 loc) · 11.5 KB
/
supabase_schema.sql
File metadata and controls
421 lines (393 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
-- LLMCostGuide Database Schema for Supabase
-- Run this SQL in your Supabase SQL Editor to set up the database
-- Create llm_models table
CREATE TABLE llm_models (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
model_name VARCHAR(255) NOT NULL,
provider VARCHAR(100) NOT NULL,
context_limit INTEGER NOT NULL,
input_price_per_1m_tokens DECIMAL(10,6) NOT NULL DEFAULT 0,
output_price_per_1m_tokens DECIMAL(10,6) NOT NULL DEFAULT 0,
caching_price_per_1m_tokens DECIMAL(10,6),
model_type VARCHAR(50) NOT NULL DEFAULT 'Text',
added_on TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_on TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
external_model_id VARCHAR(255) UNIQUE,
context_window VARCHAR(50),
description TEXT,
is_active BOOLEAN DEFAULT true
);
-- Create data_sync_logs table
CREATE TABLE data_sync_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
provider VARCHAR(100) NOT NULL,
sync_type VARCHAR(50) NOT NULL,
records_added INTEGER DEFAULT 0,
records_updated INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'completed',
error_message TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for better performance
CREATE INDEX idx_llm_models_provider ON llm_models(provider);
CREATE INDEX idx_llm_models_active ON llm_models(is_active);
CREATE INDEX idx_llm_models_model_type ON llm_models(model_type);
CREATE INDEX idx_llm_models_name ON llm_models(model_name);
CREATE INDEX idx_data_sync_logs_created_at ON data_sync_logs(created_at DESC);
-- Create function to update updated_on timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_on = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
-- Create trigger for llm_models
CREATE TRIGGER update_llm_models_updated_at
BEFORE UPDATE ON llm_models
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
-- Insert sample data
INSERT INTO llm_models (
model_name,
provider,
context_limit,
input_price_per_1m_tokens,
output_price_per_1m_tokens,
caching_price_per_1m_tokens,
model_type,
external_model_id,
context_window,
description
) VALUES
(
'GPT-4 Turbo',
'OpenRouter',
128000,
0.01,
0.03,
0.005,
'Text',
'openai/gpt-4-turbo',
'128K',
'Latest GPT-4 Turbo model with improved performance and longer context'
),
(
'Claude 3 Opus',
'OpenRouter',
200000,
0.015,
0.075,
0.003,
'Text',
'anthropic/claude-3-opus',
'200K',
'Anthropic''s most capable model for complex reasoning tasks'
),
(
'LLaMA 2 70B',
'TogetherAI',
4096,
0.0007,
0.0009,
NULL,
'Text',
'togethercomputer/llama-2-70b',
'4K',
'Meta''s LLaMA 2 70B model hosted on Together AI'
),
(
'DALL-E 3',
'OpenRouter',
4000,
0.04,
0.12,
0.01,
'Images',
'openai/dall-e-3',
'4K',
'OpenAI''s latest image generation model'
),
(
'Mistral 7B Instruct',
'TogetherAI',
32768,
0.0002,
0.0002,
NULL,
'Text',
'mistralai/mistral-7b-instruct',
'32K',
'Mistral AI''s 7B parameter instruction-tuned model'
),
(
'Gemini Pro',
'OpenRouter',
30720,
0.00025,
0.0005,
NULL,
'Text',
'google/gemini-pro',
'32K',
'Google''s Gemini Pro model for text generation'
),
(
'Code Llama 34B',
'TogetherAI',
16384,
0.0003,
0.0004,
NULL,
'Text',
'codellama/CodeLlama-34b-Instruct',
'16K',
'Meta''s Code Llama for code generation and completion'
),
(
'Whisper Large',
'OpenRouter',
48000,
0.006,
0.006,
NULL,
'Audio',
'openai/whisper-large-v2',
'48K',
'OpenAI''s Whisper for speech-to-text transcription'
);
-- Insert sample sync log
INSERT INTO data_sync_logs (
provider,
sync_type,
records_added,
records_updated,
status
) VALUES
('manual', 'sample_data_import', 8, 0, 'completed');
-- Create RLS policies (Row Level Security)
-- Enable RLS on tables
ALTER TABLE llm_models ENABLE ROW LEVEL SECURITY;
ALTER TABLE data_sync_logs ENABLE ROW LEVEL SECURITY;
-- Policy to allow public read access to active models
CREATE POLICY "Allow public read access to active models"
ON llm_models FOR SELECT
USING (is_active = true);
-- Policy to allow all operations for service role (for admin functions)
CREATE POLICY "Allow all operations for service role"
ON llm_models FOR ALL
USING (auth.role() = 'service_role');
-- Policy to allow all operations for data_sync_logs
CREATE POLICY "Allow all operations for data_sync_logs"
ON data_sync_logs FOR ALL
USING (auth.role() = 'service_role');
-- Grant permissions
-- Note: In production, you might want to be more restrictive
GRANT SELECT ON llm_models TO anon, authenticated;
GRANT ALL ON llm_models TO service_role;
GRANT ALL ON data_sync_logs TO service_role;
-- Create a view for easy access to active models
CREATE OR REPLACE VIEW active_models AS
SELECT
id,
model_name,
provider,ß
context_limit,
input_price_per_1m_tokens,
output_price_per_1m_tokens,
caching_price_per_1m_tokens,
model_type,
added_on,
context_window,
description,
external_model_id
FROM llm_models
WHERE is_active = true
ORDER BY model_name;
-- Grant select on view
GRANT SELECT ON active_models TO anon, authenticated;
-- Create function to get models with filters
CREATE OR REPLACE FUNCTION get_models(
provider_filter TEXT DEFAULT NULL,
model_type_filter TEXT DEFAULT NULL,
search_query TEXT DEFAULT NULL,
sort_by TEXT DEFAULT 'model_name',
sort_order TEXT DEFAULT 'ASC',
limit_count INTEGER DEFAULT 50,
offset_count INTEGER DEFAULT 0
)
RETURNS TABLE (
id UUID,
model_name VARCHAR(255),
provider VARCHAR(100),
context_limit INTEGER,
input_price_per_1m_tokens DECIMAL(10,6),
output_price_per_1m_tokens DECIMAL(10,6),
caching_price_per_1m_tokens DECIMAL(10,6),
model_type VARCHAR(50),
added_on TIMESTAMP WITH TIME ZONE,
context_window VARCHAR(50),
description TEXT,
external_model_id VARCHAR(255)
) AS $$
BEGIN
RETURN QUERY
SELECT
m.id,
m.model_name,
m.provider,
m.context_limit,
m.input_price_per_1m_tokens,
m.output_price_per_1m_tokens,
m.caching_price_per_1m_tokens,
m.model_type,
m.added_on,
m.context_window,
m.description,
m.external_model_id
FROM llm_models m
WHERE
m.is_active = TRUE
AND (provider_filter IS NULL OR m.provider = provider_filter)
AND (model_type_filter IS NULL OR m.model_type = model_type_filter)
AND (
search_query IS NULL
OR m.model_name ILIKE '%' || search_query || '%'
OR m.provider ILIKE '%' || search_query || '%'
OR m.description ILIKE '%' || search_query || '%'
)
ORDER BY
-- Dynamic sorting column
CASE WHEN sort_by = 'model_name' THEN m.model_name END,
CASE WHEN sort_by = 'provider' THEN m.provider END,
CASE WHEN sort_by = 'input_price_per_1m_tokens' THEN m.input_price_per_1m_tokens END,
CASE WHEN sort_by = 'output_price_per_1m_tokens' THEN m.output_price_per_1m_tokens END,
CASE WHEN sort_by = 'context_limit' THEN m.context_limit END,
CASE WHEN sort_by = 'added_on' THEN m.added_on END,
-- Dynamic sort direction (DESC branches)
CASE WHEN sort_by = 'model_name' AND sort_order = 'DESC' THEN m.model_name END DESC,
CASE WHEN sort_by = 'provider' AND sort_order = 'DESC' THEN m.provider END DESC,
CASE WHEN sort_by = 'input_price_per_1m_tokens' AND sort_order = 'DESC' THEN m.input_price_per_1m_tokens END DESC,
CASE WHEN sort_by = 'output_price_per_1m_tokens' AND sort_order = 'DESC' THEN m.output_price_per_1m_tokens END DESC,
CASE WHEN sort_by = 'context_limit' AND sort_order = 'DESC' THEN m.context_limit END DESC,
CASE WHEN sort_by = 'added_on' AND sort_order = 'DESC' THEN m.added_on END DESC,
-- Default sort if unknown column
m.model_name -- fallback
LIMIT limit_count
OFFSET offset_count;
END;
$$ LANGUAGE plpgsql;
-- Grant execute on function
GRANT EXECUTE ON FUNCTION get_models TO anon, authenticated;
-- Create function to get model count with filters
CREATE OR REPLACE FUNCTION get_models_count(
provider_filter TEXT DEFAULT NULL,
model_type_filter TEXT DEFAULT NULL,
search_query TEXT DEFAULT NULL
)
RETURNS INTEGER AS $$
DECLARE
model_count INTEGER;
BEGIN
SELECT COUNT(*) INTO model_count
FROM llm_models
WHERE
is_active = true
AND (provider_filter IS NULL OR provider = provider_filter)
AND (model_type_filter IS NULL OR model_type = model_type_filter)
AND (search_query IS NULL OR
model_name ILIKE '%' || search_query || '%' OR
provider ILIKE '%' || search_query || '%' OR
description ILIKE '%' || search_query || '%');
RETURN model_count;
END;
$$ LANGUAGE plpgsql;
-- Grant execute on function
GRANT EXECUTE ON FUNCTION get_models_count TO anon, authenticated;
-- Create function to bulk import models
CREATE OR REPLACE FUNCTION import_models(models_data JSONB)
RETURNS JSONB AS $$
DECLARE
imported_count INTEGER := 0;
updated_count INTEGER := 0;
model_record JSONB;
result JSONB;
BEGIN
-- Process each model in the JSON array
FOR model_record IN SELECT * FROM jsonb_array_elements(models_data)
LOOP
INSERT INTO llm_models (
model_name,
provider,
context_limit,
input_price_per_1M_tokens,
output_price_per_1M_tokens,
caching_price_per_1M_tokens,
model_type,
external_model_id,
context_window,
description,
added_on,
updated_on,
is_active
) VALUES (
(model_record->>'model_name')::VARCHAR(255),
(model_record->>'provider')::VARCHAR(100),
(model_record->>'context_limit')::INTEGER,
(model_record->>'input_price_per_1m_tokens')::DECIMAL(10,6),
(model_record->>'output_price_per_1m_tokens')::DECIMAL(10,6),
CASE
WHEN model_record->>'caching_price_per_1m_tokens' IS NOT NULL
THEN (model_record->>'caching_price_per_1m_tokens')::DECIMAL(10,6)
ELSE NULL
END,
(model_record->>'model_type')::VARCHAR(50),
(model_record->>'external_model_id')::VARCHAR(255),
(model_record->>'context_window')::VARCHAR(50),
(model_record->>'description')::TEXT,
COALESCE((model_record->>'added_on')::TIMESTAMP WITH TIME ZONE, NOW()),
NOW(),
COALESCE((model_record->>'is_active')::BOOLEAN, true)
)
ON CONFLICT (external_model_id) DO UPDATE SET
model_name = EXCLUDED.model_name,
provider = EXCLUDED.provider,
context_limit = EXCLUDED.context_limit,
input_price_per_1m_tokens = EXCLUDED.input_price_per_1m_tokens,
output_price_per_1m_tokens = EXCLUDED.output_price_per_1m_tokens,
caching_price_per_1m_tokens = EXCLUDED.caching_price_per_1m_tokens,
model_type = EXCLUDED.model_type,
context_window = EXCLUDED.context_window,
description = EXCLUDED.description,
updated_on = NOW(),
is_active = EXCLUDED.is_active;
imported_count := imported_count + 1;
END LOOP;
-- Log the import operation
INSERT INTO data_sync_logs (
provider,
sync_type,
records_added,
records_updated,
status
) VALUES (
'manual',
'csv_import',
imported_count,
updated_count,
'completed'
);
-- Return result
result := jsonb_build_object(
'success', true,
'imported_count', imported_count,
'updated_count', updated_count
);
RETURN result;
END;
$$ LANGUAGE plpgsql;
-- Grant execute on function
GRANT EXECUTE ON FUNCTION import_models TO service_role;
-- Success message
SELECT 'LLMCostGuide database setup completed successfully!' AS status;
SELECT 'Tables created: llm_models, data_sync_logs' AS tables;
SELECT 'Sample data inserted. You can now use the application!' AS message;