您好,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中文网其他相关文章!

可以使用多种方法在Python中连接两个列表:1.使用 操作符,简单但在大列表中效率低;2.使用extend方法,效率高但会修改原列表;3.使用 =操作符,兼具效率和可读性;4.使用itertools.chain函数,内存效率高但需额外导入;5.使用列表解析,优雅但可能过于复杂。选择方法应根据代码上下文和需求。

有多种方法可以合并Python列表:1.使用 操作符,简单但对大列表不内存高效;2.使用extend方法,内存高效但会修改原列表;3.使用itertools.chain,适用于大数据集;4.使用*操作符,一行代码合并小到中型列表;5.使用numpy.concatenate,适用于大数据集和性能要求高的场景;6.使用append方法,适用于小列表但效率低。选择方法时需考虑列表大小和应用场景。

CompiledLanguagesOffersPeedAndSecurity,而interneterpretledlanguages provideeaseafuseanDoctability.1)commiledlanguageslikec arefasterandSecureButhOnderDevevelmendeclementCyclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesclesandentency.2)cransportedeplatectentysenty

Python中,for循环用于遍历可迭代对象,while循环用于条件满足时重复执行操作。1)for循环示例:遍历列表并打印元素。2)while循环示例:猜数字游戏,直到猜对为止。掌握循环原理和优化技巧可提高代码效率和可靠性。

要将列表连接成字符串,Python中使用join()方法是最佳选择。1)使用join()方法将列表元素连接成字符串,如''.join(my_list)。2)对于包含数字的列表,先用map(str,numbers)转换为字符串再连接。3)可以使用生成器表达式进行复杂格式化,如','.join(f'({fruit})'forfruitinfruits)。4)处理混合数据类型时,使用map(str,mixed_list)确保所有元素可转换为字符串。5)对于大型列表,使用''.join(large_li

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增强效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通过多种方法连接列表并管理重复元素:1)使用 运算符或extend()方法可以保留所有重复元素;2)转换为集合再转回列表可以去除所有重复元素,但会丢失原有顺序;3)使用循环或列表推导式结合集合可以去除重复元素并保持原有顺序。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

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

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具