Next.js Discord

Discord Forum

How to load and save documents using FaissStore from Langchain in the Next.js pages/api ???

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Trying to run node pages/api/indexes.js but it does nothing, it doesn't even give me an error... it is suppose to create 2 documents but I can't see them anywhere...
I might be calling the wrong path from the new TextLoader?? const loader = new TextLoader("./restaurant.txt"); <-- the restaurant.txt is under pages/api

What would be wrong that this code doesn't do anything when I call it?

Complete code here:

const { OpenAIEmbeddings } = require("langchain/embeddings/openai"); const { FaissStore } = require("langchain/vectorstores/faiss"); const { CharacterTextSplitter } = require("langchain/text_splitter"); const { TextLoader } = require("langchain/document_loaders/fs/text"); const indexes = async (req, res) => { const loader = new TextLoader("./restaurant.txt"); /// <--- this is the call to the file console.log(loader); const docs = await loader.load(); const splitter = new CharacterTextSplitter({ chunkSize: 200, chunkOverlap: 50, }); const documents = await splitter.splitDocuments(docs); console.log(documents); const embeddings = new OpenAIEmbeddings(); const vectorStore = await FaissStore.fromDocuments(documents, embeddings); await vectorStore.save("./"); /// <-- here is where it should be saving 2 other files, but I can't find them anywhere. res.status(200).json({ ok: ok }); }; module.exports = { indexes };

0 Replies