update
parent
28b65e2f33
commit
6b54401371
|
@ -65,7 +65,7 @@
|
||||||
|
|
||||||
# 部分效果预览
|
# 部分效果预览
|
||||||
|
|
||||||
## 内置一个ChatGPT聊天Demo
|
## 一个聊天Demo,调用了ChatGPT的接口
|
||||||
|
|
||||||
![](doc/preview/chatgpt.png)
|
![](doc/preview/chatgpt.png)
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ void ChatController::sendMessage(const QString& text){
|
||||||
QUrl apiUrl("https://api.openai.com/v1/engines/text-davinci-003/completions");
|
QUrl apiUrl("https://api.openai.com/v1/engines/text-davinci-003/completions");
|
||||||
QNetworkRequest request(apiUrl);
|
QNetworkRequest request(apiUrl);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
request.setRawHeader("Authorization", "Bearer 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");
|
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("prompt", text);
|
||||||
requestData.insert("max_tokens", 1000);
|
requestData.insert("max_tokens", 1000);
|
||||||
|
@ -21,6 +21,7 @@ void ChatController::sendMessage(const QString& text){
|
||||||
QByteArray requestDataBytes = requestDoc.toJson();
|
QByteArray requestDataBytes = requestDoc.toJson();
|
||||||
QNetworkReply* reply = networkManager->post(request, requestDataBytes);
|
QNetworkReply* reply = networkManager->post(request, requestDataBytes);
|
||||||
connect(reply, &QNetworkReply::finished,this, [=]() {
|
connect(reply, &QNetworkReply::finished,this, [=]() {
|
||||||
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
QString responseString = QString::fromUtf8(reply->readAll());
|
QString responseString = QString::fromUtf8(reply->readAll());
|
||||||
qDebug() << responseString;
|
qDebug() << responseString;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(responseString.toUtf8());
|
QJsonDocument doc = QJsonDocument::fromJson(responseString.toUtf8());
|
||||||
|
@ -32,10 +33,8 @@ void ChatController::sendMessage(const QString& text){
|
||||||
responseData(text);
|
responseData(text);
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
isLoading(false);
|
isLoading(false);
|
||||||
});
|
} else {
|
||||||
connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::errorOccurred), this, [=](QNetworkReply::NetworkError) {
|
responseData("网络错误:"+reply->errorString());
|
||||||
qDebug() << "Network error occurred: " << reply->errorString();
|
}
|
||||||
reply->deleteLater();
|
|
||||||
isLoading(false);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager* networkManager;
|
QNetworkAccessManager* networkManager;
|
||||||
|
QString baseKey = "c2stbXgxWm5MQkZ5TzhNYzNmRWl6eDZUM0JsYmtGSnNBWjNiakJjSXB6WGN3QW9KSk11";
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CHATCONTROLLER_H
|
#endif // CHATCONTROLLER_H
|
||||||
|
|
Loading…
Reference in New Issue