2024-02-28 17:11:08 -08:00
#!/usr/bin/env python3 -m pytest
2024-01-14 14:55:53 -08:00
import asyncio
2024-01-05 17:24:49 +03:00
import os
2024-04-05 10:26:06 +08:00
import sys
import pytest
2023-11-03 21:01:49 -07:00
from test_assistant_agent import KEY_LOC , OAI_CONFIG_LIST
2023-10-02 14:52:38 -04:00
2024-04-05 10:26:06 +08:00
import autogen
2024-04-15 05:34:26 -07:00
sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , " .. " ) )
from conftest import reason , skip_openai
2024-01-14 10:24:05 -08:00
func_def = {
" name " : " get_random_number " ,
" description " : " Get a random number between 0 and 100 " ,
" parameters " : {
" type " : " object " ,
" properties " : { } ,
} ,
}
2023-10-02 14:52:38 -04:00
@pytest.mark.skipif (
2024-04-15 05:34:26 -07:00
skip_openai ,
reason = reason ,
2023-10-02 14:52:38 -04:00
)
2024-01-14 10:24:05 -08:00
@pytest.mark.parametrize (
" key, value, sync " ,
[
( " tools " , [ { " type " : " function " , " function " : func_def } ] , False ) ,
( " functions " , [ func_def ] , True ) ,
( " tools " , [ { " type " : " function " , " function " : func_def } ] , True ) ,
] ,
)
2024-01-14 14:55:53 -08:00
@pytest.mark.asyncio
async def test_function_call_groupchat ( key , value , sync ) :
2023-10-02 14:52:38 -04:00
import random
2024-01-14 14:55:53 -08:00
class Function :
call_count = 0
def get_random_number ( self ) :
self . call_count + = 1
return random . randint ( 0 , 100 )
2023-10-02 14:52:38 -04:00
2024-04-17 13:10:18 -07:00
# llm_config without functions
config_list_35 = autogen . config_list_from_json (
2023-11-03 21:01:49 -07:00
OAI_CONFIG_LIST ,
file_location = KEY_LOC ,
2024-04-17 13:10:18 -07:00
filter_dict = { " tags " : [ " gpt-3.5-turbo " , " gpt-3.5-turbo-16k " ] } ,
2023-10-02 14:52:38 -04:00
)
2024-04-17 13:10:18 -07:00
llm_config_no_function = { " config_list " : config_list_35 }
config_list_tool = autogen . filter_config ( config_list_35 , { " tags " : [ " tool " ] } )
2023-10-02 14:52:38 -04:00
llm_config = {
2024-04-17 13:10:18 -07:00
" config_list " : config_list_tool ,
2024-01-14 10:24:05 -08:00
key : value ,
2023-10-02 14:52:38 -04:00
}
2024-01-14 10:24:05 -08:00
2024-01-14 14:55:53 -08:00
func = Function ( )
2023-10-02 14:52:38 -04:00
user_proxy = autogen . UserProxyAgent (
2024-01-14 10:24:05 -08:00
name = " Executor " ,
2024-01-14 14:55:53 -08:00
description = " An executor that executes function_calls. " ,
function_map = { " get_random_number " : func . get_random_number } ,
2023-10-02 14:52:38 -04:00
human_input_mode = " NEVER " ,
)
2024-01-14 10:24:05 -08:00
player = autogen . AssistantAgent (
2023-10-02 14:52:38 -04:00
name = " Player " ,
2024-01-14 10:24:05 -08:00
system_message = " You will use function `get_random_number` to get a random number. Stop only when you get at least 1 even number and 1 odd number. Reply TERMINATE to stop. " ,
2024-01-14 14:55:53 -08:00
description = " A player that makes function_calls. " ,
2023-10-02 14:52:38 -04:00
llm_config = llm_config ,
)
2024-01-14 10:24:05 -08:00
observer = autogen . AssistantAgent (
name = " Observer " ,
2024-01-14 14:55:53 -08:00
system_message = " You observe the the player ' s actions and results. Summarize in 1 sentence. " ,
description = " An observer. " ,
2024-01-14 10:24:05 -08:00
llm_config = llm_config_no_function ,
)
2024-01-14 14:55:53 -08:00
groupchat = autogen . GroupChat (
agents = [ player , user_proxy , observer ] , messages = [ ] , max_round = 7 , speaker_selection_method = " round_robin "
)
2023-12-09 20:33:46 -05:00
# pass in llm_config with functions
with pytest . raises (
ValueError ,
match = " GroupChatManager is not allowed to make function/tool calls. Please remove the ' functions ' or ' tools ' config in ' llm_config ' you passed in. " ,
) :
manager = autogen . GroupChatManager ( groupchat = groupchat , llm_config = llm_config )
2024-01-14 10:24:05 -08:00
manager = autogen . GroupChatManager ( groupchat = groupchat , llm_config = llm_config_no_function )
2023-10-02 14:52:38 -04:00
2024-01-14 10:24:05 -08:00
if sync :
2024-02-07 12:17:05 -05:00
res = observer . initiate_chat ( manager , message = " Let ' s start the game! " , summary_method = " reflection_with_llm " )
2024-01-14 10:24:05 -08:00
else :
2024-02-07 12:17:05 -05:00
res = await observer . a_initiate_chat (
manager , message = " Let ' s start the game! " , summary_method = " reflection_with_llm "
)
2024-01-14 14:55:53 -08:00
assert func . call_count > = 1 , " The function get_random_number should be called at least once. "
2024-02-07 12:17:05 -05:00
print ( " Chat summary: " , res . summary )
print ( " Chat cost: " , res . cost )
2023-10-02 14:52:38 -04:00
2024-01-02 19:23:11 -05:00
def test_no_function_map ( ) :
dummy1 = autogen . UserProxyAgent (
name = " User_proxy " ,
system_message = " A human admin that will execute function_calls. " ,
human_input_mode = " NEVER " ,
)
dummy2 = autogen . UserProxyAgent (
name = " User_proxy " ,
system_message = " A human admin that will execute function_calls. " ,
human_input_mode = " NEVER " ,
)
groupchat = autogen . GroupChat ( agents = [ dummy1 , dummy2 ] , messages = [ ] , max_round = 7 )
groupchat . messages = [
{
" role " : " assistant " ,
" content " : None ,
" function_call " : { " name " : " get_random_number " , " arguments " : " {} " } ,
}
]
with pytest . raises (
ValueError ,
match = " No agent can execute the function get_random_number. Please check the function_map of the agents. " ,
) :
groupchat . _prepare_and_select_agents ( dummy2 )
2023-10-02 14:52:38 -04:00
if __name__ == " __main__ " :
2024-01-14 14:55:53 -08:00
asyncio . run ( test_function_call_groupchat ( " functions " , [ func_def ] , True ) )
2024-01-14 10:24:05 -08:00
# test_no_function_map()