赞
踩
Chroma是一个用于构建带有嵌入式AI应用程序的数据库。它已经内置了你需要开始的所有内容,并且可以在你的计算机上运行。一个hosted version即将推出!
npm install --save chromadb # yarn add chromadb
您需要安装Chroma Python包才能使用Chroma命令行界面(CLI)和后端服务器。
pip install chromadb
或者,您可以使用Docker容器来运行Chroma后端服务器。
启动Chroma后端服务器:
chroma run --path /db_path
然后创建一个连接到它的客户端:
// CJS
const { ChromaClient } = require("chromadb");
// ESM
import { ChromaClient } from 'chromadb'
const client = new ChromaClient();
集合(Collections)是您即将存储嵌入(embeddings)、文档和任何附加元数据的地方。您可以创建一个集合使用以下名称:
在这个例子中,我们想要从文本生成嵌入向量。OpenAI的ada-002模型非常受欢迎,免费且注册快捷。获取您的API密钥并返回。Chroma的API具有多态性(可以在浏览器或服务器端运行),但OpenAI的API则没有。因此,请在服务器端运行此示例。
// CJS
const { OpenAIEmbeddingFunction } = require("chromadb");
// ESM
import { OpenAIEmbeddingFunction } from 'chromadb'
const embedder = new OpenAIEmbeddingFunction({
openai_api_key: "your_api_key",
});
const collection = await client.createCollection({
name: "my_collection",
embeddingFunction: embedder,
});
Chroma会自动存储您的文本,并处理分词、嵌入和索引等操作。
await collection.add({
ids: ["id1", "id2"],
metadatas: [{ source: "my_source" }, { source: "my_source" }],
documents: ["This is a document", "This is another document"],
});
如果您已经自己生成了嵌入(embeddings),您可以直接将它们加载进去:
await collection.add({
ids: ["id1", "id2"],
embeddings: [
[1.2, 2.3, 4.5],
[6.7, 8.2, 9.2],
],
where: [{ source: "my_source" }, { source: "my_source" }],
documents: ["This is a document", "This is another document"],
});
您可以使用一组查询文本对集合进行查询,Chroma将返回与查询最相似的前n个结果。就是这么简单!
const results = await collection.query({
nResults: 2,
queryTexts: ["This is a query document"],
});
查找 chromadb on PyPI.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。