Skip to main content
兼容性仅在 Node.js 上可用。
USearch 是一个用于密集向量的高效相似性搜索和聚类的库。

设置

安装 usearch 包,它是 USearch 的 Node.js 绑定。
npm
npm install -S usearch
请参阅 此部分 以获取有关安装 LangChain 包的一般说明。
npm
npm install @langchain/openai @langchain/community @langchain/core

用法

从文本创建新索引

import { USearch } from "@langchain/community/vectorstores/usearch";
import { OpenAIEmbeddings } from "@langchain/openai";

const vectorStore = await USearch.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);

从加载器创建新索引

import { USearch } from "@langchain/community/vectorstores/usearch";
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
const vectorStore = await USearch.fromDocuments(docs, new OpenAIEmbeddings());

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

相关