crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

How to avoid reaching maximum context length with repetitive task

Open profnimrod opened this issue 1 year ago • 9 comments

I am using Crew AI to draft a short booklet on various subjects. The first task generates a list of essay titles (or book layout) given the title entered by the user. The second task then goes through each essay title (book section) and writes a 750-word essay for each. About halfway through I'm getting the error below. I think this is happening because the task that is writing the next essay for each section is including all the previous essays in the context when writing the next one. Assuming I have interpreted this correctly, how might I limit the context for this task?

`Exception has occurred: BadRequestError Error code: 400 - {'error': {'message': "This model's maximum context length is 8192 tokens. However, your messages resulted in 8327 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}} httpx.HTTPStatusError: Client error '400 Bad Request' for url 'https://api.openai.com/v1/chat/completions' For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

During handling of the above exception, another exception occurred:

File "E:\EmergentPublications\WellnessPress\CrewAI\main.py", line 71, in result = crew.kickoff() ^^^^^^^^^^^^^^ openai.BadRequestError: Error code: 400 - {'error': {'message': "This model's maximum context length is 8192 tokens. However, your messages resulted in 8327 tokens. Please reduce the length of the messages.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}`

profnimrod avatar Jan 15 '24 21:01 profnimrod

some problem

Prayforhanluo avatar Jan 18 '24 03:01 Prayforhanluo

Would be great if CrewAI could integrate MemGPT or some other type of method to manage context window without exceeding maximum.

gururise avatar Jan 19 '24 17:01 gururise

There should be a guardrails for that already so I'll look into it and why it's not working. Some version of MemGPT integration is coming btw 😎👉👉

joaomdmoura avatar Jan 21 '24 16:01 joaomdmoura

I have found the Nous Hermes2 Yi works very well, but is unusable for most tasks because it constantly exceeds its max context length of 4096.

gururise avatar Jan 21 '24 17:01 gururise

I keep running into this issue as the max context length of my local LLM is 2048.

jfjensen avatar Feb 29 '24 13:02 jfjensen

Any solution for this. Having the same problem with GPT4

robshox avatar Apr 12 '24 14:04 robshox

Hey folks, we are working on a more native fix for this, probably a rolling context window, we needed to add the memory first before we could support it, but now that we have it we should be able to improve on this.

Sometime this could also indicated that you agent is trying to do too much and could be a good candidate to breaking into smaller agents / tasks.

That is not always de case tho.

joaomdmoura avatar Apr 12 '24 15:04 joaomdmoura

I came across this issue when running the sample code in Getting Started so you might want to include a simpler example that works.

Abbie2020 avatar Apr 22 '24 16:04 Abbie2020

Any update on this issue?

twelsh37 avatar May 07 '24 15:05 twelsh37

The "Create agents to research and write an article" lesson in the Multi-agent Systems with crewAI Tutorial on deeplearning.ai Shorts works just fine with "HuggingFaceH4/zephyr-7b-beta". The markdown output has 2280 characters. But when I try the exact same notebook on Google Colab Pro and on my local Mac Mini M1 the output truncates at 485 characters without the markdown. Any suggestion for a fix? Thanks in advance.

rs8893 avatar Jun 04 '24 19:06 rs8893

Any update on this @joaomdmoura . If there is native support to handle this , then an example would be helpful. Appreciate the good work on crewAI

dhirajnair avatar Jul 04 '24 12:07 dhirajnair

Any solution for this ?

LeoAnt02 avatar Jul 14 '24 16:07 LeoAnt02

We are actively developing this issue now. Will share the progress with everyone within the following week

lorenzejay avatar Jul 24 '24 15:07 lorenzejay

Still looking forward to a resolution

codebrain001 avatar Jul 30 '24 12:07 codebrain001