2024-02-28 17:11:08 -08:00
#!/usr/bin/env python3 -m pytest
2023-11-11 21:54:18 +08:00
import os
import sys
2024-04-05 10:26:06 +08:00
import uuid
from unittest . mock import MagicMock
2024-02-17 10:00:23 -08:00
import openai
2024-04-05 10:26:06 +08:00
import pytest
2023-11-11 21:54:18 +08:00
import autogen
2024-04-05 10:26:06 +08:00
from autogen import OpenAIWrapper , UserProxyAgent
2024-02-17 10:00:23 -08:00
from autogen . agentchat . contrib . gpt_assistant_agent import GPTAssistantAgent
from autogen . oai . openai_utils import retrieve_assistants_by_name
2024-01-05 17:24:49 +03:00
sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , " ../.. " ) )
2024-02-17 10:00:23 -08:00
from conftest import skip_openai as skip # noqa: E402
2023-11-11 21:54:18 +08:00
sys . path . append ( os . path . join ( os . path . dirname ( __file__ ) , " .. " ) )
from test_assistant_agent import KEY_LOC , OAI_CONFIG_LIST # noqa: E402
2024-01-03 08:58:58 -08:00
if not skip :
2024-02-15 13:29:08 +08:00
openai_config_list = autogen . config_list_from_json (
2024-02-24 11:56:39 +08:00
OAI_CONFIG_LIST ,
file_location = KEY_LOC ,
# The Retrieval tool requires at least gpt-3.5-turbo-1106 (newer versions are supported) or gpt-4-turbo-preview models.
# https://platform.openai.com/docs/models/overview
filter_dict = {
" api_type " : [ " openai " ] ,
" model " : [
" gpt-4-turbo-preview " ,
" gpt-4-0125-preview " ,
" gpt-4-1106-preview " ,
" gpt-3.5-turbo " ,
" gpt-3.5-turbo-0125 " ,
" gpt-3.5-turbo-1106 " ,
] ,
} ,
2024-01-03 08:58:58 -08:00
)
2024-02-15 13:29:08 +08:00
aoai_config_list = autogen . config_list_from_json (
OAI_CONFIG_LIST ,
file_location = KEY_LOC ,
filter_dict = { " api_type " : [ " azure " ] , " api_version " : [ " 2024-02-15-preview " ] } ,
)
2023-11-12 12:35:03 -08:00
2023-11-11 21:54:18 +08:00
2024-02-02 17:27:13 +01:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2024-02-02 17:27:13 +01:00
)
def test_config_list ( ) - > None :
2024-02-15 13:29:08 +08:00
assert len ( openai_config_list ) > 0
assert len ( aoai_config_list ) > 0
2023-11-11 21:54:18 +08:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-11-11 21:54:18 +08:00
)
2024-02-02 17:27:13 +01:00
def test_gpt_assistant_chat ( ) - > None :
2024-02-15 13:29:08 +08:00
for gpt_config in [ openai_config_list , aoai_config_list ] :
2024-02-24 11:56:39 +08:00
_test_gpt_assistant_chat ( { " config_list " : gpt_config } )
_test_gpt_assistant_chat ( gpt_config [ 0 ] )
2024-02-15 13:29:08 +08:00
def _test_gpt_assistant_chat ( gpt_config ) - > None :
2023-11-11 21:54:18 +08:00
ossinsight_api_schema = {
" name " : " ossinsight_data_api " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" question " : {
" type " : " string " ,
" description " : " Enter your GitHub data question in the form of a clear and specific question to ensure the returned data is accurate and valuable. For optimal results, specify the desired format for the data table in your request. " ,
}
} ,
" required " : [ " question " ] ,
} ,
2023-12-23 00:00:46 +08:00
" description " : " This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the related and structured data. " ,
2023-11-11 21:54:18 +08:00
}
2024-02-02 17:27:13 +01:00
ask_ossinsight_mock = MagicMock ( )
def ask_ossinsight ( question : str ) - > str :
ask_ossinsight_mock ( question )
return " The repository microsoft/autogen has 123,456 stars on GitHub. "
2023-11-11 21:54:18 +08:00
2024-02-02 17:27:13 +01:00
name = f " For test_gpt_assistant_chat { uuid . uuid4 ( ) } "
2023-11-11 21:54:18 +08:00
analyst = GPTAssistantAgent (
2023-11-25 01:46:50 +08:00
name = name ,
2024-03-17 01:21:59 +08:00
llm_config = gpt_config ,
assistant_config = { " tools " : [ { " type " : " function " , " function " : ossinsight_api_schema } ] } ,
2023-11-11 21:54:18 +08:00
instructions = " Hello, Open Source Project Analyst. You ' ll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform " ,
)
2024-02-02 17:27:13 +01:00
try :
analyst . register_function (
function_map = {
" ossinsight_data_api " : ask_ossinsight ,
}
)
ok , response = analyst . _invoke_assistant (
[ { " role " : " user " , " content " : " How many stars microsoft/autogen has on GitHub? " } ]
)
executable = analyst . can_execute_function ( " ossinsight_data_api " )
analyst . reset ( )
threads_count = len ( analyst . _openai_threads )
finally :
analyst . delete_assistant ( )
# check response
2023-11-11 21:54:18 +08:00
assert ok is True
assert response . get ( " role " , " " ) == " assistant "
2024-02-02 17:27:13 +01:00
# check the question asked
ask_ossinsight_mock . assert_called_once ( )
question_asked = ask_ossinsight_mock . call_args [ 0 ] [ 0 ] . lower ( )
2024-02-17 10:00:23 -08:00
for word in " microsoft autogen star " . split ( " " ) :
2024-02-02 17:27:13 +01:00
assert word in question_asked
# check the answer
response_content = response . get ( " content " , " " ) . lower ( )
assert len ( response_content ) > 0
for word in " microsoft autogen 123 456 " . split ( " " ) :
assert word in response_content
2023-11-25 01:46:50 +08:00
assert executable is False
assert threads_count == 0
2023-11-11 21:54:18 +08:00
2023-11-11 22:03:51 -08:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-11-11 22:03:51 -08:00
)
2024-02-02 17:27:13 +01:00
def test_get_assistant_instructions ( ) - > None :
2024-02-15 13:29:08 +08:00
for gpt_config in [ openai_config_list , aoai_config_list ] :
_test_get_assistant_instructions ( gpt_config )
def _test_get_assistant_instructions ( gpt_config ) - > None :
2023-11-11 22:03:51 -08:00
"""
Test function to create a new GPTAssistantAgent , set its instructions , retrieve the instructions ,
and assert that the retrieved instructions match the set instructions .
"""
2024-02-02 17:27:13 +01:00
name = f " For test_get_assistant_instructions { uuid . uuid4 ( ) } "
2023-11-11 22:03:51 -08:00
assistant = GPTAssistantAgent (
2023-11-25 01:46:50 +08:00
name ,
2023-11-11 22:03:51 -08:00
instructions = " This is a test " ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : gpt_config ,
2023-11-11 22:03:51 -08:00
} ,
)
instruction_match = assistant . get_assistant_instructions ( ) == " This is a test "
assistant . delete_assistant ( )
assert instruction_match is True
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-11-11 22:03:51 -08:00
)
2024-02-02 17:27:13 +01:00
def test_gpt_assistant_instructions_overwrite ( ) - > None :
2024-02-15 13:29:08 +08:00
for gpt_config in [ openai_config_list , aoai_config_list ] :
_test_gpt_assistant_instructions_overwrite ( gpt_config )
def _test_gpt_assistant_instructions_overwrite ( gpt_config ) - > None :
2023-11-11 22:03:51 -08:00
"""
Test that the instructions of a GPTAssistantAgent can be overwritten or not depending on the value of the
` overwrite_instructions ` parameter when creating a new assistant with the same ID .
Steps :
1. Create a new GPTAssistantAgent with some instructions .
2. Get the ID of the assistant .
3. Create a new GPTAssistantAgent with the same ID but different instructions and ` overwrite_instructions = True ` .
4. Check that the instructions of the assistant have been overwritten with the new ones .
"""
2024-02-02 17:27:13 +01:00
name = f " For test_gpt_assistant_instructions_overwrite { uuid . uuid4 ( ) } "
2023-11-11 22:03:51 -08:00
instructions1 = " This is a test #1 "
instructions2 = " This is a test #2 "
assistant = GPTAssistantAgent (
2023-11-25 01:46:50 +08:00
name ,
2023-11-11 22:03:51 -08:00
instructions = instructions1 ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : gpt_config ,
2023-11-11 22:03:51 -08:00
} ,
)
2024-02-02 17:27:13 +01:00
try :
assistant_id = assistant . assistant_id
assistant = GPTAssistantAgent (
name ,
instructions = instructions2 ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : gpt_config ,
2024-03-17 01:21:59 +08:00
# keep it to test older version of assistant config
2024-02-02 17:27:13 +01:00
" assistant_id " : assistant_id ,
} ,
overwrite_instructions = True ,
)
2023-11-11 22:03:51 -08:00
2024-02-02 17:27:13 +01:00
instruction_match = assistant . get_assistant_instructions ( ) == instructions2
finally :
assistant . delete_assistant ( )
2023-11-11 22:03:51 -08:00
assert instruction_match is True
@pytest.mark.skipif (
2024-03-17 01:21:59 +08:00
skip ,
2024-02-17 10:00:23 -08:00
reason = " requested to skip " ,
2023-11-11 22:03:51 -08:00
)
2024-02-02 17:27:13 +01:00
def test_gpt_assistant_existing_no_instructions ( ) - > None :
2023-11-11 22:03:51 -08:00
"""
Test function to check if the GPTAssistantAgent can retrieve instructions for an existing assistant
even if the assistant was created with no instructions initially .
"""
2024-02-02 17:27:13 +01:00
name = f " For test_gpt_assistant_existing_no_instructions { uuid . uuid4 ( ) } "
2023-11-11 22:03:51 -08:00
instructions = " This is a test #1 "
assistant = GPTAssistantAgent (
2023-11-25 01:46:50 +08:00
name ,
2023-11-11 22:03:51 -08:00
instructions = instructions ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2023-11-11 22:03:51 -08:00
} ,
)
2024-02-02 17:27:13 +01:00
try :
assistant_id = assistant . assistant_id
# create a new assistant with the same ID but no instructions
assistant = GPTAssistantAgent (
name ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-02-02 17:27:13 +01:00
} ,
2024-03-17 01:21:59 +08:00
assistant_config = { " assistant_id " : assistant_id } ,
2024-02-02 17:27:13 +01:00
)
2023-11-11 22:03:51 -08:00
2024-02-02 17:27:13 +01:00
instruction_match = assistant . get_assistant_instructions ( ) == instructions
finally :
assistant . delete_assistant ( )
2023-11-11 22:03:51 -08:00
assert instruction_match is True
2023-11-16 20:51:32 +08:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-11-16 20:51:32 +08:00
)
2024-02-02 17:27:13 +01:00
def test_get_assistant_files ( ) - > None :
2023-11-16 20:51:32 +08:00
"""
Test function to create a new GPTAssistantAgent , set its instructions , retrieve the instructions ,
and assert that the retrieved instructions match the set instructions .
"""
current_file_path = os . path . abspath ( __file__ )
2024-02-15 13:29:08 +08:00
openai_client = OpenAIWrapper ( config_list = openai_config_list ) . _clients [ 0 ] . _oai_client
2023-11-16 20:51:32 +08:00
file = openai_client . files . create ( file = open ( current_file_path , " rb " ) , purpose = " assistants " )
2024-02-02 17:27:13 +01:00
name = f " For test_get_assistant_files { uuid . uuid4 ( ) } "
2023-11-16 20:51:32 +08:00
2024-03-17 01:21:59 +08:00
# keep it to test older version of assistant config
2023-11-16 20:51:32 +08:00
assistant = GPTAssistantAgent (
2023-11-25 01:46:50 +08:00
name ,
2023-11-16 20:51:32 +08:00
instructions = " This is a test " ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2023-11-16 20:51:32 +08:00
" tools " : [ { " type " : " retrieval " } ] ,
" file_ids " : [ file . id ] ,
} ,
)
2024-02-02 17:27:13 +01:00
try :
files = assistant . openai_client . beta . assistants . files . list ( assistant_id = assistant . assistant_id )
retrieved_file_ids = [ fild . id for fild in files ]
expected_file_id = file . id
finally :
assistant . delete_assistant ( )
2024-03-17 01:21:59 +08:00
openai_client . files . delete ( file . id )
2023-11-16 20:51:32 +08:00
2023-12-23 00:00:46 +08:00
assert expected_file_id in retrieved_file_ids
2023-11-16 20:51:32 +08:00
2023-11-25 01:46:50 +08:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-11-25 01:46:50 +08:00
)
2024-02-02 17:27:13 +01:00
def test_assistant_retrieval ( ) - > None :
2023-11-25 01:46:50 +08:00
"""
Test function to check if the GPTAssistantAgent can retrieve the same assistant
"""
2024-02-02 17:27:13 +01:00
name = f " For test_assistant_retrieval { uuid . uuid4 ( ) } "
2023-11-25 01:46:50 +08:00
2023-12-27 22:21:09 +08:00
function_1_schema = {
" name " : " call_function_1 " ,
" parameters " : { " type " : " object " , " properties " : { } , " required " : [ ] } ,
" description " : " This is a test function 1 " ,
}
function_2_schema = {
" name " : " call_function_1 " ,
" parameters " : { " type " : " object " , " properties " : { } , " required " : [ ] } ,
" description " : " This is a test function 2 " ,
}
2024-02-15 13:29:08 +08:00
openai_client = OpenAIWrapper ( config_list = openai_config_list ) . _clients [ 0 ] . _oai_client
2023-12-27 22:21:09 +08:00
current_file_path = os . path . abspath ( __file__ )
2024-02-02 17:27:13 +01:00
2023-12-27 22:21:09 +08:00
file_1 = openai_client . files . create ( file = open ( current_file_path , " rb " ) , purpose = " assistants " )
file_2 = openai_client . files . create ( file = open ( current_file_path , " rb " ) , purpose = " assistants " )
2024-02-02 17:27:13 +01:00
try :
all_llm_config = {
2024-03-17 01:21:59 +08:00
" config_list " : openai_config_list ,
}
assistant_config = {
2024-02-02 17:27:13 +01:00
" tools " : [
{ " type " : " function " , " function " : function_1_schema } ,
{ " type " : " function " , " function " : function_2_schema } ,
{ " type " : " retrieval " } ,
{ " type " : " code_interpreter " } ,
] ,
" file_ids " : [ file_1 . id , file_2 . id ] ,
}
2023-12-27 22:21:09 +08:00
2024-02-02 17:27:13 +01:00
name = f " For test_assistant_retrieval { uuid . uuid4 ( ) } "
2023-12-27 22:21:09 +08:00
2024-02-02 17:27:13 +01:00
assistant_first = GPTAssistantAgent (
name ,
instructions = " This is a test " ,
llm_config = all_llm_config ,
2024-03-17 01:21:59 +08:00
assistant_config = assistant_config ,
2024-02-02 17:27:13 +01:00
)
candidate_first = retrieve_assistants_by_name ( assistant_first . openai_client , name )
2023-11-25 01:46:50 +08:00
2024-02-02 17:27:13 +01:00
try :
assistant_second = GPTAssistantAgent (
name ,
instructions = " This is a test " ,
llm_config = all_llm_config ,
2024-03-17 01:21:59 +08:00
assistant_config = assistant_config ,
2024-02-02 17:27:13 +01:00
)
candidate_second = retrieve_assistants_by_name ( assistant_second . openai_client , name )
2023-11-25 01:46:50 +08:00
2024-02-02 17:27:13 +01:00
finally :
assistant_first . delete_assistant ( )
with pytest . raises ( openai . NotFoundError ) :
assistant_second . delete_assistant ( )
2023-11-25 01:46:50 +08:00
2024-02-02 17:27:13 +01:00
finally :
openai_client . files . delete ( file_1 . id )
openai_client . files . delete ( file_2 . id )
2023-12-27 22:21:09 +08:00
2023-11-25 01:46:50 +08:00
assert candidate_first == candidate_second
2023-12-27 22:21:09 +08:00
assert len ( candidate_first ) == 1
candidates = retrieve_assistants_by_name ( openai_client , name )
assert len ( candidates ) == 0
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2023-12-27 22:21:09 +08:00
)
2024-02-02 17:27:13 +01:00
def test_assistant_mismatch_retrieval ( ) - > None :
2023-12-27 22:21:09 +08:00
""" Test function to check if the GPTAssistantAgent can filter out the mismatch assistant """
2024-02-02 17:27:13 +01:00
name = f " For test_assistant_retrieval { uuid . uuid4 ( ) } "
2023-12-27 22:21:09 +08:00
function_1_schema = {
" name " : " call_function " ,
" parameters " : { " type " : " object " , " properties " : { } , " required " : [ ] } ,
" description " : " This is a test function 1 " ,
}
function_2_schema = {
" name " : " call_function " ,
" parameters " : { " type " : " object " , " properties " : { } , " required " : [ ] } ,
" description " : " This is a test function 2 " ,
}
function_3_schema = {
" name " : " call_function_other " ,
" parameters " : { " type " : " object " , " properties " : { } , " required " : [ ] } ,
" description " : " This is a test function 3 " ,
}
2024-02-15 13:29:08 +08:00
openai_client = OpenAIWrapper ( config_list = openai_config_list ) . _clients [ 0 ] . _oai_client
2023-12-27 22:21:09 +08:00
current_file_path = os . path . abspath ( __file__ )
file_1 = openai_client . files . create ( file = open ( current_file_path , " rb " ) , purpose = " assistants " )
file_2 = openai_client . files . create ( file = open ( current_file_path , " rb " ) , purpose = " assistants " )
2024-02-02 17:27:13 +01:00
try :
2024-03-17 01:21:59 +08:00
# keep it to test older version of assistant config
2024-02-02 17:27:13 +01:00
all_llm_config = {
" tools " : [
{ " type " : " function " , " function " : function_1_schema } ,
{ " type " : " function " , " function " : function_2_schema } ,
{ " type " : " retrieval " } ,
{ " type " : " code_interpreter " } ,
] ,
" file_ids " : [ file_1 . id , file_2 . id ] ,
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-02-02 17:27:13 +01:00
}
2023-12-27 22:21:09 +08:00
2024-02-02 17:27:13 +01:00
name = f " For test_assistant_retrieval { uuid . uuid4 ( ) } "
assistant_first , assistant_instructions_mistaching = None , None
assistant_file_ids_mismatch , assistant_tools_mistaching = None , None
try :
assistant_first = GPTAssistantAgent (
name ,
instructions = " This is a test " ,
llm_config = all_llm_config ,
)
candidate_first = retrieve_assistants_by_name ( assistant_first . openai_client , name )
assert len ( candidate_first ) == 1
# test instructions mismatch
assistant_instructions_mistaching = GPTAssistantAgent (
name ,
instructions = " This is a test for mismatch instructions " ,
llm_config = all_llm_config ,
)
candidate_instructions_mistaching = retrieve_assistants_by_name (
assistant_instructions_mistaching . openai_client , name
)
assert len ( candidate_instructions_mistaching ) == 2
# test mismatch fild ids
file_ids_mismatch_llm_config = {
" tools " : [
{ " type " : " code_interpreter " } ,
{ " type " : " retrieval " } ,
{ " type " : " function " , " function " : function_2_schema } ,
{ " type " : " function " , " function " : function_1_schema } ,
] ,
" file_ids " : [ file_2 . id ] ,
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-02-02 17:27:13 +01:00
}
assistant_file_ids_mismatch = GPTAssistantAgent (
name ,
instructions = " This is a test " ,
llm_config = file_ids_mismatch_llm_config ,
)
candidate_file_ids_mismatch = retrieve_assistants_by_name ( assistant_file_ids_mismatch . openai_client , name )
assert len ( candidate_file_ids_mismatch ) == 3
# test tools mismatch
tools_mismatch_llm_config = {
" tools " : [
{ " type " : " code_interpreter " } ,
{ " type " : " retrieval " } ,
{ " type " : " function " , " function " : function_3_schema } ,
] ,
" file_ids " : [ file_2 . id , file_1 . id ] ,
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-02-02 17:27:13 +01:00
}
assistant_tools_mistaching = GPTAssistantAgent (
name ,
instructions = " This is a test " ,
llm_config = tools_mismatch_llm_config ,
)
candidate_tools_mismatch = retrieve_assistants_by_name ( assistant_tools_mistaching . openai_client , name )
assert len ( candidate_tools_mismatch ) == 4
finally :
if assistant_first :
assistant_first . delete_assistant ( )
if assistant_instructions_mistaching :
assistant_instructions_mistaching . delete_assistant ( )
if assistant_file_ids_mismatch :
assistant_file_ids_mismatch . delete_assistant ( )
if assistant_tools_mistaching :
assistant_tools_mistaching . delete_assistant ( )
finally :
openai_client . files . delete ( file_1 . id )
openai_client . files . delete ( file_2 . id )
2023-12-27 22:21:09 +08:00
candidates = retrieve_assistants_by_name ( openai_client , name )
assert len ( candidates ) == 0
2023-11-25 01:46:50 +08:00
2024-02-01 10:11:54 -05:00
@pytest.mark.skipif (
2024-02-17 10:00:23 -08:00
skip ,
reason = " requested to skip " ,
2024-02-01 10:11:54 -05:00
)
2024-02-02 17:27:13 +01:00
def test_gpt_assistant_tools_overwrite ( ) - > None :
2024-02-01 10:11:54 -05:00
"""
Test that the tools of a GPTAssistantAgent can be overwritten or not depending on the value of the
` overwrite_tools ` parameter when creating a new assistant with the same ID .
Steps :
1. Create a new GPTAssistantAgent with a set of tools .
2. Get the ID of the assistant .
3. Create a new GPTAssistantAgent with the same ID but different tools and ` overwrite_tools = True ` .
4. Check that the tools of the assistant have been overwritten with the new ones .
"""
original_tools = [
{
" type " : " function " ,
" function " : {
" name " : " calculateTax " ,
" description " : " Calculate tax for a given amount " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" amount " : { " type " : " number " , " description " : " The amount to calculate tax on " } ,
" tax_rate " : { " type " : " number " , " description " : " The tax rate to apply " } ,
} ,
" required " : [ " amount " , " tax_rate " ] ,
} ,
} ,
} ,
{
" type " : " function " ,
" function " : {
" name " : " convertCurrency " ,
" description " : " Convert currency from one type to another " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" amount " : { " type " : " number " , " description " : " The amount to convert " } ,
" from_currency " : { " type " : " string " , " description " : " Currency type to convert from " } ,
" to_currency " : { " type " : " string " , " description " : " Currency type to convert to " } ,
} ,
" required " : [ " amount " , " from_currency " , " to_currency " ] ,
} ,
} ,
} ,
]
new_tools = [
{
" type " : " function " ,
" function " : {
" name " : " findRestaurant " ,
" description " : " Find a restaurant based on cuisine type and location " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" cuisine " : { " type " : " string " , " description " : " Type of cuisine " } ,
" location " : { " type " : " string " , " description " : " City or area for the restaurant search " } ,
} ,
" required " : [ " cuisine " , " location " ] ,
} ,
} ,
} ,
{
" type " : " function " ,
" function " : {
" name " : " calculateMortgage " ,
" description " : " Calculate monthly mortgage payments " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" principal " : { " type " : " number " , " description " : " The principal loan amount " } ,
" interest_rate " : { " type " : " number " , " description " : " Annual interest rate " } ,
" years " : { " type " : " integer " , " description " : " Number of years for the loan " } ,
} ,
" required " : [ " principal " , " interest_rate " , " years " ] ,
} ,
} ,
} ,
]
2024-02-02 17:27:13 +01:00
name = f " For test_gpt_assistant_tools_overwrite { uuid . uuid4 ( ) } "
2024-02-01 10:11:54 -05:00
# Create an assistant with original tools
2024-02-02 17:27:13 +01:00
assistant_org = GPTAssistantAgent (
2024-02-01 10:11:54 -05:00
name ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-03-17 01:21:59 +08:00
} ,
assistant_config = {
2024-02-01 10:11:54 -05:00
" tools " : original_tools ,
} ,
)
2024-02-02 17:27:13 +01:00
assistant_id = assistant_org . assistant_id
2024-02-01 10:11:54 -05:00
2024-02-02 17:27:13 +01:00
try :
# Create a new assistant with new tools and overwrite_tools set to True
assistant = GPTAssistantAgent (
name ,
llm_config = {
2024-02-15 13:29:08 +08:00
" config_list " : openai_config_list ,
2024-03-17 01:21:59 +08:00
} ,
assistant_config = {
2024-02-02 17:27:13 +01:00
" assistant_id " : assistant_id ,
" tools " : new_tools ,
} ,
overwrite_tools = True ,
)
2024-02-01 10:11:54 -05:00
2024-02-02 17:27:13 +01:00
# Add logic to retrieve the tools from the assistant and assert
2024-03-17 01:21:59 +08:00
retrieved_tools = assistant . openai_assistant . tools
retrieved_tools_name = [ tool . function . name for tool in retrieved_tools ]
2024-02-02 17:27:13 +01:00
finally :
assistant_org . delete_assistant ( )
2024-02-01 10:11:54 -05:00
2024-03-17 01:21:59 +08:00
assert retrieved_tools_name == [ tool [ " function " ] [ " name " ] for tool in new_tools ]
@pytest.mark.skipif (
skip ,
reason = " requested to skip " ,
)
def test_gpt_reflection_with_llm ( ) - > None :
gpt_assistant = GPTAssistantAgent (
name = " assistant " , llm_config = { " config_list " : openai_config_list , " assistant_id " : None }
)
user_proxy = UserProxyAgent (
name = " user_proxy " ,
code_execution_config = False ,
is_termination_msg = lambda msg : " TERMINATE " in msg [ " content " ] ,
human_input_mode = " NEVER " ,
max_consecutive_auto_reply = 1 ,
)
result = user_proxy . initiate_chat ( gpt_assistant , message = " Write a Joke! " , summary_method = " reflection_with_llm " )
assert result is not None
# use the assistant configuration
agent_using_assistant_config = GPTAssistantAgent (
name = " assistant " ,
llm_config = { " config_list " : openai_config_list } ,
assistant_config = { " assistant_id " : gpt_assistant . assistant_id } ,
)
result = user_proxy . initiate_chat (
agent_using_assistant_config , message = " Write a Joke! " , summary_method = " reflection_with_llm "
)
assert result is not None
2024-02-01 10:11:54 -05:00
2023-11-11 21:54:18 +08:00
if __name__ == " __main__ " :
test_gpt_assistant_chat ( )
2023-11-11 22:03:51 -08:00
test_get_assistant_instructions ( )
test_gpt_assistant_instructions_overwrite ( )
test_gpt_assistant_existing_no_instructions ( )
2023-11-16 20:51:32 +08:00
test_get_assistant_files ( )
2023-12-27 22:21:09 +08:00
test_assistant_mismatch_retrieval ( )
2024-02-01 10:11:54 -05:00
test_gpt_assistant_tools_overwrite ( )