USearch
兼容性
仅适用于 Node.js。
USearch 是一个用于对密集向量进行高效相似性搜索和聚类的库。
安装
安装 usearch 包,它是 USearch 的 Node.js 绑定。
- npm
- Yarn
- pnpm
npm install -S usearch
yarn add usearch
pnpm add usearch
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
用法
从文本创建新索引
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);
API 参考
- USearch 来自
@langchain/community/vectorstores/usearch
- OpenAIEmbeddings 来自
@langchain/openai
从加载器创建新索引
import { USearch } from "@langchain/community/vectorstores/usearch";
import { OpenAIEmbeddings } from "@langchain/openai";
import { TextLoader } from "langchain/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);
API 参考
- USearch 来自
@langchain/community/vectorstores/usearch
- OpenAIEmbeddings 来自
@langchain/openai
- TextLoader 来自
langchain/document_loaders/fs/text