OpenAI’s GPT 3.5 API — Custom Assistant Integration with Python

Somanath Balakrishnan
3 min readFeb 12, 2024

--

As you know, we can now create custom assistants in Open AI and custom assistants can be accessed from your program by providing API key and the assistant id.

Where is my custom assistant?

Who is custom assistant?

Since you already know, the OpenAI APIs available for us to use from programs (yeah, definitely you have to have credits or an OpenAI paid account.) Custom Assistant is as name clearly said, custom guy who can take care of the specific function/s what you asked him to do. Below example will explain how you can create a custom assistant from OpenAI and use the assistant from your python program.

Go to https://platform.openai.com/assistants and click on “+ Create” button and then, below screen pops up, you can provide necessary details there,

custom assistant creation screen
OpenAI Custom Assistant — Creation Screen

After creating this successfully, you can see your assistant with it’s assistant id like below,

Custom Assitant with Assistant ID

Please note that, you are going to use this assistant ID while accessing the assistant from your program. Now, let’s see how the program going to connect and talk to assistant,

1. Create client completion by giving the details,

client = OpenAI()
OpenAI.api_key = os.getenv(‘OPENAI_API_KEY’)
completion = client.completions.create(
model=”gpt-3.5-turbo-instruct”,
prompt=”Say this is a test”,
max_tokens=7,
temperature=0
)

print(completion.choices[0].text)

Output:

This is a test.

2. Create thread for custom Assistant and run

Let’s the method to handle this,

def create_thread_and_run(user_input):

custom_gpt_id = ‘ASSISTANT_ID_HERE’

thread = client.beta.threads.create()

run = submit_message(custom_gpt_id, thread, user_input)

return thread, run

Let’s see what doing under submit_message:

def submit_message(assistant_id, thread, user_message):

client.beta.threads.messages.create(

thread_id=thread.id, role=”user”, content=user_message

)

return client.beta.threads.runs.create(

thread_id=thread.id,

assistant_id=assistant_id,

)

3. We can try calling create_thread_and_run method,

thread1, run1 = create_thread_and_run(

“I need to solve the equation `3x + 11 = 14`. Can you help me?”

)

4. See below method that wait for the run to complete,

def wait_on_run(run, thread):

while run.status == “queued” or run.status == “in_progress”:

run = client.beta.threads.runs.retrieve(

thread_id=thread.id,

run_id=run.id,

)

time.sleep(0.5)

return run

It will return after run completes.

5. Below method will be printing the received output properly,

def pretty_print(messages):

print(“# Messages”)

for m in messages:

print(f”{m.role}: {m.content[0].text.value}”)

print()

Let’s see how we can call above method by using thread and run we have,

pretty_print(get_response(thread1))

See below how get_response coded,

def get_response(thread):

return client.beta.threads.messages.list(thread_id=thread.id, order=”asc”)

pretty_print’s Output:

# Messages
user: I need to solve the equation `3x + 11 = 14`. Can you help me?
assistant: Sure, I can help you. To solve the equation 3x + 11 = 14, we need to isolate the variable x.

First, let’s subtract 11 from both sides of the equation:
3x + 11–11 = 14–11
3x = 3

Next, let’s divide both sides of the equation by 3 to solve for x:
3x/3 = 3/3
x = 1

So, the solution to the equation 3x + 11 = 14 is x = 1.

Recap

  1. We just created a sample custom assistant in OpenAI
  2. Created thread and run for the assistant to run
  3. We let thread execute and was waiting for run to complete.
  4. Finally, we were able to see the response from our custom dude (OpenAI Custom Assistant)

What’s Next?

I am trying out the steps with javascript that something work in google apps script (I haven’t found any links or help for this during my lil search and our cool buddy (ChatGPT) not yet learned this, so, let’s create a place for him to learn:-)

Interested to try?

Here is the full code: https://github.com/somanathtv/openai_gpt3.5_custom_assistant

References

--

--

Somanath Balakrishnan

Technical Consultant @ Digitalcore Technologies, Kochi, India