From 27ed1e78a55060f69e626963bab9195ea5c48dc0 Mon Sep 17 00:00:00 2001 From: hedrekao <82450784+Hedrekao@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:52:00 +0100 Subject: [PATCH 1/2] bugfix: create full copy of history (including last message) --- autogen/agentchat/contrib/web_surfer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/contrib/web_surfer.py b/autogen/agentchat/contrib/web_surfer.py index 1a2dd2e3236..290212bfa25 100644 --- a/autogen/agentchat/contrib/web_surfer.py +++ b/autogen/agentchat/contrib/web_surfer.py @@ -230,7 +230,7 @@ def _page_down() -> str: def _find_on_page_ctrl_f( search_string: Annotated[ str, "The string to search for on the page. This search string supports wildcards like '*'" - ] + ], ) -> str: find_result = self.browser.find_on_page(search_string) header, content = _browser_state() @@ -344,7 +344,7 @@ def generate_surfer_reply( # Clone the messages to give context self._assistant.chat_messages[self._user_proxy] = list() - history = messages[0 : len(messages) - 1] + history = messages[:] for message in history: self._assistant.chat_messages[self._user_proxy].append(message) From dc6687f077478a5c0ef7964cf01734460baac8ae Mon Sep 17 00:00:00 2001 From: hedrekao <82450784+Hedrekao@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:38:47 +0100 Subject: [PATCH 2/2] websurfer: copy entire message context only when last message is tool message --- autogen/agentchat/contrib/web_surfer.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/autogen/agentchat/contrib/web_surfer.py b/autogen/agentchat/contrib/web_surfer.py index 290212bfa25..7ceed7d7917 100644 --- a/autogen/agentchat/contrib/web_surfer.py +++ b/autogen/agentchat/contrib/web_surfer.py @@ -344,7 +344,17 @@ def generate_surfer_reply( # Clone the messages to give context self._assistant.chat_messages[self._user_proxy] = list() - history = messages[:] + + # If the last message is a tool message it has to be included in context, + # otherwise openAI will throw exception that not all tool calls are followed by corresponding tool messages + # In a case where the last message is not a tool message, we fallback to default behavior in the library + # which is copying all messages except the last one + # Issue is described more thoroughly in PR https://github.com/microsoft/autogen/pull/4050 + if messages[-1].get("role", "assistant") == "tool": + history = messages[:] + else: + history = messages[0 : len(messages) - 1] + for message in history: self._assistant.chat_messages[self._user_proxy].append(message)