当前位置: 首页 > news >正文

chromadb向量数据库使用 (1)

目录

    • 完整代码
    • 代码解释

完整代码

import chromadb
chroma_client = chromadb.Client()
collection = chroma_client.create_collection(name="my_collection")
collection.add(documents=["This is a document about pineapple","This is a document about oranges"],ids=["id1", "id2"]
)
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'>]}
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'>]}

代码解释

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

# 导入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)
# 获取或创建集合(如果已存在则直接获取)
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

http://www.lryc.cn/news/545558.html

相关文章:

  • CSS—text文本、font字体、列表list、表格table、表单input、下拉菜单select
  • 关于大型语言模型的结构修剪
  • PostgreSQL 生产环境升级指南:pg_upgrade 快速完成版本升级!
  • Ubuntu2204下使用NVIDIA GeForce RTX 4090进行DeepSeek-R1-Distill-Llama-8B模型微调
  • JAVA面试常见题_基础部分_mybatis面试题
  • RISC-V汇编学习(一)—— 基础认识
  • 【Delphi】如何解决使用webView2时主界面置顶,而导致网页选择文件对话框被覆盖问题
  • 基于POI的Excel下拉框自动搜索,包括数据验证的单列删除
  • 基金 word-->pdf图片模糊的解决方法
  • React底层原理详解
  • Word 插入图片会到文字底下解决方案
  • 基于DeepSeek 的图生文最新算法 VLM-R1
  • Composer如何通过GitHub Personal Access Token安装私有包:完整教程
  • postgresql postgis扩展相关
  • 基于Python Django的人脸识别上课考勤系统(附源码,部署)
  • 神经网络之RNN和LSTM(基于pytorch-api)
  • leetcode第39题组合总和
  • 【UI设计——视频播放界面分享】
  • 动态规划刷题
  • stm32week5
  • fastapi中的patch请求
  • 系统架构设计师—计算机基础篇—计算机网络
  • MATLAB中asManyOfPattern函数用法
  • Kafka面试题及原理
  • Grok 3 AI 角色扮演提示词 化身顶级设计师
  • 从零开始设计一个完整的网站:HTML、CSS、PHP、MySQL 和 JavaScript 实战教程
  • CSS 对齐:深入理解与技巧实践
  • oracle游标为什么没有共享,统计一下原因
  • IDEA中.gitignore未忽略指定文件的问题排查与解决
  • 通往 AI 之路:Python 机器学习入门-语法基础