跳到主要内容
兼容性可在浏览器和 Node.js 上使用
CloseVector 是一个跨平台向量数据库,可在浏览器和 Node.js 中运行。例如,您可以在 Node.js 上创建索引,然后在浏览器上加载/查询。更多信息,请访问 CloseVector 文档

设置

CloseVector Web

npm
npm install -S closevector-web

CloseVector Node

npm
npm install -S closevector-node
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/openai @langchain/community @langchain/core

用法

从文本创建新索引

// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";

export const run = async () => {
  // If you want to import the browser version, use the following line instead:
  // const vectorStore = await CloseVectorWeb.fromTexts(
  const vectorStore = await CloseVectorNode.fromTexts(
    ["Hello world", "Bye bye", "hello nice world"],
    [{ id: 2 }, { id: 1 }, { id: 3 }],
    new OpenAIEmbeddings()
  );

  const resultOne = await vectorStore.similaritySearch("hello world", 1);
  console.log(resultOne);
};

从加载器创建新索引

// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";
import { TextLoader } from "@langchain/classic/document_loaders/fs/text";

// Create docs with a loader
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();

// Load the docs into the vector store
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromDocuments(
const vectorStore = await CloseVectorNode.fromDocuments(
  docs,
  new OpenAIEmbeddings()
);

// Search for the most similar document
const resultOne = await vectorStore.similaritySearch("hello world", 1);
console.log(resultOne);

将索引保存到 CloseVector CDN 并再次加载

CloseVector 支持将索引保存/加载到云端。要使用此功能,您需要注册一个 CloseVector 账户。请阅读 CloseVector 文档,并通过登录首先生成您的 API 密钥。
// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { OpenAIEmbeddings } from "@langchain/openai";

// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
  ["Hello world", "Bye bye", "hello nice world"],
  [{ id: 2 }, { id: 1 }, { id: 3 }],
  new OpenAIEmbeddings(),
  undefined,
  {
    key: "your access key",
    secret: "your secret",
  }
);

// Save the vector store to cloud
await vectorStore.saveToCloud({
  description: "example",
  public: true,
});

const { uuid } = vectorStore.instance;

// Load the vector store from cloud
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.loadFromCloud({
  uuid,
  embeddings: new OpenAIEmbeddings(),
  credentials: {
    key: "your access key",
    secret: "your secret",
  },
});

// If you want to import the node version, use the following lines instead:
// const loadedVectorStoreOnNode = await CloseVectorNode.loadFromCloud({
//   uuid,
//   embeddings: new OpenAIEmbeddings(),
//   credentials: {
//     key: "your access key",
//     secret: "your secret"
//   }
// });

const loadedVectorStoreOnBrowser = await CloseVectorWeb.loadFromCloud({
  uuid,
  embeddings: new OpenAIEmbeddings(),
  credentials: {
    key: "your access key",
    secret: "your secret",
  },
});

// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);

// or
const resultOnBrowser = await loadedVectorStoreOnBrowser.similaritySearch(
  "hello world",
  1
);
console.log(resultOnBrowser);

将索引保存到文件并再次加载

// If you want to import the browser version, use the following line instead:
// import { CloseVectorWeb } from "@langchain/community/vectorstores/closevector/web";
import { CloseVectorNode } from "@langchain/community/vectorstores/closevector/node";
import { OpenAIEmbeddings } from "@langchain/openai";

// Create a vector store through any method, here from texts as an example
// If you want to import the browser version, use the following line instead:
// const vectorStore = await CloseVectorWeb.fromTexts(
const vectorStore = await CloseVectorNode.fromTexts(
  ["Hello world", "Bye bye", "hello nice world"],
  [{ id: 2 }, { id: 1 }, { id: 3 }],
  new OpenAIEmbeddings()
);

// Save the vector store to a directory
const directory = "your/directory/here";

await vectorStore.save(directory);

// Load the vector store from the same directory
// If you want to import the browser version, use the following line instead:
// const loadedVectorStore = await CloseVectorWeb.load(
const loadedVectorStore = await CloseVectorNode.load(
  directory,
  new OpenAIEmbeddings()
);

// vectorStore and loadedVectorStore are identical
const result = await loadedVectorStore.similaritySearch("hello world", 1);
console.log(result);

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.