How to reuse the same assistant for multiple instances instead of creating a separate one for each instance

Using the Python OpenAI SDK, I am developing a personal assistant. The issue I am facing is that every time I launch my Streamlit app (new sessions), it creates a new assistant. I don’t mind launching a new thread, but I believe recreating the same assistant for the same app for every new session is inefficient. Is there a way to list all available assistants and select one instead of creating a new one each time?

3 Likes

You need to use the listAssistants endpoint

If i am correct listassistant only returns the list of assistants that are already created.
Any chance you could provide a code snippet how we meant to use it.
Goal, avoided creating new assistants each time and just call the already existing one (e.g. by using assistant id)

Thanks

You could use the assistant ID from your existing assistant, to then initate the assistant with
assistant = client.beta.assistants.retrieve("{your_assistant_id}")

Then proceed as normally!

3 Likes

Here are some python code i wrote that helps with the issue:


def list_assistants():

    assistant_object = client.beta.assistants.list()
    return assistant_object

def delete_assistant(assistant_id):
    """Delete an assistant by ID."""
    delete_url = f"{BASE_URL}/{assistant_id}"
    response = requests.delete(delete_url, headers=HEADERS)
    if response.status_code == 200:
        print(f"Deleted assistant with ID: {assistant_id}")
    else:
        print(f"Failed to delete assistant with ID: {assistant_id}. Status Code: {response.status_code}")

def delete_all_assistants():
    """Delete all assistants."""
    a_list = list_assistants()
    assitant_obj_list = a_list.data
    for i in range(len(assitant_obj_list)):
        delete_assistant(assitant_obj_list[i].id)

def select_assistant(assistant_id):
    # Use the 'beta.assistants' attribute, not 'Assistant'
    assistant = client.beta.assistants.retrieve(assistant_id)
    return assistant.id

def create_assistant(name, instructions, tools, model):
    assistant = client.beta.assistants.create(
        name=name,
        instructions=instructions,
        tools=tools,
        model=model
    )
    return assistant.id  # Return the assistant ID



def get_assistant_by_id(assistant_id):
    assistant = client.beta.assistants.retrieve(assistant_id)
    return assistant.id


def create_thread():
    
    thread = client.beta.threads.create()
    return thread


def select_assistant(assistant_id):
    return get_assistant_by_id(assistant_id)
6 Likes

In Langroid I store the assistant, thread ids in a redis cache, based on a hash from user + machine + org. This way I can retrieve these ids across sessions and re-use the same assistant and thread (or not) depending on user’s config setup. You can see code here for example:

2 Likes

Incredibly helpful, thank you!

Thanks a lot.
I did some tinkering on the code and it is working!
Got back my assistant!

1 Like

Would this be the same method for initiating an assistant that already exists on the openai assistants page?

1 Like