I’m trying to run a worker that returns some HTML on GET /
and from that HTML on click on a button make a POST call to /langchainQA to use a Q&A chain to answer questions about a given document.
How the code looks like:
// imports
import { Router } from 'itty-router';
import { OpenAI } from 'langchain/llms/openai';
import { FaissStore } from 'langchain/vectorstores/faiss';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
const router = Router();
const model = new OpenAI({
temperature: 0,
openAIApiKey: 'APIKEY',
});
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const embeddings = new OpenAIEmbeddings({
openAIApiKey: 'APIKEY',
});
const HTML = <!DOCTYPE html> ... </html>
router.get('/', async () => {
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
});
router.post('/qa', async (request) => {
const body = await request.json();
const { question, pdfContent } = body;
const chunks = await textSplitter.splitText(pdfContent);
const knowledgeStore = await FaissStore.fromTexts(chunks, {}, embeddings);
const docs = knowledgeStore.similaritySearch(question);
const chain = loadQAChain(model);
const res = chain.run({ question, docs });
const answer = res.answer;
return new Response(JSON.stringify(answer), {
headers: { 'content-type': 'application/json' },
});
});
On const knowledgeStore = await FaissStore.fromTexts(chunks, {}, embeddings);
I’m getting this error:
✘ [ERROR] Build failed with 2 errors:
node_modules/langchain/dist/vectorstores/faiss.js:94:32:
ERROR: Could not resolve “node:fs/promises”
node_modules/langchain/dist/vectorstores/faiss.js:95:34:
ERROR: Could not resolve “node:path”
I have node_compat=true in my wrangler config, and these are my dependencies:
“dependencies”: {
“faiss-node”: “^0.1.1”,
“itty-router”: “^3.0.12”,
“langchain”: “^0.0.79”
}
This is a screenshot of the error and the UI:
Any idea how to fix the unresolved imports?
Thanks!