Skip to content

Commit 8033fc6

Browse files
initate chats enhancement (#2404)
Co-authored-by: Chi Wang <[email protected]>
1 parent d307818 commit 8033fc6

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

autogen/agentchat/chat.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ def initiate_chats(chat_queue: List[Dict[str, Any]]) -> List[ChatResult]:
171171
- `"carryover"` - It can be used to specify the carryover information to be passed
172172
to this chat. If provided, we will combine this carryover with the "message" content when
173173
generating the initial chat message in `generate_init_message`.
174+
- `"finished_chat_indexes_to_exclude_from_carryover"` - It can be used by specifying a list of indexes of the finished_chats list,
175+
from which to exclude the summaries for carryover. If 'finished_chat_indexes_to_exclude_from_carryover' is not provided or an empty list,
176+
then summary from all the finished chats will be taken.
174177
Returns:
175178
(list): a list of ChatResult objects corresponding to the finished chats in the chat_queue.
176179
"""
@@ -182,9 +185,16 @@ def initiate_chats(chat_queue: List[Dict[str, Any]]) -> List[ChatResult]:
182185
while current_chat_queue:
183186
chat_info = current_chat_queue.pop(0)
184187
_chat_carryover = chat_info.get("carryover", [])
188+
finished_chat_indexes_to_exclude_from_carryover = chat_info.get(
189+
"finished_chat_indexes_to_exclude_from_carryover", []
190+
)
191+
185192
if isinstance(_chat_carryover, str):
186193
_chat_carryover = [_chat_carryover]
187-
chat_info["carryover"] = _chat_carryover + [r.summary for r in finished_chats]
194+
chat_info["carryover"] = _chat_carryover + [
195+
r.summary for i, r in enumerate(finished_chats) if i not in finished_chat_indexes_to_exclude_from_carryover
196+
]
197+
188198
__post_carryover_processing(chat_info)
189199
sender = chat_info["sender"]
190200
chat_res = sender.initiate_chat(**chat_info)

autogen/agentchat/conversable_agent.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,23 @@ def _reflection_with_llm(
11801180
response = self._generate_oai_reply_from_client(llm_client=llm_client, messages=messages, cache=cache)
11811181
return response
11821182

1183+
def _check_chat_queue_for_sender(self, chat_queue: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
1184+
"""
1185+
Check the chat queue and add the "sender" key if it's missing.
1186+
1187+
Args:
1188+
chat_queue (List[Dict[str, Any]]): A list of dictionaries containing chat information.
1189+
1190+
Returns:
1191+
List[Dict[str, Any]]: A new list of dictionaries with the "sender" key added if it was missing.
1192+
"""
1193+
chat_queue_with_sender = []
1194+
for chat_info in chat_queue:
1195+
if chat_info.get("sender") is None:
1196+
chat_info["sender"] = self
1197+
chat_queue_with_sender.append(chat_info)
1198+
return chat_queue_with_sender
1199+
11831200
def initiate_chats(self, chat_queue: List[Dict[str, Any]]) -> List[ChatResult]:
11841201
"""(Experimental) Initiate chats with multiple agents.
11851202
@@ -1189,16 +1206,13 @@ def initiate_chats(self, chat_queue: List[Dict[str, Any]]) -> List[ChatResult]:
11891206
11901207
Returns: a list of ChatResult objects corresponding to the finished chats in the chat_queue.
11911208
"""
1192-
_chat_queue = chat_queue.copy()
1193-
for chat_info in _chat_queue:
1194-
chat_info["sender"] = self
1209+
_chat_queue = self._check_chat_queue_for_sender(chat_queue)
11951210
self._finished_chats = initiate_chats(_chat_queue)
11961211
return self._finished_chats
11971212

11981213
async def a_initiate_chats(self, chat_queue: List[Dict[str, Any]]) -> Dict[int, ChatResult]:
1199-
_chat_queue = chat_queue.copy()
1200-
for chat_info in _chat_queue:
1201-
chat_info["sender"] = self
1214+
1215+
_chat_queue = self._check_chat_queue_for_sender(chat_queue)
12021216
self._finished_chats = await a_initiate_chats(_chat_queue)
12031217
return self._finished_chats
12041218

0 commit comments

Comments
 (0)