You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to do my project in Javascript. I followed the example here. Below is my code.
import { OpenAI } from "langchain/llms/openai";
import { RetrievalQAChain } from "langchain/chains";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import * as fs from "fs";
const filepath = 'src/documents/pvc-table-cloths.txt'
export const generateResponse = async (question: any) => {
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const model = new OpenAI({});
const text = fs.readFileSync(filepath, "utf8");
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
// Create a vector store from the documents.
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
// Create a chain that uses the OpenAI LLM and HNSWLib vector store.
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever());
const res = await chain.call({
query: question.toString(),
});
console.log({ res });
}
Whenever I ask question , the answer is either "I don't know" or returned me some portion of the documentation but not accurate answer. Can anyone tell me where am I missing?
Alternatively, When I do it in python, it works
from langchain.document_loaders import UnstructuredWordDocumentLoader
import nest_asyncio
nest_asyncio.apply()
loader = UnstructuredWordDocumentLoader("PVC Table Cloths.docx")
data = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(data)
embeddings = OpenAIEmbeddings()
db = FAISS.from_documents(texts, embeddings)
query = "this pvc curtain is black, I want clear. Please refund"
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=db.as_retriever())
print(qa.run(query))
I try to convert the same python code to JS, however I am stuck in the part where Im unsure which is the JS version of RetrievalQA.from_chain_type.
Would appreciate anyone to point me to the right direction to figure out why it doesnt work on JS and how to make it work 🙏
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to do my project in Javascript. I followed the example here. Below is my code.
Whenever I ask question , the answer is either "I don't know" or returned me some portion of the documentation but not accurate answer. Can anyone tell me where am I missing?
Alternatively, When I do it in python, it works
I try to convert the same python code to JS, however I am stuck in the part where Im unsure which is the JS version of
RetrievalQA.from_chain_type
.Would appreciate anyone to point me to the right direction to figure out why it doesnt work on JS and how to make it work 🙏
Beta Was this translation helpful? Give feedback.
All reactions