阿里云服务器部署向量数据库Chroma并通过Client连接

Chroma简介

官网:https://docs.trychroma.com/
Chroma是一个开源的向量数据库。可以为大语言模型提供额外的知识存储。

Chroma的优势

  • 开源
  • 简单轻便
    • 内存模式下无需额外部署
  • 相对较快

Server模式的部署

内存模式虽然简单轻便,但也只限于开发验证(重启应用数据会丢失)。实际使用还是需要在服务器部署起来。

前置条件

开通云服务器并安装docker
ali云官方安装教程

获取Docker镜像并部署

你可以直接从DockerHub拉取Chroma官方镜像

docker pull chromadb/chroma

之后执行命令,运行docker

docker run -d --name chromadb-container -p 8899:8000 chromadb/chroma

注:上面8899是指宿主机(host)的端口号,后面的8000 (即 -p 参数后的8000) 代表的是容器(container)内部的端口号。
这样chroma就在服务器上运行了

客户端访问

创建chroma 客户端

import os
import chromadb
from langchain_openai import OpenAIEmbeddings

# 创建client
    client = chromadb.HttpClient(
    							 host=os.environ.get("CHROMA_SERVER_IP"),  # 你的服务器ip
                                 port=8899,  # 你的服务器端口
                                 )

创建collection

# 创建collection
    collection = client.get_or_create_collection(
        name="hello", # collection名称
    )

向collection添加文档

# embedding函数,这里用了langchain的openai embedding函数
    embeddings = OpenAIEmbeddings().embed_documents
    # 向collection中添加文档
    collection.add(
        embeddings=embeddings(texts=["hello world", "goodbye world"]),  # 每个文档的向量
        documents=["hello world", "goodbye world"],  # 文档的原文
        ids=[f"id{i}" for i in range(len(["hello world", "goodbye world"]))]  # 每个文档的 id
    )

查询

# 查询向量数据库
    result = collection.query(
        query_embeddings=embeddings(texts=["hello"]),
        n_results=1
    )

    print(result)

结果:

{'ids': [['id0']], 'distances': [[0.21401809153891344]], 'embeddings': None, 'metadatas': [[None]], 'documents': [['hello world']], 'uris': None, 'data': None}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值