WeaviateStore
Weaviate 是一个开源向量数据库,它存储对象和向量,允许将向量搜索与结构化过滤相结合。LangChain 通过 weaviate-ts-client 包连接到 Weaviate,weaviate-ts-client 包是 Weaviate 的官方 Typescript 客户端。
本指南简要概述了 Weaviate 向量存储 的入门方法。有关所有 WeaviateStore
功能和配置的详细文档,请查看 API 参考。
概述
集成详细信息
类 | 包 | PY 支持 | 最新包 |
---|---|---|---|
WeaviateStore | @langchain/weaviate | ✅ |
设置
要使用 Weaviate 向量存储,您需要设置一个 Weaviate 实例并安装 @langchain/weaviate
集成包。您还应该安装 weaviate-ts-client
包以初始化一个客户端连接到您的实例,如果要为索引文档分配 ID,还要安装 uuid
包。
本指南还将使用 OpenAI 嵌入,这需要您安装 @langchain/openai
集成包。您也可以使用 其他支持的嵌入模型。
- npm
- yarn
- pnpm
npm i @langchain/weaviate @langchain/core weaviate-ts-client uuid @langchain/openai
yarn add @langchain/weaviate @langchain/core weaviate-ts-client uuid @langchain/openai
pnpm add @langchain/weaviate @langchain/core weaviate-ts-client uuid @langchain/openai
您需要在本地或服务器上运行 Weaviate。查看 Weaviate 文档 以了解更多信息。
凭据
设置好实例后,请设置以下环境变量
// http or https
process.env.WEAVIATE_SCHEME = "";
// If running locally, include port e.g. "localhost:8080"
process.env.WEAVIATE_HOST = "YOUR_HOSTNAME";
// Optional, for cloud deployments
process.env.WEAVIATE_API_KEY = "YOUR_API_KEY";
如果您在本指南中使用 OpenAI 嵌入,还需要设置 OpenAI 密钥
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
如果您希望自动跟踪模型调用,还可以通过取消注释以下内容来设置 LangSmith API 密钥
// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"
实例化
import { WeaviateStore } from "@langchain/weaviate";
import { OpenAIEmbeddings } from "@langchain/openai";
import weaviate from "weaviate-ts-client";
// import { ApiKey } from "weaviate-ts-client"
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
// The Weaviate SDK has an issue with types
const weaviateClient = (weaviate as any).client({
scheme: process.env.WEAVIATE_SCHEME ?? "http",
host: process.env.WEAVIATE_HOST ?? "localhost",
// If necessary
// apiKey: new ApiKey(process.env.WEAVIATE_API_KEY ?? "default"),
});
const vectorStore = new WeaviateStore(embeddings, {
client: weaviateClient,
// Must start with a capital letter
indexName: "Langchainjs_test",
// Default value
textKey: "text",
// Any keys you intend to set as metadata
metadataKeys: ["source"],
});
管理向量存储
向向量存储中添加项目
注意: 如果您希望将 ID 与索引文档关联,则它们必须是 UUID。
import type { Document } from "@langchain/core/documents";
import { v4 as uuidv4 } from "uuid";
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];
const uuids = [uuidv4(), uuidv4(), uuidv4(), uuidv4()];
await vectorStore.addDocuments(documents, { ids: uuids });
[
'610f9b92-9bee-473f-a4db-8f2ca6e3442d',
'995160fa-441e-41a0-b476-cf3785518a0d',
'0cdbe6d4-0df8-4f99-9b67-184009fee9a2',
'18a8211c-0649-467b-a7c5-50ebb4b9ca9d'
]
从向量存储中删除项目
您可以通过 ID 删除,方法是传递 filter
参数
await vectorStore.delete({ ids: [uuids[3]] });
查询向量存储
创建向量存储并添加相关文档后,您很可能希望在链或代理运行期间查询它。
直接查询
执行简单的相似度搜索可以按如下方式完成
const filter = {
where: {
operator: "Equal" as const,
path: ["source"],
valueText: "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"}]
查看 此页面 以了解有关 Weaviate 过滤语法的信息。
如果您希望执行相似度搜索并接收相应的得分,您可以运行
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 参考
有关所有 WeaviateStore
功能和配置的详细文档,请查看 API 参考。