How to Make Friends with C++ and Neural Networks: Writing a Client for the ChatGPT API

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why C++ and ChatGPT — Not Strange, But Powerful

\n\n

When it comes to integrating neural networks, everyone immediately thinks of Python. Yes, Pythonistas have elegant libraries like openai and requests. But what if your project is a high-load C++ server, a desktop application, or a game engine? Should you rewrite everything in Python?

\n\n

The good news: integration with the ChatGPT API in C++ is not magic, but a perfectly solvable engineering task. You get native code speed, memory control, and the ability to embed AI functions directly into the core of your application. No Python or Node.js layers — just pure C++ and direct API work.

\n\n

In this article, we will break down how to write a client for the OpenAI API in C++ from scratch. You will learn how to send HTTP requests, process JSON, and receive responses from ChatGPT. Let's go!

\n\n

What We Need: Toolkit

\n\n

To work with the ChatGPT API in C++, we need to solve two basic tasks:

\n\n
    \n
  • Send HTTP requests (preferably with HTTPS support)
  • \n
  • Parse and generate JSON
  • \n
\n\n

We will use two proven libraries:

\n\n
    \n
  • libcurl — the de facto standard for HTTP in C/C++. Available in any package manager.
  • \n
  • nlohmann/json — a modern, convenient JSON library. Just include the header file.
  • \n
\n\n

We will also need an API key from OpenAI. If you don't have one yet, go to platform.openai.com, register, and create a key in the API keys section. Keep it safe, preferably in an environment variable.

\n\n

Architecture of a Simple Client

\n\n

Our client will do one thing: send a user message to ChatGPT and receive a response. All communication with the API happens via the endpoint https://api.openai.com/v1/chat/completions using the POST method.

\n\n

The structure will be as follows:

\n\n
    \n
  1. Form a JSON with request parameters (model, messages, temperature)
  2. \n
  3. Send a POST request via libcurl
  4. \n
  5. Receive a JSON response and parse it
  6. \n
  7. Extract the assistant's response text
  8. \n
\n\n

Sounds simple? Let's write the code.

\n\n

Step 1: Preparing the JSON Request

\n\n

Let's start with a function that forms the request body. We will use the nlohmann/json library. Installing it is very simple: download json.hpp from GitHub and place it in your project folder.

\n\n
#include <string>\n#include "json.hpp"\n\nusing json = nlohmann::json;\n\nstd::string build_chat_request(const std::string& user_message) {\n    json request;\n    request["model"] = "gpt-3.5-turbo";\n\n    json messages = json::array();\n    json user_msg;\n    user_msg["role"] = "user";\n    user_msg["content"] = user_message;\n    messages.push_back(user_msg);\n\n    request["messages"] = messages;\n    request["temperature"] = 0.7;\n    request["max_tokens"] = 1024;\n\n    return request.dump();\n}
\n\n

Note: we are using the gpt-3.5-turbo model — this is the optimal balance between quality and speed. If you need something more powerful, you can specify gpt-4, but it is more expensive.

\n\n

Step 2: Sending an HTTP Request with libcurl

\n\n

Now for the interesting part — working with C++ HTTP requests. libcurl requires some initialization, but overall it is straightforward.

\n\n

Let's write a function that takes the request body and API key, and returns the raw response from the server.

\n\n
#include <curl/curl.h>\n#include <string>\n\nsize_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {\n    size_t total_size = size * nmemb;\n    output->append((char*)contents, total_size);\n    return total_size;\n}\n\nstd::string send_chat_request(const std::string& json_body, const                

Blogs

Book Recommendations