AutoGPT icon indicating copy to clipboard operation
AutoGPT copied to clipboard

openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens

Open B1t3My8B1t opened this issue 1 year ago • 21 comments

⚠️ Search for existing issues first ⚠️

  • [X] I have searched the existing issues, and there is no existing issue for my problem

Which Operating System are you using?

Windows

Which version of Auto-GPT are you using?

Latest Release

GPT-3.5

Steps to reproduce 🕹

Building a business plan give me this error: openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 10134 tokens. Please reduce the length of the messages.

Current behavior 😯

CRITICISM: We need to ensure that our Python script is optimized for performance and that it is based on accurate and reliable information. We also need to be mindful of potential legal and regulatory challenges, and ensure that our technology infrastructure is secure and reliable. NEXT ACTION: COMMAND = write_to_file ARGUMENTS = {'filename': 'C:\Auto-GPT-0.3.0\autogpt\auto_gpt_workspace\newproject.py', 'text': 'def generate_improved_code(suggestions, code):\n # TODO: Add code here\n\n return improved_code\n'} SYSTEM: Command write_to_file returned: File written to successfully. Traceback (most recent call last): File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code exec(code, run_globals) File "C:\Auto-GPT-0.3.0\autogpt_main.py", line 5, in autogpt.cli.main() File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1130, in call return self.main(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1635, in invoke rv = super().invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke return __callback(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) File "C:\Auto-GPT-0.3.0\autogpt\cli.py", line 90, in main run_auto_gpt( File "C:\Auto-GPT-0.3.0\autogpt\main.py", line 171, in run_auto_gpt agent.start_interaction_loop() File "C:\Auto-GPT-0.3.0\autogpt\agent\agent.py", line 94, in start_interaction_loop assistant_reply = chat_with_ai( File "C:\Auto-GPT-0.3.0\autogpt\llm\chat.py", line 166, in chat_with_ai agent.summary_memory = update_running_summary( File "C:\Auto-GPT-0.3.0\autogpt\memory_management\summary_memory.py", line 114, in update_running_summary current_memory = create_chat_completion(messages, cfg.fast_llm_model) File "C:\Auto-GPT-0.3.0\autogpt\llm\llm_utils.py", line 166, in create_chat_completion response = api_manager.create_chat_completion( File "C:\Auto-GPT-0.3.0\autogpt\llm\api_manager.py", line 55, in create_chat_completion response = openai.ChatCompletion.create( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 226, in request resp, got_stream = self._interpret_response(result, stream) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 619, in _interpret_response self._interpret_response_line( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 682, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 10134 tokens. Please reduce the length of the messages. Press any key to continue . . .

Expected behavior 🤔

openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 10134 tokens. Please reduce the length of the messages.

I have followed this instructions and still havent worked: https://github.com/Significant-Gravitas/Auto-GPT/discussions/1061

Your prompt 📝

# Paste your prompt here

CRITICISM: We need to ensure that our Python script is optimized for performance and that it is based on accurate and reliable information. We also need to be mindful of potential legal and regulatory challenges, and ensure that our technology infrastructure is secure and reliable. NEXT ACTION: COMMAND = write_to_file ARGUMENTS = {'filename': 'C:\Auto-GPT-0.3.0\autogpt\auto_gpt_workspace\newproject.py', 'text': 'def generate_improved_code(suggestions, code):\n # TODO: Add code here\n\n return improved_code\n'} SYSTEM: Command write_to_file returned: File written to successfully. Traceback (most recent call last): File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code exec(code, run_globals) File "C:\Auto-GPT-0.3.0\autogpt_main.py", line 5, in autogpt.cli.main() File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1130, in call return self.main(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1635, in invoke rv = super().invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke return __callback(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) File "C:\Auto-GPT-0.3.0\autogpt\cli.py", line 90, in main run_auto_gpt( File "C:\Auto-GPT-0.3.0\autogpt\main.py", line 171, in run_auto_gpt agent.start_interaction_loop() File "C:\Auto-GPT-0.3.0\autogpt\agent\agent.py", line 94, in start_interaction_loop assistant_reply = chat_with_ai( File "C:\Auto-GPT-0.3.0\autogpt\llm\chat.py", line 166, in chat_with_ai agent.summary_memory = update_running_summary( File "C:\Auto-GPT-0.3.0\autogpt\memory_management\summary_memory.py", line 114, in update_running_summary current_memory = create_chat_completion(messages, cfg.fast_llm_model) File "C:\Auto-GPT-0.3.0\autogpt\llm\llm_utils.py", line 166, in create_chat_completion response = api_manager.create_chat_completion( File "C:\Auto-GPT-0.3.0\autogpt\llm\api_manager.py", line 55, in create_chat_completion response = openai.ChatCompletion.create( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 226, in request resp, got_stream = self._interpret_response(result, stream) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 619, in _interpret_response self._interpret_response_line( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 682, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 10134 tokens. Please reduce the length of the messages. Press any key to continue . . .

Your Logs 📒

<insert your logs here>

CRITICISM: We need to ensure that our Python script is optimized for performance and that it is based on accurate and reliable information. We also need to be mindful of potential legal and regulatory challenges, and ensure that our technology infrastructure is secure and reliable. NEXT ACTION: COMMAND = write_to_file ARGUMENTS = {'filename': 'C:\Auto-GPT-0.3.0\autogpt\auto_gpt_workspace\newproject.py', 'text': 'def generate_improved_code(suggestions, code):\n # TODO: Add code here\n\n return improved_code\n'} SYSTEM: Command write_to_file returned: File written to successfully. Traceback (most recent call last): File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code exec(code, run_globals) File "C:\Auto-GPT-0.3.0\autogpt_main.py", line 5, in autogpt.cli.main() File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1130, in call return self.main(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main rv = self.invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1635, in invoke rv = super().invoke(ctx) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke return __callback(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\click\decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) File "C:\Auto-GPT-0.3.0\autogpt\cli.py", line 90, in main run_auto_gpt( File "C:\Auto-GPT-0.3.0\autogpt\main.py", line 171, in run_auto_gpt agent.start_interaction_loop() File "C:\Auto-GPT-0.3.0\autogpt\agent\agent.py", line 94, in start_interaction_loop assistant_reply = chat_with_ai( File "C:\Auto-GPT-0.3.0\autogpt\llm\chat.py", line 166, in chat_with_ai agent.summary_memory = update_running_summary( File "C:\Auto-GPT-0.3.0\autogpt\memory_management\summary_memory.py", line 114, in update_running_summary current_memory = create_chat_completion(messages, cfg.fast_llm_model) File "C:\Auto-GPT-0.3.0\autogpt\llm\llm_utils.py", line 166, in create_chat_completion response = api_manager.create_chat_completion( File "C:\Auto-GPT-0.3.0\autogpt\llm\api_manager.py", line 55, in create_chat_completion response = openai.ChatCompletion.create( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\chat_completion.py", line 25, in create return super().create(*args, **kwargs) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 226, in request resp, got_stream = self._interpret_response(result, stream) File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 619, in _interpret_response self._interpret_response_line( File "C:\Users\xxxx\AppData\Local\Programs\Python\Python310\lib\site-packages\openai\api_requestor.py", line 682, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 10134 tokens. Please reduce the length of the messages. Press any key to continue . . .

B1t3My8B1t avatar May 08 '23 14:05 B1t3My8B1t

Not sure if there is a solution or workaround to this....my program exits whenever it hits this limit midway...

Rykimaruh avatar May 08 '23 16:05 Rykimaruh

yep shame here !!!what is this demon now ? how to fix this issue ?

gnosisd avatar May 08 '23 17:05 gnosisd

Same problem here, tried a lot o f things but no solution at the moment. I tried to ask autogpt to cut all files in max 4000 tokens before reading as it was told in a comment, but it did not work.

FX7495 avatar May 08 '23 18:05 FX7495

same issue here

samfreudiger avatar May 12 '23 12:05 samfreudiger

I am getting the same error on linux

moltra avatar May 12 '23 14:05 moltra

Also getting this, using VS Code + Containers

ZylerKade avatar May 13 '23 05:05 ZylerKade

Also getting this, using VS Code + Containers

+1

jacobweiss2305 avatar May 13 '23 14:05 jacobweiss2305

Also getting this, using VS Code + Containers

+1

Walidso avatar May 13 '23 15:05 Walidso

+1

nithin-nk avatar May 14 '23 03:05 nithin-nk

+1 Same Issue

kellytaylor avatar May 15 '23 19:05 kellytaylor

+1

sander110419 avatar May 15 '23 19:05 sander110419

+1

MonkeyVision avatar May 15 '23 23:05 MonkeyVision

+1

Gupich23 avatar May 16 '23 04:05 Gupich23

+1

sfresta avatar May 16 '23 09:05 sfresta

+1

anerib avatar May 16 '23 09:05 anerib

+1

chenqiaorui avatar May 16 '23 10:05 chenqiaorui

+1

shubham184 avatar May 16 '23 12:05 shubham184

screenshort Same issue here. Using GPT3.5 and continuous mode.

kei-ya-131 avatar May 16 '23 15:05 kei-ya-131

so there hasn't been any news regarding this issue yet?

Rykimaruh avatar May 16 '23 15:05 Rykimaruh

the issue is in summary_memory.py -> update_running_summary:

when adding "Latest Development:" to the prompt for the history, after running auto-gpt for a while there are more and more entires... need to limit the history which is kept, otherwise prompt gets too long

(that's at least the issue each time i run into this error)

anyone wants to have a go at a patch? (havent contributed any fixes yet, but done a few locally for testing, could have a go at it)

daice77 avatar May 17 '23 05:05 daice77

+1

a1264113629 avatar May 17 '23 17:05 a1264113629

the issue is in summary_memory.py -> update_running_summary:

when adding "Latest Development:" to the prompt for the history, after running auto-gpt for a while there are more and more entires... need to limit the history which is kept, otherwise prompt gets too long

(that's at least the issue each time i run into this error)

anyone wants to have a go at a patch? (havent contributed any fixes yet, but done a few locally for testing, could have a go at it)

+1, has intesest to try, thanks

wyang22 avatar May 18 '23 05:05 wyang22

  • 1 on this issue.

ericflecher avatar May 19 '23 15:05 ericflecher

Who has a summary of the game plan for handling aip_requestor.py and memory token overruns? I have not had any long term (more than 10 minutes) run go by without exhausting token limit and crashing instead of trying to automatically handle the exception or error and retry by auto condensing the prompts generated then trying again or opened a new agent to run part of the prompt and another for the rest of the prompt in chunks of XXX... anyhow - there has got to be some workarounds on this that are not too difficult to implement.

GoZippy avatar May 19 '23 17:05 GoZippy

Not sure if there is a solution or workaround to this....my program exits whenever it hits this limit midway...

any luck?

GoZippy avatar May 19 '23 17:05 GoZippy

Try changing api_manager.py to include a method for chat completion

import openai
from openai.error import InvalidRequestError

# ...

class ApiManager(metaclass=Singleton):
    # ...

    def create_chat_completion(
        self,
        messages: list,  # type: ignore
        model: str | None = None,
        temperature: float = None,
        max_tokens: int | None = None,
        deployment_id=None,
    ) -> str:
        cfg = Config()
        if temperature is None:
            temperature = cfg.temperature
        try:
            if deployment_id is not None:
                response = openai.ChatCompletion.create(
                    deployment_id=deployment_id,
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    api_key=cfg.openai_api_key,
                )
            else:
                response = openai.ChatCompletion.create(
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    api_key=cfg.openai_api_key,
                )
        except InvalidRequestError as e:
            # Handle the specific error, e.g., if it's a token limit error
            if e.code == "invalid_request" and "token limit" in e.message.lower():
                # Implement your logic to reduce the length of messages or split them into smaller parts
                # Modify messages or take appropriate action
                pass
            else:
                # Handle other specific error cases or fallback to a general error handling strategy
                pass
        else:
            if not hasattr(response, "error"):
                logger.debug(f"Response: {response}")
                prompt_tokens = response.usage.prompt_tokens
                completion_tokens = response.usage.completion_tokens
                self.update_cost(prompt_tokens, completion_tokens, model)
        return response

Lets see if something like this would work.

GoZippy avatar May 19 '23 17:05 GoZippy

Try changing api_manager.py to include a method for chat completion

import openai
from openai.error import InvalidRequestError

# ...

class ApiManager(metaclass=Singleton):
    # ...

    def create_chat_completion(
        self,
        messages: list,  # type: ignore
        model: str | None = None,
        temperature: float = None,
        max_tokens: int | None = None,
        deployment_id=None,
    ) -> str:
        cfg = Config()
        if temperature is None:
            temperature = cfg.temperature
        try:
            if deployment_id is not None:
                response = openai.ChatCompletion.create(
                    deployment_id=deployment_id,
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    api_key=cfg.openai_api_key,
                )
            else:
                response = openai.ChatCompletion.create(
                    model=model,
                    messages=messages,
                    temperature=temperature,
                    max_tokens=max_tokens,
                    api_key=cfg.openai_api_key,
                )
        except InvalidRequestError as e:
            # Handle the specific error, e.g., if it's a token limit error
            if e.code == "invalid_request" and "token limit" in e.message.lower():
                # Implement your logic to reduce the length of messages or split them into smaller parts
                # Modify messages or take appropriate action
                pass
            else:
                # Handle other specific error cases or fallback to a general error handling strategy
                pass
        else:
            if not hasattr(response, "error"):
                logger.debug(f"Response: {response}")
                prompt_tokens = response.usage.prompt_tokens
                completion_tokens = response.usage.completion_tokens
                self.update_cost(prompt_tokens, completion_tokens, model)
        return response

Lets see if something like this would work.

Hi I have added your code to the api_manager.py file, but I am still encountering the following error. Please take a look:

Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/etc/Auto-GPT/autogpt/main.py", line 5, in autogpt.cli.main() File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1130, in call return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1635, in invoke rv = super().invoke(ctx) ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/etc/Auto-GPT/autogpt/cli.py", line 90, in main run_auto_gpt( File "/etc/Auto-GPT/autogpt/main.py", line 186, in run_auto_gpt agent.start_interaction_loop() File "/etc/Auto-GPT/autogpt/agent/agent.py", line 113, in start_interaction_loop assistant_reply = chat_with_ai( ^^^^^^^^^^^^^ File "/etc/Auto-GPT/autogpt/llm/chat.py", line 165, in chat_with_ai agent.summary_memory = update_running_summary( ^^^^^^^^^^^^^^^^^^^^^^^ File "/etc/Auto-GPT/autogpt/memory_management/summary_memory.py", line 128, in update_running_summary current_memory = create_chat_completion(messages, cfg.fast_llm_model) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/etc/Auto-GPT/autogpt/llm/llm_utils.py", line 166, in create_chat_completion response = api_manager.create_chat_completion( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/etc/Auto-GPT/autogpt/llm/api_manager.py", line 55, in create_chat_completion response = openai.ChatCompletion.create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create response, _, api_key = requestor.request( ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/openai/api_requestor.py", line 226, in request resp, got_stream = self._interpret_response(result, stream) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/openai/api_requestor.py", line 619, in _interpret_response self._interpret_response_line( File "/usr/local/lib/python3.11/site-packages/openai/api_requestor.py", line 682, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: This model's maximum context length is 4097 tokens. However, your messages resulted in 15707 tokens. Please reduce the length of the messages.

kei-ya-131 avatar May 20 '23 14:05 kei-ya-131

Yes, there's updates on my fork I'm working on and updates to the scripts that call api_manager within the LLM folder that need fixes... I think I updated my fork last night but I might not have pushed from my local to GitHub. I'll check later today when I get home.

GoZippy avatar May 20 '23 16:05 GoZippy

+1

arvin-chou avatar May 21 '23 04:05 arvin-chou

+1

yanivfi1978 avatar May 21 '23 19:05 yanivfi1978