chromadb向量数据库使用 (1)

目录

完整代码

python 复制代码
import chromadb
chroma_client = chromadb.Client()
python 复制代码
collection = chroma_client.create_collection(name="my_collection")
python 复制代码
collection.add(
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ],
    ids=["id1", "id2"]
)
python 复制代码
results = collection.query(
    query_texts=["This is a query document about hawaii"], 
    n_results=2 
)
print(results)
复制代码
{'ids': [['id1', 'id2']], 'embeddings': None, 'documents': [['This is a document about pineapple', 'This is a document about oranges']], 'uris': None, 'data': None, 'metadatas': [[None, None]], 'distances': [[1.0404009819030762, 1.2430799007415771]], 'included': [<IncludeEnum.distances: 'distances'>, <IncludeEnum.documents: 'documents'>, <IncludeEnum.metadatas: 'metadatas'>]}
python 复制代码
import chromadb
chroma_client = chromadb.Client()


collection = chroma_client.get_or_create_collection(name="my_collection")


collection.upsert(
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ],
    ids=["id1", "id2"]
)

results = collection.query(
    query_texts=["..."], 
    n_results=2 
)

print(results)
复制代码
{'ids': [['id2', 'id1']], 'embeddings': None, 'documents': [['This is a document about oranges', 'This is a document about pineapple']], 'uris': None, 'data': None, 'metadatas': [[None, None]], 'distances': [[1.8110723495483398, 1.846815824508667]], 'included': [<IncludeEnum.distances: 'distances'>, <IncludeEnum.documents: 'documents'>, <IncludeEnum.metadatas: 'metadatas'>]}

代码解释

以下是逐行代码的中文解释:

python 复制代码
# 导入ChromaDB客户端库
import chromadb

# 创建ChromaDB客户端实例(默认使用内存存储)
chroma_client = chromadb.Client()

# 创建名为"my_collection"的集合(类似数据库表)
collection = chroma_client.create_collection(name="my_collection")

# 向集合添加文档数据
collection.add(
    documents=[
        "This is a document about pineapple",  # 文档1:关于菠萝
        "This is a document about oranges"     # 文档2:关于橙子
    ],
    ids=["id1", "id2"]  # 为每个文档指定唯一ID
)

# 执行相似性查询
results = collection.query(
    query_texts=["This is a query document about hawaii"],  # 查询文本(夏威夷相关)
    n_results=2  # 返回最相似的2个结果
)

# 打印查询结果(包含相似文档及其距离分数)
print(results)
python 复制代码
# 获取或创建集合(如果已存在则直接获取)
collection = chroma_client.get_or_create_collection(name="my_collection")

# 使用upsert方法添加/更新文档(存在则更新,不存在则插入)
collection.upsert(
    documents=[
        "This is a document about pineapple",  # 文档内容与之前相同
        "This is a document about oranges"
    ],
    ids=["id1", "id2"]  # 使用相同ID
)

# 执行空查询(使用"..."作为占位符)
results = collection.query(
    query_texts=["..."],  # 无效查询文本示例
    n_results=2
)

# 打印不同查询条件的结果对比
print(results)

关键点解析:

  1. 存储方式:默认使用内存存储,重启后数据会丢失
  2. 集合操作:
    • create_collection() 严格创建新集合
    • get_or_create_collection() 更安全的获取方式
  3. 文档操作:
    • add() 单纯添加新文档
    • upsert() 支持更新已有文档(基于ID)
  4. 查询结果:
    • distances越小表示相似度越高
    • 无效查询可能返回随机/全部结果
    • 结果排序基于相似度得分

典型使用场景:构建简单的文本相似性搜索系统,适用于知识库检索、FAQ问答等场景。建议后续添加文本向量化模型(如Sentence-BERT)来提升搜索质量。

参考链接:https://docs.trychroma.com/docs/overview/getting-started

相关推荐
向成科技1 小时前
XC3588N工控主板助力电力巡检机器人
人工智能·rk3588·安卓·硬件·工控主板·主板
taxunjishu1 小时前
DeviceNet 转 EtherCAT:发那科焊接机器人与倍福 CX5140 在汽车焊装线的高速数据同步通讯配置案例
人工智能·区块链·工业物联网·工业自动化·总线协议
sali-tec2 小时前
C# 基于halcon的视觉工作流-章33-矩状测量
开发语言·人工智能·算法·计算机视觉·c#
格林威2 小时前
短波红外相机在机器视觉检测方向的应用
运维·人工智能·深度学习·数码相机·计算机视觉·视觉检测
这儿有一堆花2 小时前
DeepSeek-VL 解析:混合视觉-语言模型如何超越传统计算机视觉方法
人工智能·计算机视觉·语言模型
model20052 小时前
ubuntu24.04+5070ti训练yolo模型(2)
人工智能·yolo
CV-杨帆3 小时前
论文阅读:openai 2025 Why Language Models Hallucinate
论文阅读·人工智能·语言模型
javastart3 小时前
OpenRLHF:面向超大语言模型的高性能RLHF训练框架
人工智能·自然语言处理·aigc
IT_陈寒3 小时前
《Java 21新特性实战:5个必学的性能优化技巧让你的应用快30%》
前端·人工智能·后端
说私域3 小时前
定制开发开源AI智能名片S2B2C商城小程序在互联网族群化中的作用与影响
人工智能·小程序·开源