How to Build an API Using ChatGPT

Thomas Lore
by Thomas Lore on September 11, 2023 9 min read

Guide to Building an API using ChatGPT

Opinions about ChatGPT are different. Some say it is here to improve our lives, others believe it will destroy our future, and another group doesn’t even treat it as a real deal. No matter what group you belong to, it is a versatile tool, and it is here to stay. ChatGPT can be used for anything – from playing around with it to tasks like info synthesis, queries, coding help, and even as a potential search engine alternative. 

Whether you’re an API developer or just a user discovering the technology, ChatGPT has more to offer than just its chatbot features. Being designed with an API in mind, it is a game-changer for the AI-integration into as many niches as possible. ChatGPT API functionality makes it a perfect fit for chatbot development and beyond. In this blog, we will discover several use cases of how to build an API with ChatGPT in the most effective way.

How and Why Should You Start Using ChatGPT API

OpenAI API: The Basics for Beginners

OpenAI’s API leverages the powerful Generative Pre-training Transformer (aka GPT) model. It is trained on vast amounts of text data to mimic human-like language. A key feature is its natural understanding and response, which is ideal for chatbots. Besides, it generates text for automated answers, summaries, or articles, aiding content creation, while its customization enhances accuracy (to a certain extent).

Scalability is another plus of this API, which allows you to integrate diverse platforms and systems. Apart from being user-friendly and well-documented, the technology offers SDKs (Software Development Kits) for different skill levels. This makes it useful not only for developers but also less-knowledgeable folks. You can use ChatGPT API to create text-generation apps, image generation, moderation, and much more.

Building an API using ChatGPT can be useful for any type of business in any niche. You could have a local hair salon and want your clients to have 24-hour support via a chatbot. Or you could be a translator who wants to speed up the audio and video translation by transcribing it into a text format. And, of course, there are countless uses for this API in marketing. You can delegate anything from automating your new campaign to monitoring which programs can help you find technology blogs that accept guest posts. So, how do you leverage all this potential?

How to Use ChatGPT API

API creation involves the following 6 steps:

  1. Getting an API Key. You can start by signing up for an API key on the OpenAI website to access the ChatGPT API.
  2. Choosing a Language. Selecting a programming language like Python, Java, or JavaScript is a second step. You’ll find SDKs and libraries for each.
  3. Installing the SDK. You need to install the chosen SDK or library. You can do it by running ‘pip install openai’ for Python or ‘npm install openai@^4.0.0’ for Node.js. 
  4. Creating an API Instance. Continue to set up a new API instance. For this, you need to provide your API key and other necessary details.
  5. Making API Requests. At this step, you can start making requests to the API. Using the ‘generate’ method allows the creation of text-based prompts.
  6. Processing the Response. Just handle the API’s response according to your needs, such as extracting and displaying the generated text in your application, for example.

4 Tips to Using ChatGPT API Effectively

Here, we will investigate together how to build an API with ChatGPT based on 4 different use cases. If you decide to use several of them, you can organize them into an API catalog.

Tip 1: API for Chat Completions

Chat models work by taking a list of messages as an input and generating a message as an output. This format is effective for single-turn tasks without any full-scale conversation. The key input is the ‘messages’ parameter. It is an array of message objects with roles (e.g., ‘system,’ ‘user,’ ‘assistant,’ etc.) and content. Yet conversations can be one-message or multi-turn exchanges. Here is a query you can take as an example:

 

code sample

Source: OpenAI

Usually, conversations start with a system message. It is followed by alternating user and assistant messages. System messages shape the assistant behavior, modifying its personality or giving instructions. User messages make requests, while assistant answers store past responses or provide examples. Since models lack memory, all needed context should be in the conversation. If a dialog is too long, it might need to be shorter due to token limits. 

If you want to go one step further in your personal assistant/chatbot journey, you can create your own avatar. This way, you can add more personality to your AI-based customer support. The truth is that nowadays, this technology is far more developed than many might think. You can generate photorealistic humans in a matter of seconds. Thanks to such tools, you can experiment to see if your avatar can improve the customer satisfaction rate or maybe even boost your sales. 

Tip 2: API for Turning Audio Into Text

The speech-to-text API in the advanced Whisper model offers two endpoints for the API creation. The first one is ‘transcriptions,’ and the second is ‘translations.’ You can transcribe audio into its original language. Also, it is possible to translate and transcribe the audio into English. Keep in mind that file uploads have a 25 MB limit. Yet, they should support any of the following formats: ‘mp3,’ ‘mp4,’ ‘wav,’ etc.

The transcriptions API requires your audio file and desired output format. The default response is in JSON with raw text. To set additional parameters in a request, you can add more ‘–form’ lines with the relevant options. Here is an example of an audio transcription query.

 

code sample

Source: OpenAI

The translations API converts audio in supported languages to English text. At the same time, the transcription endpoint keeps the output in the original language. Here is an example of an audio translation query.

Source: OpenAI

It’s important to mention that the underlying ChatGPT model was trained in 98 languages.  Yet, only 57 exceeded the <50% word error rate, which is an industry-standard benchmark for speech-to-text model accuracy. The model will return results for languages not listed above, but the quality will be rather low.

Tip 3: API for Image Generations

The ChatGPT API offers three ways to work with images. You can generate them from your text prompts or create edits of images using new text prompts. Also, with this API, you can produce variations of existing illustrations. Let’s discover it a bit closer.

With the image generations endpoint, you can create original visuals using text prompts. You can get several sizes: 256×256, 512×512, or 1024×1024 pixels. Remember that smaller sizes generate faster. You can request 1-10 images at once using the n parameter. Here is an example of an image generation query.

 

code sample

Source: OpenAI

Just keep in mind that a detailed description increases the chances of getting the desired result for you. For example, you can use the prompt ‘a white Siamese cat’ and get a simple average image. But, if you specify something like ‘a close-up, studio photographic portrait of a white Siamese cat that looks curious, backlit ears,’ you will get something that fits better.

cat promptSource: OpenAI

 

The image edit endpoint allows you to upload a mask that extends and modifies your visuals. The mask’s transparent parts are the ones guiding the editing process. Note that the prompt describes the entire new image, not just the erased area. As for the particular requirements, both the uploaded illustration and the mask must be square PNG files under 4MB. The non-transparent mask areas aren’t used in the output generation. That’s why they don’t have to match the original image.

 

Source: OpenAI

image example

Source: OpenAI

There is also an image variation endpoint. Like the edits endpoint, the input visual should be a square PNG file under 4MB in size. You can receive a final image as a URL or Base64 data by indicating your preferences in the response_format parameter. Just keep in mind that these URLs expire in an hour.

 

code sample

Source: OpenAI

cat image output

Source: OpenAI

Tip 4: API for Moderations

The ‘moderations’ endpoint helps check that content follows OpenAI’s usage policies. It focuses on identifying and handling prohibited content. These are the endpoints you will encounter:

  • The flagged endpoint indicates if the content violates OpenAI’s usage policies (true/false).
  • The categories endpoint shows violation flags for specific usage policy categories.
  • The category_scores represents the model’s confidence of policy violation in each category. Scores range from 0 to 1, indicating confidence but not probabilities.

 

sample

Source: OpenAI

code sample

Source: OpenAI

If you decide to add any additional moderation to your chatbots using ChatGPT API, you have to train AI to understand your audience and its expectations. It will have to react to different types of queries depending on your user profile. Moderating content for the general public and specific professional groups is different. For example, in the case of software engineers, Collabnix recommends paying special attention to the developers’ mindset. This way, you can train your system accordingly to avoid the unnecessary censoring of some terminology and tech slang.

When you know how to use ChatGPT to build an API, it’s time to pay attention to some useful tips. Developers aim to craft user-friendly APIs, while users desire straightforward interfaces. Luckily, there are strategies developers can use to enhance their APIs. You can use simple namings, standardize responses, versioning, and try some new technical hacks.

Conclusions

Now you know how to use ChatGPT for API creation efficiently. Also, with this guide, you can get a better understanding of how to create proper chatbots, turn audio into text, generate and improve images, and the main moderation principles. Of course, ChatGPT API is a powerful tool that can bring your development to the next level. Even if it seems too complicated at first glance, it gets easier with practice and reading through more documentation. 

Share this post

Stoplight to Join SmartBear!

As a part of SmartBear, we are excited to offer a world-class API solution for all developers' needs.

Learn More
The blog CTA goes here! If you don't need a CTA, make sure you turn the "Show CTA Module" option off.