向量数据库的使用

向量数据库

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

安装

安装chromadb向量数据库

pip install chromadb 

创建链接

创建客户端

import chromadb
chroma_client = chromadb.Client()

创建集合

Chroma数据结构,包括集合、文档和Embedding。

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"], # Chroma will embed this for you
    n_results=2 # how many results to return
)
print(results)

查看结果

可以看到夏威夷和菠萝更相似。

{
  'documents': [[
      'This is a document about pineapple',
      'This is a document about oranges'
  ]],
  'ids': [['id1', 'id2']],
  'distances': [[1.0404009819030762, 1.243080496788025]],
  'uris': None,
  'data': None,
  'metadatas': [[None, None]],
  'embeddings': None,
}

总结

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

相关推荐

  1. 向量数据库使用

    2024-05-25 21:04:25       34 阅读
  2. 向量数据库介绍

    2024-05-25 21:04:25       52 阅读
  3. 向量数据库

    2024-05-25 21:04:25       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 21:04:25       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 21:04:25       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 21:04:25       82 阅读
  4. Python语言-面向对象

    2024-05-25 21:04:25       91 阅读

热门阅读

  1. day50

    2024-05-25 21:04:25       37 阅读
  2. 一个程序员的牢狱生涯(35)惊疑

    2024-05-25 21:04:25       31 阅读
  3. vim方向键乱码

    2024-05-25 21:04:25       34 阅读
  4. C++常见知识点总结

    2024-05-25 21:04:25       30 阅读
  5. 美国服务器如何避免网络漏洞?

    2024-05-25 21:04:25       33 阅读
  6. leetcode119-Pascal‘s Triangle II

    2024-05-25 21:04:25       27 阅读
  7. 如何在Ubuntu上安装NVIDIA显卡驱动并禁止自动更新

    2024-05-25 21:04:25       35 阅读