适用于 SQL 思维的 ChromaDB
您好,Chroma DB 是一个矢量数据库,对于使用 GenAI 应用程序非常有用。在本文中,我将探讨如何通过查看 MySQL 中的类似关系来在 Chroma DB 上运行查询。
模式
与 SQL 不同,您无法定义自己的架构。在 Chroma 中,您会获得固定的列,每个列都有自己的用途:
import chromadb #setiing up the client client = chromadb.Client() collection = client.create_collection(name="name") collection.add( documents = ["str1","str2","str3",...] ids = [1,2,3,....] metadatas=[{"chapter": "3", "verse": "16"},{"chapter":"3", "verse":"5"}, ..] embeddings = [[1,2,3], [3,4,5], [5,6,7]] )
Ids:它们是唯一的 id。请注意,您需要自己提供它们,与 sql 不同,没有自动增量
文档: 用于插入用于生成嵌入的文本数据。您可以提供文本,它会自动创建嵌入。或者您可以直接提供嵌入并将文本存储在其他位置。
嵌入: 在我看来,它们是数据库中最重要的部分,因为它们用于执行相似性搜索。
元数据:这用于关联您可能想要添加到数据库中以获得任何额外上下文的任何其他数据。
现在集合的基础知识已经清楚了,让我们继续进行 CRUD 操作,我们将了解如何查询数据库。
增删改查操作
注意:集合就像 Chroma 中的表格
要创建集合,我们可以使用 create_collection() 并根据需要执行我们的操作,但如果集合已经创建并且我们需要再次引用它,我们必须使用 get_collection() 否则我们会收到错误。
Create Table tablename
#Create a collection collection = client.create_collection(name="name") #If a collection is already made and you need to use it again the use collection = client.get_collection(name="name")
Insert into tablename Values(... , ..., ...)
collection.add( ids = [1] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] )
要更新插入的数据或删除数据,我们可以使用以下命令
collection.update( ids = [2] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] ) # If the id does not exist update will do nothing. to add data if id does not exist use collection.upsert( ids = [2] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] ) # To delete data use delete and refrence the document or id or the feild collection.delete( documents = ["some text"] ) # Or you can delete from a bunch of ids using where that will apply filter on metadata collection.delete( ids=["id1", "id2", "id3",...], where={"chapter": "20"} )
查询
现在我们将看看某些查询的样子
Select * from tablename Select * from tablename limit value Select Documents, Metadata from tablename
collection.get() collection.get(limit = val) collection.get(include = ["documents","metadata"])
虽然 get() 用于获取大量表以进行更高级的查询,但您需要使用查询方法
Select A,B from table limit val
collection.query( n_results = val #limit includes = [A,B] )
现在我们有3种可能的方法来过滤数据:相似性搜索(矢量数据库主要用于什么),元数据过滤器和文档过滤器
相似性搜索
我们可以根据文本或嵌入进行搜索并获得最相似的输出
collection.query(query_texts=["string"]) collection.query(query_embeddings=[[1,2,3]])
在 ChromaDB 中,where 和 where_document 参数用于在查询期间过滤 结果。这些过滤器允许您根据元数据或特定文档内容优化相似性搜索。
按元数据过滤
where 参数可让您根据关联的元数据过滤文档。元数据通常是您在文档插入期间提供的键值对的字典。
按类别、作者或日期等元数据过滤文档。
import chromadb #setiing up the client client = chromadb.Client() collection = client.create_collection(name="name") collection.add( documents = ["str1","str2","str3",...] ids = [1,2,3,....] metadatas=[{"chapter": "3", "verse": "16"},{"chapter":"3", "verse":"5"}, ..] embeddings = [[1,2,3], [3,4,5], [5,6,7]] )
Create Table tablename
按文档内容过滤
where_document 参数允许直接根据文档内容进行过滤。
仅检索包含特定关键字的文档。
#Create a collection collection = client.create_collection(name="name") #If a collection is already made and you need to use it again the use collection = client.get_collection(name="name")
要点:
- 使用 $contains、$startsWith 或 $endsWith 等运算符。
- $contains:匹配包含子字符串的文档。
- $startsWith:匹配以子字符串开头的文档。
- $endsWith:匹配以子字符串结尾的文档。
-
例如:
Insert into tablename Values(... , ..., ...)
登录后复制登录后复制
常见用例:
我们可以像这样组合所有三个过滤器:
-
在特定类别中搜索:
collection.add( ids = [1] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] )
登录后复制登录后复制 -
搜索包含特定术语的文档:
collection.update( ids = [2] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] ) # If the id does not exist update will do nothing. to add data if id does not exist use collection.upsert( ids = [2] documents = ["some text"] metadatas = [{"key":"value"}] embeddings = [[1,2,3]] ) # To delete data use delete and refrence the document or id or the feild collection.delete( documents = ["some text"] ) # Or you can delete from a bunch of ids using where that will apply filter on metadata collection.delete( ids=["id1", "id2", "id3",...], where={"chapter": "20"} )
登录后复制登录后复制 -
组合元数据和文档内容过滤器:
Select * from tablename Select * from tablename limit value Select Documents, Metadata from tablename
登录后复制登录后复制
这些过滤器提高了相似性搜索的精度,使 ChromaDB 成为目标文档检索的强大工具。
结论
我写这篇文章是因为我觉得该文档在尝试制作自己的程序时还有很多不足之处,我希望这会有所帮助!
感谢您的阅读,如果您喜欢这篇文章,请点赞和分享。另外,如果您是软件架构新手并且想了解更多信息,我将开始一个基于小组的队列,我将亲自与您和一个小组一起工作,教您有关软件架构和设计原理的所有知识。如果您有兴趣,可以填写下面的表格。 https://forms.gle/SUAxrzRyvbnV8uCGA
以上是适用于 SQL 思维的 ChromaDB的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。
