File size: 1,168 Bytes
065bc2a
 
 
7d88664
 
 
 
 
 
 
 
065bc2a
7d88664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- Drop old function
drop function if exists match_documents (vector(1536), int);

-- Create a table to store your documents
    create table documents (
        id bigserial primary key,
        content text, -- corresponds to Document.pageContent
        metadata jsonb, -- corresponds to Document.metadata
        embedding vector(768) -- 768 works for Gemini embeddings, change if needed
    );

-- Create a function to search for documents
    create function match_documents (
            query_embedding vector(768),
            match_count int DEFAULT null,
            filter jsonb DEFAULT '{}'
        ) returns table (
            id bigint,
            content text,
            metadata jsonb,
            similarity float
        )
        language plpgsql
        as $$
        #variable_conflict use_column
        begin
        return query
        select
            id,
            content,
            metadata,
            1 - (documents.embedding <=> query_embedding) as similarity
        from documents
        where metadata @> filter
        order by documents.embedding <=> query_embedding
        limit match_count;
        end;
        $$;