Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

How To Create Simple Generative AI Code In C++

In the short space of just over a year we have had very important developments happening in AI technologies. One of the biggest leaps was by ChatGPT-3 which is a Generative Pre-trained Transformer model in AI technology. Now we have GPT-4 and GPT-5 is on the way. In public tests on text and Images, it is well known that Generative AI is a very creative technology. Generative AI is also called Generative Artificial Intelligence which is a subset of Deep learning in Artificial Intelligence. In reality all these seemingly magical technologies happen mostly thanks to a lot of math. In this post, we will create some very simple C++ code that learns from a given input and generates random outputs.

What is AI? What is artificial intelligence?

Artificial Intelligence, also called AI refers to the simulation of human intelligence in machines that are programmed so they appear 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).  If you want to learn more AI terms you can check this post below.

If you want to learn more about basic AI examples in C++ then please check our Artificial Intelligence Technologies category.

What is Generative Artificial Intelligence?

Generative artificial intelligence (generative AIGenAI, or GAI) is an artificial intelligence technology that is used to generate text, images, videos, or other data by using generative models. Generative AI models learn from the given input and then generate new data that is similar to the given inputs.

Generative AI models are learned by the given inputs (such as text, images, or datasets) and then trained with these inputs by a generative model, then they can generate new output examples. Generative AI models are capable of generating new content with or without any human instructions. Generally, a user gives simple instructions (which is called “prompt”) then it generates a new sample appropriate with these instructions and a trained data set. Here is a very simple Generative AI model which we will give an example below.

Some of the Generative AI models are; Generative Adversarial Networks (GANs), Variational Autoencoders (VAEs), Autoregressive models, and Transformers.

How can we create simple generative AI code in C++?

First, let’s create our simple Generative AI that can be trained by given text inputs and learn from it, that means it can be trained by given inputs. Then we can create a generator model to generate new sentences. Here we will have a class that has a map. This map will hold strings and related strings with each one. Let’s define our class and map as below,

We can teach relations between words by adding them to a string vector. Note that, this is a very simple learning example. In real, generative AI models, you should consider more details and can be trained by the weights in different activation function models, more complicated professional models have more layers, biases, hidden neurons, filters, transformers, tensor models, and so on.

Let’s create a method that can learn from a given text in string types. Here is a learn_fromstring() method that can be added under the public section.

In our learn_fromstring() method above, we convert the sentence to istringstream, then we create a word (token) vector from this stream. Then we handle each word, and we push the next word to its mapped order. In an example, if we train “I am “, “I was”, and “I have” sentences it will add the “I” map and “am”, “was”, and “have” to this vector. In other words, it stores possible next words coming after the “I” word. In real these relations are made with word indexes, weights, activations functions, etc. For example “am” is generally used with “I”, so their relation is high, the weight coefficient between them should be higher. To make a simple example, we do not consider about weights and activations.

Now, we need a sentence generator under the public section that generates random text by considering the relation between words. First, let’s give our generator model example, and then we can explain.

Here above in the sentence_generator() method, we define the first word (you can store the first used words in a store, and you can choose randomly too) and the length of sentences to be generated wisely. Then we generate intermediate words of sentence in a loop. At the end, it will return the newly generated sequence sentence.

That’s it, very simple!

To understand what is happening inside this map, let’s create another method to print the word relations as below,

Now we can develop our main function. Let’s create a new Generative AI object from this class, and we can train some sentences by using the learn_fromstring() method. Let’s print word relations after the learning process from given sentences. Then we can create many new sentences. We can use the sentence_generator() method to create a new string in each loop. Here is how we can train some sentences and generate 10 new sentences as an output.

Is there a full example of simple generative AI code in C++?

Here is a full example of generative artificial intelligence using C++.

Here are the results:

I should note that I try to do as simple as it is, to make it understandable and easy to code. I tried to make about 20 different examples (including some support from Copilot) but finally, I found this is the most simple and easy generative AI model.

How To Create Simple Generative AI Code In C++ C++ logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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
C++C++17C++20C++23Learn C++Syntax

How To Use std::make_from_tuple in C++ 17

C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?