Skip to content

Commit

Permalink
websurfer: copy entire message context only when last message is tool…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
Hedrekao committed Nov 25, 2024
1 parent b0d05e1 commit dc6687f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion autogen/agentchat/contrib/web_surfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit dc6687f

Please sign in to comment.