向量库集成

默认 VectorStoreIndex 把向量放在 内存 SimpleVectorStore 里,适合教程。生产需要:进程重启不丢、可并发、能过滤 metadata、可水平扩展。做法是安装对应集成包,构造官方的 *VectorStore,放进 StorageContext,再 from_documents

统一插头:

pip install llama-index-vector-stores-<name>
→ 厂商客户端 + LlamaIndex 包装类
→ StorageContext.from_defaults(vector_store=...)
→ VectorStoreIndex.from_documents(..., storage_context=...)
→ 之后 VectorStoreIndex.from_vector_store(vector_store)

集成清单:Using Vector StoresStoring。构造函数参数以各 store 文档为准。


Chroma(本地文件、上手最快)

包名概念:llama-index-vector-stores-chroma(同时需要 chromadb)。

import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.vector_stores.chroma import ChromaVectorStore

documents = SimpleDirectoryReader("./data").load_data()
db = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = db.get_or_create_collection("quickstart")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
query_engine = index.as_query_engine()
print(query_engine.query("What is the meaning of life?"))

再次启动 不要 from_documents(会重复 embedding),改为:

db = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = db.get_or_create_collection("quickstart")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
index = VectorStoreIndex.from_vector_store(vector_store)

适合单机、演示、中小语料。多副本写入同一目录前先看 Chroma 的并发说明。


pgvector(PostgreSQL)

包名概念:llama-index-vector-stores-postgres。数据库需启用 pgvector。Embedding 维度必须与模型一致(许多 OpenAI 模型是 1536;换模型就改 embed_dim 并重建表)。

from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.vector_stores.postgres import PGVectorStore

vector_store = PGVectorStore.from_params(
    database="vector_db",
    host="localhost",
    password="password",
    port=5432,
    user="postgres",
    table_name="handbook_chunks",
    embed_dim=1536,
    hnsw_kwargs={
        "hnsw_m": 16,
        "hnsw_ef_construction": 64,
        "hnsw_ef_search": 40,
        "hnsw_dist_method": "vector_cosine_ops",
    },
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)

适合已有 Postgres、要和业务数据同库备份、用 SQL 做权限与审计。连接串、SSL、schema 名见 Postgres 集成页。本站 PostgreSQL 课管数据库本身,不管 LlamaIndex 包装类。


Qdrant

包名概念:llama-index-vector-stores-qdrant(另装 qdrant-client)。可本地路径、Docker,或 Qdrant Cloud。

from qdrant_client import QdrantClient
from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.vector_stores.qdrant import QdrantVectorStore

client = QdrantClient(url="http://localhost:6333")
# 本地嵌入式也可以:QdrantClient(path="./qdrant_storage")
vector_store = QdrantVectorStore(client=client, collection_name="handbook")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)

Qdrant 同时存文档与向量,过滤与 hybrid 能力较强;enable_hybrid 等开关以当前集成文档为准。加载:VectorStoreIndex.from_vector_store(vector_store)


选型与注意

需求倾向
本机试跑、无运维Chroma PersistentClient
已有 Postgres / 要 SQL 事务pgvector
独立向量服务、过滤与 hybridQdrant(或官方列表里的 Pinecone、Weaviate 等)
教程、单测默认 SimpleVectorStore + persist_dir

同一 embedding 模型 贯穿写入与查询。换模型 = 新 collection / 新表 + 全量重建。外接向量库后,docstore 有时仍要 persist;若官方示例只 from_vector_store,说明该后端已存文本,按该页复制即可。

不要把 API Key、数据库密码写进仓库。连接参数用环境变量。向量库不是权限系统:租户隔离靠 分 collectionmetadata filter,并在应用层强制。


下一步

评论