Skip to content

Commit

Permalink
WIP: Qt Network replacement of CURL @ Win #1514
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Jan 29, 2024
1 parent 2fc8c7e commit 218671e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ TEMPLATE = lib
CONFIG += staticlib
CONFIG -= qt

# Qt Network as CURL replacement on Windows
win32 {
CONFIG += qt
QT += network
}

# Dependencies:
# - INCLUDEPATH is used during compilation to find included header files.
# - DEPENDPATH is used to resolve dependencies between header and source files,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// - configuration: DEFINES = DO_MF_DEBUG
// - command line: CONFIG+=mfunits

// use Qt Network instead of CURL for the communication with OpenAI
// #define MF_OPENAI_QT_NETWORK

#ifdef DO_MF_DEBUG
#include <chrono>
#include <iostream>
Expand Down
40 changes: 40 additions & 0 deletions lib/src/mind/ai/llm/openai_wingman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,45 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {
<< "<<<"
<< endl);

#ifdef MF_OPENAI_QT_NETWORK
// Set up Qt networking options
QNetworkRequest request;
request.setUrl(QUrl("https://api.openai.com/v1/chat/completions"));

QByteArray requestBody(requestJSonStr.toUtf8());
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
request.setRawHeader("Authorization", "Bearer " + apiKey.toUtf8());

// Create a network access manager
QNetworkAccessManager manager;

// Send the request
QNetworkReply *reply = manager.post(request, requestBody);

// Connect to the finished signal to handle the response
connect(reply, &QNetworkReply::finished, [&]() {
if (reply->error() == QNetworkReply::NoError) {
command.httpResponse = reply->readAll().toUtf8();
command.answerHtml = parseHtmlFromJson(command.httpResponse);
command.answerTokens = countTokens(command.answerHtml);
command.answerLlmModel = llmModel;
command.status = WingmanStatusCode::WINGMAN_STATUS_CODE_SUCCESS;
} else {
command.status = WingmanStatusCode::WINGMAN_STATUS_CODE_ERROR;
command.errorMessage = reply->errorString();
std::cerr << "Error: Wingman OpenAI Qt networking request failed: " << command.errorMessage << std::endl;

command.httpResponse.clear();
command.answerHtml.clear();
command.answerTokens = 0;
command.answerLlmModel = llmModel;
return;
}
});

// Delete the network reply when it's finished
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
#else
// set up cURL options
command.httpResponse.clear();
curl_easy_setopt(
Expand Down Expand Up @@ -139,6 +178,7 @@ void OpenAiWingman::curlGet(CommandWingmanChat& command) {

return;
}
#endif

// parse JSon
/*
Expand Down
4 changes: 4 additions & 0 deletions lib/src/mind/ai/llm/openai_wingman.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include "wingman.h"

#ifdef MF_OPENAI_QT_NETWORK
#include <QtNetwork>
#endif

namespace m8r {

/**
Expand Down

0 comments on commit 218671e

Please sign in to comment.