11from __future__ import annotations
22
3+ import asyncio
34import logging
45import sys
56
@@ -30,14 +31,14 @@ class Config:
3031
3132class AgenticStrategyV2 :
3233 def __init__ (
33- self ,
34- api_key : str ,
35- template_data : dict [str , str ],
36- system_prompt_template : str ,
37- user_prompt_template : str ,
38- agent_configs : list [AgentConfig ],
39- example_json : Union [str , dict [str , Any ]] = '{"output":"output text"}' ,
40- limit : Optional [int ] = None ,
34+ self ,
35+ api_key : str ,
36+ template_data : dict [str , str ],
37+ system_prompt_template : str ,
38+ user_prompt_template : str ,
39+ agent_configs : list [AgentConfig ],
40+ example_json : Union [str , dict [str , Any ]] = '{"output":"output text"}' ,
41+ limit : Optional [int ] = None ,
4142 ):
4243 self .__limit = limit
4344 self .__template_data = template_data
@@ -67,13 +68,14 @@ def __init__(
6768
6869 def execute (self , limit : Optional [int ] = None ) -> dict :
6970 agents_result = dict ()
71+ loop = asyncio .new_event_loop ()
7072 try :
7173 for index , agent in enumerate (self .__agents ):
7274 user_message = mustache_render (self .__user_prompt_template , self .__template_data )
7375 message_history = None
7476 agent_output = None
7577 for i in range (limit or self .__limit or sys .maxsize ):
76- agent_output = agent .run_sync (user_message , message_history = message_history )
78+ agent_output = loop . run_until_complete ( agent .run (user_message , message_history = message_history ) )
7779 message_history = agent_output .all_messages ()
7880 if getattr (agent_output .data , _COMPLETION_FLAG_ATTRIBUTE , False ):
7981 break
@@ -86,26 +88,27 @@ def execute(self, limit: Optional[int] = None) -> dict:
8688 return dict ()
8789
8890 if len (agents_result ) == 1 :
89- final_result = self .__summariser .run_sync (
91+ final_result = loop . run_until_complete ( self .__summariser .run (
9092 "From the actions taken by the assistant. Please give me the result." ,
9193 message_history = next (v for _ , v in agents_result .items ()).all_messages (),
92- )
94+ ))
9395 else :
9496 agent_summaries = []
9597 for agent_result in agents_result .values ():
96- agent_summary_result = self .__summariser .run_sync (
98+ agent_summary_result = loop . run_until_complete ( self .__summariser .run (
9799 "From the actions taken by the assistant. Please give me the result." ,
98100 message_history = agent_result .all_messages (),
99- )
101+ ))
100102 agent_summary = getattr (agent_summary_result .data , _MESSAGE_ATTRIBUTE , None )
101103 if agent_summary is None :
102104 continue
103105
104106 agent_summaries .append (agent_summary )
105107 agent_summary_list = "\n * " + "\n * " .join (agent_summaries )
106- final_result = self .__summariser .run_sync (
108+ final_result = loop . run_until_complete ( self .__summariser .run (
107109 "Please give me the result from the following summary of what the assistants have done."
108110 + agent_summary_list ,
109- )
111+ ))
110112
113+ loop .close ()
111114 return final_result .data .dict ()
0 commit comments