BM25 +jieba +查询:https://el.psy.congroo.com/archives/2327
向量化(内存)+查询
混合查询:BM25+(检索器+节点后处理+响应器(溯源))
chroma向量存储+sqlite节点存储(动态BM25) (增删)
BM25 +jieba +查询:https://el.psy.congroo.com/archives/2327
向量化(内存)+查询
混合查询:BM25+(检索器+节点后处理+响应器(溯源))
chroma向量存储+sqlite节点存储(动态BM25) (增删)
需要安装 python-docx
from llama_index.core import SimpleDirectoryReader
from llama_index.core.node_parser import SentenceSplitter
from llama_index.retrievers import bm25
documents=SimpleDirectoryReader("data").load_data()
print(documents)
splitter=SentenceSplitter(chunk_size=512,chunk_overlap=30)
new_nodes=splitter.get_nodes_from_documents(documents)
print(new_nodes)
documents=SimpleDirectoryReader(input_files=["add.docx",]).load_data()
#input_files接受的是一个文件目录
print(documents)
documents[0].metadata['MAC']='adgfa-192ga'
#添加metadata
documents[0].metadata['document_id']=documents[0].id_
#默认没有documenta_id 需要自己添加
print(documents)
new_nodes=splitter.get_nodes_from_documents(documents)
print(new_nodes)
llama-index-readers-file
专用用于读取图片类文档
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)
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")
— 匹配 metadata 中 category=技术文档 AND author=张三
SELECT * FROM vector_store
WHERE json_extract(metadata, ‘$.category’) = ‘技术文档’
AND json_extract(metadata, ‘$.author’) = ‘张三’;