-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-database.ts
More file actions
65 lines (58 loc) · 1.6 KB
/
Copy pathsetup-database.ts
File metadata and controls
65 lines (58 loc) · 1.6 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
import 'dotenv/config'
import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import pg from 'pg'
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
async function main() {
console.log('Setting up database functions...')
await prisma.$executeRawUnsafe(`
CREATE OR REPLACE FUNCTION match_chunks(
query_embedding vector(1536),
match_project_id uuid,
match_threshold float DEFAULT 0.7,
match_count int DEFAULT 5
)
RETURNS TABLE (
id uuid,
content text,
metadata jsonb,
similarity float,
source_id uuid
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
chunks.id,
chunks.content,
chunks.metadata,
(1 - (chunks.embedding <=> query_embedding))::float as similarity,
chunks.source_id
FROM chunks
WHERE chunks.project_id = match_project_id
AND chunks.embedding IS NOT NULL
AND 1 - (chunks.embedding <=> query_embedding) > match_threshold
ORDER BY chunks.embedding <=> query_embedding
LIMIT match_count;
END;
$$;
`)
console.log('Created match_chunks function')
await prisma.$executeRawUnsafe(`
CREATE INDEX IF NOT EXISTS chunks_embedding_idx
ON chunks
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);
`)
console.log('Created vector index')
console.log('Database setup complete!')
}
main()
.catch(console.error)
.finally(() => {
pool.end()
process.exit(0)
})