惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
V
Visual Studio Blog
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
Last Week in AI
Last Week in AI
C
Check Point Blog
D
Docker
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
博客园 - 叶小钗
博客园 - 聂微东
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
腾讯CDC
S
SegmentFault 最新的问题
博客园 - 【当耐特】

博客园 - 网无忌

体验Coding Plan 本地安装Dify 关于Docker Desktop的常用配置 渐变文字的小技巧 机器学习基础 pydantic中关于属性必填和选填的区别 CentOS的常用命令 密码中含有特殊字符造成mysqldump备份失败的一个小教训 通过模拟Cron执行环境来复现脚本执行失败的过程 查看mysql当前的执行任务,并关闭其中的指定任务 配置WSL2实现与宿主机的网络互通 超简单的 rsync 命令,实现文件的增量同步 十九年白驹过隙,老园子聊发少年狂 Linux中安装anaconda 在wsl中部署puppeteer的相关笔记 向量数据库横比 整理了一下目前各Linux发行版的清单 盘点各领域的包管理器 使用 jstat 命令查看 JVM 的GC信息 记录一个在js环境生成随机(伪造)数据的小插件,方便生成调试数据 使用Puppeter实现的全屏网页截图的小工具 开启 mysql 的 general_log
矢量数据库Chromadb的入门信息
网无忌 · 2024-07-06 · via 博客园 - 网无忌

一. 概述

 
Chromadb是比较年轻的矢量数据库,也是LangChain默认使用的矢量数据库,使用简单,上手很容易。
官网地址:https://docs.trychroma.com/
Github:https://github.com/chroma-core/chroma
 

二. 安装

 
官网的指南:https://docs.trychroma.com/getting-started
 

三. 使用模式

  1. 内存模式
     
    该模式下,数据不会被持久化。
import chromadb
# 创建客户端
chroma_client = chromadb.Client()
# 创建集合
collection = chroma_client.create_collection(name="my_collection")
# 添加数据
collection.add(
  documents=["Document 1", "Document 2"],
  ids=["id1", "id2"]
)
# 查询数据
results = collection.query(
  query_texts=["Document"],
  n_results=2
)
print(results)

 
2. 本地模式
 
该模式下,可在指定位置创建sqlite数据库进行持久化。

import chromadb
client = chromadb.PersistentClient(path="/path/to/data")

 
3. 服务模式
 
首先启动Chroma服务:

chroma run --path /db_path

之后在程序中连接该服务:

import chromadb
chroma_client = chromadb.HttpClient(host='localhost', port=8000)

 
使用服务模式时,客户端不需要安装全部的chromadb模块,只需要安装chromadb-client即可:
pip install chromadb-client
此包是用于服务模式下的轻量级HTTP客户机,具有最小的依赖占用。
 

四. 创建和管理集合

 
集合(collection)是ChromaDB中存储嵌入,文档和元数据的地方,类似于关系数据库中的表(table)。你可以用客户端对象的create_collection方法创建一个集合,指定一个名称:
collection = chroma_client.create_collection(name="my_collection")
 
还有一些其他常用的方法:

# 获取一个存在的Collection对象
collection = chroma_client.get_collection("testname")

# 如果不存在就创建collection对象,一般用这个更多一点
collection = chroma_client.get_or_create_collection("testname")

# 查看已有的集合
chroma_client.list_collections()

# 删除集合
chroma_client.delete_collection(name="my_collection")

五. 矢量模型

 
Chroma默认使用的是all-MiniLM-L6-v2模型来进行embeddings。
也可以直接使用官方预训练的托管在Huggingface上的模型:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('model_name')

选择非常多,可以点击官网查看每种预训练模型的详细信息:https://www.sbert.net/docs/sentence_transformer/pretrained_models.html

 
还可以使用其他第三方模型,包括第三方平台,例如:

openai_ef = embedding_functions.OpenAIEmbeddingFunction(
    api_key="YOUR_API_KEY",
    model_name="text-embedding-ada-002"
)

 
比较吸引我的是,chromadb还支持集成Ollama中的模型进行embedding:

import chromadb.utils.embedding_functions as embedding_functions

ollama_ef = embedding_functions.OllamaEmbeddingFunction(
    url="http://localhost:11434/api/embeddings",
    model_name="llama2",
)

embeddings = ollama_ef(["This is my first text to embed",
                        "This is my second document"])

 
记录一个适合中文矢量化的模型:coROM中文通用文本表示模型
这是阿里旗下的Embedding模型,基于Pytorch的,等以后尝试加载到Ollama,用起来就更方便了。
 

六. 链接

 
ChromaDB python 使用教程及记录
向量数据库Chroma极简教程(含案例)