当我使用以下代码(总结长 PDF)时,它对于第一个 PDF 工作得很好。但是,如果我将它用于第二个 PDF(即,我将文件路径更改为另一个 PDF),它仍然会显示第一个 PDF 的摘要,就好像第一个 PDF/上一轮的嵌入以某种方式存储而不是已删除。
from langchain.document_loaders import PyPDFLoader # for loading the pdf
from langchain.embeddings import OpenAIEmbeddings # for creating embeddings
from langchain.vectorstores import Chroma # for the vectorization part
from langchain.chains import ChatVectorDBChain # for chatting with the pdf
from langchain.llms import OpenAI # the LLM model we'll use (CHatGPT)
import os
os.environ["OPENAI_API_KEY"] = "my_API_KEY"
pdf_path = "file_path"
loader = PyPDFLoader(pdf_path)
pages = loader.load_and_split()
print(pages[1].page_content)
embeddings = OpenAIEmbeddings()
vectordb = Chroma.from_documents(pages, embedding=embeddings,
persist_directory=".")
vectordb.persist()
pdf_qa = ChatVectorDBChain.from_llm(OpenAI(temperature=0.9, model_name="gpt-3.5-turbo"),
vectordb, return_source_documents=True)
query = "Write a summary of the text."
result = pdf_qa({"question": query, "chat_history": ""})
print(result["answer"])
即使重新启动 Python 或尝试许多其他 PDF 时,这种行为仍然成立。我开始重命名所有对象,有时这会有所帮助。但现在,即使重命名所有对象后,它仍然会显示之前 PDF 的摘要。我对这种行为感到很困惑。
有什么线索可以删除上一轮中的向量或解决这个问题吗?
最佳答案
更改您的持久目录,以便每个目录都不同
关于python - 当同时在多个 PDF 上运行时,带有 langchain 的 Summarizer PDF 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76074574/