向量数据库的使用

向量数据库

向量数据库是一种专门用于存储、管理和查询高维向量数据的数据库系统。随着人工智能和机器学习的广泛应用,向量数据库在处理非结构化数据(如文本、图像、音频和视频等)的任务中变得越来越重要。本文将介绍快速使用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 中的重要组件之一,文档索引会存储在向量数据库中,随着大模型的流行,感觉向量数据库也会持续发展,进一步提高性能。

相关推荐
OceanBase数据库官方博客12 小时前
向量检索新选择:FastGPT + OceanBase,快速构建RAG
人工智能·oceanbase·分布式数据库·向量数据库·rag
ai大师3 天前
如何在LangChain中构建并使用自定义向量数据库
langchain·claude·向量数据库·中转api·apikey·中转apikey·免费apikey
码观天工3 天前
.NET 原生驾驭 AI 新基建实战系列(四):Qdrant ── 实时高效的向量搜索利器
c#·.net·向量数据库·qdrant
码观天工10 天前
.NET 原生驾驭 AI 新基建实战系列(三):Chroma ── 轻松构建智能应用的向量数据库
ai·c#·.net·向量数据库
码观天工17 天前
.NET 原生驾驭 AI 新基建实战系列(二):Semantic Kernel 整合对向量数据库的统一支持
ai·.net·向量数据库·semantic kernel
ejinxian20 天前
大模型应用初学指南
人工智能·大模型·向量数据库
玩电脑的辣条哥1 个月前
向量数据库是什么,它有什么作用?
数据库·向量数据库
重整旗鼓~1 个月前
3.milvus索引-HNSW
milvus·向量数据库
重整旗鼓~1 个月前
4.milvus索引FLAT
milvus·向量数据库
无极低码1 个月前
基于deepseek的智能语音客服【第四讲】封装milvus数据库连接池封装
数据库·人工智能·milvus·连接池·向量数据库·向量化