load_summarize_chain with map_reduce error : InvalidRequestError: This model's maximum context length is 8192 tokens. However, your messages resulted in 13516 tokens. Please reduce the length of the messages.
Labels: name: auto:bug, name: area: models

### System Info

from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
from langchain.chains.summarize import load_summarize_chain
from fastapi.encoders import jsonable_encoder
from langchain.chains.mapreduce import MapReduceChain
from time import monotonic

gpt_4_8k_max_tokens = 8000 #https://platform.openai.com/docs/models/gpt-4
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(model_name=model_name, chunk_size=gpt_4_8k_max_tokens, chunk_overlap=0) 
verbose = False

# Initialize output dataframe with all the columns in the patient history class
column_names = list(PatientHistory.model_fields.keys())
df_AOAI_extracted_text = pd.DataFrame(columns=column_names)

# Create documents from the input text
texts = text_splitter.split_text(test_text) 
docs = [Document(page_content=t) for t in texts]
print(f"Number of Documents {len(docs)}")

# Count the number of tokens in the document
num_tokens = num_tokens_from_string(test_text, model_name)
print(f"Number of Tokens {num_tokens}")

# call langchain summarizer to get the output for the given prompt
summaries = []
if num_tokens < gpt_4_8k_max_tokens:
#Stuffing is the simplest method, whereby you simply stuff all the related data into the prompt as context to pass to the language model. This is implemented in LangChain as the StuffDocumentsChain.
#This method is sutable for smaller piece of data.
        
    chain = load_summarize_chain(llm, chain_type="stuff", prompt=TABLE_PROMPT, verbose=verbose)
else:      
        #MapReduceDocumentsChain is an advanced document processing technique that extends the capabilities of the conventional MapReduce framework. 
        #It goes beyond the typical MapReduce approach by executing a distinct prompt to consolidate the initial outputs. 
        #This method is designed to generate a thorough and cohesive summary or response that encompasses the entire document.
    print('mapreduce')
    chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=TABLE_PROMPT, combine_prompt=TABLE_PROMPT, verbose=verbose, return_intermediate_steps=False)

start_time = monotonic()
summary = chain.run(docs)
print(summary)

### Who can help?

_No response_

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [X] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [X] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
from langchain.chains.summarize import load_summarize_chain
from fastapi.encoders import jsonable_encoder
from langchain.chains.mapreduce import MapReduceChain
from time import monotonic

gpt_4_8k_max_tokens = 8000 #https://platform.openai.com/docs/models/gpt-4
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(model_name=model_name, chunk_size=gpt_4_8k_max_tokens, chunk_overlap=0) 
verbose = False

# Initialize output dataframe with all the columns in the patient history class
column_names = list(PatientHistory.model_fields.keys())
df_AOAI_extracted_text = pd.DataFrame(columns=column_names)

# Create documents from the input text
texts = text_splitter.split_text(test_text) 
docs = [Document(page_content=t) for t in texts]
print(f"Number of Documents {len(docs)}")

# Count the number of tokens in the document
num_tokens = num_tokens_from_string(test_text, model_name)
print(f"Number of Tokens {num_tokens}")

# call langchain summarizer to get the output for the given prompt
summaries = []
if num_tokens < gpt_4_8k_max_tokens:
#Stuffing is the simplest method, whereby you simply stuff all the related data into the prompt as context to pass to the language model. This is implemented in LangChain as the StuffDocumentsChain.
#This method is sutable for smaller piece of data.
        
    chain = load_summarize_chain(llm, chain_type="stuff", prompt=TABLE_PROMPT, verbose=verbose)
else:      
        #MapReduceDocumentsChain is an advanced document processing technique that extends the capabilities of the conventional MapReduce framework. 
        #It goes beyond the typical MapReduce approach by executing a distinct prompt to consolidate the initial outputs. 
        #This method is designed to generate a thorough and cohesive summary or response that encompasses the entire document.
    print('mapreduce')
    chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=TABLE_PROMPT, combine_prompt=TABLE_PROMPT, verbose=verbose, return_intermediate_steps=False)

start_time = monotonic()
summary = chain.run(docs)
print(summary)

### Expected behavior

Should go through all docs and provide the summary