mirror of
https://github.com/microsoft/autogen.git
synced 2026-04-20 03:02:16 -04:00
Refactor GPTAssistantAgent (#632)
* Refactor GPTAssistantAgent constructor to handle instructions and overwrite_instructions flag - Ensure that `system_message` is always consistent with `instructions` - Ensure provided instructions are always used - Add option to permanently modify the instructions of the assistant * Improve default behavior * Add a test; add method to delete assistant * Add a new test for overwriting instructions * Add test case for when no instructions are given for existing assistant * Add pytest markers to test_gpt_assistant.py * add test in workflow * update * fix test_client_stream * comment out test_hierarchy_ --------- Co-authored-by: Chi Wang <wang.chi@microsoft.com> Co-authored-by: kevin666aa <yrwu000627@gmail.com>
This commit is contained in:
@@ -38,9 +38,10 @@ def test_gpt_assistant_chat():
|
||||
"description": "This is an API endpoint allowing users (analysts) to input question about GitHub in text format to retrieve the realted and structured data.",
|
||||
}
|
||||
|
||||
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
|
||||
analyst = GPTAssistantAgent(
|
||||
name="Open_Source_Project_Analyst",
|
||||
llm_config={"tools": [{"type": "function", "function": ossinsight_api_schema}]},
|
||||
llm_config={"tools": [{"type": "function", "function": ossinsight_api_schema}], "config_list": config_list},
|
||||
instructions="Hello, Open Source Project Analyst. You'll conduct comprehensive evaluations of open source projects or organizations on the GitHub platform",
|
||||
)
|
||||
analyst.register_function(
|
||||
@@ -62,5 +63,114 @@ def test_gpt_assistant_chat():
|
||||
assert len(analyst._openai_threads) == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin", "win32"] or skip_test,
|
||||
reason="do not run on MacOS or windows or dependency is not installed",
|
||||
)
|
||||
def test_get_assistant_instructions():
|
||||
"""
|
||||
Test function to create a new GPTAssistantAgent, set its instructions, retrieve the instructions,
|
||||
and assert that the retrieved instructions match the set instructions.
|
||||
"""
|
||||
|
||||
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
|
||||
assistant = GPTAssistantAgent(
|
||||
"assistant",
|
||||
instructions="This is a test",
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
},
|
||||
)
|
||||
|
||||
instruction_match = assistant.get_assistant_instructions() == "This is a test"
|
||||
assistant.delete_assistant()
|
||||
|
||||
assert instruction_match is True
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin", "win32"] or skip_test,
|
||||
reason="do not run on MacOS or windows or dependency is not installed",
|
||||
)
|
||||
def test_gpt_assistant_instructions_overwrite():
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
|
||||
instructions1 = "This is a test #1"
|
||||
instructions2 = "This is a test #2"
|
||||
|
||||
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
|
||||
assistant = GPTAssistantAgent(
|
||||
"assistant",
|
||||
instructions=instructions1,
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
},
|
||||
)
|
||||
|
||||
assistant_id = assistant.assistant_id
|
||||
assistant = GPTAssistantAgent(
|
||||
"assistant",
|
||||
instructions=instructions2,
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
"assistant_id": assistant_id,
|
||||
},
|
||||
overwrite_instructions=True,
|
||||
)
|
||||
|
||||
instruction_match = assistant.get_assistant_instructions() == instructions2
|
||||
assistant.delete_assistant()
|
||||
|
||||
assert instruction_match is True
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform in ["darwin", "win32"] or skip_test,
|
||||
reason="do not run on MacOS or windows or dependency is not installed",
|
||||
)
|
||||
def test_gpt_assistant_existing_no_instructions():
|
||||
"""
|
||||
Test function to check if the GPTAssistantAgent can retrieve instructions for an existing assistant
|
||||
even if the assistant was created with no instructions initially.
|
||||
"""
|
||||
instructions = "This is a test #1"
|
||||
|
||||
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, file_location=KEY_LOC)
|
||||
assistant = GPTAssistantAgent(
|
||||
"assistant",
|
||||
instructions=instructions,
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
},
|
||||
)
|
||||
|
||||
assistant_id = assistant.assistant_id
|
||||
|
||||
# create a new assistant with the same ID but no instructions
|
||||
assistant = GPTAssistantAgent(
|
||||
"assistant",
|
||||
llm_config={
|
||||
"config_list": config_list,
|
||||
"assistant_id": assistant_id,
|
||||
},
|
||||
)
|
||||
|
||||
instruction_match = assistant.get_assistant_instructions() == instructions
|
||||
assistant.delete_assistant()
|
||||
assert instruction_match is True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_gpt_assistant_chat()
|
||||
test_get_assistant_instructions()
|
||||
test_gpt_assistant_instructions_overwrite()
|
||||
test_gpt_assistant_existing_no_instructions()
|
||||
|
||||
@@ -18,7 +18,7 @@ def test_aoai_chat_completion_stream():
|
||||
filter_dict={"api_type": ["azure"], "model": ["gpt-3.5-turbo"]},
|
||||
)
|
||||
client = OpenAIWrapper(config_list=config_list)
|
||||
response = client.create(messages=[{"role": "user", "content": "2+2="}], seed=None, stream=True)
|
||||
response = client.create(messages=[{"role": "user", "content": "2+2="}], stream=True)
|
||||
print(response)
|
||||
print(client.extract_text_or_function_call(response))
|
||||
|
||||
@@ -31,7 +31,7 @@ def test_chat_completion_stream():
|
||||
filter_dict={"model": ["gpt-3.5-turbo"]},
|
||||
)
|
||||
client = OpenAIWrapper(config_list=config_list)
|
||||
response = client.create(messages=[{"role": "user", "content": "1+1="}], seed=None, stream=True)
|
||||
response = client.create(messages=[{"role": "user", "content": "1+1="}], stream=True)
|
||||
print(response)
|
||||
print(client.extract_text_or_function_call(response))
|
||||
|
||||
@@ -63,7 +63,6 @@ def test_chat_functions_stream():
|
||||
response = client.create(
|
||||
messages=[{"role": "user", "content": "What's the weather like today in San Francisco?"}],
|
||||
functions=functions,
|
||||
seed=None,
|
||||
stream=True,
|
||||
)
|
||||
print(response)
|
||||
@@ -74,7 +73,7 @@ def test_chat_functions_stream():
|
||||
def test_completion_stream():
|
||||
config_list = config_list_openai_aoai(KEY_LOC)
|
||||
client = OpenAIWrapper(config_list=config_list)
|
||||
response = client.create(prompt="1+1=", model="gpt-3.5-turbo-instruct", seed=None, stream=True)
|
||||
response = client.create(prompt="1+1=", model="gpt-3.5-turbo-instruct", stream=True)
|
||||
print(response)
|
||||
print(client.extract_text_or_function_call(response))
|
||||
|
||||
|
||||
@@ -84,12 +84,12 @@ def _test_oai_chatgpt_gpt4(save=False):
|
||||
run_notebook("oai_chatgpt_gpt4.ipynb", save=save)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
skip or not sys.version.startswith("3.10"),
|
||||
reason="do not run if openai is not installed or py!=3.10",
|
||||
)
|
||||
def test_hierarchy_flow_using_select_speaker(save=False):
|
||||
run_notebook("agentchat_hierarchy_flow_using_select_speaker.ipynb", save=save)
|
||||
# @pytest.mark.skipif(
|
||||
# skip or not sys.version.startswith("3.10"),
|
||||
# reason="do not run if openai is not installed or py!=3.10",
|
||||
# )
|
||||
# def test_hierarchy_flow_using_select_speaker(save=False):
|
||||
# run_notebook("agentchat_hierarchy_flow_using_select_speaker.ipynb", save=save)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user