This commit is contained in:
zhuzihcu 2023-03-17 17:56:50 +08:00
parent 5fadb582c9
commit 4e95923847
3 changed files with 22 additions and 9 deletions

View File

@ -7,16 +7,17 @@ ChatController::ChatController(QObject *parent)
networkManager = new QNetworkAccessManager(this); networkManager = new QNetworkAccessManager(this);
} }
void ChatController::sendMessage(const QString& text){ void ChatController::sendMessage(const QString& text){
isLoading(true); 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); QNetworkRequest request(apiUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Authorization", QString::fromStdString("Bearer %1").arg(QString::fromUtf8(QByteArray::fromBase64(baseKey.toUtf8()))).toUtf8()); request.setRawHeader("Authorization", QString::fromStdString("Bearer %1").arg(QString::fromUtf8(QByteArray::fromBase64(baseKey.toUtf8()))).toUtf8());
QJsonObject requestData; QJsonObject requestData;
requestData.insert("prompt", text); requestData.insert("model", "gpt-3.5-turbo");
requestData.insert("max_tokens", 1000); messages.append(createMessage("user",text));
requestData.insert("temperature", 0.5); requestData.insert("messages", messages);
QJsonDocument requestDoc(requestData); QJsonDocument requestDoc(requestData);
QByteArray requestDataBytes = requestDoc.toJson(); QByteArray requestDataBytes = requestDoc.toJson();
QNetworkReply* reply = networkManager->post(request, requestDataBytes); QNetworkReply* reply = networkManager->post(request, requestDataBytes);
@ -26,11 +27,13 @@ void ChatController::sendMessage(const QString& text){
qDebug() << responseString; qDebug() << responseString;
QJsonDocument doc = QJsonDocument::fromJson(responseString.toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(responseString.toUtf8());
QJsonObject jsonObj = doc.object(); 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()){ if(text.isEmpty()){
text = "不好意思,我似乎听不懂您的意思"; text = "不好意思,我似乎听不懂您的意思";
}else{
messages.append(createMessage("assistant",text));
} }
responseData(text); responseData(text.trimmed());
} else { } else {
responseData("网络错误:"+reply->errorString()); responseData("网络错误:"+reply->errorString());
} }
@ -38,3 +41,10 @@ void ChatController::sendMessage(const QString& text){
reply->deleteLater(); reply->deleteLater();
}); });
} }
QJsonObject ChatController::createMessage(const QString& role,const QString& content){
QJsonObject message;
message.insert("role",role);
message.insert("content",content);
return message;
}

View File

@ -19,9 +19,12 @@ public:
explicit ChatController(QObject *parent = nullptr); explicit ChatController(QObject *parent = nullptr);
Q_INVOKABLE void sendMessage(const QString& text); Q_INVOKABLE void sendMessage(const QString& text);
private:
QJsonObject createMessage(const QString& role,const QString& content);
private: private:
QNetworkAccessManager* networkManager; QNetworkAccessManager* networkManager;
QJsonArray messages;
QString baseKey = "c2stbXgxWm5MQkZ5TzhNYzNmRWl6eDZUM0JsYmtGSnNBWjNiakJjSXB6WGN3QW9KSk11"; QString baseKey = "c2stbXgxWm5MQkZ5TzhNYzNmRWl6eDZUM0JsYmtGSnNBWjNiakJjSXB6WGN3QW9KSk11";
}; };

View File

@ -42,13 +42,13 @@ FluWindow {
Component{ Component{
id:com_text id:com_text
TextEdit { TextEdit {
text: modelData.text text: message
wrapMode: Text.WrapAnywhere wrapMode: Text.WrapAnywhere
readOnly: true readOnly: true
textFormat: Text.RichText
selectByMouse: true selectByMouse: true
selectByKeyboard: true selectByKeyboard: true
selectedTextColor: color selectedTextColor: color
textFormat:TextEdit.AutoText
color:FluColors.Black color:FluColors.Black
selectionColor: { selectionColor: {
if(FluTheme.isDark){ if(FluTheme.isDark){
@ -130,7 +130,7 @@ FluWindow {
Loader{ Loader{
id:item_msg_loader id:item_msg_loader
property var modelData: model property var message: model.text
anchors.centerIn: parent anchors.centerIn: parent
sourceComponent: com_text sourceComponent: com_text
} }