Open lawetis opened 1 month ago
To resolve the issue where the documents
variable in the formattedDocs
function is undefined, ensure the following:
Correctly Pass the docs
Parameter:
Make sure the docs
parameter is not undefined
when calling the fromDocuments
method. Here is an example:
const docs = [
new Document({ pageContent: 'Document 1 content', metadata: { key: 'value1' } }),
new Document({ pageContent: 'Document 2 content', metadata: { key: 'value2' } }),
];
const embeddings = /* initialize your embeddings instance */;
const dbConfig = { postgresConnectionOptions: { connectionString: 'your-connection-string' } };
const vercelPostgresInstance = await VercelPostgres.fromDocuments(docs, embeddings, dbConfig);
Ensure documents
Array is Populated:
Verify that the documents
array is properly populated before passing it to the formatDocuments
function.
Check the createStuffDocumentsChain
Function:
Ensure the createStuffDocumentsChain
function is correctly set up to pass the documents
array to the formatDocuments
function.
Example Usage:
Here is an example of how to use the createStuffDocumentsChain
function:
const loader = new TextLoader("./state_of_the_union.txt");
const rawDocuments = await loader.load();
const splitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000, chunkOverlap: 0 });
const documents = await splitter.splitDocuments(rawDocuments);
const vectorPgConfig = { tableName: 'vector_store' };
const vectorstore = await VercelPostgres.initialize(new OpenAIEmbeddings(), vectorPgConfig);
await vectorstore.addDocuments(documents);
const model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" });
const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
["system", "Answer the user's questions based on the below context:\n\n{context}"],
["human", "{input}"],
]);
const combineDocsChain = await createStuffDocumentsChain({ llm: model, prompt: questionAnsweringPrompt });
const chain = await createRetrievalChain({ retriever: vectorstore.asRetriever(), combineDocsChain });
const response = await chain.stream({ input: "What is the president's top priority regarding prices?" });
console.log("Chain response:", response.answer);
By following these steps, you should be able to resolve the issue of the documents
variable being undefined in the formattedDocs
function. Ensure that the documents
array is properly populated and passed through the chain of functions.
To continue the conversation, mention @dosu.
Yes, I believe the file exists and the data is correct.But the error still exists.
async addDocuments(documents, options) {
console.info('addDocuments-documents-1', documents) // Data is correct
const texts = documents.map(({ pageContent }) => pageContent);
return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
}
Perhaps it's because there's an issue with retrieving data using VercelPostgres
. Can you try
const simpleRes = await vectorStore.similaritySearch("What is the president's top priority regarding prices?");
console.log(simpleRes);
and see what the output is? @lawetis
I'm better now the other way around and it works fine now, thanks. @jeasonnow
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
where the error occurred:node_modules\langchain\dist\chains\combine_documents\base.cjs:12 error code:
Description
I'm using Vercel Postgres as vector store and I'm getting the following error
System Info