microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
34.14k stars 4.94k forks source link

Discussion about nested_chats design #3314

Open lfygh opened 3 months ago

lfygh commented 3 months ago

Discussed in https://github.com/microsoft/autogen/discussions/3292

Originally posted by **lfygh** August 5, 2024 The document describes that nested chat is a powerful conversation pattern that allows you to package complex workflows into a single agent. I'm a bit confused about the nested chat design. The nested chat is implemented by registering the reply_func_from_nested_chats method. In this method, only the last_msg is used to initialize a new chat, causing the loss of other chat history. I feel that this is not consistent with the design of the generate_reply method, which accepts a message list. ```python def _summary_from_nested_chats( chat_queue: List[Dict[str, Any]], recipient: Agent, messages: Union[str, Callable], sender: Agent, config: Any ) -> Tuple[bool, str]: last_msg = messages[-1].get("content") chat_to_run = [] for i, c in enumerate(chat_queue): current_c = c.copy() if current_c.get("sender") is None: current_c["sender"] = recipient message = current_c.get("message") # If message is not provided in chat_queue, we by default use the last message from the original chat history as the first message in this nested chat (for the first chat in the chat queue). # NOTE: This setting is prone to change. if message is None and i == 0: message = last_msg if callable(message): message = message(recipient, messages, sender, config) # We only run chat that has a valid message. NOTE: This is prone to change dependin on applications. if message: current_c["message"] = message chat_to_run.append(current_c) if not chat_to_run: return True, None res = initiate_chats(chat_to_run) return True, res[-1].summary ``` If I want to inject some context before the conversation, it won't work. ``` llm_config = { "config_list": [{"model": "gpt-4o-mini", "api_key": os.environ["OPENAI_API_KEY"]}]} agent = ConversableAgent( name="Agent", llm_config=llm_config, human_input_mode="NEVER", ) agent1 = ConversableAgent( name="Agent1",llm_config=llm_config,human_input_mode="NEVER", ) agent.register_nested_chats( [{ "recipient": agent1,"max_turns": 1 }], trigger=lambda sender: sender not in [agent1, agent2], ) r = agent.generate_reply([ { "role": "user","content": "hi,what's your name?"}, {"role": "system","content": "my name is agent"}, {"role": "user","content": "what's your name?"}, ]) print(r) ``` ```python ******************************************************************************** Agent (to Agent1): what's your name? -------------------------------------------------------------------------------- Agent1 (to Agent): I'm called Assistant! How can I help you today? ``` _summary_from_nested_chats should handle the message list, right? or something i'm wrong?
smallQQ0227 commented 5 days ago

Strongly Agree! And I open an new issue#issue