假设已知目标文章的节点 ID 列表
target_node_ids = [“node_id_1”, “node_id_2”, “node_id_3”]
执行删除
vector_store.delete(node_ids=target_node_ids)
重新构建索引
new_index = VectorStoreIndex.from_vector_store(vector_store)
知道document_id 删除 chroma 的信息
import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
# -------------------------- 1. 初始化Chroma客户端和向量存储 --------------------------
# 本地Chroma(持久化),若为远程Chroma替换为 chromadb.HttpClient
chroma_client = chromadb.PersistentClient(path="./chroma_db")
# 替换为你的集合名(创建向量库时的名称)
chroma_collection = chroma_client.get_or_create_collection("your_collection_name")
# 关联LlamaIndex的Chroma向量存储
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# -------------------------- 2. 根据document_id删除对应向量 --------------------------
def delete_vector_by_document_id(target_document_id: str):
"""
根据document_id删除Chroma中的对应向量
:param target_document_id: 要删除的文档ID
"""
# 步骤1:查询Chroma中所有包含该document_id的向量(元数据过滤)
# metadata={"document_id": target_document_id} 精准匹配
results = chroma_collection.get(
where={"document_id": target_document_id}, # 按document_id过滤
include=["ids"] # 只返回向量ID,提升效率
)
# 步骤2:提取匹配的向量ID列表
vector_ids = results["ids"]
if not vector_ids:
print(f"未找到document_id为 {target_document_id} 的向量")
return
# 步骤3:删除对应向量
chroma_collection.delete(ids=vector_ids)
print(f"成功删除document_id={target_document_id} 对应的 {len(vector_ids)} 个向量")
# -------------------------- 3. 调用删除函数 --------------------------
# 替换为你要删除的document_id
delete_vector_by_document_id("your_target_document_id")