开启左侧

向量数据库的使用

[复制链接]
AI小编 发表于 3 小时前 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
作者:CSDN博客
向量数据库

向量数据库是一种专门用于存储、管理和查询高维向量数据的数据库系统。随着人工智能和机器学习的广泛应用,向量数据库在处理非结构化数据(如文本、图像、音频和视频等)的任务中变得越来越重要。本文将介绍快速使用Chroma
安装

安装chromadb向量数据库
  1. pip install chromadb
复制代码
创建链接

创建客户端
  1. import chromadb
  2. chroma_client = chromadb.Client()
复制代码
创建集合

Chroma数据结构,包括集合、文档和Embedding。
  1. collection = chroma_client.create_collection(name="my_collection")
复制代码
添加文档

添加文档到集合中
  1. collection.add(
  2.     documents=[
  3.         "This is a document about pineapple",
  4.         "This is a document about oranges"
  5.     ],
  6.     ids=["id1", "id2"]
  7. )
复制代码
搜索

搜索文档并指定返回文档数
  1. results = collection.query(
  2.     query_texts=["This is a query document about hawaii"], # Chroma will embed this for you
  3.     n_results=2 # how many results to return
  4. )
  5. print(results)
复制代码
查看结果

可以看到夏威夷和菠萝更相似。
  1. {
  2.   'documents': [[
  3.       'This is a document about pineapple',
  4.       'This is a document about oranges'
  5.   ]],
  6.   'ids': [['id1', 'id2']],
  7.   'distances': [[1.0404009819030762, 1.243080496788025]],
  8.   'uris': None,
  9.   'data': None,
  10.   'metadatas': [[None, None]],
  11.   'embeddings': None,
  12. }
复制代码
总结

向量数据库是 RAG 中的重要组件之一,文档索引会存储在向量数据库中,随着大模型的流行,感觉向量数据库也会持续发展,进一步提高性能。

原文地址:https://blog.csdn.net/hawk2014bj/article/details/139179719
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by Discuz! X3.4© 2001-2013 Discuz Team.( 京ICP备17022993号-3 )