cpacker / MemGPT

Letta (fka MemGPT) is a framework for creating stateful LLM services.
https://letta.com
Apache License 2.0
11.88k stars 1.3k forks source link

[Better Prompt] For `maximum context length` Exception #271

Closed 52cs closed 11 months ago

52cs commented 11 months ago

Let's make a better prompt to solve issue https://github.com/cpacker/MemGPT/issues/252

The original error_msg is as following: https://github.com/cpacker/MemGPT/blob/main/memgpt/agent.py#L561-L567

            try:
                function_response_string = function_to_call(**function_args)
                function_response = package_function_response(True, function_response_string)
                function_failed = False
            except Exception as e:
                error_msg = f"Error calling function {function_name} with args {function_args}: {str(e)}"
                printd(error_msg)

Let's enhance the prompt to conduct the llm do the right thing:

            try:
                function_response_string = function_to_call(**function_args)
                function_response = package_function_response(True, function_response_string)
                function_failed = False
            except Exception as e:
                error_msg = f"Error calling function {function_name} with args {function_args}: {str(e)}"

                if "maximum context length" in str(e):
                    error_msg = error_msg + "\n Do the following function chain in the next steps: <trim_messages> <recall_memory_search> <{function_name}>"

                printd(error_msg)

Note: The trim_messages function tool should be add into function signature.

Maybe you still want summarize_messages_inplace, hmm, maybe it is better, who knows. Just replace with and have some experiments to compare them.

Also, maybe you want to move the prompt described above into system prompt: Let's say something like this: Whenever you receive a failed function call with exceed maximum context length Error, you should do the following function chain <trim_messages> <recall_memory_search> <{the failed function name}>

vivi commented 11 months ago

The error in #252 is coming from the summarizer failing, which happens in step(), not handle_ai_response() https://github.com/cpacker/MemGPT/blob/main/memgpt/agent.py#L676

We just fixed the behavior of the summarizer too, which should fix #252.

52cs commented 11 months ago

hehe

52cs commented 11 months ago

Got it