OpenAI API Bible Volume 2Chapter 24
4. Step-by-Step Explanation (Referencing the Code Above)
Section 4 of 4-~ 12 min read-Synced from Cuantum content
- Setup & Initialization:
- - The script imports necessary libraries (openai, os, time, dotenv, datetime).
- It loads the OPENAIAPIKEY from your .env file.
- It initializes the client = OpenAI(...) object, which will be used for all API interactions. Error handling is included here.
- Constants for the assistant's configuration, polling interval, and timeout are defined.
- Create/Retrieve Assistant (createorretrieve_assistant function):
- - This function first checks if an assistant with the specified ASSISTANT_NAME already exists to avoid creating duplicates.
- If not found, it calls client.beta.assistants.create(...) using the defined name, instructions, model (gpt-4o), and crucially enables the image_generation tool. This tool uses DALL·E 3 behind the scenes.
- Start a Thread (generatevisualstory function):
- - A new conversation thread is created using client.beta.threads.create(). Threads store the message history.
- Send User Prompt (generatevisualstory function):
- - The user's story idea (user_prompt) is added to the thread using client.beta.threads.messages.create(...) with role="user".
- Run the Assistant (generatevisualstory function):
- - The assistant is instructed to process the thread using client.beta.threads.runs.create(...), passing the assistantid and threadid.
- Wait for Completion (pollrunstatus function):
- - Creating the story and images takes time. The script enters a loop, periodically checking the run's status using client.beta.threads.runs.retrieve(...).
- It prints the status (queued, in_progress, completed, etc.) for user feedback.
- The loop continues until the status is completed or another terminal state (failed, cancelled, expired), or until the timeout is reached.
- Retrieve Story Text & Image File IDs (generatevisualstory function):
- - Once the run is complete, client.beta.threads.messages.list(...) retrieves all messages from the thread (using order="asc" to get them chronologically).
- The code iterates through the assistant's messages.
- For content of type="text", it prints the scene description.
- For content of type="imagefile", it prints the fileid associated with the generated image. This ID is the key to getting the actual image.
- Retrieve Image Content (saveimagefromfileid function):
- - This is the crucial step added. For each file_id obtained in the previous step, this helper function is called.
- It uses client.files.retrievecontent(fileid) to fetch the raw binary data of the image.
- It then saves this binary data into a .png file in a specified output directory (story_images), including a timestamp and the file ID in the filename for uniqueness.
Optional Extensions
- Add page numbers or captions based on the scene_count.
- Modify the ASSISTANT_INSTRUCTIONS to accept a visual style parameter (e.g., "Generate images in a watercolor style").
- Implement logic to load a previous thread_id to continue or remix stories.
- Use a library like ReportLab or HTML generation to create a formatted PDF or web page output.
What You’ve Built
By running this script, you'll have executed a complete AI storytelling engine that:
- Accepts a natural language prompt.
- Uses GPT-4o to generate sequential scenes with descriptions.
- Invokes the DALL·E 3 tool to render matching illustrations.
- Retrieves both text descriptions and actual image files.
- Builds the components of a visually compelling narrative experience.
This project effectively combines multimodal handling (text prompt -> text + image generation), tool chaining within the Assistants API, asynchronous operation handling, and file retrieval into one practical and fun application.