| 419 | } |
| 420 | |
| 421 | void ChatManager::initLLM(AbstractLLM *llm) |
| 422 | { |
| 423 | if (!llm) |
| 424 | return; |
| 425 | chatLLM->setLocale(locale == Chat::Zh ? AbstractLLM::Zh : AbstractLLM::En); |
| 426 | connect(chatLLM, &AbstractLLM::dataReceived, this, [=](const QString &data, AbstractLLM::ResponseState state) { |
| 427 | if (state == AbstractLLM::Canceled) |
| 428 | return; |
| 429 | if (state == AbstractLLM::Failed) { |
| 430 | QString errStr; |
| 431 | bool valid = chatLLM->checkValid(&errStr); |
| 432 | if (!valid) |
| 433 | emit notify(2, tr("LLM is not valid. %1").arg(errStr)); |
| 434 | else |
| 435 | emit notify(2, tr("Error: %1, try again later").arg(data)); |
| 436 | emit terminated(); |
| 437 | return; |
| 438 | } |
| 439 | onResponse(QString::number(answerFlag), data, state); |
| 440 | }); |
| 441 | connect(chatLLM, &AbstractLLM::customDataReceived, this, [=](const QString &key, const QJsonObject &obj) { |
| 442 | QList<websiteReference> websites; |
| 443 | if (key == "crawl") { |
| 444 | for (auto it = obj.begin(); it != obj.end(); ++it) { |
| 445 | websiteReference website; |
| 446 | QString citationKey = it.key(); |
| 447 | QJsonObject citationObj = it.value().toObject(); |
| 448 | |
| 449 | website.citation = citationKey; |
| 450 | website.status = citationObj["status"].toString(); |
| 451 | website.url = citationObj["url"].toString(); |
| 452 | website.title = citationObj["title"].toString(); |
| 453 | |
| 454 | websites.append(website); |
| 455 | } |
| 456 | emit crawledWebsite(QString::number(answerFlag), websites); |
| 457 | } |
| 458 | }); |
| 459 | connect(this, &ChatManager::requestStop, chatLLM, &AbstractLLM::cancel); |
| 460 | } |
| 461 | |
| 462 | // todo: storage of session records |
| 463 | void ChatManager::fetchSessionRecords() |