You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched existing ideas and did not find a similar one
I added a very descriptive title
I've clearly described the feature request and motivation for it
Feature request
Add support for self-query with pgvector vector store. Currently, the SelfQueryRetriever retriever tool doesn't work with pgvector.
Motivation
When trying to integrate SelfQueryRetriever with pgvector there is no working BaseTranslator working. I have tried with different ones searching for compatibility but all lead to the same issue while building the database query to search for the documents.
The issue is in the metadata query build, while translating the structure query into a working filter.
Proposal (If applicable)
I have wrote a class PGVectorTranslator extending from BaseTranslator that worked for my use case. With this, I managed to get my LLM to query the pgvector with dynamic metadata filters extracted and built from the query.
import{castValue,Comparator,Comparators,Comparison,isFilterEmpty,Operator,Operators,StructuredQuery,}from'langchain/chains/query_constructor/ir';import{BaseTranslator}from'langchain/retrievers/self_query';exporttypeFilter={[k: string]: string|number};exportclassPGVectorTranslatorextendsBaseTranslator{declareVisitOperationOutput: Filter;declareVisitComparisonOutput: Filter;declareVisitStructuredQueryOutput: {filter: Filter}|{[k: string]: never};allowedOperators: Operator[]=[Operators.and,Operators.or];allowedComparators: Comparator[]=[Comparators.eq,Comparators.ne,Comparators.gt,Comparators.gte,Comparators.lt,Comparators.lte,];formatFunction(): string{thrownewError('Not implemented');}/** * Not required. */getComparatorFunction(){returnnull;}/** * Not required. */getOperatorFunction(){returnnull;}/** * Not required. */visitOperation(){return{};}/** * Visits the comparison part of a structured query and translates it into * a functional filter. * @param comparison The comparison part of a structured query. * @returns A function that takes a `Document` as an argument and returns a boolean based on the comparison. */visitComparison(comparison: Comparison): this['VisitComparisonOutput']{const{ comparator, attribute, value }=comparison;if(this.allowedComparators.includes(comparator)){return{[attribute]: castValue(value)};}else{thrownewError('Comparator not allowed');}}/** * Visits a structured query and translates it into a functional filter. * @param query The structured query to translate. * @returns An object containing a `filter` property, which is a function that takes a `Document` as an argument and returns a boolean based on the structured query. */visitStructuredQuery(query: StructuredQuery): this['VisitStructuredQueryOutput']{if(!query.filter){return{};}constfilterFunction=query.filter?.accept(this);return{filter: filterFunctionasFilter};}/** * Merges two filters into one, based on the specified merge type. * @param defaultFilter The default filter function. * @param generatedFilter The generated filter function. * @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'. Default is 'and'. * @returns A function that takes a `Document` as an argument and returns a boolean based on the merged filters, or `undefined` if both filters are empty. */mergeFilters(defaultFilter: Filter,generatedFilter: Filter,mergeType='and',forceDefaultFilter?: boolean,): Filter{if(isFilterEmpty(defaultFilter)&&isFilterEmpty(generatedFilter)){return{};}if(isFilterEmpty(defaultFilter)||mergeType==='replace'){if(isFilterEmpty(generatedFilter)){return{};}returngeneratedFilter;}if(isFilterEmpty(generatedFilter)){if(mergeType==='and'){return{};}returndefaultFilter;}if(forceDefaultFilter){returndefaultFilter;}if(mergeType==='and'){return{
...defaultFilter,
...generatedFilter,};}else{thrownewError('Unknown merge type');}}}
I believe this works as a baseline for the feature, however, I'm not sure if this implementation makes sense, giving the scope of a Translator class. In case this make sense, I can improve and finish it to submit it as a pull request if needed!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked
Feature request
Add support for self-query with pgvector vector store. Currently, the
SelfQueryRetriever
retriever tool doesn't work with pgvector.Motivation
When trying to integrate
SelfQueryRetriever
withpgvector
there is no workingBaseTranslator
working. I have tried with different ones searching for compatibility but all lead to the same issue while building the database query to search for the documents.The issue is in the metadata query build, while translating the structure query into a working filter.
Proposal (If applicable)
I have wrote a class
PGVectorTranslator
extending fromBaseTranslator
that worked for my use case. With this, I managed to get my LLM to query the pgvector with dynamic metadata filters extracted and built from the query.I believe this works as a baseline for the feature, however, I'm not sure if this implementation makes sense, giving the scope of a
Translator
class. In case this make sense, I can improve and finish it to submit it as a pull request if needed!Beta Was this translation helpful? Give feedback.
All reactions