Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set owner for document while embedding and querying (Supabase) #7

Open
anhhtca opened this issue Jul 13, 2023 · 0 comments
Open

Set owner for document while embedding and querying (Supabase) #7

anhhtca opened this issue Jul 13, 2023 · 0 comments

Comments

@anhhtca
Copy link

anhhtca commented Jul 13, 2023

Hello,

This is not an issue, just a question need help:

I added owner_sid to the documents table to determine the ownership of documents by users and only allow users with sid (uuid) to query their own documents. Here is the SQL snippet I have customized. I am unsure how to pass the user sid when 'embedding' and 'querying' though.

-- Enable the pgvector extension to work with embedding vectors
create extension vector;

-- 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(1536), -- 1536 works for OpenAI embeddings, change if needed
  -- customize
  owner_sid uuid not null -- only users can query their documents
);

-- Create a function to search for documents
create function match_documents (
  query_embedding vector(1536),
  match_count int,
  -- owner
  document_owner uuid
) 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,
    -- owner
    owner_sid
  from documents
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;

-- Create an index to be used by the search function
create index on documents
  using ivfflat (embedding vector_cosine_ops)
  with (lists = 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant