OPEN KNOWLEDGE: AI
OpenAI Assistant API with Code Interpreter
The tutorial explains usage of Assistant API — Code Interpreter.
Introduction
Assistant API enables developers to create complex workflows using: functions, code interpreter and knowledge retrieval. In this tutorial, I will explain usage of them in concrete manner, but what makes them useful?
Assistant API enables GPT-4 model to decide automatically when to retrieve knowledge, call function or use code interpreter. The automatization simplifies design of Autonomous Agents workflows.
Code interpreter
I will start by importing the required libraries and retrieve the OpenAI API key.
!pip install --upgrade openai
import os
import time
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("openaikey"))
I next define parameters in advance, which I will be using. These variables will make more sense later in this tutorial, but you can modify them for your use case.
assistant_name = "Power Point Generator"
output_file_name = "Presentation.pptx"
assistant_instruction = r"Generate {} file, always. You are subject-matter expert in the topic and professional in creating PowerPoints.. Betweem 1-5 slides. Background, colors, fonts and styling must be modern and easy to read. Make content engaging. Make the file id available to download.".format(output_file_name)
prompt_user = "Make a presentation for runner practicing for a half-marathon with an aim for a personal record. Make a presentaton with useful insights, training plan for different levels and some tips before, during and after the training period. Give insights."
I will create next the assistant.
assistant = client.beta.assistants.create(
name=assistant_name,
instructions=assistant_instruction,
tools=[{"type": "retrieval"},{"type": "code_interpreter"}],
model="gpt-4-1106-preview")
I will create then the thread within the assistant, which makes it possible to create messages within the thread.
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt_user)
I can now run the thread to generate the respond by the assistant api.
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id)
Once the response is available, I can retrieve it.
timeout = 180
interval_time = 5
time_taken = 0
while time_taken < timeout:
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id)
if run.status == 'completed':
break
else:
print(run.status)
time.sleep(interval_time)
time_taken += interval_time
# Alternatively solution is to run below simpler code and ensure run.status printed is 'completed', which can take up to 2 minutes:
#run = client.beta.threads.runs.retrieve(
# thread_id=thread.id,
# run_id=run.id)
#time.sleep(2)
# print(run.status)
I then pull the required information about the file id and filepath.
messages = client.beta.threads.messages.list(
thread_id=thread.id)
file_path = messages.data[0].content[0].text.annotations[0].file_path.file_id
file_name = client.files.with_raw_response.retrieve_content(file_path)
I can then save the file locally.
with open(output_file_name, "wb") as file:
file.write(file_name.content)
I finally delete the file and the assistant. I remind, that the Assistant API pricing is different from the other APIs, so I recommend not to leave the Assistants or files running longer periods.
client.files.delete(file_path)
client.beta.assistants.delete(assistant.id)
Conclusions
The tutorial explained usage of the Code Interpreter within the Assistant API. I created with the Code Interpreter a Powerpoint presentation by giving it a topic and I can then retrieve this file to the local file system.
Please make sure to familiarize to the Assistant API pricing before using it.
This article is in the series: Open Knowledge: AI.
The aim is to share at least 10% of my articles without the Medium paywall for free to everybody.
References
[1] Github. https://github.com/tmgthb/LLMs/tree/main. Teemu Maatta.