Skip to content

Commit

Permalink
Ugly code, but Wingman works @ QNetwork on Win for OpenAi #1514
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Feb 3, 2024
1 parent dbcefd8 commit b1a4ea0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ INCLUDEPATH += ./src/qt/spelling
#
win32{
QMAKE_CXXFLAGS += /MP
QMAKE_CXX = ccache $$QMAKE_CXX
} else {
# linux and macos
mfnoccache {
Expand Down
23 changes: 23 additions & 0 deletions app/qnetwork-get-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "chatcmpl-8oIy1BN3YeHaGgc3AIaYcEsOWHXbG",
"object": "chat.completion",
"created": 1707000001,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 18,
"completion_tokens": 9,
"total_tokens": 27
},
"system_fingerprint": null
}
1 change: 1 addition & 0 deletions lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mfdebug|mfunits {
# compiler options (qmake CONFIG+=mfnoccache ...)
win32{
QMAKE_CXXFLAGS += /MP
QMAKE_CXX = ccache $$QMAKE_CXX
} else {
# linux and macos
mfnoccache {
Expand Down
101 changes: 94 additions & 7 deletions lib/src/mind/ai/llm/openai_wingman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
/*
OpenAI API JSon request example (see unit test):
...
{
"messages": [
{
"content": "You are a helpful assistant.",
"role": "system"
},
{
"content": "Hey hello! I'm MindForger user - how can you help me?",
"role": "user"
}
],
"model": "gpt-3.5-turbo"
}
*/
nlohmann::json messageSystemJSon{};
Expand Down Expand Up @@ -106,27 +118,102 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
/* Qt Networking examples:
*
* - https://community.openai.com/t/qt-interface-w-chatgpt-api/354900
* - https://forum.qt.io/topic/116601/qnetworkaccessmanager-reply-is-always-empty/7
* - https://gist.github.com/FONQRI/d8fb13150c1e6760f1b1617730559418
*/
#ifdef THIS_WORKS_GET
QNetworkAccessManager nam;

QNetworkRequest request(QUrl("https://www.walkman-pictures.com/"));
auto reply = nam.get(request);

QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

reply->deleteLater();
auto error = reply->error();
if(error != QNetworkReply::NoError) {
qDebug() << "network error:" << error << reply->errorString();
return;
}
QByteArray read = reply->readAll();
if(read.isEmpty()) {
qDebug() << "response is empty";
return;
}

QString fileName(QDir::currentPath() + "/" + "qnetwork-get-test.html");
QFile file(fileName);
//[CHANGED]Remove QIODevice::Text to prevent line terminator mis-translating
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "file open error:" << file.error() << file.errorString();
return;
}
//NOTE: Don't use QTextStream to write QByteArray to file, that's not the right way even if the result is correct!!!
file.write(read);
file.close();
#endif


QNetworkAccessManager networkManager;
string prompt{"Write a simple 'Hello World' program in Python."};
int maxTokens = 300;

QString qApiKey = QString::fromStdString(apiKey);

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

/*
QJsonObject jsonPayload;
jsonPayload.insert("prompt", QJsonValue(QString::fromStdString(prompt)));
jsonPayload.insert("max_tokens", maxTokens);
*/

// Send API request
QNetworkReply* reply = networkManager.post(request, QJsonDocument(jsonPayload).toJson());
QNetworkReply* reply = networkManager.post(
request,
requestJSonStr.c_str()
//QJsonDocument(jsonPayload).toJson()
);

QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

reply->deleteLater();
auto error = reply->error();
if(error != QNetworkReply::NoError) {
qDebug() << "network error:" << error << reply->errorString();
return;
}
QByteArray read = reply->readAll();
if(read.isEmpty()) {
qDebug() << "response is empty";
return;
}

QString qCommandResponse = QString{read};
qDebug() << "Response is: '" << qCommandResponse << "'";
command.httpResponse = qCommandResponse.toStdString();

/*
QString fileName(QDir::currentPath() + "/" + "qnetwork-get-test.html");
QFile file(fileName);
//[CHANGED]Remove QIODevice::Text to prevent line terminator mis-translating
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "file open error:" << file.error() << file.errorString();
return;
}
//NOTE: Don't use QTextStream to write QByteArray to file, that's not the right way even if the result is correct!!!
file.write(read);
file.close();
*/

/*
QObject::connect(
reply, &QNetworkReply::finished,
Expand All @@ -142,7 +229,7 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
}
reply->deleteLater();
});
*/
for(int i=0; i<120; i++) {
MF_DEBUG("Step " << i << endl);
if(reply->isRunning()) {
Expand All @@ -156,7 +243,7 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
}
QThread::msleep(1000);
}

*/


#ifdef DISABLED_DEBUG_BLAH
Expand Down Expand Up @@ -314,7 +401,7 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
// parse response string to JSon object
nlohmann::json httpResponseJSon;
try {
auto httpResponseJSon = nlohmann::json::parse(command.httpResponse);
httpResponseJSon = nlohmann::json::parse(command.httpResponse);
} catch (...) {
// catch ALL exceptions
MF_DEBUG(
Expand Down

0 comments on commit b1a4ea0

Please sign in to comment.