Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Examples of how to register a reply function #559

Closed
sonichi opened this issue Nov 5, 2023 · 3 comments · Fixed by #579
Closed

Examples of how to register a reply function #559

sonichi opened this issue Nov 5, 2023 · 3 comments · Fixed by #579
Assignees
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@sonichi
Copy link
Contributor

sonichi commented Nov 5, 2023

          @xmi1an you can hook in function that is called whenever an agent receives a message. 

Assuming you have two agents user_proxy and assistant, you can register a function with these agents that gets called with some parameters.

def print_messages(recipient, messages, sender, config): 
    if "callback" in config and  config["callback"] is not None:
        callback = config["callback"]
        callback(sender, recipient, messages[-1])
    print(f"Messages sent to: {recipient.name} | num messages: {len(messages)}")
    return False, None  # required to ensure the agent communication flow continues

user_proxy.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
)

assistant.register_reply(
    [autogen.Agent, None],
    reply_func=print_messages, 
    config={"callback": None},
) 

In the above, we register a print_messages message that is called each time the agent receive a message.

Originally posted by @victordibia in #478 (comment)

It's a good example to add to FAQ.

@afourney afourney added documentation Improvements or additions to documentation good first issue Good for newcomers labels Nov 6, 2023
@olaoluwasalami olaoluwasalami self-assigned this Nov 7, 2023
@abdullamatar
Copy link

Hi, can you please elaborate as to why returning False, None is required to continue the conversation flow? From the register_reply() Args documentation the following line is present under the trigger argument:

- If a callable is provided, the reply function will be called when the callable returns True.

However as noted above the reply_func must return False, None and the trigger in this case is the autogen.Agent class.

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 31, 2024

Hi, can you please elaborate as to why returning False, None is required to continue the conversation flow? From the register_reply() Args documentation the following line is present under the trigger argument:

  • If a callable is provided, the reply function will be called when the callable returns True.

However as noted above the reply_func must return False, None and the trigger in this case is the autogen.Agent class.

The triggers decides whether to enter the registered reply function. In this case the trigger basically says any message from any agent is accepted and will enter the reply function.

Autogen uses a pipeline of register reply functions to handle incoming messages. If one reply function gets triggered, it will process the message, and returns a boolean signal. If the signal is True, then the rest of the pipeline gets short-circuited and the message generated from the triggered reply funciton will be returned. If the signal is False, then the message continue to the next reply function in the pipeline.

In this example, the message gets passed on to the next reply function, because the signal is False. However the message was processed (i.e., printed). It is just that the control gets handed back to the pipeline.

@abdullamatar
Copy link

Thank you :)

@microsoft microsoft locked and limited conversation to collaborators Feb 2, 2024
@ekzhu ekzhu converted this issue into discussion #1517 Feb 2, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants