创建虚拟环境: python -m venv llama-env python 3.10 以上
激活: source llama-env/bin/activate
退出: deactivate
安装: chromadb
chromadb 默认依赖数据库是sqlite 需3.35以上
dart
python -c "import sqlite3; print(sqlite3.sqlite_version)"
不是的话 先升级sqlite
dart
pip install chromadb
demo验证: 参考: https://blog.csdn.net/ShuaiQIXiaoLuo/article/details/145134626 (可以看看)
dart
import chromadb
chroma_client = chromadb.Client()
collection = chroma_client.create_collection(name="my_collection")
collection.add(
documents=["This is a document about engineer", "This is a document about steak"],
metadatas=[{"source": "doc1"}, {"source": "doc2"}],
ids=["id1", "id2"]
)
results = collection.query(
query_texts=["Which food is the best?"],
n_results=2
)
print(results)

启动 Chroma 服务(服务器模式)像数据库一样远程连接使用
dart
# 自定义端口(默认 8000)
chroma run --path /data/chroma --port 8080
--path /data/chroma 数据存储在指定目录
# 启用详细日志(调试用)
chroma run --path /data/chroma --verbose
# 绑定到所有网络接口(允许外部设备访问,默认仅本地)
chroma run --path /data/chroma --host 0.0.0.0
# 后台运行并将日志输出到文件
nohup chroma run --path /data/chroma --port 8000 > chroma.log 2>&1 &
不输出日志
nohup chroma run --path /opt/llama/chromadb/ --port 8000 --host 0.0.0.0 > /dev/null 2>&1 &
使用浏览器访问http://ip地址:8000/api/v2/version 显示版本号 即启动成功
测试: 添加文档
dart
import chromadb
chroma_client = chromadb.HttpClient(host="10.10.0.12", port=8000)
collection = chroma_client.get_or_create_collection(name="my_collection")
# 添加文档 测试 由chromadb转为向量 然后存储 这里需要一个模型组件很容易报错
collection.add(
documents=["This is a document about engineer", "This is a document about steak"],
metadatas=[{"source": "doc1"}, {"source": "doc2"}],
ids=["id1", "id2"],
)
results = collection.query(query_texts=["Which food is the best?"], n_results=2)
print(results)
# 添加向量测试 直接存储向量
chroma_client.delete_collection("my_collection")
collection = chroma_client.get_or_create_collection(name="my_collection")
collection.add(
embeddings=[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], # 向量数据
metadatas=[{"name": "item1"}, {"name": "item2"}], # 元数据,描述向量的数据
ids=["id1", "id2"],
)
results = collection.get(where={"verse": "5"})
print(results)
第一次使用中间会缺少包,