# LangChain JS/TS
# 介绍
LangChain 是一个基于语言模型开发应用程序的框架。LangChain JS/TS (opens new window) 的目标是与 LangChain Python 包 (opens new window) 尽可能无缝地集成。具体来说,这意味着所有对象(提示、LLM、链等)都被设计成可以在不同语言之间进行序列化和共享。
# 先决条件
在开始之前,我们需要安装 langchain (opens new window) 和 ClickHouse JS (opens new window)。
# npm
npm install -S langchain @clickhouse/client
# yarn
yarn add langchain @clickhouse/client
# 环境设置
要使用 OpenAI 嵌入模型,我们需要在 OpenAI (opens new window) 注册一个 API 密钥。我们还需要从 MyScale 控制台(连接详细信息)中获取集群主机、用户名和密码信息。
运行以下命令设置环境变量:
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"
# 索引和查询文档
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);
# 从现有集合查询文档
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", // 默认为 "default"
table: "your_table", // 默认为 "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);