From 4e95923847bb32d8a006af81a1213fd3eee005e7 Mon Sep 17 00:00:00 2001 From: zhuzihcu Date: Fri, 17 Mar 2023 17:56:50 +0800 Subject: [PATCH] update --- example/ChatController.cpp | 22 ++++++++++++++++------ example/ChatController.h | 3 +++ example/page/ChatPage.qml | 6 +++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/example/ChatController.cpp b/example/ChatController.cpp index 18d3f8e6..855751e2 100644 --- a/example/ChatController.cpp +++ b/example/ChatController.cpp @@ -7,16 +7,17 @@ ChatController::ChatController(QObject *parent) networkManager = new QNetworkAccessManager(this); } + void ChatController::sendMessage(const QString& text){ isLoading(true); - QUrl apiUrl("https://api.openai.com/v1/engines/text-davinci-003/completions"); + QUrl apiUrl("https://api.openai.com/v1/chat/completions"); QNetworkRequest request(apiUrl); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setRawHeader("Authorization", QString::fromStdString("Bearer %1").arg(QString::fromUtf8(QByteArray::fromBase64(baseKey.toUtf8()))).toUtf8()); QJsonObject requestData; - requestData.insert("prompt", text); - requestData.insert("max_tokens", 1000); - requestData.insert("temperature", 0.5); + requestData.insert("model", "gpt-3.5-turbo"); + messages.append(createMessage("user",text)); + requestData.insert("messages", messages); QJsonDocument requestDoc(requestData); QByteArray requestDataBytes = requestDoc.toJson(); QNetworkReply* reply = networkManager->post(request, requestDataBytes); @@ -26,11 +27,13 @@ void ChatController::sendMessage(const QString& text){ qDebug() << responseString; QJsonDocument doc = QJsonDocument::fromJson(responseString.toUtf8()); QJsonObject jsonObj = doc.object(); - QString text = jsonObj.value("choices").toArray().at(0).toObject().value("text").toString(); + QString text = jsonObj.value("choices").toArray().at(0).toObject().value("message").toObject().value("content").toString(); if(text.isEmpty()){ text = "不好意思,我似乎听不懂您的意思"; + }else{ + messages.append(createMessage("assistant",text)); } - responseData(text); + responseData(text.trimmed()); } else { responseData("网络错误:"+reply->errorString()); } @@ -38,3 +41,10 @@ void ChatController::sendMessage(const QString& text){ reply->deleteLater(); }); } + +QJsonObject ChatController::createMessage(const QString& role,const QString& content){ + QJsonObject message; + message.insert("role",role); + message.insert("content",content); + return message; +} diff --git a/example/ChatController.h b/example/ChatController.h index 30d23a5b..f70ade60 100644 --- a/example/ChatController.h +++ b/example/ChatController.h @@ -19,9 +19,12 @@ public: explicit ChatController(QObject *parent = nullptr); Q_INVOKABLE void sendMessage(const QString& text); +private: + QJsonObject createMessage(const QString& role,const QString& content); private: QNetworkAccessManager* networkManager; + QJsonArray messages; QString baseKey = "c2stbXgxWm5MQkZ5TzhNYzNmRWl6eDZUM0JsYmtGSnNBWjNiakJjSXB6WGN3QW9KSk11"; }; diff --git a/example/page/ChatPage.qml b/example/page/ChatPage.qml index 5e3ead54..f47ae942 100644 --- a/example/page/ChatPage.qml +++ b/example/page/ChatPage.qml @@ -42,13 +42,13 @@ FluWindow { Component{ id:com_text TextEdit { - text: modelData.text + text: message wrapMode: Text.WrapAnywhere readOnly: true - textFormat: Text.RichText selectByMouse: true selectByKeyboard: true selectedTextColor: color + textFormat:TextEdit.AutoText color:FluColors.Black selectionColor: { if(FluTheme.isDark){ @@ -130,7 +130,7 @@ FluWindow { Loader{ id:item_msg_loader - property var modelData: model + property var message: model.text anchors.centerIn: parent sourceComponent: com_text }