Search code examples
pythonlarge-language-modelautogen

Autogen response in a variable


import autogen
from nicegui import ui, context
from uuid import uuid4

# AutoGen Configuration
config_list = [
    {
        'model': 'gpt-4',
        'api_key': '' 
    }
]
llm_config = {
    'seed': 42,
    'config_list': config_list,
    'temperature': 0.2
}

# Initialize AutoGen Agents
assistant = autogen.AssistantAgent(name='Albert', llm_config=llm_config)
user_proxy = autogen.UserProxyAgent(name='user_proxy', human_input_mode="NEVER", max_consecutive_auto_reply=1, is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"), code_execution_config={"work_dir": "web"}, llm_config=llm_config)

@ui.page('/')
def main():
    messages = []
    user_id = str(uuid4())  # Unique ID for each user session

    @ui.refreshable
    def chat_messages():
        for name, text in messages:
            ui.chat_message(text=text, name=name, sent=name == 'You')
        if context.get_client().has_socket_connection:
            ui.run_javascript('setTimeout(() => window.scrollTo(0, document.body.scrollHeight), 0)')


    async def send():
        user_message = task_input.value
        messages.append(('You', user_message))  # Append user's message to the messages list
        chat_messages.refresh()  # Refresh chat messages to display the latest message
        task_input.value = ''  # Clear the input field after sending the message

        try:
            response = await user_proxy.initiate_chat(assistant, message=user_message)
            if response and 'content' in response[0]:
                assistant_response = response[0]['content']
                messages.append(('Albert', assistant_response))  # Append assistant's response to messages
            else:
                messages.append(('Albert', "Assistant did not provide a response."))
        except Exception as e:
            messages.append(('Albert', f"Error: {e}"))
        finally:
            chat_messages.refresh()


    with ui.scroll_area().classes('w-full h-60 p-3 bg-white overflow-auto'):
        chat_messages()

    with ui.footer().style('position: fixed; left: 0; bottom: 0; width: 100%; background: white; padding: 10px; box-shadow: 0 -2px 5px rgba(0,0,0,0.1);'):
        task_input = ui.input().style('width: calc(100% - 100px);')
        ui.button('Send', on_click=send).style('width: 90px;')

ui.run(title='Chat with Albert')

trying to use this GUI over Autogen. However, I cannot figure out where the response is coming from? The response variable doesn't seem to have it. When there is an exception, it is printed in the UI, when it works well, Autogen prints the answer in the terminal but not the UI.


Solution

  • You need to pass your callback function via register_reply function of the Agent class to get or print the response on your web UI. Below is the concept of the code:

    def print_messages(recipient, messages, sender, config):
        # each time when the agent receive the message
        # do your own logic here
        messages.append((messages[-1]['name'], messages[-1]['content']))
        return False, None
    
    assistant.register_reply(
        [autogen.Agent, None],
        reply_func=print_messages, 
        config={"callback": None},
    )
    

    You may want to check out the offical document