This adds the ID value of the row the document belongs to into the metadata so it is fetched as part of every element in the Document[] array. This is because, out of the box from LangChain. Document object only brings in:
pageContent: string;
metadata: Metadata;
I found myself needing to keep track of these records in the database for my needs and this change helped me avoid multiple trips to DB when using LangChain libraries.
The same change can be added to the kw_match_documents_2 function as well.
It would look like below with the escapes for the quotes as the whole script is in quotes.
jsonb_set(metadata,''{record_id}''::text[],(''"'' || id || ''"'')::jsonb, true) metadata,
This adds the ID value of the row the document belongs to into the metadata so it is fetched as part of every element in the Document[] array. This is because, out of the box from LangChain. Document object only brings in:
pageContent: string; metadata: Metadata;
I found myself needing to keep track of these records in the database for my needs and this change helped me avoid multiple trips to DB when using LangChain libraries.
The same change can be added to the kw_match_documents_2 function as well. It would look like below with the escapes for the quotes as the whole script is in quotes.
jsonb_set(metadata,''{record_id}''::text[],(''"'' || id || ''"'')::jsonb, true) metadata,