llama_index 如何更新RedisVectorStore?

2cmtqfgy  于 23天前  发布在  Redis
关注(0)|答案(4)|浏览(22)

问题验证

  • 我已经在文档和discord上寻找答案。

问题

假设我加载了一个向量存储,如下所示:
vector_store = RedisVectorStore(redis_client=redis_client, schema=index_schema, overwrite=False)
我如何删除所有具有元数据的文档并插入新的文档?
似乎我不能这样做:
existing_docs = vector_store.query(filter=MetadataFilters(filters=[ExactMatchFilter(key="merchant_id", value=merchant_id)]))
总是需要查询字符串。

txu3uszq

txu3uszq1#

要删除具有特定元数据的文档并在 RedisVectorStore 中插入新文档,可以按照以下步骤操作:

  1. 删除具有特定元数据的文档:
    使用 delete_nodes 方法根据元数据过滤器删除文档。
  2. 插入新文档:
    加载新文档并将其插入到 RedisVectorStore 中。
    以下是一个示例代码片段,用于实现此操作:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.vector_stores.redis import RedisVectorStore
from redis import Redis
from llama_index.core.vector_stores import MetadataFilters, MetadataFilter, FilterOperator

# Create a Redis client connection
redis_client = Redis.from_url("redis://localhost:6379")

# Create the vector store wrapper
vector_store = RedisVectorStore(redis_client=redis_client, overwrite=True)

# Load storage context
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# Define metadata filters
filters = MetadataFilters(
    filters=[
        MetadataFilter(key="merchant_id", value="specific_merchant_id", operator=FilterOperator.EQ),
        # Add more filters as needed
    ]
)

# Delete documents with specific metadata
vector_store.delete_nodes(filters=filters)

# Load new documents
documents = SimpleDirectoryReader("./path_to_new_documents").load_data()

# Insert new documents
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)

此代码片段演示了如何删除具有特定元数据的文档并将新文档插入到 RedisVectorStore 中。根据您的特定用例调整过滤器和路径。

  • 要继续与 Dosu 对话,请提及 @dosu。*
6za6bjd0

6za6bjd02#

@dosu delete_nodes not implemented
aemubtdh

aemubtdh3#

在未实现 delete_nodes 方法的情况下,要删除具有特定元数据的 RedisVectorStore 文档,可以使用带有适当过滤器的 delete 方法。以下是一个示例:

from llama_index.vector_stores.redis import RedisVectorStore
from llama_index.core.vector_stores import MetadataFilters, MetadataFilter, FilterOperator
from redis import Redis

Create a Redis client connection

redis_client = Redis.from_url("redis://localhost:6379")

Create the vector store wrapper

vector_store = RedisVectorStore(redis_client=redis_client, overwrite=False)

Define metadata filters

filters = MetadataFilters(
filters=[
MetadataFilter(key="merchant_id", value="specific_merchant_id", operator=FilterOperator.EQ),
# Add more filters as needed
]
)

Use the delete method with the filters

vector_store.delete(filters=filters)


此代码片段演示了如何删除元数据键 "merchant_id" 等于 "specific_merchant_id" 的文档。请根据您的具体条件调整 `MetadataFilter` 中的 `key` 、 `value` 和 `operator`。
如果 `delete` 方法不可用,您可能需要实现类似于 `RedisVectorStore` 示例中显示的 `delete` 方法的自定义方法:

def delete_documents_with_metadata(redis_store, filters):
filter_expression = redis_store._create_redis_filter_expression(filters)
total = redis_store._index.query(CountQuery(filter_expression))
delete_query = FilterQuery(
return_fields=[NODE_ID_FIELD_NAME],
filter_expression=filter_expression,
num_results=total,
)
docs_to_delete = redis_store._index.search(delete_query.query, delete_query.params)
with redis_store._index.client.pipeline(transaction=False) as pipe:
for doc in docs_to_delete.docs:
pipe.delete(doc.id)
pipe.execute()

Example usage

delete_documents_with_metadata(vector_store, filters)


此自定义方法检索与过滤器匹配的节点并通过它们的 ID 删除它们。

* 要继续与 [Dosu](https://dosu.dev) 对话,请提及 @dosu。*
4xrmg8kj

4xrmg8kj4#

有人需要贡献并实现delete_nodes(说实话,也需要get_nodes)

相关问题