Hi. I am trying to set up an application in which I create an assistant directly with name, instructions, etc.
Locally, I have in .env defined the OPENAI_API_KEY, but left out OPENAI_ASSISTANT_ID since my objective is to define this assistant locally (overall, the objective is to have multiple chat experiences because of different instructions). However, when I run the app without the assistant ID, then an error occurs due to a programmed warning when missing this ID.
My question is whether I can omit the assistant ID or define it to be a random number, e.g, “1” or if it must be an existing assistant_ID from my OpenAI account. In case of the latter, why would anyone define an assistant like this, if the instructions etc. are overrun by the the assistant configuration in https://platform.openai.com/assistants?
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
model="gpt-4-1106-preview",
)
show_json(assistant)
Yes, this is exactly what your code does. It makes a create assistant API call on the Assistants API.
The assistant configuration lives on the API hence if you make changes to the same assistant in playground, your products consuming the same assistant will also be affected.
I’m pretty new to this but I think you must have an Assistant defined in order to progress through the code, otherwise it will throw an error when it doesn’t get an expected response from the assistant in your first step.
The platform.openai.com user interface is pretty new, it just offers a graphical interface for what is available programmatically via the API. It’s pretty neat. You can create and work with the same assistant either way, depending on what you’re comfortable with.
Personally, I am far more comfortable working with the platform’s graphical interface to make, and work with, an assistant. But sometimes newer features are only available programmatically.
Instead of Creating an Assistant in your Script, you’re going to want to Retrieve an Assistant you already made in a previous step… hrm… And if you want a different Assistant to activate depending on the context of your prompt, I think you’d need an Assistant with Function Calling as a Tool to be able to flag the proper routing choice.
I’m still keeping all of my Assistants in my .env, then looking up what I need in code, it seems like a good security practice.
You mention that you keep all your assists in your .env - so how do you do this and ensure the right assistant is retrieved? Cannot seem to make this work, so that the user in the UI can select between different assistants?
The Assistant ID cannot be omitted or set to an arbitrary value. When creating a new Assistant, you need to define the model to be used, as well as tools like the code interpreter, file search, or function calling.
The thread is associated with the instructions, and while the model and tool types used by the Assistant cannot be changed within the thread, the instructions can be modified or overwritten.
Additionally, the Assistant ID is randomly assigned by the OpenAI server, so users cannot specify the Assistant ID themselves.
#lookup assistant ID from .env
assistant1 = os.getenv('FIRST_ASSISTANT_ID')
assistant2 = os.getenv('SECOND_ASSISTANT_ID')
#retrieve the assistants.
first_assistant = client.beta.assistants.retrieve(assistant1)
second_assistant = client.beta.assistants.retrieve(assistant2)
#print it all out to check.
print(first_assistant)
print(second_assistant)