llama_index [Bug]:文本匹配过滤器在PostgreSQL过滤搜索中不起作用,并给出未知操作符错误,

vqlkdk9b  于 23天前  发布在  PostgreSQL
关注(0)|答案(2)|浏览(26)

Bug描述

我有一个带有Llamaindex的PostgreSQL数据库,并在其中进行过滤搜索。许多操作符都可以正常工作,但文本匹配过滤器无法正常工作。我使用的是Llamaindex的最新版本。包括文本匹配在内的过滤操作符列表如下:https://docs.llamaindex.ai/en/v0.10.17/api/llama_index.core.vector_stores.FilterOperator.html

版本

llama-index-0.10.45

重现步骤

我的代码:

filters = MetadataFilters(
            filters=[
                MetadataFilter(key="title", 
                operator=FilterOperator.TEXT_MATCH,        
                value="Graph RAG")
            ]
        )

或者:
operator="text_match"
其余代码基本上类似于https://docs.llamaindex.ai/en/stable/examples/vector_stores/postgres/

相关日志/回溯

It gives this error: 
 operator: FilterOperator.TEXT_MATCH, fallback to '='
mkshixfv

mkshixfv1#

嘿 @BurnyCoder! 我来帮助你解决仓库中的任何错误、问题或贡献。让我们一起解决你的问题。
sql_operator_mapper 字典中,TEXT_MATCH 过滤器操作符Map到 SQL 的 LIKE 操作符。如果 TEXT_MATCH 过滤器操作符无法识别并回退到默认的 = 操作符,可能是由于代码中 FilterOperator 的使用或引用存在问题。
确保 FilterOperator.TEXT_MATCH 在你的过滤搜索逻辑中正确导入和使用。以下是相关的代码片段:

from llama_index.core.vector_stores.types import (
    FilterOperator,
)

sql_operator_mapper = {
    FilterOperator.EQ: " = ",
    FilterOperator.GT: " > ",
    FilterOperator.GTE: " >= ",
    FilterOperator.LTE: " <= ",
    FilterOperator.TEXT_MATCH: " LIKE ",
    FilterOperator.NE: " NOT LIKE ",
    FilterOperator.IN: " IN ",
}

此外,PGVectorStore 类中的 _to_postgres_operator 方法负责将 FilterOperator 值转换为相应的 SQL 操作符。请确保此方法包括 FilterOperator.TEXT_MATCH 的情况:

def _to_postgres_operator(self, operator: FilterOperator) -> str:
    if operator == FilterOperator.EQ:
        return "="
    elif operator == FilterOperator.GT:
        return ">"
    elif operator == FilterOperator.LT:
        return "<"
    elif operator == FilterOperator.NE:
        return "!="
    elif operator == FilterOperator.GTE:
        return ">="
    elif operator == FilterOperator.LTE:
        return "<="
    elif operator == FilterOperator.IN:
        return "IN"
    elif operator == FilterOperator.NIN:
        return "NOT IN"
    elif operator == FilterOperator.CONTAINS:
        return "@>"
    elif operator == FilterOperator.TEXT_MATCH:
        return "LIKE"
    else:
        _logger.warning(f"Unknown operator: {operator}, fallback to '='")
        return "="

确保你的过滤搜索逻辑正确引用 FilterOperator.TEXT_MATCH 并使用 sql_operator_mapper 字典将其Map到 LIKE 操作符。如果问题仍然存在,可能还有其他地方的代码中处理或应用过滤器操作符时出现问题。
此外,你可以参考 llama-index-integrations/vector_stores/llama-index-vector-stores-postgres/tests/test_postgres.py 文件中的测试用例,以确保你的实现与预期行为一致。测试用例涵盖了各种场景,包括使用不同的过滤器操作符,可以帮助你发现代码中的任何差异 1(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/vector_stores/llama-index-vector-stores-lancedb/llama_index/vector_stores/lancedb/util.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/vector_stores/llama-index-vector-stores-postgres/llama_index/vector_stores/postgres/base.py)3(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/vector_stores/llama-index-vector-stores-postgres/tests/test_postgres.py)。

  • 要继续与 dosu 交流,请提及 @dosu。*
eiee3dmh

eiee3dmh2#

该过滤器类型目前尚未实现,有人应该提交PR来添加它。

相关问题