ClickHouse
兼容性
仅适用于 Node.js。
ClickHouse 是一个强大且开源的列式数据库,用于处理分析查询和高效存储。ClickHouse 的设计旨在将向量搜索和分析功能结合在一起,提供强大的性能支持。
配置步骤
- 启动一个 ClickHouse 集群。详细信息请参考 ClickHouse 安装指南。
- 在启动 ClickHouse 集群后,从集群的
操作(Actions)菜单中获取连接详情(Connection Details)。您需要获取主机地址、端口、用户名和密码。 - 在您的工作区中安装 ClickHouse 所需的 Node.js 对等依赖项。
您需要安装以下对等依赖项:
- npm
- Yarn
- pnpm
npm install -S @clickhouse/client mysql2
yarn add @clickhouse/client mysql2
pnpm add @clickhouse/client mysql2
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
索引和查询文档
import { ClickHouseStore } from "@langchain/community/vectorstores/clickhouse";
import { OpenAIEmbeddings } from "@langchain/openai";
// Initialize ClickHouse store from texts
const vectorStore = await ClickHouseStore.fromTexts(
["Hello world", "Bye bye", "hello nice world"],
[
{ id: 2, name: "2" },
{ id: 1, name: "1" },
{ id: 3, name: "3" },
],
new OpenAIEmbeddings(),
{
host: process.env.CLICKHOUSE_HOST || "localhost",
port: process.env.CLICKHOUSE_PORT || 8443,
username: process.env.CLICKHOUSE_USER || "username",
password: process.env.CLICKHOUSE_PASSWORD || "password",
database: process.env.CLICKHOUSE_DATABASE || "default",
table: process.env.CLICKHOUSE_TABLE || "vector_table",
}
);
// Sleep 1 second to ensure that the search occurs after the successful insertion of data.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));
// Perform similarity search without filtering
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(results);
// Perform similarity search with filtering
const filteredResults = await vectorStore.similaritySearch("hello world", 1, {
whereStr: "metadata.name = '1'",
});
console.log(filteredResults);
API Reference:
- ClickHouseStore from
@langchain/community/vectorstores/clickhouse - OpenAIEmbeddings from
@langchain/openai
从现有集合中查询文档
import { ClickHouseStore } from "@langchain/community/vectorstores/clickhouse";
import { OpenAIEmbeddings } from "@langchain/openai";
// Initialize ClickHouse store
const vectorStore = await ClickHouseStore.fromExistingIndex(
new OpenAIEmbeddings(),
{
host: process.env.CLICKHOUSE_HOST || "localhost",
port: process.env.CLICKHOUSE_PORT || 8443,
username: process.env.CLICKHOUSE_USER || "username",
password: process.env.CLICKHOUSE_PASSWORD || "password",
database: process.env.CLICKHOUSE_DATABASE || "default",
table: process.env.CLICKHOUSE_TABLE || "vector_table",
}
);
// Sleep 1 second to ensure that the search occurs after the successful insertion of data.
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => setTimeout(resolve, 1000));
// Perform similarity search without filtering
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(results);
// Perform similarity search with filtering
const filteredResults = await vectorStore.similaritySearch("hello world", 1, {
whereStr: "metadata.name = '1'",
});
console.log(filteredResults);
API Reference:
- ClickHouseStore from
@langchain/community/vectorstores/clickhouse - OpenAIEmbeddings from
@langchain/openai
相关内容
Related
- Vector store conceptual guide
- Vector store how-to guides