Repository: daveshap/OpenAI_Agent_Swarm
File: agent_builder/create.py (Python)

Code before:
            existing_files[existing_file.filename] = existing_file


    if os.path.isdir(agent_folder):
        # Read contents from the 'instructions.md' file
        instructions = ''
        instructions_file_path = os.path.join(agent_folder, 'instructions.md')
        if os.path.isfile(instructions_file_path):
            with open(instructions_file_path, 'r') as f:
                instructions = f.read()
        
        # Check for the 'files' subfolder and process its contents
        files = []
        files_folder = os.path.join(agent_folder, 'files')
        if os.path.isdir(files_folder):
            for filename in os.listdir(files_folder):
                requested_files.append(filename)
                # Doesn't handle if file has been modified
                if filename not in existing_files:
                    file_path = os.path.join(files_folder, filename)
                    with open(file_path, 'rb') as file_data:
                        # Upload each file to OpenAI
                        file_object = client.files.create(file=file_data, purpose='assistants')
                        files.append({"name": filename, "id": file_object.id})

        model = 'gpt-4-1106-preview'          

        print(agent_name)
        print("")
        print(instructions)
        if files:
            print("")
            print(f"Files: {list(map(lambda x: x['name'], files))}")

        assistant={}

        if existing_agent:
            print(f"{agent_name} already exists... validating properties")
            update_model = existing_agent.model != model
            update_instructions = existing_agent.instructions != instructions
            #need to evaluate tools
            
            update_params = {}
            
            requested_files_set = set(requested_files)
            existing_files_set = set(existing_files.keys())

            if update_model:
                update_params["model"] = model
            if update_instructions:
                update_params["instructions"] = instructions

Review comment (suggestion):
Let’s move the model label onto an environment variable or at least a config file as this will change in the future. We may also want to parameterise it to compare our unit/integration tests against previous versions

Code after:
            existing_file = client.files.retrieve(file_id=file_id)
            existing_files[existing_file.filename] = existing_file


    if os.path.isdir(agent_folder):
        # Read contents from the 'instructions.md' file
        instructions = ''
        instructions_file_path = os.path.join(agent_folder, 'instructions.md')
        if os.path.isfile(instructions_file_path):
            with open(instructions_file_path, 'r') as f:
                instructions = f.read()

        # Read contents from the 'settings.json' file
        settings = {}
        settings_file_path = os.path.join(agent_folder, 'settings.json')
        if os.path.isfile(settings_file_path):
            with open(settings_file_path, 'r') as f:
                settings = json.load(f)

        # Check for the 'files' subfolder and process its contents
        files = []
        files_folder = os.path.join(agent_folder, 'files')
        if os.path.isdir(files_folder):
            for filename in os.listdir(files_folder):
                requested_files.append(filename)
                # Doesn't handle if file has been modified
                if filename not in existing_files:
                    file_path = os.path.join(files_folder, filename)
                    with open(file_path, 'rb') as file_data:
                        # Upload each file to OpenAI
                        file_object = client.files.create(file=file_data, purpose='assistants')
                        files.append({"name": filename, "id": file_object.id})        

        print(agent_name)
        print("")
        print(instructions)
        if files:
            print("")
            print(f"Files: {list(map(lambda x: x['name'], files))}")

        assistant={}

        if existing_agent:
            print(f"{agent_name} already exists... validating properties")
            update_model = existing_agent.model != settings["model"]
            update_instructions = existing_agent.instructions != instructions
            #need to evaluate tools
            
            update_params = {}
            
            requested_files_set = set(requested_files)