- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.6k
Enhancing RolePlaying with custom ChatAgent support #3199
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
          
     Open
      
      
            ChenziqiAdam
  wants to merge
  5
  commits into
  camel-ai:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
ChenziqiAdam:main
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3fc5e7b
              
                Enhancing RolePlaying with custom ChatAgent support
              
              
                 002aecb
              
                Update docs/key_modules/societies.md with new features
              
              
                 78d2ca2
              
                Ensure functionality consistence, and fix redundant sys msg generatio…
              
              
                 04cf144
              
                Merge branch 'camel-ai:master' into main
              
              
                ChenziqiAdam 7957806
              
                Merge branch 'master' into main
              
              
                Saedbhati File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -81,6 +81,12 @@ class RolePlaying: | |
| stop_event (Optional[threading.Event], optional): Event to signal | ||
| termination of the agent's operation. When set, the agent will | ||
| terminate its execution. (default: :obj:`None`) | ||
| assistant_agent (ChatAgent, optional): A pre-configured ChatAgent to use as the | ||
| assistant. If provided, this will override the creation of a new assistant agent. | ||
| (default: :obj:`None`) | ||
| user_agent (ChatAgent, optional): A pre-configured ChatAgent to use as the | ||
| user. If provided, this will override the creation of a new user agent. | ||
| (default: :obj:`None`) | ||
| """ | ||
|  | ||
| def __init__( | ||
|  | @@ -106,6 +112,8 @@ def __init__( | |
| extend_task_specify_meta_dict: Optional[Dict] = None, | ||
| output_language: Optional[str] = None, | ||
| stop_event: Optional[threading.Event] = None, | ||
| assistant_agent: Optional[ChatAgent] = None, | ||
| user_agent: Optional[ChatAgent] = None, | ||
| ) -> None: | ||
| if model is not None: | ||
| logger.warning( | ||
|  | @@ -143,28 +151,36 @@ def __init__( | |
| **(sys_msg_generator_kwargs or {}), | ||
| ) | ||
|  | ||
| ( | ||
| init_assistant_sys_msg, | ||
| init_user_sys_msg, | ||
| sys_msg_meta_dicts, | ||
| ) = self._get_sys_message_info( | ||
| assistant_role_name, | ||
| user_role_name, | ||
| sys_msg_generator, | ||
| extend_sys_msg_meta_dicts=extend_sys_msg_meta_dicts, | ||
| ) | ||
|  | ||
| self.assistant_agent: ChatAgent | ||
| self.user_agent: ChatAgent | ||
| self.assistant_sys_msg: Optional[BaseMessage] | ||
| self.user_sys_msg: Optional[BaseMessage] | ||
|  | ||
| # Do not regenerate sys_msg when using custom agents | ||
| if self.assistant_agent is not None and self.user_agent is not None: | ||
| ( | ||
| init_assistant_sys_msg, | ||
| init_user_sys_msg, | ||
| sys_msg_meta_dicts, | ||
| ) = self._get_sys_message_info( | ||
| assistant_role_name, | ||
| user_role_name, | ||
| sys_msg_generator, | ||
| extend_sys_msg_meta_dicts=extend_sys_msg_meta_dicts, | ||
| ) | ||
| else: | ||
| init_assistant_sys_msg = assistant_agent.system_message | ||
| init_user_sys_msg = user_agent.system_message | ||
| 
      Comment on lines
    
      +172
     to 
      +173
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assistant_agent.system_message and user_agent.system_message could be None here, also lead to AttributeError | ||
|  | ||
| self._init_agents( | ||
| init_assistant_sys_msg, | ||
| init_user_sys_msg, | ||
| assistant_agent_kwargs=assistant_agent_kwargs, | ||
| user_agent_kwargs=user_agent_kwargs, | ||
| output_language=output_language, | ||
| stop_event=stop_event, | ||
| assistant_agent=assistant_agent, | ||
| user_agent=user_agent, | ||
| ) | ||
| self.critic: Optional[Union[CriticAgent, Human]] = None | ||
| self.critic_sys_msg: Optional[BaseMessage] = None | ||
|  | @@ -326,6 +342,8 @@ def _init_agents( | |
| user_agent_kwargs: Optional[Dict] = None, | ||
| output_language: Optional[str] = None, | ||
| stop_event: Optional[threading.Event] = None, | ||
| assistant_agent: Optional[ChatAgent] = None, | ||
| user_agent: Optional[ChatAgent] = None, | ||
| ) -> None: | ||
| r"""Initialize assistant and user agents with their system messages. | ||
|  | ||
|  | @@ -343,6 +361,12 @@ def _init_agents( | |
| stop_event (Optional[threading.Event], optional): Event to signal | ||
| termination of the agent's operation. When set, the agent will | ||
| terminate its execution. (default: :obj:`None`) | ||
| assistant_agent (ChatAgent, optional): A pre-configured ChatAgent to use as the | ||
| assistant. If provided, this will override the creation of a new assistant agent. | ||
| (default: :obj:`None`) | ||
| user_agent (ChatAgent, optional): A pre-configured ChatAgent to use as the | ||
| user. If provided, this will override the creation of a new user agent. | ||
| (default: :obj:`None`) | ||
| """ | ||
| if self.model is not None: | ||
| if assistant_agent_kwargs is None: | ||
|  | @@ -353,22 +377,36 @@ def _init_agents( | |
| user_agent_kwargs = {'model': self.model} | ||
| elif 'model' not in user_agent_kwargs: | ||
| user_agent_kwargs.update(dict(model=self.model)) | ||
|  | ||
| # Use provided assistant agent if available, otherwise create a new one | ||
| if assistant_agent is not None: | ||
| # Ensure functionality consistent | ||
| assistant_agent.output_language = output_language | ||
| assistant_agent.stop_event = stop_event | ||
| self.assistant_agent = assistant_agent | ||
|  | ||
| self.assistant_sys_msg = assistant_agent.system_message | ||
|         
                  ChenziqiAdam marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| else: | ||
| self.assistant_agent = ChatAgent( | ||
| init_assistant_sys_msg, | ||
|         
                  ChenziqiAdam marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| output_language=output_language, | ||
| stop_event=stop_event, | ||
| **(assistant_agent_kwargs or {}), | ||
| ) | ||
| self.assistant_sys_msg = self.assistant_agent.system_message | ||
|  | ||
| self.assistant_agent = ChatAgent( | ||
| init_assistant_sys_msg, | ||
| output_language=output_language, | ||
| stop_event=stop_event, | ||
| **(assistant_agent_kwargs or {}), | ||
| ) | ||
| self.assistant_sys_msg = self.assistant_agent.system_message | ||
|  | ||
| self.user_agent = ChatAgent( | ||
| init_user_sys_msg, | ||
| output_language=output_language, | ||
| stop_event=stop_event, | ||
| **(user_agent_kwargs or {}), | ||
| ) | ||
| self.user_sys_msg = self.user_agent.system_message | ||
| # Use provided user agent if available, otherwise create a new one | ||
| if user_agent is not None: | ||
| self.user_agent = user_agent | ||
| self.user_sys_msg = user_agent.system_message | ||
| else: | ||
| self.user_agent = ChatAgent( | ||
| init_user_sys_msg, | ||
| output_language=output_language, | ||
| stop_event=stop_event, | ||
| **(user_agent_kwargs or {}), | ||
| ) | ||
| self.user_sys_msg = self.user_agent.system_message | ||
|  | ||
| def _init_critic( | ||
| self, | ||
|  | @@ -727,4 +765,4 @@ def _is_multi_response(self, agent: ChatAgent) -> bool: | |
| and agent.model_backend.model_config_dict['n'] > 1 | ||
| ): | ||
| return True | ||
| return False | ||
| return False | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems self.assistant_agent and self.user_agent have not yet been assigned, which will lead to an AttributeError?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, Both self.assistant_agent and self.user_agent have not yet been initialized with ChatAgent objects at this point.