- Python
- TypeScript
- Rust
针对生产环境,Chroma 提供了 Chroma 云 —— 一种快速、可扩展且无服务器的数据库即服务(DBaaS)。30 秒即可开启使用,包含 5 美元的免费额度。
通过 AI 安装
将以下提示词发送给 Claude Code、Cursor、Codex 或您喜爱的 AI 智能体。它将帮助您快速搭建 Chroma 环境。报告代码错误
复制
咨询 AI
In this directory create a new Python project with Chroma set up.
Use a virtual environment.
Write a small example that adds some data to a collection and queries it.
Do not delete the data from the collection when it's complete.
Run the script when you are done setting up the environment and writing the
script. The output should show what data was ingested, what was the query,
and the results.
Your own summary should include this output so the user can see it.
First, install `chromadb`.
The project should be set up with Chroma Cloud. When you install `chromadb`,
you get access to the Chroma CLI. You can run `chroma login` to authenticate.
This will open a browser for authentication and save a connection profile
locally.
You can also use `chroma profile show` to see if the user already has an
active profile saved locally. If so, you can skip the login step.
Then create a DB using the CLI with `chroma db create chroma-getting-started`.
This will create a DB with this name.
Then use the CLI command `chroma db connect chroma-getting-started --env-file`.
This will create a .env file in the current directory with the connection
variables for this DB and account, so the CloudClient can be instantiated
with chromadb.CloudClient(api_key=os.getenv("CHROMA_API_KEY"), ...).
手动安装
创建集合 (Collection)
集合是您存储嵌入(embeddings)、文档和任何附加元数据的地方。集合会对您的嵌入和文档进行索引,从而实现高效的检索和过滤。您可以通过指定名称来创建集合。
Python
报告代码错误
复制
咨询 AI
collection = chroma_client.create_collection(name="my_collection")
向集合中添加文本文档
Chroma 会存储您的文本并自动处理嵌入和索引工作。您也可以自定义嵌入模型。您必须为每个文档提供唯一的字符串 ID。
Python
报告代码错误
复制
咨询 AI
collection.add(
ids=["id1", "id2"],
documents=[
"This is a document about pineapple",
"This is a document about oranges"
]
)
查询集合
您可以使用一系列查询文本来查询集合,Chroma 将返回 n 个最相似的结果。就是这么简单!如果未提供 n_results,Chroma 默认返回 10 个结果。在此示例中我们仅添加了 2 个文档,因此设置 n_results=2。
Python
报告代码错误
复制
咨询 AI
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)
检查结果
从上述结果中,您可以看到关于“hawaii”(夏威夷)的查询在语义上与关于“pineapple”(菠萝)的文档最相似。
Python
报告代码错误
复制
咨询 AI
{
'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,
}
亲自尝试一下
如果我们尝试查询 “This is a document about florida”(这是一篇关于佛罗里达的文档)会怎样?这是一个完整的示例。
Python
报告代码错误
复制
咨询 AI
import chromadb
chroma_client = chromadb.Client()
# switch \`create_collection\` to \`get_or_create_collection\` to avoid creating a new collection every time
collection = chroma_client.get_or_create_collection(name="my_collection")
# switch \`add\` to \`upsert\` to avoid adding the same documents every time
collection.upsert(
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 florida"], # Chroma will embed this for you
n_results=2 # how many results to return
)
print(results)
后续步骤
为了简单起见,本指南使用了 Chroma 的内存客户端。它在内存中启动 Chroma 服务,因此当程序终止时,任何摄入的数据都会丢失。如果您需要数据持久化,可以使用持久化客户端或以客户端-服务器模式运行 Chroma。- 了解如何将 Chroma 部署到服务器
- 加入 Chroma 的 Discord 社区 进行提问和寻求帮助
- 在 X (@trychroma) 上关注 Chroma 获取更新动态
针对生产环境,Chroma 提供了 Chroma 云 —— 一种快速、可扩展且无服务器的数据库即服务(DBaaS)。30 秒即可开启使用,包含 5 美元的免费额度。
通过 AI 安装
将以下提示词发送给 Claude Code、Cursor、Codex 或您喜爱的 AI 智能体。它将帮助您快速搭建 Chroma 环境。报告代码错误
复制
咨询 AI
In this directory create a new Typescript project with Chroma set up.
Write a small example that adds some data to a collection and queries it.
Do not delete the data from the collection when it's complete.
Run the script when you are done setting up the environment and writing the
script. The output should show what data was ingested, what was the query,
and the results.
Your own summary should include this output so the user can see it.
First, install `chromadb`.
The project should be set up with Chroma Cloud. When you install `chromadb`,
you get access to the Chroma CLI. You can run `chroma login` to authenticate.
This will open a browser for authentication and save a connection profile
locally.
You can also use `chroma profile show` to see if the user already has an
active profile saved locally. If so, you can skip the login step.
Then create a DB using the CLI with `chroma db create chroma-getting-started`.
This will create a DB with this name.
Then use the CLI command `chroma db connect chroma-getting-started --env-file`.
This will create a .env file in the current directory with the connection
variables for this DB and account, so the CloudClient can be instantiated
with: new CloudClient().
手动安装
创建 Chroma 客户端
运行 Chroma 后端然后创建一个与之连接的客户端
终端 CLI
报告代码错误
复制
咨询 AI
chroma run --path ./getting-started
终端 Docker
报告代码错误
复制
咨询 AI
docker pull chromadb/chroma
docker run -p 8000:8000 chromadb/chroma
TypeScript ESM
报告代码错误
复制
咨询 AI
import { ChromaClient } from "chromadb";
const client = new ChromaClient();
TypeScript CJS
报告代码错误
复制
咨询 AI
const { ChromaClient } = require("chromadb");
const client = new ChromaClient();
创建集合 (Collection)
集合是您存储嵌入(embeddings)、文档和任何附加元数据的地方。集合会对您的嵌入和文档进行索引,从而实现高效的检索和过滤。您可以通过指定名称来创建集合。
TypeScript
报告代码错误
复制
咨询 AI
const collection = await client.createCollection({
name: "my_collection",
});
向集合中添加文本文档
Chroma 会存储您的文本并自动处理嵌入和索引工作。您也可以自定义嵌入模型。您必须为每个文档提供唯一的字符串 ID。
TypeScript
报告代码错误
复制
咨询 AI
await collection.add({
ids: ["id1", "id2"],
documents: [
"This is a document about pineapple",
"This is a document about oranges",
],
});
查询集合
您可以使用一系列查询文本来查询集合,Chroma 将返回 n 个最相似的结果。就是这么简单!如果未提供 n_results,Chroma 默认返回 10 个结果。在此示例中我们仅添加了 2 个文档,因此设置 n_results=2。
TypeScript
报告代码错误
复制
咨询 AI
const results = await collection.query({
queryTexts: "This is a query document about hawaii", // Chroma will embed this for you
nResults: 2, // how many results to return
});
console.log(results);
检查结果
从上述结果中,您可以看到关于“hawaii”(夏威夷)的查询在语义上与关于“pineapple”(菠萝)的文档最相似。
TypeScript
报告代码错误
复制
咨询 AI
{
documents: [
[
'This is a document about pineapple',
'This is a document about oranges'
]
],
ids: [
['id1', 'id2']
],
distances: [[1.0404009819030762, 1.243080496788025]],
uris: null,
data: null,
metadatas: [[null, null]],
embeddings: null
}
亲自尝试一下
如果我们尝试查询 “This is a document about florida”(这是一篇关于佛罗里达的文档)会怎样?这是一个完整的示例。
TypeScript
报告代码错误
复制
咨询 AI
import { ChromaClient } from "chromadb";
const client = new ChromaClient();
// switch `createCollection` to `getOrCreateCollection` to avoid creating a new collection every time
const collection = await client.getOrCreateCollection({
name: "my_collection",
});
// switch `addRecords` to `upsertRecords` to avoid adding the same documents every time
await collection.upsert({
documents: [
"This is a document about pineapple",
"This is a document about oranges",
],
ids: ["id1", "id2"],
});
const results = await collection.query({
queryTexts: ["This is a query document about florida"], // Chroma will embed this for you
nResults: 2, // how many results to return
});
console.log(results);
后续步骤
- 我们通过嵌入函数(embedding function)接口为各种嵌入提供商提供 一流的支持。每个嵌入函数都以独立的 npm 包形式发布。
- 了解如何将 Chroma 部署到服务器
- 加入 Chroma 的 Discord 社区 进行提问和寻求帮助
- 在 X (@trychroma) 上关注 Chroma 获取更新动态
我们的 Rust 文档托管在 docs.rs 上!然后创建一个与之连接的客户端
手动安装
报告代码错误
复制
咨询 AI
cargo add chroma
创建 Chroma 客户端
运行 Chroma 后端报告代码错误
复制
咨询 AI
chroma run --path ./getting-started
报告代码错误
复制
咨询 AI
use chroma::ChromaHttpClient;
let client = ChromaHttpClient::new(Default::default());
创建集合 (Collection)
报告代码错误
复制
咨询 AI
let collection = client
.create_collection("my_collection", None, None)
.await?;
向集合中添加文本文档
Rust 客户端要求直接提供嵌入向量。请使用您的提供商 SDK 生成嵌入,然后将其随文档一起传递。报告代码错误
复制
咨询 AI
let embeddings = vec![vec![0.1, 0.2, 0.3], vec![0.4, 0.5, 0.6]];
collection
.add(
vec!["id1".to_string(), "id2".to_string()],
embeddings,
Some(vec![
Some("This is a document about pineapple".to_string()),
Some("This is a document about oranges".to_string()),
]),
None,
None,
)
.await?;
查询集合
报告代码错误
复制
咨询 AI
let results = collection
.query(vec![vec![0.1, 0.2, 0.3]], Some(2), None, None, None)
.await?;
后续步骤
- 在 docs.rs 上阅读 Rust API 文档
- 了解如何将 Chroma 部署到服务器
- 加入 Chroma 的 Discord 社区 进行提问和寻求帮助