chroma删除节点信息

假设已知目标文章的节点 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")

PDF扫描文件加入RAG

1 PDF分页为PNG输出 pdf2image

from pdf2image import convert_from_path
pages = convert_from_path(pdf_path, dpi=200, fmt='png')  # 逐页 PIL.Image
for i, page_img in enumerate(pages, 1):
        # 图片 → base64(不落地磁盘)
        import base64, io
        buffer = io.BytesIO()
        page_img.save(buffer, format='PNG')
        img_b64 = base64.b64encode(buffer.getvalue()).decode()

2调用OCR程序 ,可以使用ollama 直接将图片转为text 然后逐步添加为一个文本变量 添加文件信息的metadata

import ollama  # 需安装:pip install ollama
response = ollama.chat( model=model, messages=[ { "role": "user", "content": [ # 文本指令 {"type": "text", "text": "请识别这张图片中的所有文字,按原格式输出,不要解释。"}, # Base64 图片(type 需匹配格式:image/png 或 image/jpeg) {"type": "image/png", "data": img_b64} ] } ] )

3直接是将文本变量切分 然后向量化

RAG原理

MVP 小规模 1万文档
SQlite 存储node信息,查询后实时BM25再关键词召回
chroma向量召回

大规模企业 10万文档以下
PostgreSQL  节点+关键词;直接关键词召回
(使用 PostgreSQL 全文检索(tsvector,适合英文/分词后中文)
直接SQL语句 需要提前使用jieba分词)
把 PostgreSQL检索封装成 BaseRetriever
Milvus向量召回