Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2643 - groupchat model registration #2696

Merged

Conversation

Matteo-Frattaroli
Copy link

@Matteo-Frattaroli Matteo-Frattaroli commented May 15, 2024

Why are these changes needed?

When using a custom model client with GroupChatManager, the underlying GroupChat internally defines two ConversableAgents that receive the llm_config passed to GroupChatManager but don't handle model registration (and there's no way to do so externally if not subclassing GroupChat and overriding the two methods that do so). This contribution allows a user to pass a ModelClient or a list of ModelClient through a 'model_client_cls' attribute used for automatic registration of custom models only.

Related issue number

Closes #2643

Checks

@Matteo-Frattaroli
Copy link
Author

@microsoft-github-policy-service agree

autogen/agentchat/groupchat.py Outdated Show resolved Hide resolved
website/docs/topics/groupchat/using_custom_models.md Outdated Show resolved Hide resolved
website/docs/topics/groupchat/using_custom_models.md Outdated Show resolved Hide resolved
autogen/agentchat/groupchat.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented May 15, 2024

Codecov Report

Attention: Patch coverage is 18.91892% with 30 lines in your changes missing coverage. Please review.

Project coverage is 20.41%. Comparing base (6279247) to head (40131dc).
Report is 107 commits behind head on main.

Files Patch % Lines
autogen/agentchat/groupchat.py 18.91% 30 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2696       +/-   ##
===========================================
- Coverage   32.90%   20.41%   -12.49%     
===========================================
  Files          94       95        +1     
  Lines       10235    10701      +466     
  Branches     2193     2294      +101     
===========================================
- Hits         3368     2185     -1183     
- Misses       6580     8361     +1781     
+ Partials      287      155      -132     
Flag Coverage Δ
unittests 20.40% <18.91%> (-12.50%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Matteo-Frattaroli Matteo-Frattaroli force-pushed the fix/groupchat-model-registration branch from 8065f72 to d120cbe Compare May 17, 2024 17:13
Copy link
Collaborator

@ekzhu ekzhu left a comment

Choose a reason for hiding this comment

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

Add a unit test with mocks to check if the select speaker agent has gotten the propagated custom client.

@Matteo-Frattaroli Matteo-Frattaroli force-pushed the fix/groupchat-model-registration branch from 85f6672 to faf8b20 Compare May 21, 2024 18:18
@Matteo-Frattaroli
Copy link
Author

@ekzhu @sonichi @marklysze I completed all the changes you requested but after merging main several builds are failing because of a missing package (namely "packaging").

@ekzhu I see you submitted two commits before because of this bug on requests.

I'm pretty sure that as for the latter, this problem with the "packaging" library is not related to my code. Could you maybe take a look at your earliest convenience?

Do you think after solving this the PR could be approved?

@Matteo-Frattaroli Matteo-Frattaroli force-pushed the fix/groupchat-model-registration branch from e8e8214 to e83eef1 Compare May 23, 2024 18:36
@marklysze
Copy link
Collaborator

@Matteo-Frattaroli, thanks for the hard work on this! Your code has come at just the right time as I'm testing a new custom client class for Mistral and I wanted to test Group Chat :).

My first run worked with your code as is, which is great... I'll test more...

@Matteo-Frattaroli
Copy link
Author

@marklysze thank you for the positive feedback!

As I'm sure you noticed though there is some problem with the CI which is why I rolled back the latest merge from master to try and isolate the problem locally. Once this is fixed I'll resolve the conflicts (which is really just about a method i extracted to create the agents) and maybe ask for a new review.

Meanwhile if you have any directions on the current issue I can try to help (even if I know that you're probably already looking into it and are certainly more capable of doing so than me)

@marklysze
Copy link
Collaborator

hi, thank you so much for your effort. i'm currently have issue when register custom model class and waiting to this PR to be merged. i've do some test with this PR and got some issue, can you please check it out:

  1. Got runtime error when using async chat since selector param has been removed from a_select_speaker fuction but a_run_chat function still passing the param
  2. Unable to pass kwargs when register custom model because in _register_client_from_config function, you calling register function without passing any param: agent.register_model_client(select_speaker_auto_model_client_cls). i think you can add a additional param when init grouchat (eg select_speaker_auto_model_client_cls_kwarg) and then pass it to register model function as kwarg
    i'm supper new to this project so not sure if i'm do it right and thank you again for your work here

Hey @thaind-taureau, thanks for your comment...

For #1, I've updated the async select speaker, a_select_speaker, are you able to test that, now?

For #2, can you show some code that you'd like to see so we can see if there's a way around it?

@thaind-taureau
Copy link

thaind-taureau commented Oct 9, 2024

Hi @marklysze

  1. im going to test it now
  2. this is the workaround i'm currently using. i just add model_client_cls_kwargs as dictionary and then override the last line of _register_client_from_config function:
class CustomGroupChat(GroupChat):
    def __init__(
            self,
            model_client_cls_kwargs: Optional[Dict] = None, # Add this parameter
            *args, **kwargs
    ):
        super().__init__(*args, **kwargs)
        self.model_client_cls_kwargs = model_client_cls_kwargs

# override _register_client_from_config
    def _register_client_from_config(self, agent: Agent, config: Dict):
           # change the last line of function, keep everything else
          ...
            agent.register_model_client(select_speaker_auto_model_client_cls, **self.model_client_cls_kwargs) # I change this line

@thaind-taureau
Copy link

@marklysze
Update for #1: tested the code and it's work. thank you

@marklysze
Copy link
Collaborator

Hi @marklysze

  1. im going to test it now
  2. this is the workaround i'm currently using. i just add model_client_cls_kwargs as dictionary and then override the last line of _register_client_from_config function:
class CustomGroupChat(GroupChat):
    def __init__(
            self,
            model_client_cls_kwargs: Optional[Dict] = None, # Add this parameter
            *args, **kwargs
    ):
        super().__init__(*args, **kwargs)
        self.model_client_cls_kwargs = model_client_cls_kwargs

# override _register_client_from_config
    def _register_client_from_config(self, agent: Agent, config: Dict):
           # change the last line of function, keep everything else
          ...
            agent.register_model_client(select_speaker_auto_model_client_cls, **self.model_client_cls_kwargs) # I change this line

Thanks for testing number 1 @thaind-taureau, I'm glad that it worked.

For number 2, are you able to provide an example of the kwargs you're passing in?

@marklysze
Copy link
Collaborator

Add a unit test with mocks to check if the select speaker agent has gotten the propagated custom client.

Hey @ekzhu - the test has been added, I've corrected the formatting

@rysweet
Copy link
Collaborator

rysweet commented Oct 10, 2024

This PR is against AutoGen 0.2. AutoGen 0.2 has been moved to the 0.2 branch. Please rebase your PR on the 0.2 branch or update it to work with the new AutoGen 0.4 that is now in main.

@rysweet rysweet added the awaiting-op-response Issue or pr has been triaged or responded to and is now awaiting a reply from the original poster label Oct 10, 2024
@thaind-taureau
Copy link

thaind-taureau commented Oct 11, 2024

Hi @marklysze

  1. im going to test it now
  2. this is the workaround i'm currently using. i just add model_client_cls_kwargs as dictionary and then override the last line of _register_client_from_config function:
class CustomGroupChat(GroupChat):
    def __init__(
            self,
            model_client_cls_kwargs: Optional[Dict] = None, # Add this parameter
            *args, **kwargs
    ):
        super().__init__(*args, **kwargs)
        self.model_client_cls_kwargs = model_client_cls_kwargs

# override _register_client_from_config
    def _register_client_from_config(self, agent: Agent, config: Dict):
           # change the last line of function, keep everything else
          ...
            agent.register_model_client(select_speaker_auto_model_client_cls, **self.model_client_cls_kwargs) # I change this line

Thanks for testing number 1 @thaind-taureau, I'm glad that it worked.

For number 2, are you able to provide an example of the kwargs you're passing in?

Hi @marklysze. Sorry for the late, but i'm not sure what you want to see. Please tell me if this not answer your question. Currently when using this workaround, i'm just passing a dictionary that conatin some extra object because my custom model class require some extra work , something like this:

model_client_cls_kwargs = {
    "user_id": user.id,
    "model": llm_model,
    "db_session": db_session
}
group_chat = CustomGroupChat(
    model_client_cls_kwargs=model_client_cls_kwargs
    ...
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.2 Issues which were filed before re-arch to 0.4 awaiting-op-response Issue or pr has been triaged or responded to and is now awaiting a reply from the original poster group chat/teams group-chat-related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: speaker_selection_agent does not get registered in GroupChat if the selector is a custom model client
9 participants