From 5ec67461e4033a5947e8dc01af700b52a4461ca0 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 12 Jun 2024 10:20:23 +0200 Subject: [PATCH 1/5] wip --- .../captn_agents/backend/tools/_functions.py | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/captn/captn_agents/backend/tools/_functions.py b/captn/captn_agents/backend/tools/_functions.py index 18baae44..dd13f78f 100644 --- a/captn/captn_agents/backend/tools/_functions.py +++ b/captn/captn_agents/backend/tools/_functions.py @@ -686,10 +686,14 @@ def _format_last_message(url: str, summary: Summary) -> str: def _is_termination_msg(x: Dict[str, Optional[str]]) -> bool: + print("Inside is_termination_msg") content = x.get("content") + print(f"Content of the message: {content}") if content is None or not isinstance(content, str): + print("Content is None or not a string") return False - return ( + + result = ( content.startswith('{"type":"SUMMARY"') or content.startswith('{"type": "SUMMARY"') or "TERMINATE" in content @@ -697,6 +701,8 @@ def _is_termination_msg(x: Dict[str, Optional[str]]) -> bool: or "I GIVE UP" in content or content.strip() == "" ) + print(f"Is termination message: {result}") + return result def get_webpage_status_code(url: str) -> Optional[int]: @@ -765,6 +771,7 @@ def get_get_info_from_the_web_page( websurfer_navigator_llm_config: Optional[Dict[str, Any]] = None, timestamp: Optional[str] = None, max_retires_before_give_up_message: int = 7, + max_round: int = 50, ) -> Callable[[str, int, int], str]: fx = summarizer_llm_config, websurfer_llm_config, websurfer_navigator_llm_config @@ -833,6 +840,16 @@ def get_info_from_the_web_page( # is_termination_msg=_is_termination_msg, ) + groupchat = autogen.GroupChat( + agents=[web_surfer, web_surfer_navigator], + messages=[], + max_round=max_round, + speaker_selection_method="round_robin", + ) + manager = autogen.GroupChatManager( + groupchat=groupchat, + ) + initial_message = ( f"Time now is {timestamp_copy}." if timestamp_copy else "" ) @@ -844,9 +861,7 @@ def get_info_from_the_web_page( """ try: - web_surfer_navigator.initiate_chat( - web_surfer, message=initial_message - ) + manager.initiate_chat(recipient=web_surfer, message=initial_message) except Exception as e: print(f"Exception '{type(e)}' in initiating chat: {e}") @@ -868,8 +883,8 @@ def get_info_from_the_web_page( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - web_surfer.send( - retry_message, + manager.send( + message=retry_message, recipient=web_surfer_navigator, ) continue @@ -879,8 +894,8 @@ def get_info_from_the_web_page( Message to web_surfer: Please click on the link which you think is the most relevant for the task. After that, I will guide you through the next steps.""" # In this case, web_surfer_navigator is sending the message to web_surfer - web_surfer_navigator.send( - retry_message, + manager.send( + message=retry_message, recipient=web_surfer, ) continue @@ -899,8 +914,8 @@ def get_info_from_the_web_page( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - web_surfer.send( - retry_message, + manager.send( + message=retry_message, recipient=web_surfer_navigator, ) continue @@ -923,8 +938,8 @@ def get_info_from_the_web_page( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - web_surfer.send( - retry_message, + manager.send( + message=retry_message, recipient=web_surfer_navigator, ) @@ -935,7 +950,9 @@ def get_info_from_the_web_page( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - web_surfer.send(retry_message, recipient=web_surfer_navigator) + manager.send( + message=retry_message, recipient=web_surfer_navigator + ) except Exception as e: # todo: log the exception failure_message = str(e) From 3f25e6207d4a09c2012105b9e01945c2201e01e9 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 12 Jun 2024 10:50:21 +0200 Subject: [PATCH 2/5] wip --- .../captn_agents/backend/tools/_functions.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/captn/captn_agents/backend/tools/_functions.py b/captn/captn_agents/backend/tools/_functions.py index dd13f78f..3266f21a 100644 --- a/captn/captn_agents/backend/tools/_functions.py +++ b/captn/captn_agents/backend/tools/_functions.py @@ -840,11 +840,27 @@ def get_info_from_the_web_page( # is_termination_msg=_is_termination_msg, ) + def get_state_transition( + web_surfer: WebSurferAgent, web_surfer_navigator: AssistantAgent + ) -> Callable[[AssistantAgent, autogen.GroupChat], AssistantAgent]: + def state_transition( + last_speaker: AssistantAgent, groupchat: autogen.GroupChat + ) -> AssistantAgent: + # messages = groupchat.messages + + if last_speaker is web_surfer: + return web_surfer_navigator + return web_surfer + + return state_transition + groupchat = autogen.GroupChat( agents=[web_surfer, web_surfer_navigator], messages=[], max_round=max_round, - speaker_selection_method="round_robin", + speaker_selection_method=get_state_transition( + web_surfer, web_surfer_navigator + ), ) manager = autogen.GroupChatManager( groupchat=groupchat, @@ -885,7 +901,7 @@ def get_info_from_the_web_page( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=web_surfer, ) continue if last_message.strip() == "": @@ -916,7 +932,7 @@ def get_info_from_the_web_page( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=web_surfer, ) continue last_message = _format_last_message(url=url, summary=summary) @@ -940,7 +956,7 @@ def get_info_from_the_web_page( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=web_surfer, ) except Exception as e: @@ -950,9 +966,7 @@ def get_info_from_the_web_page( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - manager.send( - message=retry_message, recipient=web_surfer_navigator - ) + manager.send(message=retry_message, recipient=web_surfer) except Exception as e: # todo: log the exception failure_message = str(e) From 2bc317e0503995c1184da4b23c14d96b1a9fb641 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 12 Jun 2024 11:34:31 +0200 Subject: [PATCH 3/5] wip --- .../captn_agents/backend/tools/_functions.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/captn/captn_agents/backend/tools/_functions.py b/captn/captn_agents/backend/tools/_functions.py index 3266f21a..4cc42a26 100644 --- a/captn/captn_agents/backend/tools/_functions.py +++ b/captn/captn_agents/backend/tools/_functions.py @@ -846,10 +846,18 @@ def get_state_transition( def state_transition( last_speaker: AssistantAgent, groupchat: autogen.GroupChat ) -> AssistantAgent: - # messages = groupchat.messages + print("I am choosing the next speaker") + print(f"Last speaker: {last_speaker.name}") + messages = groupchat.messages # nosec: [B023] + + if _is_termination_msg(messages[-1]): + print("Last message is termination message") if last_speaker is web_surfer: return web_surfer_navigator + + if _is_termination_msg(messages[-1]): + return web_surfer_navigator return web_surfer return state_transition @@ -901,7 +909,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer, + recipient=web_surfer_navigator, ) continue if last_message.strip() == "": @@ -932,7 +940,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer, + recipient=web_surfer_navigator, ) continue last_message = _format_last_message(url=url, summary=summary) @@ -956,7 +964,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer, + recipient=web_surfer_navigator, ) except Exception as e: @@ -966,7 +974,9 @@ def state_transition( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - manager.send(message=retry_message, recipient=web_surfer) + manager.send( + message=retry_message, recipient=web_surfer_navigator + ) except Exception as e: # todo: log the exception failure_message = str(e) From c1ccb409930daf74f180a1f6920fd7a98deb9968 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 12 Jun 2024 15:28:39 +0200 Subject: [PATCH 4/5] In groupchat, manager needs to send and receive the first message when starting or continuing the converstion --- .../captn_agents/backend/tools/_functions.py | 49 ++++--------------- 1 file changed, 9 insertions(+), 40 deletions(-) diff --git a/captn/captn_agents/backend/tools/_functions.py b/captn/captn_agents/backend/tools/_functions.py index 4cc42a26..7fe05805 100644 --- a/captn/captn_agents/backend/tools/_functions.py +++ b/captn/captn_agents/backend/tools/_functions.py @@ -686,14 +686,12 @@ def _format_last_message(url: str, summary: Summary) -> str: def _is_termination_msg(x: Dict[str, Optional[str]]) -> bool: - print("Inside is_termination_msg") content = x.get("content") - print(f"Content of the message: {content}") if content is None or not isinstance(content, str): print("Content is None or not a string") return False - result = ( + return ( content.startswith('{"type":"SUMMARY"') or content.startswith('{"type": "SUMMARY"') or "TERMINATE" in content @@ -701,8 +699,6 @@ def _is_termination_msg(x: Dict[str, Optional[str]]) -> bool: or "I GIVE UP" in content or content.strip() == "" ) - print(f"Is termination message: {result}") - return result def get_webpage_status_code(url: str) -> Optional[int]: @@ -840,35 +836,11 @@ def get_info_from_the_web_page( # is_termination_msg=_is_termination_msg, ) - def get_state_transition( - web_surfer: WebSurferAgent, web_surfer_navigator: AssistantAgent - ) -> Callable[[AssistantAgent, autogen.GroupChat], AssistantAgent]: - def state_transition( - last_speaker: AssistantAgent, groupchat: autogen.GroupChat - ) -> AssistantAgent: - print("I am choosing the next speaker") - print(f"Last speaker: {last_speaker.name}") - messages = groupchat.messages # nosec: [B023] - - if _is_termination_msg(messages[-1]): - print("Last message is termination message") - - if last_speaker is web_surfer: - return web_surfer_navigator - - if _is_termination_msg(messages[-1]): - return web_surfer_navigator - return web_surfer - - return state_transition - groupchat = autogen.GroupChat( agents=[web_surfer, web_surfer_navigator], messages=[], max_round=max_round, - speaker_selection_method=get_state_transition( - web_surfer, web_surfer_navigator - ), + speaker_selection_method="round_robin", ) manager = autogen.GroupChatManager( groupchat=groupchat, @@ -885,13 +857,13 @@ def state_transition( """ try: - manager.initiate_chat(recipient=web_surfer, message=initial_message) + manager.initiate_chat(recipient=manager, message=initial_message) except Exception as e: print(f"Exception '{type(e)}' in initiating chat: {e}") for i in range(inner_retries): print(f"Inner retry {i + 1}/{inner_retries}") - last_message = str(web_surfer_navigator.last_message()["content"]) + last_message = str(groupchat.messages[-1]["content"]) try: if "I GIVE UP" in last_message: @@ -909,7 +881,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=manager, ) continue if last_message.strip() == "": @@ -917,10 +889,9 @@ def state_transition( Message to web_surfer: Please click on the link which you think is the most relevant for the task. After that, I will guide you through the next steps.""" - # In this case, web_surfer_navigator is sending the message to web_surfer manager.send( message=retry_message, - recipient=web_surfer, + recipient=manager, ) continue @@ -940,7 +911,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=manager, ) continue last_message = _format_last_message(url=url, summary=summary) @@ -964,7 +935,7 @@ def state_transition( ) manager.send( message=retry_message, - recipient=web_surfer_navigator, + recipient=manager, ) except Exception as e: @@ -974,9 +945,7 @@ def state_transition( current_retries=i, max_retires_before_give_up_message=max_retires_before_give_up_message, ) - manager.send( - message=retry_message, recipient=web_surfer_navigator - ) + manager.send(message=retry_message, recipient=manager) except Exception as e: # todo: log the exception failure_message = str(e) From 56610de32de5a10800bb3c0f0bf1e4673a13c41c Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 12 Jun 2024 15:31:37 +0200 Subject: [PATCH 5/5] Cleanup --- captn/captn_agents/backend/tools/_functions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/captn/captn_agents/backend/tools/_functions.py b/captn/captn_agents/backend/tools/_functions.py index 7fe05805..74e03d7b 100644 --- a/captn/captn_agents/backend/tools/_functions.py +++ b/captn/captn_agents/backend/tools/_functions.py @@ -688,9 +688,7 @@ def _format_last_message(url: str, summary: Summary) -> str: def _is_termination_msg(x: Dict[str, Optional[str]]) -> bool: content = x.get("content") if content is None or not isinstance(content, str): - print("Content is None or not a string") return False - return ( content.startswith('{"type":"SUMMARY"') or content.startswith('{"type": "SUMMARY"')