Skip to content
November 10 2023

#110 A Cakewalk with Gen AI: Effortlessly Creating GPT with Assistants API

Blog Details

During the OpenAI DevDay, a revelation shook the world of technology. It marked the birth of GPTs through the Assistants API, heralding a new era of generative agents. Today, we won't just tread the usual path; we're embarking on an enchanting coding adventure. Imagine this code not just as a sequence of commands, but as a magical incantation, breathing life into a world where each line pulsates with the energy of a mystical being.

Preparing the Magic Lamp

In our first act of magic, we conjure the connection to OpenAI's vast knowledge, our digital Aladdin's lamp. This step is crucial, serving as the foundation of our enchanting journey, linking us to a world of boundless possibilities.

<code> 
import time
from openai import OpenAI
from config import api_key
lamp = OpenAI(api_key=api_key)
</code>

Here, we initiate our magic lamp, ready to illuminate the path to the genie's wisdom.

Unlike Hollywood, which uses the iconic 555.01xx numbers for phone scenes, the tech industry lacks a universal placeholder for API keys. We don't have a standardized '500.01xx' equivalent to use in demonstrations or examples.

Summoning the Genie

Now, let's breathe life into our genie, a being not only of immense wisdom but also of a playful spirit, ready to tackle the queries of the modern world with ancient insight and a hint of whimsy.

<code> # Create a 'genie' outside the loop since it's a one-time setup
genie = lamp.beta.assistants.create(
name="Tech Wizard",
instructions="You are a wise and ancient genie, skilled in the art of technology and coding. Answer the seekers' questions with your vast knowledge, but remember to add a touch of magic and wit to your responses.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-1106-preview"
) </code>

This incantation is the heart of our journey, setting the stage for the mystical interactions that follow.

Creating a Realm for Queries

In our digital tale, 'Curious George' isn't just a thread in our program; he's a character embodyed with endless curiosity. Reminiscent of the beloved character from children's literature, he is always eager to learn and explore. He approaches our AI genie, setting the stage for a dialogue where each question is an opportunity for discovery and magic.

curiousGeorge = lamp.beta.threads.create()

In this realm, our genie interacts with Curious George, weaving answers from an expansive tapestry of knowledge.

The Interactive Spell

As Curious George approaches with his questions, each query is a gentle whisper into the genie's domain. The genie, attentive and wise, prepares to respond with insight and a touch of whimsy.

# Main interaction loop
while True:
seeker_question = input("Oh mighty genie, I seek your wisdom (type 'exit' to quit): ")
if seeker_question.lower() == 'exit':
break

# Send the seeker's question to the 'genie'
query = lamp.beta.threads.messages.create(
thread_id=curiousGeorge.id,
role="user",
content=seeker_question
)

In this magical cycle, each of Curious George's questions opens a new door of adventure, inviting the genie to share its wisdom.

Granting the Wishes

As Curious George poses his inquiries, each one becomes a wish for our genie to fulfill. A mystical process begins, translating his words into responses infused with wisdom and a hint of enchantment.

# Create a run for the seeker's question
run = lamp.beta.threads.runs.create(
thread_id=curiousGeorge.id,
assistant_id=genie.id,
instructions="Respond as a genie would, with wisdom and a hint of mystical flair. Remember, you're not just an assistant; you're a guide through the realms of knowledge."
)

In this moment, the genie contemplates each question, ready to reveal answers that blend knowledge with a whimsical charm.

Waiting for the Genie's Wisdom

In the world of magic and code, patience is a virtue. Our program waits, giving the genie time to craft thoughtful responses. This ensures that each answer Curious George receives is as insightful as it is enchanting.

# Continuously check the status of the run until it's complete
while True:
run = lamp.beta.threads.runs.retrieve(
thread_id=curiousGeorge.id,
run_id=run.id
)
if run.status == 'completed':
break
time.sleep(1) # Wait for a second before checking the status again to avoid rate limiting

This waiting period is the calm before the storm of wisdom, where the genie prepares to reveal its insights.

Revealing the Genie's Insights

And then, the moment arrives. The genie speaks, its responses a blend of ancient wisdom and contemporary flair, enlightening Curious George's inquiries.

# Retrieve and print the messages
messages = lamp.beta.threads.messages.list(thread_id=curiousGeorge.id)
for msg in messages.data:
if msg.role == 'assistant':
for content_item in msg.content:
if hasattr(content_item, 'type') and content_item.type == 'text':
print(f"Genie: {content_item.text.value}")
elif msg.role == 'user':
print(f"Seeker: {seeker_question}")

With each response, the genie illuminates the path of knowledge, casting light on the queries posed by Curious George.