Getting started with OpenAI Assistant APIs and Python

Earlier I had discussed using OpenAI Embeddings API to come up with a product recommendation system. In this post, I am going to discuss Assistant APIs, The APIs OpenAI provided to integrate custom GPTs. I am going to create a blog title generator in Flask that will be generating blog titles in the tech/gadget space. In case you are in a hurry you may want to watch the demo video of it:

What is a custom GPT?

According to OpenAI:

…ChatGPT that combine instructions, extra knowledge, and any combination of skills.

Custom GPTs, as the name says, are the customized versions of chatGPT that rely on your own data and instructions. You can instruct your version of GPT to respond in a certain format or to fetch data information from your own provided knowledge.

One wonders, why there’s a need to integrate a custom GPT with an app. Imagine a situation where you, as a business owner, have come up with an onboarding custom GPT and you want it to be available for your staff so that they do not bother you again and again or a digital version of a professional like you or an influencer who can provide benefit to his/her followers without a need of accessing him directly. Imagine you are questioning Warren Buffet, Seth Godin, or Malcolm Gladwell. What is required to upload all content related to them and an instruction set, that’s it!

Custom GPT Creation and Configuration

As of now both custom GPTs and Assistant APIs are only available to paid users. There’s a free $5 credit that is provided to new accounts that last for 2 months. Once it’s expired you gotta pay to OpenAI guys to use their APIs.

I have created a customGPT for this blog post, called Tech Blog Title Generator. If you are a paid user and visit the link, you’ll see something like the below:

So for instance, if I look for articles related to Google Bard, I will do something like this:

I used the following text as Instructions, also I updated this and this in PDF format in the knowledge base:

Focus: Generate blog titles that are specifically related to technology and gadgets. The titles should cover the latest innovations, reviews, and insights in the tech world.

Keywords Usage: Incorporate relevant keywords such as “smartphones”, “AI”, “virtual reality”, “IoT”, and “wearables”. Ensure these keywords are used logically and contextually to attract tech enthusiasts.

Engagement and Curiosity: Create titles that are catchy and engaging. Use action verbs, questions, or surprising facts to spark curiosity and encourage clicks.

SEO Considerations: While crafting titles, keep SEO in mind. Include primary keywords at the beginning of the title if possible. Make sure the titles are also concise, preferably under 60 characters, to perform well in search results.

Format Variety: Use a variety of title formats such as questions, lists (e.g., “Top 10…”, “5 Best…”), how-tos, and guides. This helps address different reader preferences and search intents.

Clarity and Accuracy: Ensure that each title accurately reflects the content of the potential blog post. Avoid misleading phrases that don’t directly relate to the content.

Timeliness and Trending Topics: Focus on current and emerging trends in the technology sector. Titles should feel up-to-date and relevant to current tech discussions and advancements.

**Important**

– You must refer the uploaded documents where needed for the better understanding.
– User will input some keyword, for instance AI, or LLM and you must generate maximum two titles
– You do not need to provide any explanations, You must return blog titles only

Now let’s automate this customGPT and integrate it with a web app by using OpenAI Assistant APIs. We will be creating an assistant and then by using the assistant ID we will be using it to ask about blog titles.

Let’s run it by testing it on the playground

Not bad! Now the goal is to come up with a webpage where you can query like we did on the chatGPT interface and playground and get the response from the assistant.

The user sends a query via our web app that is then sent to OpenAI Assistant via API which then sends a response based on your instructions and uploaded documents. I will be creating a webpage in Python Flask that will interact with our assistant via a REST API. The flow of sending/receiving messages using Assistant APis is given below (Credit: OpenAI):

Assistants API Diagram

Before I move further, I’d like to share updated instructions that are specific to the assistant only:

Focus: Generate blog titles that are specifically related to technology and gadgets. The titles should cover the latest innovations, reviews, and insights in the tech world.

Keywords Usage: Incorporate relevant keywords such as “smartphones”, “AI”, “virtual reality”, “IoT”, and “wearables”. Ensure these keywords are used logically and contextually to attract tech enthusiasts.

Engagement and Curiosity: Create titles that are catchy and engaging. Use action verbs, questions, or surprising facts to spark curiosity and encourage clicks.

SEO Considerations: While crafting titles, keep SEO in mind. Include primary keywords at the beginning of the title if possible. Make sure the titles are also concise, preferably under 60 characters, to perform well in search results.

Format Variety: Use a variety of title formats such as questions, lists (e.g., “Top 10…”, “5 Best…”), how-tos, and guides. This helps address different reader preferences and search intents.

Clarity and Accuracy: Ensure that each title accurately reflects the content of the potential blog post. Avoid misleading phrases that don’t directly relate to the content.

Timeliness and Trending Topics: Focus on current and emerging trends in the technology sector. Titles should feel up-to-date and relevant to current tech discussions and advancements.

**Important**

– You must refer the uploaded documents where needed for the better understanding.
– User will input some keyword, for instance AI, or LLM and you must generate maximum two titles
– You do not need to provide any explanations, You must return blog titles only

– You must return in a JSON Object array

As you can see, I am forcing the assistant to return the response in JSON format which makes sense because, unlike the custom GPT, the assistant will be communicating with an app hence it is easier to get a response in a standardized format instead of some random one. Now when I ask a question it’d return a JSON object.

Integration of Web App with Assistant API

I will not be sharing and everything here as the entire code will be available on GitHub. I am going to show how you integrate Assistant APIs with a Flask app.

@app.route("/ask", methods=['POST'])
def ask_assistant():
    config = dotenv_values(".env")
    API_KEY = config['API_KEY']
    ASSISTANT_ID = config['ASSISTANT_ID']
    client = OpenAI(api_key=API_KEY)
    # Create Thread
    thread_id = create_thread(client)
    data = request.get_json()
    query = data['query']
    # Create Message
    message_id = create_message(client, thread_id, query)

    # Create Runa and Get Response
    message = create_run_messages(client, thread_id, ASSISTANT_ID)
    message = json.loads(message)
    return jsonify({'data': message}), 200

Here is the ask REST API takes input, communicates with Assistant API, and returns the JSON response back to the web app. If everything works fine you’ll see an output like the one below:

The main thing is done, now you have the option to monetize your custom GPT. You may give one or two free usages and then can ask to pay via Stripe or any other gateway to charge from your customers. In this way, you can not only monetize your GPTs but can also provide them to people who are not even chatGPT users.

Conclusion

As you see, it is so easy to convert a custom GPT into an app and make it available for those who are on a free plan of chatGPTs or not even chatGPT users at all. I have converted a few custom GPTs of my clients into web apps and made them available for their internal staff. Like always the code is available on GitHub

If you like this post then you should subscribe to my blog for future updates.

* indicates required



Leave a Reply

Your email address will not be published. Required fields are marked *

19 + 10 =