@@ -34,7 +34,7 @@ def conversable_agent():
34
34
)
35
35
36
36
37
- def test_trigger ():
37
+ def test_sync_trigger ():
38
38
agent = ConversableAgent ("a0" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
39
39
agent1 = ConversableAgent ("a1" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
40
40
agent .register_reply (agent1 , lambda recipient , messages , sender , config : (True , "hello" ))
@@ -72,6 +72,114 @@ def test_trigger():
72
72
pytest .raises (ValueError , agent ._match_trigger , 1 , agent1 )
73
73
74
74
75
+ @pytest .mark .asyncio
76
+ async def test_async_trigger ():
77
+ agent = ConversableAgent ("a0" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
78
+ agent1 = ConversableAgent ("a1" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
79
+
80
+ async def a_reply (recipient , messages , sender , config ):
81
+ print ("hello from a_reply" )
82
+ return (True , "hello" )
83
+
84
+ agent .register_reply (agent1 , a_reply )
85
+ await agent1 .a_initiate_chat (agent , message = "hi" )
86
+ assert agent1 .last_message (agent )["content" ] == "hello"
87
+
88
+ async def a_reply_a1 (recipient , messages , sender , config ):
89
+ print ("hello from a_reply_a1" )
90
+ return (True , "hello a1" )
91
+
92
+ agent .register_reply ("a1" , a_reply_a1 )
93
+ await agent1 .a_initiate_chat (agent , message = "hi" )
94
+ assert agent1 .last_message (agent )["content" ] == "hello a1"
95
+
96
+ async def a_reply_conversable_agent (recipient , messages , sender , config ):
97
+ print ("hello from a_reply_conversable_agent" )
98
+ return (True , "hello conversable agent" )
99
+
100
+ agent .register_reply (ConversableAgent , a_reply_conversable_agent )
101
+ await agent1 .a_initiate_chat (agent , message = "hi" )
102
+ assert agent1 .last_message (agent )["content" ] == "hello conversable agent"
103
+
104
+ async def a_reply_a (recipient , messages , sender , config ):
105
+ print ("hello from a_reply_a" )
106
+ return (True , "hello a" )
107
+
108
+ agent .register_reply (lambda sender : sender .name .startswith ("a" ), a_reply_a )
109
+ await agent1 .a_initiate_chat (agent , message = "hi" )
110
+ assert agent1 .last_message (agent )["content" ] == "hello a"
111
+
112
+ async def a_reply_b (recipient , messages , sender , config ):
113
+ print ("hello from a_reply_b" )
114
+ return (True , "hello b" )
115
+
116
+ agent .register_reply (lambda sender : sender .name .startswith ("b" ), a_reply_b )
117
+ await agent1 .a_initiate_chat (agent , message = "hi" )
118
+ assert agent1 .last_message (agent )["content" ] == "hello a"
119
+
120
+ async def a_reply_agent2_or_agent1 (recipient , messages , sender , config ):
121
+ print ("hello from a_reply_agent2_or_agent1" )
122
+ return (True , "hello agent2 or agent1" )
123
+
124
+ agent .register_reply (["agent2" , agent1 ], a_reply_agent2_or_agent1 )
125
+ await agent1 .a_initiate_chat (agent , message = "hi" )
126
+ assert agent1 .last_message (agent )["content" ] == "hello agent2 or agent1"
127
+
128
+ async def a_reply_agent2_or_agent3 (recipient , messages , sender , config ):
129
+ print ("hello from a_reply_agent2_or_agent3" )
130
+ return (True , "hello agent2 or agent3" )
131
+
132
+ agent .register_reply (["agent2" , "agent3" ], a_reply_agent2_or_agent3 )
133
+ await agent1 .a_initiate_chat (agent , message = "hi" )
134
+ assert agent1 .last_message (agent )["content" ] == "hello agent2 or agent1"
135
+
136
+ with pytest .raises (ValueError ):
137
+ agent .register_reply (1 , a_reply )
138
+
139
+ with pytest .raises (ValueError ):
140
+ agent ._match_trigger (1 , agent1 )
141
+
142
+
143
+ def test_async_trigger_in_sync_chat ():
144
+ agent = ConversableAgent ("a0" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
145
+ agent1 = ConversableAgent ("a1" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
146
+ agent2 = ConversableAgent ("a2" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
147
+
148
+ reply_mock = unittest .mock .MagicMock ()
149
+
150
+ async def a_reply (recipient , messages , sender , config ):
151
+ reply_mock ()
152
+ print ("hello from a_reply" )
153
+ return (True , "hello from reply function" )
154
+
155
+ agent .register_reply (agent1 , a_reply )
156
+
157
+ with pytest .raises (RuntimeError ) as e :
158
+ agent1 .initiate_chat (agent , message = "hi" )
159
+
160
+ assert (
161
+ e .value .args [0 ] == "Async reply functions can only be used with ConversableAgent.a_initiate_chat(). "
162
+ "The following async reply functions are found: a_reply"
163
+ )
164
+
165
+ agent2 .register_reply (agent1 , a_reply , ignore_async_in_sync_chat = True )
166
+ reply_mock .assert_not_called ()
167
+
168
+
169
+ @pytest .mark .asyncio
170
+ async def test_sync_trigger_in_async_chat ():
171
+ agent = ConversableAgent ("a0" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
172
+ agent1 = ConversableAgent ("a1" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
173
+
174
+ def a_reply (recipient , messages , sender , config ):
175
+ print ("hello from a_reply" )
176
+ return (True , "hello from reply function" )
177
+
178
+ agent .register_reply (agent1 , a_reply )
179
+ await agent1 .a_initiate_chat (agent , message = "hi" )
180
+ assert agent1 .last_message (agent )["content" ] == "hello from reply function"
181
+
182
+
75
183
def test_context ():
76
184
agent = ConversableAgent ("a0" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
77
185
agent1 = ConversableAgent ("a1" , max_consecutive_auto_reply = 0 , llm_config = False , human_input_mode = "NEVER" )
0 commit comments