OpenAI Assistant API allows the client to upload additional “knowledge” in a file so that the Assistant work with ChatGPT to provide more customized/private AI responses.
My client is C based code in QT Creator IDE. I use QT 6.5 which has QT classes QNetWorkManager and QNetworkRequest which provides the REST API GET/POST/PUT interface (and thus I dont need to use the curl library). I’m attempting to write wrapper functions that would approximate OpenAI’s Python based OpenAI library (pip install openai). I do know how to do achieve the upload via Python OpenAI library but support in (libai) C Library is unofficial and scarce and does not cover Assistant API functions. Hence I decided to have a go at it myself.
The OpenAI API reference is pretty straighforward to implement except for one (at least for me). That is the File Upload. OpenAI reference is at:
https://platform.openai.com/docs/api-reference/files/create
Specifically OpenAI states:
Upload file
POST https://api.openai.com/v1/files
My attempt to call the url via QNetWorkManager and QNetworkRequest did not work. Appreciate your help.
The following are the key excerpts of the code in QT. I stripped the code down to bare essentials to the problem only.
#define OpenAI_UPLOAD_FILE_URL "https://api.openai.com/v1/files"
/*
* Supporting function only
* Pretty Print API response from JSON Object
*/
void AI_Dialog::printJson(QJsonObject json){
QJsonDocument doc(json);
QString jsonString = doc.toJson(QJsonDocument::Indented);
qDebug().noquote() << jsonString.toStdString().c_str();
}
/*
* Wrapper function to OpenAI to upload a file
* Note: reply is defined in AI_Dialog class as QNetworkReply *reply.
* OpenAI_apiKey is key to OpenAI key (not shown)
*/
bool AI_Dialog::upload_file()
{
QUrl APIurl(OpenAI_UPLOAD_FILE_URL);
QNetworkRequest request(APIurl);
bool status = false;
//If not API key do not proceed
if (this->OpenAI_apiKey.isEmpty())
return false;
//Setup RestAPI Header
request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("multipart/form-data"));
request.setRawHeader("Authorization", "Bearer " + this->OpenAI_apiKey.toUtf8());
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart valuePart;
valuePart.setRawHeader("purpose", "assistants");
multiPart->append(valuePart);
valuePart.setRawHeader("file", "@contents.json");
valuePart.setBody("bla bla");
multiPart->append(valuePart);
// Do REST API Post operation
reply = this->network_access_manager.post(request, multiPart);
//Setup REST AI completion callbacks. Mian one is ReadyRead
status = connect(reply, &QNetworkReply::readyRead, this, &AI_Dialog::readyRead);
return true;
}
//Rest API callback completion for POST, GET etc to OPenAI
void AI_Dialog::readyRead()
{
QUrl url;
QJsonArray array;
QJsonObject ar1;
if (reply->error() == QNetworkReply::NoError){
//Get Data from reply
QString reply_str = reply->readAll();
QByteArray byte_array = reply_str.toUtf8();
QJsonDocument doc = QJsonDocument::fromJson(byte_array);
QJsonObject obj = doc.object();
//Debug Only
printJson(obj);
}
}
Result in Response (via printJson)
{
“error”: {
“code”: null,
“message”: “‘file’ is a required property”,
“param”: null,
“type”: “invalid_request_error”
}
}