chromadb向量数据库搭建和使用

一、服务端搭建

1.本地拉取容器镜像

|---------------------------------|
| docker pull chromadb``/chroma |

2.将本地容器镜像上传到远程服务器(如果远程服务器可以直接拉取镜像可以直接拉取,无需这一步)

|------------------------------------------------------------------------------------------------------------------|
| docker save -o chromadb.``tar chromadb``/chroma scp /Users/本地目录/chromadb``.``tar root@远程机器ip:``/远程机器路径 |

3.远程服务器加载并运行容器镜像

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| docker load -i chromadb.``tar docker run --``rm --name chromadb -p 8001:8000 -``v .``/chromadb``:``/chroma/chroma --``env``-``file ./.chroma_env chromadb``/chroma |

注释:

复制代码
-p 8001:8000   将服务器8001端口映射到容器8000端口
复制代码
-v ./chromadb:/chroma/chroma     将服务器存储路径./chromadb映射到容器路径/chroma/chroma
复制代码
--env-file ./.chroma_env        容器服务chromadb运行时的相关配置

.chroma_env配置内容

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 容器中数据是否持久化配置 IS_PERSISTENT=TRUE # 容器中向量数据存储路径 chroma-data=``/chroma/chroma/ # 服务端认证token CHROMA_SERVER_AUTHN_CREDENTIALS=``test``-token # 服务端认证方式 CHROMA_SERVER_AUTHN_PROVIDER=chromadb.auth.token_authn.TokenAuthenticationServerProvider |

二、客户端使用

需要安装依赖:pip install chromadb-client

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| import chromadb import chromadb.utils.embedding_functions as embedding_functions import logging import sys from chromadb.config ``import Settings from dotenv ``import load_dotenv from datetime ``import datetime # 使用日志记录查看查询和事件 # log_format = '%(asctime)s - %(levelname)s - %(message)s' # logging.basicConfig(stream=sys.stdout, level=logging.INFO,format=log_format) # 获取向量数据库客户端 chroma_client ``= chromadb.HttpClient(host``=``'服务器ip'``, ``port``=``8001``, ``settings``=``Settings( ``chroma_client_auth_provider``=``"chromadb.auth.token_authn.TokenAuthClientProvider"``, ``chroma_client_auth_credentials``=``"test-token"``) ``) # 定义向量化函数(使用微软的azure) openai_ef ``= embedding_functions.OpenAIEmbeddingFunction( ``api_key``=``""``, ``api_base``=""``, ``api_type``=``"azure"``, ``api_version``=``""``, ``model_name``=``"embedding-3-small" ``) # 创建数据集 # collection = chroma_client.create_collection(name="my_collection", # embedding_function=openai_ef, # metadata={ # "description": "my first Chroma collection", # "created": str(datetime.now()) # } ) # 删除数据集 # chroma_client.delete_collection(name="my_collection") # 获取数据集 collection ``= chroma_client.get_collection(name``=``"my_collection"``,embedding_function``=``openai_ef) # 向数据集中增加数据 # collection.add( # documents=[ # "This is a document about pineapple", # "This is a document about hawai" # ], # metadatas=[{"chapter": "3", "verse": "16"}, {"chapter": "3", "verse": "5"}], # ids=["id1", "id2"] # ) # 文本查询数据 # results = collection.query( # query_texts=["This is a query document about hawai"], # Chroma will embed this for you # n_results=1 # how many results to return # ) # 文本查询并过滤 results ``= collection.query( ``query_texts``=``[``"This is a query document about hawai"``], ``n_results``=``2``, ``# metadatas 元数据过滤 ``# where={"verse": "16"}, ``# 文本内容过滤 ``where_document``=``{ ``"$contains"``:``"pineapple"``} ) print``(results) |

相关推荐
全栈前端老曹21 小时前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
神梦流21 小时前
ops-math 算子库的扩展能力:高精度与复数运算的硬件映射策略
服务器·数据库
让学习成为一种生活方式21 小时前
trf v4.09.1 安装与使用--生信工具42-version2
数据库
啦啦啦_999921 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
生产队队长21 小时前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存
老邓计算机毕设21 小时前
SSM找学互助系统52568(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·javaweb 毕业设计
痴儿哈哈21 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
Σίσυφος19001 天前
PCL法向量估计 之 方向约束法向量(Orientation Guided Normal)
数据库
老毛肚1 天前
手写mybatis
java·数据库·mybatis
海山数据库1 天前
移动云大云海山数据库(He3DB)postgresql_anonymizer插件原理介绍与安装
数据库·he3db·大云海山数据库·移动云数据库