66
77import argparse
88import agp_bindings
9+ from agp_bindings import GatewayConfig
910
10- gateway = agp_bindings .Gateway ()
1111
12- async def run_agent (address ):
12+ async def run_agent (message , address , iterations ):
1313 agent = simple_autogen_app ()
1414
1515 local_organization = "cisco"
1616 local_namespace = "default"
1717 local_agent = "autogen"
1818
19- # Connect to the gateway server
20- local_agent_id = await gateway .create_agent (
21- local_organization , local_namespace , local_agent
22- )
23-
24- # Connect to the service and subscribe for the local name
25- _ = await gateway .connect (address , insecure = True )
26- await gateway .subscribe (
27- local_organization , local_namespace , local_agent , local_agent_id
28- )
29-
30- while True :
31- # receive messages
32- src , msg = await gateway .receive ()
33-
34- # handle received messages
35- result = await agent .initate_chat (msg )
36- print (result )
37-
38- # process response
39- result .inner_messages
40- weather_question = result .inner_messages [- 1 ].content [- 1 ].content .split (":" )
41- if weather_question [0 ] == "WEATHER" :
42- print ("about to send back: " , weather_question [1 ])
43- await gateway .publish_to (weather_question [1 ].encode (), src )
44-
45- def main ():
19+ remote_organization = "cisco"
20+ remote_namespace = "default"
21+ remote_agent = "langchain"
22+
23+ # create new gateway object
24+ gateway = await agp_bindings .Gateway .new (local_organization , local_namespace , local_agent )
25+
26+ # Configure gateway
27+ config = GatewayConfig (endpoint = address , insecure = True )
28+ gateway .configure (config )
29+
30+ # Connect to remote gateway server
31+ print (f"connecting to: { address } " )
32+ _ = await gateway .connect ()
33+
34+ # Get the local agent instance from env
35+ instance = "autogen_instance"
36+
37+ async with gateway :
38+ if message :
39+ # Create a route to the remote ID
40+ await gateway .set_route (remote_organization , remote_namespace , remote_agent )
41+
42+ # create a session
43+ session = await gateway .create_ff_session (agp_bindings .PyFireAndForgetConfiguration ())
44+
45+ for i in range (0 , iterations ):
46+ try :
47+ # Send the message
48+ await gateway .publish (
49+ session ,
50+ message .encode (),
51+ remote_organization ,
52+ remote_namespace ,
53+ remote_agent ,
54+ )
55+ print (f"{ instance } sent:" , message )
56+
57+ # Wait for a reply
58+ session_info , msg = await gateway .receive (session = session .id )
59+ print (
60+ f"{ instance .capitalize ()} received (from session { session_info .id } ):" ,
61+ f"{ msg .decode ()} " ,
62+ )
63+ except Exception as e :
64+ print ("received error: " , e )
65+
66+ await asyncio .sleep (1 )
67+ else :
68+ # Wait for a message and reply in a loop
69+ while True :
70+ session_info , _ = await gateway .receive ()
71+ print (
72+ f"{ instance .capitalize ()} received a new session:" ,
73+ f"{ session_info .id } " ,
74+ )
75+
76+ async def background_task (session_id ):
77+ while True :
78+ # Receive the message from the session
79+ session , msg = await gateway .receive (session = session_id )
80+ print (
81+ f"{ instance .capitalize ()} received (from session { session_id } ):" ,
82+ f"{ msg .decode ()} " ,
83+ )
84+
85+ # handle received messages
86+ result = await agent .initate_chat (msg )
87+ print (result )
88+
89+ # process response
90+ result .inner_messages
91+ weather_question = result .inner_messages [- 1 ].content [- 1 ].content .split (":" )
92+ if weather_question [0 ] == "WEATHER" :
93+ await gateway .publish_to (session , weather_question [1 ].encode ())
94+ print (f"{ instance .capitalize ()} replies:" , weather_question [1 ])
95+
96+ asyncio .create_task (background_task (session_info .id ))
97+
98+
99+ async def main ():
46100 parser = argparse .ArgumentParser (description = "Command line client for message passing." )
47101 parser .add_argument ("-g" , "--gateway" , type = str , help = "Gateway address." , default = "http://127.0.0.1:12345" )
102+ parser .add_argument ("-m" , "--message" , type = str , help = "Message to send." )
103+ parser .add_argument ("-i" , "--iterations" ,type = int ,help = "Number of messages to send, one per second." , default = 1 )
48104 args = parser .parse_args ()
49- asyncio .run (run_agent (args .gateway ))
105+ await run_agent (args .message , args .gateway , args .iterations )
106+
50107
51108if __name__ == "__main__" :
52- main ()
109+ try :
110+ asyncio .run (main ())
111+ except KeyboardInterrupt :
112+ print ("Program terminated by user." )
0 commit comments