Modify the match_documents of the supabase vector store for RAG

Describe the problem/error/question

I would like to return more parameters than the one by default in the match_documents function proposed when setting up the supabase vector store (here)

What is the error message (if any)?

– Create a function to search for documents

Basically I want to do something like this:

create function match_documents (
query_embedding vector(1536),
match_count int default null,
filter jsonb DEFAULT ‘{}’
) returns table (
id bigint,
content text,
metadata jsonb,
additionalcolumnthatIwanttogiveaccessto TEXT, :bangbang:
similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
return query
select
id,
content,
metadata,
additionalcolumnthatIwanttogiveaccessto TEXT, :bangbang:
1 - (documents.embedding <=> query_embedding) as similarity
from documents
where metadata @> filter
order by documents.embedding <=> query_embedding
limit match_count;
end;
$$;

But the RAG agent does not seem to recognize additional output. Are the three existing outputs (id, content, metadata) hard wired, so we can’t add others?

Please share your workflow

Basic rag, nothing fancy:

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.81.0
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): cloud
  • Operating system: /

The n8n Supabase Vector Store node expects a specific structure for the match_documents function that matches LangChain’s document format. However, you can still access additional fields by:

  1. Add your fields to the metadata JSON:

sql

create function match_documents (
  query_embedding vector(1536),
  match_count int default null,
  filter jsonb DEFAULT '{}'
) returns table (
  id bigint,
  content text,
  metadata jsonb,
  similarity float
)
language plpgsql
as $
begin
  return query
  select
    id,
    content,
    jsonb_build_object(
      'original_metadata', metadata,
      'additional_column', additional_column_that_i_want
    ) as metadata,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$;
  1. Access in your agent prompt:

text

You can reference the additional data using:
{{$node["Supabase Vector Store"].json.documents[0].metadata.additional_column}}

This approach maintains compatibility with the node while giving you access to additional fields within the workflow.

3 Likes

got it, thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.