向量数据库的使用

向量数据库

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

相关推荐
Rolei_zl2 天前
AIGC(生成式AI)试用 57 -- 向量数据库
aigc·向量数据库
circuitsosk2 天前
从零搭建行业知识平台:向量库+图数据库+传统关系库的多模检索统一层设计
数据库·python·oracle·向量数据库·rag·多模检索
数据库小学妹3 天前
AI Agent记忆怎么存?Redis+向量库三层记忆架构实战
人工智能·架构·向量数据库·ai数据库·数据库趋势·ai agent记忆
viskaz7 天前
2-向量数据库:主流近似最近邻算法(ANN)原理
近邻算法·向量数据库
ryan_9967 天前
生产环境向量检索数据库选型:Elasticsearch、Qdrant、Milvus 与 Chroma
数据库·elasticsearch·milvus·向量数据库·chroma·qdrant
孙启超19 天前
【AI应用开发】什么是混合检索(Hybrid Search)?向量检索 + BM25 关键词检索,适用场景与 RRF 融合原理
人工智能·缓存·llm·向量数据库·bm25·向量化·ai应用开发
circuitsosk21 天前
向量数据库选型与性能压测:Milvus、Pinecone、Chroma在真实业务下的对比
数据库·python·pinecone·milvus·向量数据库·chroma
deepdata_cn23 天前
向量数据库赋能研报智能分析、舆情检索、风险洞察
数据库·向量数据库
数据库小学妹1 个月前
向量数据库选型:独立vs融合对比与场景推荐(附SQL实战)
经验分享·数据库架构·向量数据库·rag·数据库选型·ai数据库