Skip to content

Latest commit

 

History

History
114 lines (92 loc) · 3.89 KB

File metadata and controls

114 lines (92 loc) · 3.89 KB
title ClickHouse integration
description Integrate with the ClickHouse vector store using LangChain JavaScript.
**Compatibility**

Only available on Node.js.

ClickHouse is an open-source columnar database for analytics that also supports vector search. For background on ClickHouse vector search (including approximate indexes), see Exact and Approximate Vector Search.

Setup

  1. Launch a ClickHouse cluster. Refer to the ClickHouse installation guide for details.
  2. After launching a ClickHouse cluster, retrieve the connection details. You will need the host, port, username, and password.
  3. Install the required Node.js peer dependency for ClickHouse in your workspace.

You will need to install the following peer dependencies:

npm install -S @clickhouse/client mysql2
See [this section for general instructions on installing LangChain packages](/oss/langchain/install).
npm install @langchain/openai @langchain/community @langchain/core

Index and query docs

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
      ? Number.parseInt(process.env.CLICKHOUSE_PORT, 10)
      : 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);

Query docs from an existing collection

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
      ? Number.parseInt(process.env.CLICKHOUSE_PORT, 10)
      : 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);

Related