Skip to content
Notifications
Clear all

Help: Getting 'maximum context length' errors even with small documents in my chain.

2 Posts
2 Users
0 Reactions
7 Views
(@cloud_ops_amy)
Estimable Member
Joined: 5 months ago
Posts: 128
Topic starter   [#9856]

Hey everyone, hoping to get some insight on a persistent error I'm hitting. I'm building a document QA chain using `ChatOpenAI` (gpt-3.5-turbo) and `RecursiveCharacterTextSplitter`, but I'm frequently hitting 'maximum context length' errors even when my source documents are quite small.

I've double-checked the chunk sizes and overlap. My splitter is configured for 1000-character chunks with 200 overlap, and I'm using a simple map-reduce chain. The documents are rarely more than 10 pages total. Here's the core setup:

```python
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
separators=["nn", "n", " ", ""]
)

docs = text_splitter.create_documents([raw_text])
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
chain = load_qa_chain(llm, chain_type="map_reduce")
```

The error seems to occur during the 'reduce' step, which suggests the combined output of the 'map' step might be too large, even with a limited number of chunks.

Has anyone else run into this? I was under the impression that map-reduce was designed to handle this exact constraint by summarizing chunks iteratively. My questions are:

* Could this be related to the `max_tokens` parameter on the LLM, separate from the chain's context management?
* Is there a hidden overhead in how prompts are constructed between steps that I'm missing?
* Would switching to `refine` or a different chain type offer a more predictable outcome for this scale?

I'm trying to avoid just throwing a larger-context model at it before understanding the root cause. Any debugging tips or configuration tweaks would be greatly appreciated.

-- Amy


Cloud cost nerd. No, I don't use Reserved Instances.


   
Quote
(@devops_grunt_2024)
Estimable Member
Joined: 4 months ago
Posts: 148
 

Map-reduce doesn't magically fix context limits. The reduce step feeds *all* the map outputs into a final LLM call. If you have 20 chunks, that's 20 answers to combine. They stack up quickly.

Check your prompts. The default combine prompt is verbose. Every chunk's answer plus the prompt template itself eats tokens.

You can try a smaller model for the reduce step, but honestly, `stuff` or `refine` chains are often simpler for small docs. Map-reduce for 10 pages is overkill. You're adding complexity for a problem you don't have.


If it ain't broke, don't 'upgrade' it.


   
ReplyQuote