Vector Stores

The default VectorStoreIndex keeps vectors in an in-memory SimpleVectorStore—fine for tutorials. Production needs survival across restarts, concurrency, metadata filters, and scale. Install the integration, wrap the vendor client, put it on StorageContext, then from_documents.

The plug looks the same every time:

pip install llama-index-vector-stores-<name>
→ vendor client + LlamaIndex wrapper
→ StorageContext.from_defaults(vector_store=...)
→ VectorStoreIndex.from_documents(..., storage_context=...)
→ later VectorStoreIndex.from_vector_store(vector_store)

Catalog: Using Vector Stores and Storing. Constructor kwargs come from each store’s page.


Chroma (local files, fastest start)

Package name, conceptually: llama-index-vector-stores-chroma (plus 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?"))

On the next process, do not call from_documents (that re-embeds). Load:

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)

Good for laptops, demos, and modest corpora. Read Chroma’s concurrency notes before several replicas write the same directory.


pgvector (PostgreSQL)

Package name, conceptually: llama-index-vector-stores-postgres. The database needs pgvector. embed_dim must match the model (1536 for many OpenAI models). Change the model → change embed_dim and rebuild.

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)

Fits teams that already run Postgres, want backups next to business data, and SQL for ACLs/audit. Connection strings, SSL, and schema: Postgres integration. This site’s PostgreSQL course covers the database, not the LlamaIndex wrapper.


Qdrant

Package name, conceptually: llama-index-vector-stores-qdrant (plus qdrant-client). Local path, Docker, or 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")
# embedded: 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 stores documents and vectors together; filters and hybrid search are strong. Flags such as enable_hybrid belong in the current integration doc. Reload with VectorStoreIndex.from_vector_store(vector_store).


Choosing a store

NeedLean toward
Laptop, no opsChroma PersistentClient
Existing Postgres / SQL transactionspgvector
Dedicated vector service, filters, hybridQdrant (or Pinecone, Weaviate, … on the official list)
Tutorials and unit testsDefault SimpleVectorStore + persist_dir

Use the same embedding model for writes and queries. A new model means a new collection/table and a full rebuild. After an external store, you may still persist the docstore; if the official sample only calls from_vector_store, that backend already holds text—copy that page.

Keep API keys and DB passwords out of git. Vector DBs are not ACLs: isolate tenants with separate collections or metadata filters, enforced in your app.


Next steps

评论