Skip to main content

OpenSearch

兼容性

仅适用于 Node.js。

OpenSearchElasticsearch 的一个分支,与 Elasticsearch API 完全兼容。关于其对近似最近邻(Approximate Nearest Neighbors)的支持,请阅读这里

Langchain.js 接受 @opensearch-project/opensearch 作为 OpenSearch 向量存储的客户端。

安装配置

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install -S @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/community/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, // 默认为 `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/community/vectorstores/opensearch";

const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
});

const vectorStore = new OpenSearchVectorStore(new OpenAIEmbeddings(), {
client,
});

/* 独立地搜索向量数据库,带元数据过滤 */
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(JSON.stringify(results, null, 2));
/* [
{
"pageContent": "Hello world",
"metadata": {
"id": 2
}
}
] */

/* 作为链的一部分使用(当前不支持元数据过滤) */
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 是一组技术的集合,允许搜索引擎以标准格式发布搜索结果,从而让用户更容易跨多个网站进行搜索。",
"sourceDocuments": [
{
"pageContent": "这是什么?",
"metadata": {
"id": 3
}
}
]
}
*/

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.