QT Interface W/ ChatGPT API

I’m trying to interface with the API using Qt libraries, and when I receive replies from the API, they’re completely unrelated to my prompt. Anyone has any idea what I’m doing wrong? I’ll attach the output at the bottom:

 // Construct API endpoint and request
    QUrl apiEndpoint("https://api.openai.com/v1/engines/davinci/completions");
    QNetworkRequest request(apiEndpoint);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    request.setRawHeader("Authorization", "Bearer " + apiKey.toUtf8());

    QJsonObject jsonPayload;
    jsonPayload.insert("prompt", "Write a simple 'Hello World' program in " + selectedLanguage + ".");
    jsonPayload.insert("max_tokens", maxTokens);

    // Send API request
    QNetworkReply *reply = networkManager->post(request, QJsonDocument(jsonPayload).toJson());
    connect(reply, &QNetworkReply::finished, this, [this, reply, selectedLanguage, saveDir, cacheKey]() {
        if (reply->error() != QNetworkReply::NoError) {
            outputArea->append("Error: " + reply->errorString());
        } else {
            QJsonObject jsonResponse = QJsonDocument::fromJson(reply->readAll()).object();
            QString code = jsonResponse.value("choices").toArray().first().toObject().value("text").toString().trimmed();
            outputArea->append("Received code:");
            outputArea->append(code);

            // Cache the response
            cache.insert(cacheKey, new QString(code));

            // Save code to file
            saveCodeToFile(code, selectedLanguage, saveDir);
        }
        reply->deleteLater();
    });

Prompt: simple qt hello world program in C++

response:
Develop a card game and write a Python program to check the winner.

Program the rover and imagine you are on the way to Mars.

Write a simple Snake game and show it using any graphical library.

The completions engines such as davinci are not expert coders, and are a bit obsolete at this point.

You should use the chatCompletions API endpoint, for gpt-3.5-turbo (or gpt-4), and send the structured messages that replace prompt.

Programming tasks will need a lot of background and programming environment and specification of what you are trying to do.

messages format, application under-specified for brevity.

messages=[
{
“role”: “system”,
“content”: “You are a programming assistant, who specializes in turning specifications into programs.”,
},
{
“role”: “system”,
“content”: “Environment: Visual Studio 2017 C++. Qt 5.15. Target: Windows 10.”,
},
{
“role”: “user”,
“content”: “Write a Qt GUI 100x200, that in the center uses a text label to display ‘Hello world’, and has its own close button at the bottom.”,
}
]

It thinks you’re asking it how to prompt someone how to write a program.

Try “Write a simple hello world…”

Also, speak to it like a person. You wouldn’t just say “hello world program” to somebody. lol. They’d say what about hello world program?

I’m pretty sure chat gpt is just forwarding me some developer conversations! lol I don’t know what’s going on, my prompt was “Hello!” and this is the response I got back:

“\n\nI know what a Handle s error looks like, so i’m looking for such a message.But i don’t really see it here.Also i don’t see it in the Working log, as well.I’m sure about it, because i played around with it a bit.-- effort needed to debug this error: ehh.1100Attempts + 5 hrs debugging: yupJust let me know if you got any hints.”

That can be the result of using an untrained completion model such as curie. Completion AI models only continue autocompleting text that might come after what you wrote.

You likely want gpt-3.5-turbo, or at the minimum, the trained completion model text-davinci-003.

You can familiarize yourself with models and their responses at https://platform.openai.com/playground - and even receive example python code.

1 Like

I don’t think im receiving messages from the AI, one of the replies was literally addressed to me:

@onabipour3
You should have received a response to your thread. I’ll send you another message

The AI has no mechanism for knowing your name if you haven’t put it in your code.

An input like “hello” has no context of what text should follow. You’ll get random output, often like that of internet forums.

To see the behavior of a completion model, input text like:
“One example of a yellow fruit is a” - and see the text that would come after.

You can refer to the documentation and API reference links on the left of the forum to learn more about the endpoints and how to interact with models. You can’t ask ChatGPT to code this for you, as its knowledge is limited to 2021. (your “davinci” url at the top is likely AI)

That’s why I reached out to the OpenAI security team. I did not provide my name in the message.