Skip to main content

Faiss

兼容性

仅适用于Node.js环境。

Faiss 是用于稠密向量的高效相似度搜索和聚类的库。

Langchainjs支持使用Faiss作为向量库,并可将其保存到文件。同时,它还提供从Python实现读取保存的文件的功能。

安装

安装faiss-node,它是Faiss的Node.js绑定。

npm install -S faiss-node

要启用从Python实现读取保存的文件的功能,还需要安装pickleparser

npm install -S pickleparser

使用

从文本创建新索引

import { FaissStore } from "langchain/vectorstores/faiss";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

export const run = async () => {
const vectorStore = await FaissStore.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 { FaissStore } from "langchain/vectorstores/faiss";
import { OpenAIEmbeddings } from "langchain/embeddings/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 FaissStore.fromDocuments(
docs,
new OpenAIEmbeddings()
);

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

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

import { FaissStore } from "langchain/vectorstores/faiss";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

// Create a vector store through any method, here from texts as an example
const vectorStore = await FaissStore.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
const loadedVectorStore = await FaissStore.load(
directory,
new OpenAIEmbeddings()
);

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

Python实现中加载保存的文件

import { FaissStore } from "langchain/vectorstores/faiss";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

// The directory of data saved from Python
const directory = "your/directory/here";

// Load the vector store from the directory
const loadedVectorStore = await FaissStore.loadFromPython(
directory,
new OpenAIEmbeddings()
);

// Search for the most similar document
const result = await loadedVectorStore.similaritySearch("test", 2);
console.log("result", result);