Chroma
Chroma 是一个 AI 原生的开源向量数据库,专注于开发者生产力和幸福感。Chroma 基于 Apache 2.0 许可。
本指南提供了 Chroma vector stores
向量存储入门的快速概览。有关所有 Chroma
功能和配置的详细文档,请访问 API 参考。
概述
集成详情
类 | 包 | PY 支持 | 最新包 |
---|---|---|---|
Chroma | @langchain/community | ✅ | ![]() |
设置
要使用 Chroma 向量存储,您需要安装 @langchain/community
集成包以及 Chroma JS SDK 作为对等依赖项。
本指南还将使用 OpenAI 嵌入,这需要您安装 @langchain/openai
集成包。如果您愿意,也可以使用其他受支持的嵌入模型。
提示
有关安装集成包的通用说明,请参阅 此部分。
- npm
- yarn
- pnpm
npm i @langchain/community @langchain/openai @langchain/core chromadb
yarn add @langchain/community @langchain/openai @langchain/core chromadb
pnpm add @langchain/community @langchain/openai @langchain/core chromadb
接下来,按照以下说明在您的计算机上使用 Docker 运行 Chroma
docker pull chromadb/chroma
docker run -p 8000:8000 chromadb/chroma
您可以在本指南中查看其他设置说明。
凭据
如果您通过 Docker 运行 Chroma,则无需提供任何凭据。
如果您在本指南中使用 OpenAI 嵌入,则还需要设置您的 OpenAI 密钥
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
如果您想获得模型调用的自动跟踪,您还可以通过取消注释下方内容来设置您的 LangSmith API 密钥
// process.env.LANGSMITH_TRACING="true"
// process.env.LANGSMITH_API_KEY="your-api-key"
实例化
import { Chroma } from "@langchain/community/vectorstores/chroma";
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const vectorStore = new Chroma(embeddings, {
collectionName: "a-test-collection",
url: "http://localhost:8000", // Optional, will default to this value
collectionMetadata: {
"hnsw:space": "cosine",
}, // Optional, can be used to specify the distance method of the embedding space https://docs.trychroma.com/usage-guide#changing-the-distance-function
});
管理向量存储
向向量存储添加项目
import type { Document } from "@langchain/core/documents";
const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { source: "https://example.com" },
};
const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { source: "https://example.com" },
};
const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { source: "https://example.com" },
};
const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { source: "https://example.com" },
};
const documents = [document1, document2, document3, document4];
await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
[ '1', '2', '3', '4' ]
从向量存储删除项目
您可以按 ID 从 Chroma 中删除文档,如下所示
await vectorStore.delete({ ids: ["4"] });
查询向量存储
一旦您的向量存储已创建并添加了相关文档,您很可能希望在运行链或代理期间查询它。
直接查询
执行简单的相似性搜索可以按如下方式完成
const filter = { source: "https://example.com" };
const similaritySearchResults = await vectorStore.similaritySearch(
"biology",
2,
filter
);
for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
有关 Chroma 过滤器语法的更多信息,请参阅此页面。
如果您想执行相似性搜索并接收相应的分数,您可以运行
const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("biology", 2, filter);
for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(
doc.metadata
)}]`
);
}
* [SIM=0.835] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.852] Mitochondria are made out of lipids [{"source":"https://example.com"}]
通过转换为检索器进行查询
您还可以将向量存储转换为检索器,以便在您的链中更轻松地使用。
const retriever = vectorStore.asRetriever({
// Optional filter
filter: filter,
k: 2,
});
await retriever.invoke("biology");
[
Document {
pageContent: 'The powerhouse of the cell is the mitochondria',
metadata: { source: 'https://example.com' },
id: undefined
},
Document {
pageContent: 'Mitochondria are made out of lipids',
metadata: { source: 'https://example.com' },
id: undefined
}
]
用于检索增强生成的用法
有关如何将此向量存储用于检索增强生成 (RAG) 的指南,请参阅以下部分
API 参考
有关所有 Chroma
功能和配置的详细文档,请访问 API 参考