const { createClient } = require('@supabase/supabase-js'); const fs = require('fs'); // Load environment variables require('dotenv').config(); const supabaseUrl = process.env.SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_SERVICE_KEY; const supabase = createClient(supabaseUrl, supabaseServiceKey); async function tryCreateFunction() { console.log('๐Ÿš€ Attempting to create vector search function...'); const functionSQL = ` CREATE OR REPLACE FUNCTION match_document_chunks( query_embedding VECTOR(1536), match_threshold FLOAT DEFAULT 0.7, match_count INTEGER DEFAULT 10 ) RETURNS TABLE ( id UUID, document_id TEXT, content TEXT, metadata JSONB, chunk_index INTEGER, similarity FLOAT ) LANGUAGE SQL STABLE AS $$ SELECT document_chunks.id, document_chunks.document_id, document_chunks.content, document_chunks.metadata, document_chunks.chunk_index, 1 - (document_chunks.embedding <=> query_embedding) AS similarity FROM document_chunks WHERE document_chunks.embedding IS NOT NULL AND 1 - (document_chunks.embedding <=> query_embedding) > match_threshold ORDER BY document_chunks.embedding <=> query_embedding LIMIT match_count; $$;`; // Try direct SQL execution try { const { data, error } = await supabase.rpc('query', { query: functionSQL }); if (error) { console.log('โŒ Direct query failed:', error.message); } else { console.log('โœ… Function created via direct query!'); } } catch (e) { console.log('โŒ Direct query method not available'); } // Alternative: Try creating via Edge Functions (if available) try { const response = await fetch(`${supabaseUrl}/rest/v1/rpc/sql`, { method: 'POST', headers: { 'apikey': supabaseServiceKey, 'Authorization': `Bearer ${supabaseServiceKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: functionSQL }) }); if (response.ok) { console.log('โœ… Function created via REST API!'); } else { console.log('โŒ REST API method failed:', response.status); } } catch (e) { console.log('โŒ REST API method not available'); } // Test if function exists now console.log('๐Ÿงช Testing if function exists...'); const testEmbedding = new Array(1536).fill(0.1); const { data, error } = await supabase.rpc('match_document_chunks', { query_embedding: testEmbedding, match_threshold: 0.5, match_count: 5 }); if (error) { console.log('โŒ Function still not available:', error.message); console.log(''); console.log('๐Ÿ“‹ Manual steps required:'); console.log('1. Go to https://supabase.com/dashboard/project/gzoclmbqmgmpuhufbnhy/sql'); console.log('2. Run the SQL from vector_function.sql'); console.log('3. Then test with: node test-vector-search.js'); } else { console.log('โœ… Function is working!'); console.log('Found', data ? data.length : 0, 'results'); } } tryCreateFunction();