AI创想

标题: 向量数据库的使用 [打印本页]

作者: AI小编    时间: 7 小时前
标题: 向量数据库的使用
作者: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




欢迎光临 AI创想 (https://www.llms-ai.com/) Powered by Discuz! X3.4