跳转到内容

Chroma向量数据库

Chroma 是一个专门针对大语言模型(LLM)应用设计的开源、轻量级嵌入向量数据库。它支持快速存储文档、元数据以及向量嵌入(Embeddings),并提供高效的近邻语义检索服务,是搭建 RAG 系统的核心基础组件。


下面提供了一个带有 Token 令牌认证 安全机制的 Docker 部署配置方案。

version: '3.1'
services:
chroma:
image: chromadb/chroma:latest
container_name: chroma
restart: always
ports:
- "8889:8000"
networks:
- share
environment:
# 启用基于 Token 的身份验证
CHROMA_SERVER_AUTHN_CREDENTIALS: "your_auth_token_here"
CHROMA_SERVER_AUTHN_PROVIDER: "chromadb.auth.token_authn.TokenAuthenticationServerProvider"
CHROMA_AUTH_TOKEN_TRANSPORT_HEADER: "X-Chroma-Token"
volumes:
- './data:/chroma/chroma'
networks:
share:
external:
name: share
Terminal window
docker-compose up -d

Terminal window
pip install chromadb
import os
import chromadb
from chromadb.config import Settings
from dotenv import load_dotenv
# 1. 载入环境变量(如果将 Token 写入了本地 env 文件)
load_dotenv('/path/to/your/.chroma_env')
# 2. 实例化 HTTP 客户端,并进行 Token 安全校验
client = chromadb.HttpClient(
host="localhost",
port=8889,
settings=Settings(
chroma_auth_token_transport_header="X-Chroma-Token",
chroma_client_auth_credentials="your_auth_token_here",
chroma_client_auth_provider="chromadb.auth.token_authn.TokenAuthClientProvider",
)
)
# 3. 检查服务健康状态
client.heartbeat()
# 4. 创建或获取数据集集合 (Collection)
collection = client.create_collection("my_collection")
# 5. 添加文档、对应向量和元数据
collection.add(
documents=["This is document1", "This is document2"],
metadatas=[{"source": "notion"}, {"source": "google-docs"}], # 用于后续过滤
ids=["doc1", "doc2"], # 必须全局唯一
embeddings=[[1.2, 2.1], [1.2, 2.1]] # 对应的嵌入维度向量
)
# 6. 进行语义相似度查询
results = collection.query(
query_texts=["This is a query document"],
n_results=2,
# where ={"source": "notion"}, # 可选:按元数据属性过滤
# where_document ={"$contains ": " document "} # 可选:按文档文本内容过滤
)
print(results)