update
parent
5fadb582c9
commit
4e95923847
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue