Skip to content

Add compression agent#131

Closed
yiranwu0 wants to merge 26 commits into
mainfrom
compression
Closed

Add compression agent#131
yiranwu0 wants to merge 26 commits into
mainfrom
compression

Conversation

@yiranwu0

@yiranwu0 yiranwu0 commented Oct 6, 2023

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Add a compression feature in conversable_agent. By default, it will terminate the conversation if token count exceeds the max token count (To avoid token limit error). If compress_config is passed, it will start compressing and update conversation history when a certain token_count is reached.

Notes:

  • Will not support async compression in this PR.
  • The compression agent created here is only intended to compress chat histories, (but will not be used as independent Agent).
  • Token_count_utils haven't gone through a rigorous testing.
  • Compression in groupchat is not supported yet.

Related issue number

Checks

@AaronWard

Copy link
Copy Markdown
Contributor

@kevin666aa I will take a look at this some time later today, thanks for posting this PR!👌

@sonichi

sonichi commented Oct 7, 2023

Copy link
Copy Markdown
Contributor

The test fails.

@AaronWard

Copy link
Copy Markdown
Contributor

@kevin666aa Let me know if this is still in draft or if it's ready for review

@yiranwu0

yiranwu0 commented Oct 7, 2023

Copy link
Copy Markdown
Contributor Author

The test fails.

in the build workflow. why pip uninstall -y openai? It fails because openai is needed.

@Siafu

Siafu commented Oct 16, 2023

Copy link
Copy Markdown

I am currently testing this PR, it works for a bit but I eventually get this error.

    assert isinstance(compressed_message, str), f"compressed_message should be a string: {compressed_message}"
AssertionError: compressed_message should be a string: {
  "role": "assistant",
  "content": null,
  "function_call": {
    "name": "subprocess",
    "arguments": "{\n  \"command\": \"nx generate @nrwl/react:component CampaignList --project=my-app\"\n}"
  }
}

I have the following function defined in my code.

def run_cli_command(command):
    try:
        # Run the CLI command in a separate process
        process = subprocess.Popen(
            command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        # Wait for the process to complete and capture the output
        stdout, stderr = process.communicate()

        # Check the return code to see if the command was successful
        return_code = process.returncode
        if return_code == 0:
            print("Command executed successfully.")
            print("Standard Output:")
            print(stdout)
        else:
            print("Command failed with the following error:")
            print(stderr)

    except Exception as e:
        print("An error occurred:", e)
   "functions": [
        {
            "name": "subprocess",
            "description": "run nx command in subprocess.",
            "parameters": {
                "type": "object",
                "properties": {
                    "command": {
                        "type": "string",
                        "description": "Valid nx command to execute.",
                    }
                },
                "required": ["command"],
            },
        },
    ],
[<OpenAIObject at 0x10f833c50> JSON: {
  "index": 0,
  "message": {
    "role": "assistant",
    "content": "SYSTEM:\nCompressed Content of Previous Chat:\n\nUSER: I made a mistake with the previous command and want to go back.\n\nASSISTANT: None\n\nUSER: Make sure to provide the \"command\" argument when calling the subprocess function.\n\nASSISTANT: None\n\nUSER: Oops, I made a mistake with the previous command. I want to go back.\n\nASSISTANT: None\n\nUSER: using nx nextjs generator create a component for the \"hive.ai\" project\n\nASSISTANT: None\n\nFUNCTION_CALL: subprocess - Execute \"nx g @nrwl/next:component hive-ai\"\n\nASSISTANT: None\n\nFUNCTION_CALL: subprocess - Execute \"nx g @nrwl/next:component hive-ai\"\n\nFUNCTION_RETURN (from \"func_name: subprocess\"): None\n\nFUNCTION_RETURN (from \"func_name: subprocess\"): None\n\nASSISTANT: Provide command to create component for \"hive.ai\" using Nx Next.js generator\n\nUSER: Provide command to create component for \"hive.ai\" using Nx Next.js generator\n\nASSISTANT: use \"nx g @nrwl/next:component --project=hive.ai\"\n\nUSER: use \"nx g @nrwl/next:component --project=hive.ai\"\n\nASSISTANT: None\n\nFUNCTION_CALL: subprocess - Execute \"nx g @nrwl/next:component --project=hive.ai\"\n\nASSISTANT: None\n\nFUNCTION_CALL: subprocess - Execute \"nx g @nrwl/next:component --project=hive.ai\"\n\nFUNCTION_RETURN (from \"func_name: subprocess\"): None\n\nFUNCTION_RETURN (from \"func_name: subprocess\"): None",
    "function_call": {
      "name": "subprocess",
      "arguments": "{\n  \"command\": \"nx g @nrwl/next:component hive-ai\"\n}"
    }
  },
  "finish_reason": "function_call"
}]

@yiranwu0

yiranwu0 commented Oct 17, 2023

Copy link
Copy Markdown
Contributor Author

@AaronWard @Siafu @sonichi I made several changes to the compression agent. I fix several bugs and revise the prompt. It would be great if you can try out the compression notebook and maybe test with your own examples?

@Siafu Let me know if this fixes your issue.

@sonichi

sonichi commented Oct 17, 2023

Copy link
Copy Markdown
Contributor

Does this PR affect the default behavior of the ConversableAgent? If so, a wide-scale regression test is needed.
In general, any change to the core functionality of ConversableAgent needs to go through scrutiny and has a chance of not getting merged.
If the goal is to get merged fast, please consider alternative options.

@yiranwu0

yiranwu0 commented Oct 17, 2023

Copy link
Copy Markdown
Contributor Author

Does this PR affect the default behavior of the ConversableAgent? If so, a wide-scale regression test is needed. In general, any change to the core functionality of ConversableAgent needs to go through scrutiny and has a chance of not getting merged. If the goal is to get merged fast, please consider alternative options.

This PR will affect the default behavior at very minimal level: The newly registered on_token_limit will terminate the conversation if it has found the token limit exceeds the max_token of the LLM. Basically catch the ratelimit before call API.
This can also be set configurable: the on_token_limit will be registered only when the user wants it.

Other than this, it will not affect default behavior at all. The compression is enabled only if the user wants to use the compression feature and passes in compress_config.

logger.error(error_msg)
raise AssertionError(error_msg)

if messages is None:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I removed the two lines here (need to confirm):

        if messages is None:
            messages = self._oai_messages[sender]

Why we can delete this:
The two lines deleted is in every generate_<>_reply function. So when both messages and sender are passed to a subsequent generate_<>_reply, it will perform the same logic.

final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])

Why needed for compression: Compression will modify self._oai_messages, and it is expected that generate_oai_reply will use the updated messages from self._oai_messages. With the two lines, the messages will not be None and the updated self._oai_messages will not be used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both justifications are not clear to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to only remove this when compress_config is provided. This ensures the original logic is unchanged in the default seting

@sonichi

sonichi commented Oct 17, 2023

Copy link
Copy Markdown
Contributor

Is there an option of not altering ConversableAgent? It will lower the bar of merging. Otherwise, I really have to ask you for a long list of tests and it will take me a long time to finish the review.

@Siafu

Siafu commented Oct 17, 2023

Copy link
Copy Markdown

@AaronWard @Siafu @sonichi I made several changes to the compression agent. I fix several bugs and revise the prompt. It would be great if you can try out the compression notebook and maybe test with your own examples?

@Siafu Let me know if this fixes your issue.

testing now

@qingyun-wu

Copy link
Copy Markdown
Contributor

@kevin666aa Thanks for the nice PR. The compression feature is very important and asked by many users. But this PR involves changes of logic in ConversableAgent, the consequences of which are unclear yet. To be safe and to merge this PR sooner, I will suggest creating a new class by inheriting theConversableAgent instead of directly revising it. E.g., You can create a CompressibleAgent in the contrib folder. This also allows the users to start experimenting with this once it is merged. Once we later confirm it indeed does not affect the existing utility of the ConverableAgent and the compression functionality is well received, we can integrate it into the ConversableAgent.

@AaronWard

Copy link
Copy Markdown
Contributor

But this PR involves changes of logic in ConversableAgent, the consequences of which are unclear yet.

@qingyun-wu this should be apparent with rigid and comprehensive unit testing. The impact of changes to the code should be measurable if there is any hope for longevity for any project. I can begin writing some tests but in all fairness ConversableAgent is pretty large so this should be a group effort

@sonichi

sonichi commented Oct 22, 2023

Copy link
Copy Markdown
Contributor

@qingyun-wu @AaronWard I agree with both of you. We should add tests, and before we have the tests in place, let's be cautious when modifying the core. BTW @afourney is working on tests too.

@yiranwu0

Copy link
Copy Markdown
Contributor Author

Thanks, then I will close this PR and add a compressible agent in the contrib first

@yiranwu0 yiranwu0 closed this Oct 22, 2023
@AaronWard AaronWard mentioned this pull request Oct 23, 2023
3 tasks
@yiranwu0 yiranwu0 mentioned this pull request Oct 26, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants