Learn How to Integrate GPT 3 API in C++

AI based software and hardware technology is rapidly increasing. Every day we see new developments. One of the great leap is we have more logical AI chat applications which are based on NLP and DL mechanisms. There are many AI Tools that can be used to develop an AI powered by a programming language like C, C++, Delphi, Python, etc. In this post we explain how you can use GPT-3 API in C++ Builder.

We will try to explain what GPT-3 is and how we can apply this Artificial Intelligence API to our C++ applications. If you don’t know anything about AI and you are not good at coding AI based apps, you can use 3rd party AI engines and their database to allow your users to experience some of the benefits from this hot topic technology.

What is AI?

Artificial Intelligence, also called AI refers to the simulation of human intelligence in machines that are programmed to ‘think’ like humans and mimic their actions. The term may also be applied to any machine that exhibits traits associated with a human mind such as learning and problem-solving. (reference: Investopedia). 

There are many other definitions about the same as above. In addition to the term AI, there are some other prominent branches of AI research. Machine learning (ML) is the study of computer algorithms that improve automatically through experience. While we have all seen advertisements that say a product is Smart or is an AI-supported thing, in reality, there is no genuine AI yet. We call all AI-related things AI Technology. The term AI in its definition may be happening with Artificial General Intelligence or AGI, also called as Strong AI. There is also Artificial Biological Intelligence (ABI) term that attempts to emulate ‘natural’ intelligence.

What is GPT-3 API?

GPT-3, Generative Pre-trained Transformer version 3 is an Artificial Intelligence API that is an autoregressive language model that uses deep learning to produce human-like text. GPT versions are created and supported by OpenAI which is a San Francisco-based AI research laboratory. This API supports different AI Engines and databases,

GPT-3 has been introduced in May 2020, and was in beta testing as of July 2020. GPT-3 is the third-generation language prediction model in the GPT series and it allows better results than  GPT-2. GPT-3’s full version has a capacity of 175 billion ML parameters. GPT-3 is part of a trend in Natural Language Processing (NLP) systems of pre-trained language representations. Microsoft announced on September 22, 2020, that it had licensed “exclusive” use of GPT-3. Now, many users and companies may use GPT-3 by the public API to receive text outputs from given text inputs.

Learn How to Integrate GPT 3 API in C++ an image of a robot hand touching a human hand
AI is getting better but its not perfect yet

How good is the GPT-3 AI?

According to most of testers, the quality of the text generated by GPT-3 is very good. It is so good that it can be difficult to determine if it was written by a human user, or by an AI API. This means it has both benefits and risks. In their paper, they warned of GPT-3’s potential dangers and called for research to mitigate risk. David Chalmers, an Australian philosopher, described GPT-3 as “one of the most interesting and important AI systems ever produced“. Personally, as an AI developing person, in my few tests in the first communication, I see a lot of wrong answers. It was very creative at generating answers from the given sentences. Note that this technology is still in development, and I found it very successful. GPT-3 might be a way towards AGI technology.

How does the GPT-3 AI work?

GPT-3 uses more than billions of tokens (words) in its data, and it generates results from this given database. It is generating sensible responses to a variety of input texts which was made possible due to the quantity of data it was trained with. It is a massive text-prediction transformer model which shattered many assumptions about the difficulty of understanding and creating written language.

Can I use GPT-3 AI today?

At the beginning, only few developers were able to access this API, and now OpenAI has opened up usage of GPT-3 to the public. It also has pricing plans for professional usage. Prices are based on number of used tokens which effectively the same as words. Generally, the term ‘token’ in programming is used for alpha numeric characters between the spaces or some other punctuation characters like dots and commas.

I found time to register to GPT-3, I wasn’t able to register before. I read some introduction and watch again a video by Embarcadero’s Jim McKeeth about GPT-3, and I tested the GPT-3 from their playground section. I was able to see that I was easily able to adapt this API to C++ Builder applications by using the REST Debugger. In this post, I will explain how you can register to this the most modern AI API and how you can use REST connection to send and receive inputs and outputs.

How to Register for GPT-3?

Directly go to https://openai.com/api/ or to https://beta.openai.com/playground and choose SignUp from the menus to sign-up to GPT-3 API. During sign-up It requires e-mail and cell phone verifications.

Then login to GPT-3 from https://beta.openai.com/playground

How to use GPT-3 from a web page without an API and C++?

Before using Playground you can use the Examples link https://beta.openai.com/examples. GPT-3 has different options to use in its Playground. You can use Chat, Q&A and many other options. Start with the Chat option or directly use the https://beta.openai.com/playground/p/default-chat link. On the right side, you can choose AI Engine, and other settings then you can talk with GPT-3 to get answers for your questions. Note that there is a token limit. The number of tokens that you use are shown at the right side of the Generate button.

For me, it was very interesting when I was able to use one of the latest versions of the most successful AI application world-wide. I am sure you will enjoy with chat.

Learn How to Integrate GPT 3 API in C++ a screenshot of the GPT 3 playground in use
OpenAI ChatGPT in action

How to get GPT-3 API keys to be used in C++?

If you want to use GPT-3 in your C++ applications, you should obtain API Keys. Then you can use them to get answers from a REST connection. To create a new API Key, click your Personal menu on the top right, then choose the “View API keys” menu. If you don’t have API key, press Create new secret key, then you can see your key by pressing Reveal or you can Copy it to clipboard, so you can paste in your use.

Learn How to Integrate GPT 3 API in C++ how to get the necessary API keys
Obtaining your GPT 3 AI API Keys

How to use an API Key to access GPT-3API with the REST Debugger in C++?

In RAD Studio, C++ Builder, there is a superbly useful tool called the REST Debugger. It comes included with RAD Studio C++ Builder. It is very useful to test REST Requests such as “Gets” and “Posts” to see what effect they have and view their results. It is very easy to automatically set up the REST components with their parameters, which can then be pasted or copied into your source code – potentially saving you hours of coding. We can easily set up the components by using the REST Debugger Tool in RAD Studio – either C++ Builder or Delphi – it works with both. If you are new to REST Debugger, maybe it is good to check this article first.

Let’s see how you can use REST debugger. If you want to use GPT3 API with REST Debugger,

It is good to test if all API Key and our messages are working. Before Requesting, we need to add API Key that we registered before in Parameters section. Here’s how you can do this.

  • Open RAD Studio, C++ Builder,
  • Open the REST Debugger from Tools menu.
  • Go to Parameters tab section.
  • Choose resource type, “engines/davinci/completions” for example.
  • In the “Request Parameters:” section, click to Add button,
    • set Kind to “HEADER
    • set Name to “Authorization
    • set Value to Bearer and add your GPT-3 API Key, i.e. “Bearer sk-Hr123DasdXcA451Sxx2z1x3D51ARg3hy99ru2d34”
    • check Do not encode

Here is an example below.

Learn How to Integrate GPT 3 API in C++ using the REST debugger

How to make a request from the GPT-3 AI API with the REST Debugger in C++?

Now we can add request details to the REST Debugger, let’s see how we can use Request from a GPT-3 In Rest Debugger.

  • In the “Request” tab select POST Method,
  • add GPT-3 API URL in the URL section (i.e. https://api.openai.com/v1, URL may be changed)
  • choose Content-Type as a application/json
  • and add a custom body, in example it can be like this.

Here, we want to use GPT3 to generate new words and sentences after the “it was a rainy day for” sentence. The maximum tokens are set to 150. Here is an example screenshot below.

Learn How to Integrate GPT 3 API in C++ returning the right API tokens

Now you can press Send Request button to test. Check the Body of Response, If the Response is okay that means you successfully run GPT-3 via REST Client. After this step, now you can create a simple REST Client in C++ Builder as in this example below.

How to connect to the GPT-3 API with REST Components in C++?

Easily a simple REST connection can be done by using these 3 REST components in C++ Builder.

  1. Create a new Multi-Device C++ Builder application, save all unit and project files to a folder
  2. Add a Button (TButton) and a Memo (TMemo) component to see all JSON data.
  3. Drag RESTClient (TRESTClient , RESTRequest (TRESTRequest) and RESTResponse (TRESTResponse) and arrange their positions to see their names.
  4. Click to RESTClient1, and on the Object Inspector window,
    set its BaseURL property to RESTFull web service ( i.e. for the GPT-3 , “https://api.openai.com/v1” )
    Double click to its Params and parameters of your web service ( i.e. for the GPT-3, Value=”Authentication” and access_key with the Value = YOUR API KEY parameters)
  5. Now we can connect to any REST Service with their appropriate parameters and we can get the response message from this server to a TJSONValue variable or to a component property as below

How to create a REST connection in code to connect to GPT-3 API in C++?

If you have RESTClient (TRESTClient , RESTRequest (TRESTRequest) and RESTResponse (TRESTResponse) components, you can use them in code too. First, you should bind them via Object Inspector or in your code as shown below.

If you have setup your REST components here is an example to connect GPT-3 AI API in C++.

Is there a full example of how to connect to the GPT-3 API in C++?

Here is a full C++ Builder FMX Project example, don’t forget to add Button, Memo, RESTClient, RESTRequest, and RESTResponse.

If you still have problems with setting up the components, or if you have connection problems to the REST Web Service, you can also use the REST Debugger to test REST Requests (Gets, Posts, etc), It is very easy to automatically set up these REST components with their parameters. This example of how to use REST with C++ may help you.

If you want to see more REST Examples, here is one of them.

If you have trouble using the API keys OpenAI has a great introduction articlehttps://beta.openai.com/docs/api-reference/introduction.

Learn How to Integrate GPT 3 API in C++ the C++ Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?