An issue in Langchain when using it for summarization #23791
Unanswered
balachander1964
asked this question in
Q&A
Replies: 1 comment
-
Hello, @balachander1964! I'm here to assist you with any issues or questions you have. Let's get started! The error you're encountering, To resolve this, ensure that you are not passing # Imports
import os, sys, datetime, path
import pypdf
import tracemalloc
from langchain import chains
import openai
from openai import ChatCompletion
from azure.cosmos import CosmosClient, PartitionKey
from langchain.chains import summarize, retrieval_qa
from langchain.chains.summarize import load_summarize_chain, map_reduce_prompt, refine_prompts
from langchain.chains import mapreduce
from langchain.chains.combine_documents.map_reduce import MapReduceDocumentsChain
from langchain.chains.combine_documents.reduce import ReduceDocumentsChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.text_splitter import NLTKTextSplitter, CharacterTextSplitter
from langchain.chains.llm import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain import hub
from langchain_community.document_loaders import PyPDFLoader, TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import AzureCosmosDBVectorSearch, azure_cosmos_db
from langchain.llms import OpenAI
from langchain_openai import ChatOpenAI
# Add your open AI key to the system variable and access it.
openai.api_key = os.environ.get('openai_key')
_MODEL = 'gpt-4o'
_LLM = ChatOpenAI(temperature=0, model_name=_MODEL)
_SRC_PATH = path.Path('d:/sunwaretech/resumes/Yuvaraj Res.pdf')
def main():
'''Main Entry'''
# Load the pdf and retrieve the document.
loader = PyPDFLoader(_SRC_PATH)
docs = loader.load_and_split()
doc_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=1024, chunk_overlap=0
)
pages = doc_splitter.split_documents(docs)
# Map
map_prompt_template = """Write a concise summary of the following chunk of text that includes the main points and any important details.
{Text}
Helpful Answer:"""
# Reduce
combine_prompt_template = """Write a concise summary of the following text, delimited by newline character.
Return your response in bullet points which covers the key points of the text.
{Text}
Helpful Answer:"""
# Map Reduce code
input_vars = ['Text']
map_prompt = PromptTemplate(template=map_prompt_template, input_variables=input_vars)
map_chain = map_prompt | _LLM | StrOutputParser()
combine_prompt = PromptTemplate(template=combine_prompt_template, input_variables=input_vars)
combine_chain = combine_prompt | _LLM | StrOutputParser()
# Generate summaries using MapReduce method
map_reduce_chain = load_summarize_chain(
llm=_LLM,
chain_type="map_reduce",
map_prompt=map_prompt,
combine_prompt=combine_prompt,
document_variable_name='pages',
return_intermediate_steps=True,
)
map_reduce_outputs = map_reduce_chain({"input_documents": pages})
print(map_reduce_outputs)
return
if __name__ == '__main__':
tracemalloc.start()
print('Starting')
main()
tracemalloc.stop()
input('Done') Ensure that you are not passing |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
Hi, I got the following error when using Langchain for Summarising a PDF document. I will appreciate if you let me know how to fix this.
Traceback (most recent call last):
File "D:\Projects2017\langchain_gpt_01\langchain_gpt_01\summarizer_qa.py", line 92, in <module>
main()
File "D:\Projects2017\langchain_gpt_01\langchain_gpt_01\summarizer_qa.py", line 60, in main
map_prompt = PromptTemplate.from_template(template = map_prompt_template, input_variables = input_vars)
File "D:\python\Anaconda3\envs\spacy_-langchain\lib\site-packages\langchain_core\prompts\prompt.py", line 269, in from_template
return cls(
TypeError: ModelMetaclass object got multiple values for keyword argument 'input_variables'
System Info
Editor : Microsoft Visual Studio 2022, Python Version : 3.8x, OS : Windows 10 Pro
Beta Was this translation helpful? Give feedback.
All reactions