From a730a63e30c65968038f5503341d4eeee33c7b3b Mon Sep 17 00:00:00 2001 From: Ian Date: Thu, 16 Nov 2023 20:51:32 +0800 Subject: [PATCH] fix assistant creating without file (#689) * fix assistant creating without file * add a simple unit test * format code --- .../agentchat/contrib/gpt_assistant_agent.py | 1 + test/agentchat/contrib/test_gpt_assistant.py | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/autogen/agentchat/contrib/gpt_assistant_agent.py b/autogen/agentchat/contrib/gpt_assistant_agent.py index b4fc5e8096e..f9e77468007 100644 --- a/autogen/agentchat/contrib/gpt_assistant_agent.py +++ b/autogen/agentchat/contrib/gpt_assistant_agent.py @@ -64,6 +64,7 @@ def __init__( instructions=instructions, tools=llm_config.get("tools", []), model=llm_config.get("model", "gpt-4-1106-preview"), + file_ids=llm_config.get("file_ids", []), ) else: # retrieve an existing assistant diff --git a/test/agentchat/contrib/test_gpt_assistant.py b/test/agentchat/contrib/test_gpt_assistant.py index 59bad7c560e..ae1e0bdf88b 100644 --- a/test/agentchat/contrib/test_gpt_assistant.py +++ b/test/agentchat/contrib/test_gpt_assistant.py @@ -2,6 +2,7 @@ import os import sys import autogen +from autogen import OpenAIWrapper sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from test_assistant_agent import KEY_LOC, OAI_CONFIG_LIST # noqa: E402 @@ -169,8 +170,42 @@ def test_gpt_assistant_existing_no_instructions(): 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_get_assistant_files(): + """ + 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__) + openai_client = OpenAIWrapper(config_list=config_list)._clients[0] + file = openai_client.files.create(file=open(current_file_path, "rb"), purpose="assistants") + + assistant = GPTAssistantAgent( + "assistant", + instructions="This is a test", + llm_config={ + "config_list": config_list, + "tools": [{"type": "retrieval"}], + "file_ids": [file.id], + }, + ) + + files = assistant.openai_client.beta.assistants.files.list(assistant_id=assistant.assistant_id) + retrived_file_ids = [fild.id for fild in files] + expected_file_id = file.id + + assistant.delete_assistant() + openai_client.files.delete(file.id) + + assert expected_file_id in retrived_file_ids + + if __name__ == "__main__": test_gpt_assistant_chat() test_get_assistant_instructions() test_gpt_assistant_instructions_overwrite() test_gpt_assistant_existing_no_instructions() + test_get_assistant_files()