English 中文(简体)
Langchain how to get the pdf page number when showing a result
原标题:

This is my langchain code to get the pdf text and query the answer, but I need to get the page number , at which page or area of page the openai get the answer.

I need your help how to get the page number from the pdf or the area of pdf,

const pdf = require("pdf-parse");
const fs = require("fs");
const { RecursiveCharacterTextSplitter } = require("langchain/text_splitter");
const { OpenAIEmbeddings } = require("langchain/embeddings");
const { HNSWLib } = require("langchain/vectorstores");
const { OpenAI } = require("langchain/llms");
const { loadQAChain, RetrievalQAChain } = require("langchain/chains");

const aiFunction = async () => {
  try {
    const fileName = "sample";
    const VECTOR_STORE_PATH = `${fileName}.index`;
    const question =
      "How does HTML contribute to the structure and content of a web page?";

    let dataBuffer = fs.readFileSync(`./pdf/${fileName}.pdf`);

    const { text } = await pdf(dataBuffer);

    const text_splitter = new RecursiveCharacterTextSplitter({
      chunkSize: 1000,
      chunkOverlap: 200,
    });

    const chunks = await text_splitter.createDocuments([text]);

    let vectorstore;

    const embeddings = new OpenAIEmbeddings();
    const modelName = embeddings.modelName;
    console.log({ modelName });
    vectorstore = await HNSWLib.fromDocuments(chunks, embeddings);
    await vectorstore.save(VECTOR_STORE_PATH);

    console.log(vectorstore);

    //method 1. Accept user questions/query
    const model = new OpenAI();

    const chain = RetrievalQAChain.fromLLM(model, vectorstore.asRetriever());

    const res = await chain.call({
      query: question,
    });
    console.log(vectorstore);
    console.log(res);
  } catch (error) {
    console.log(error);
  }
};
aiFunction();

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签