【技术专题】嵌入模型与Chroma向量数据库 - Chroma Client-Server模式

0 阅读1分钟

大家好,我是锋哥。最近连载更新《嵌入模型与Chroma向量数据库 AI大模型应用开发必备知识》技术专题。

QQ截图20260226134650.jpg 本课程主要介绍和讲解嵌入模型与向量数据库简介,Qwen3嵌入模型使用,Chroma向量数据库使用,Chroma安装,Client-Server模式,集合添加,修改,删除,查询操作以及自定义Embedding Functions。。。 同时也配套视频教程 《1天学会 嵌入模型与Chroma向量数据库 AI大模型应用开发必备知识 视频教程》

Chroma也可以配置为以客户端/服务器模式运行。在此模式下,Chroma客户端会连接到在单独进程中运行的Chroma服务器。 要启动Chroma服务器,请运行以下命令:

chroma run --path D:\chroma_db

运行好后,本地会出现数据库文件

image.png

使用Chroma HttpClient 连接服务器

import chromadb
​
chroma_client = chromadb.HttpClient(host='localhost', port=8000)

完整测试:

import chromadb
​
chroma_client = chromadb.HttpClient(host='localhost', port=8000)
​
print(chroma_client)
​
collection = chroma_client.create_collection(name="my_collection") # 创建集合
​
collection.add(  # 添加数据
    ids=["id1", "id2"],
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ]
)
​
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)

运行结果:

<chromadb.api.client.Client object at 0x000002970D89C810>
{'ids': [['id1', 'id2']], 'distances': [[1.040401, 1.2430799]], 'embeddings': None, 'metadatas': [[None, None]], 'documents': [['This is a document about pineapple', 'This is a document about oranges']], 'uris': None, 'data': None, 'included': ['metadatas', 'documents', 'distances']}