Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gh640/26f463e3eff9487377205f94c3860238 to your computer and use it in GitHub Desktop.
Save gh640/26f463e3eff9487377205f94c3860238 to your computer and use it in GitHub Desktop.
Sample: Change multiple OpenAI assistants models
"""Change multiple OpenAI Assistants' models"""
import click
from openai import OpenAI
EXCLUDED_IDS = []
MODELS_MAP = {
"gpt-4-turbo-preview": "gpt-4o",
"gpt-4-turbo": "gpt-4o",
}
@click.command()
@click.option("--dry-run", is_flag=True)
def main(dry_run: bool):
client = OpenAI()
assistants = yield_all_assistants(client)
for a in assistants:
if a.id in EXCLUDED_IDS:
continue
if new_model := MODELS_MAP.get(a.model):
print(f"Changing model of {a.name} from {a.model} to {new_model}")
if dry_run:
continue
client.beta.assistants.update(assistant_id=a.id, model=new_model)
def yield_all_assistants(client):
assistants = [*client.beta.assistants.list()]
while assistants:
yield from assistants
after = assistants[-1].id
assistants = [*client.beta.assistants.list(after=after)]
if __name__ == "__main__":
main()
@gh640
Copy link
Author

gh640 commented May 14, 2024

Prerequisites

  • Python 3.12

Preparation

# Create a virtualenv
python -m venv .venv
. .venv/bin/activate

# Install `openai` and `click`
pip install "openai=1.29.0"
pip install "click=8.1.7"

Edit EXCLUDED_IDS and MODELS_MAP in the script.

Usage

python change_multiple_openai_assistants_models.py --dry-run
python change_multiple_openai_assistants_models.py

@gh640
Copy link
Author

gh640 commented May 14, 2024

list_assistants.py:

"""Show all assistants"""

from openai import OpenAI


def main():
    client = OpenAI()

    assistants = yield_all_assistants(client)
    for a in assistants:
        print(f"{a.id=} {a.name=} {a.model=} {a.description=}")


def yield_all_assistants(client):
    assistants = [*client.beta.assistants.list()]
    while assistants:
        yield from assistants
        after = assistants[-1].id
        assistants = [*client.beta.assistants.list(after=after)]


if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment