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 weaviate-ts-client uuid @langchain/openai
yarn add @langchain/weaviate weaviate-ts-client uuid @langchain/openai
pnpm add @langchain/weaviate 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,则 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 参考.