# LangChain JS/TS

# Introduction

LangChain is a framework for developing applications powered by language models. LangChain JS/TS (opens new window) is built to integrate as seamlessly as possible with the LangChain Python package (opens new window). Specifically, this means all objects (prompts, LLMs, chains, etc) are designed in a way where they can be serialized and shared between languages.

# Prerequisites

Before we get started, we need to install the langchain (opens new window) and ClickHouse JS (opens new window).

# npm
npm install -S langchain @clickhouse/client
# yarn
yarn add langchain @clickhouse/client

# Environment Setup

To use OpenAI embedding models, we need to sign up for an OpenAI API key at OpenAI (opens new window). We also need to retrieve the cluster host, username, and password information from the MyScale console (Connection Details).

Run the following command to set the environment variables:

export MYSCALE_HOST="YOUR_CLUSTER_HOST"
export MYSCALE_PORT=443 
export MYSCALE_USERNAME="YOUR_USERNAME" 
export MYSCALE_PASSWORD="YOUR_CLUSTER_PASSWORD"
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"

# Index and Query Docs

import { MyScaleStore } from "langchain/vectorstores/myscale";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
const vectorStore = await MyScaleStore.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.MYSCALE_HOST || "localhost",
    port: process.env.MYSCALE_PORT || "443",
    username: process.env.MYSCALE_USERNAME || "username",
    password: process.env.MYSCALE_PASSWORD || "password",
  }
);
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(results);
const filteredResults = await vectorStore.similaritySearch("hello world", 1, {
  whereStr: "metadata.name = '1'",
});
console.log(filteredResults);

# Query Docs From an Existing Collection

import { MyScaleStore } from "langchain/vectorstores/myscale";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
const vectorStore = await MyScaleStore.fromExistingIndex(
  new OpenAIEmbeddings(),
  {
    host: process.env.MYSCALE_HOST || "localhost",
    port: process.env.MYSCALE_PORT || "443",
    username: process.env.MYSCALE_USERNAME || "username",
    password: process.env.MYSCALE_PASSWORD || "password",
    database: "your_database", // defaults to "default"
    table: "your_table", // defaults to "vector_table"
  }
);
const results = await vectorStore.similaritySearch("hello world", 1);
console.log(results);
const filteredResults = await vectorStore.similaritySearch("hello world", 1, {
  whereStr: "metadata.name = '1'",
});
console.log(filteredResults);
Last Updated: Sat Apr 13 2024 10:45:55 GMT+0000