openai_utility.py

import os
import pandas as pd

# display all columns
pd.set_option('display.max_columns', None)

class FilesManager:
    def __init__(self, client):
        self.client = client

    def list_files(self):
        file_list = self.client.files.list()
        if file_list is None:
            return
        return file_list.data
    
    def list_files_df(self):
        file_list = self.list_files()
        if file_list is None:
            return
        return pd.DataFrame([file.model_dump() for file in file_list])

    def upload_file(self, file_path, purpose='assistants'):
        if not os.path.exists(file_path):
            raise FileExistsError(f'{file_path} not found.')
        response = self.client.files.create(
            purpose=purpose,
            file=open(file_path, 'rb')
        )
        return response
    
class AssistantManager:
    def __init__(self, client):
        self.client = client

    def list_assistants(self):
        assistant_list = self.client.beta.assistants.list()
        if assistant_list is None:
            return
        return assistant_list.data
    
    def list_assistants_df(self):
        assistant_list = self.list_assistants()
        if assistant_list is None:
            return
        return pd.DataFrame([assistant.model_dump() for assistant in assistant_list])
    
    def delete_assistant(self, assistant_id):
        response = self.client.beta.assistants.delete(assistant_id)
        return response