You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#项目中本地使用Chroma数据库?

在C#中本地使用Chroma并复用Python持久化数据的方案

核心思路

C#的Chroma客户端(Microsoft提供的版本)基于HTTP接口实现,无法像Python那样直接通过PersistentClient读写本地文件。要实现本地使用,你需要先启动一个本地Chroma服务器,让C#客户端通过HTTP连接到这个本地服务,同时指定Python项目中使用的Chroma数据存储目录,就能共享已有的本地数据。

步骤1:启动本地Chroma服务器

  1. 确保已安装Chroma,若未安装,执行以下命令:
pip install chromadb
  1. 在命令行中启动服务器,指定Python项目里Chroma数据的存储路径:
chroma run --path "/your/python/chroma/storage/path"

把/your/python/chroma/storage/path替换成实际路径,比如Windows下可能是C:\Projects\MyPythonChromaData,Linux/macOS下是~/projects/python_chroma_storage。

服务器启动后默认在http://localhost:8000运行,这就是C#客户端需要的端点URL。

步骤2:C#中连接本地服务器并操作数据

在C#项目中,通过ChromaClient连接本地端点,即可操作Python存储的集合数据。示例代码:

using ChromaDb.Client;
using ChromaDb.Client.Models;

var client = new ChromaClient(new ChromaClientOptions
{
    Endpoint = new Uri("http://localhost:8000")
});

// 获取Python中创建的目标集合
var collection = await client.GetCollectionAsync("your-collection-name");

// 示例:执行向量查询
var queryResults = await collection.QueryAsync(
    queryEmbeddings: new float[][] { /* 你的查询向量数组 */ },
    nResults: 5
);

注意事项

  • 本地Chroma服务器需要保持运行状态,否则C#客户端无法建立连接
  • 启动服务器时指定的路径必须和Python中PersistentClient使用的路径完全一致,否则会读取到空数据集
  • 若需要服务器后台运行,Linux/macOS可使用nohup chroma run --path "xxx" &,Windows可将其注册为系统服务

内容的提问来源于stack exchange,提问作者Milucide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:12:34