OpenSearch
兼容性
仅在 Node.js 上可用。
OpenSearch 是 Elasticsearch 的一个分支,与 Elasticsearch API 完全兼容。详细了解他们对近似最近邻居的支持 这里.
Langchain.js 接受 @opensearch-project/opensearch 作为 OpenSearch 向量存储的客户端。
设置
提示
- npm
- Yarn
- pnpm
npm install -S @langchain/openai @langchain/core @opensearch-project/opensearch
yarn add @langchain/openai @langchain/core @opensearch-project/opensearch
pnpm add @langchain/openai @langchain/core @opensearch-project/opensearch
您还需要运行 OpenSearch 实例。您可以使用 官方 Docker 镜像 开始。您也可以找到一个 docker-compose 文件示例 这里.
索引文档
import { Client } from "@opensearch-project/opensearch";
import { Document } from "langchain/document";
import { OpenAIEmbeddings } from "@langchain/openai";
import { OpenSearchVectorStore } from "langchain/vectorstores/opensearch";
const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
});
const docs = [
new Document({
metadata: { foo: "bar" },
pageContent: "opensearch is also a vector db",
}),
new Document({
metadata: { foo: "bar" },
pageContent: "the quick brown fox jumped over the lazy dog",
}),
new Document({
metadata: { baz: "qux" },
pageContent: "lorem ipsum dolor sit amet",
}),
new Document({
metadata: { baz: "qux" },
pageContent:
"OpenSearch is a scalable, flexible, and extensible open-source software suite for search, analytics, and observability applications",
}),
];
await OpenSearchVectorStore.fromDocuments(docs, new OpenAIEmbeddings(), {
client,
indexName: process.env.OPENSEARCH_INDEX, // Will default to `documents`
});
查询文档
import { Client } from "@opensearch-project/opensearch";
import { VectorDBQAChain } from "langchain/chains";
import { OpenAIEmbeddings } from "@langchain/openai";
import { OpenAI } from "@langchain/openai";
import { OpenSearchVectorStore } from "langchain/vectorstores/opensearch";
const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
});
const vectorStore = new OpenSearchVectorStore(new OpenAIEmbeddings(), {
client,
});
/* Search the vector DB independently with meta filters */
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(JSON.stringify(results, null, 2));
/* [
{
"pageContent": "Hello world",
"metadata": {
"id": 2
}
}
] */
/* Use as part of a chain (currently no metadata filters) */
const model = new OpenAI();
const chain = VectorDBQAChain.fromLLM(model, vectorStore, {
k: 1,
returnSourceDocuments: true,
});
const response = await chain.call({ query: "What is opensearch?" });
console.log(JSON.stringify(response, null, 2));
/*
{
"text": " Opensearch is a collection of technologies that allow search engines to publish search results in a standard format, making it easier for users to search across multiple sites.",
"sourceDocuments": [
{
"pageContent": "What's this?",
"metadata": {
"id": 3
}
}
]
}
*/