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

feat: ChatAgent refactoring #1142

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Conversation

Wendong-Fan
Copy link
Member

@Wendong-Fan Wendong-Fan commented Nov 1, 2024

Description

Refactor ChatAgent

  • Support taking Callable as tools input to ChatAgent
  • Support prompt based tool calling for all models
  • Support tool add, remove, list
  • Support single_iteration mode to let user decide whether to loop the LLM running
  • Refactor the step method to make it more modular
  • Record external_tools calling to memory

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of example)

Implemented Tasks

  • Subtask 1
  • Subtask 2
  • Subtask 3

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

@Wendong-Fan Wendong-Fan changed the title feat: ChatAgent refactoring feat: ChatAgent refactoring (Draft) Nov 1, 2024
@Wendong-Fan Wendong-Fan self-assigned this Nov 17, 2024
@Wendong-Fan Wendong-Fan added enhancement New feature or request New Feature labels Nov 17, 2024
@Wendong-Fan Wendong-Fan added this to the Sprint 16 milestone Nov 17, 2024
@Wendong-Fan Wendong-Fan linked an issue Nov 17, 2024 that may be closed by this pull request
2 tasks
@@ -172,6 +176,8 @@ def __init__(
self.role_type: RoleType = (
getattr(system_message, 'role_type', None) or RoleType.ASSISTANT
Copy link
Collaborator

Choose a reason for hiding this comment

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

'Assistant' or 'assistant' all of them is ok?

Comment on lines +252 to +276
def add_tool(
self, tool: Union[FunctionTool, Callable], is_external: bool = False
) -> None:
r"""Add a tool to the agent, specifying if it's an external tool."""
initialized_tool = self._initialize_tools([tool])

if is_external:
self.external_tools = (
self.external_tools or []
) + initialized_tool
self.external_tool_names.extend(
tool.get_function_name() for tool in initialized_tool
)
else:
self.tools = (self.tools or []) + initialized_tool

# Reinitialize func_dict to include all tools
self.all_tools = (self.tools or []) + (self.external_tools or [])
self.func_dict = {
tool.get_function_name(): tool.func for tool in self.all_tools
}
self.tool_dict = {
tool.get_function_name(): tool for tool in self.all_tools
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

def add_tool(
    self, tool: Union[FunctionTool, Callable], is_external: bool = False
) -> None:
    """
    Add a tool to the agent, specifying if it's an external tool.

    Args:
        tool: The tool to be added, either a FunctionTool instance or a callable.
        is_external: Flag indicating if the tool is external.
    """
    # Initialize the tool
    initialized_tool = self._initialize_tools([tool])
    
    # Helper function to merge tools
    def merge_tools(existing_tools: List, new_tools: List) -> List:
        return (existing_tools or []) + new_tools

    # Update tools or external tools based on is_external flag
    if is_external:
        self.external_tools = merge_tools(self.external_tools, initialized_tool)
        self.external_tool_names.extend(
            tool.get_function_name() for tool in initialized_tool
        )
    else:
        self.tools = merge_tools(self.tools, initialized_tool)

    # Rebuild all_tools, func_dict, and tool_dict
    self.all_tools = merge_tools(self.tools, self.external_tools)
    self.func_dict = {tool.get_function_name(): tool.func for tool in self.all_tools}
    self.tool_dict = {tool.get_function_name(): tool for tool in self.all_tools}

Copy link
Collaborator

Choose a reason for hiding this comment

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

code is ok, but it's not easy to read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request New Feature
Projects
Status: No status
2 participants