Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/plugins/aimanager/option/detailwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "detailwidget.h"
#include "addmodeldialog.h"
#include "modelconfigdialog.h"
#include "aimanager.h"

Check warning on line 7 in src/plugins/aimanager/option/detailwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "aimanager.h" not found.

Check warning on line 7 in src/plugins/aimanager/option/detailwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "aimanager.h" not found.

#include <DTableView>

Check warning on line 9 in src/plugins/aimanager/option/detailwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DTableView> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 9 in src/plugins/aimanager/option/detailwidget.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DTableView> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DComboBox>
#include <DFrame>
#include <DListView>
Expand Down Expand Up @@ -98,7 +98,7 @@
friend class DetailWidget;
DListView *modelsView = nullptr;
LLMModels *LLMModel = nullptr;
AddModelDialog *addModelDialog = nullptr;
ModelConfigDialog *addModelDialog = nullptr;
DComboBox *cbCompletedLLM = nullptr;
};

Expand Down Expand Up @@ -175,10 +175,10 @@
vLayout->addLayout(buttonLayout);

connect(buttonAdd, &DToolButton::clicked, this, [=](){
auto dialog = new AddModelDialog(this);
auto dialog = new ModelConfigDialog(this);
auto code = dialog->exec();
if (code == QDialog::Accepted) {
auto newLLM = dialog->getNewLLmInfo();
auto newLLM = dialog->getLLmInfo();
d->LLMModel->appendLLM(newLLM);
if (d->cbCompletedLLM->findData(newLLM.toVariant()) != -1)
d->cbCompletedLLM->addItem(newLLM.modelName, newLLM.toVariant());
Expand All @@ -205,6 +205,31 @@
if (d->cbCompletedLLM->findData(llmInfo.toVariant()) != -1)
d->cbCompletedLLM->removeItem(d->cbCompletedLLM->findData(llmInfo.toVariant()));
});
connect(d->modelsView, &DListView::doubleClicked, this, [this](const QModelIndex &index){
if (!index.isValid())
return;

auto dialog = new ModelConfigDialog(this);
auto llmInfo = d->LLMModel->allLLMs().at(index.row());
dialog->setLLmInfo(llmInfo);
auto code = dialog->exec();
if (code == QDialog::Accepted) {
auto changedLLMInfo = dialog->getLLmInfo();
bool isSame = llmInfo == changedLLMInfo;
if (!isSame) {
// remove original one
d->LLMModel->removeLLM(llmInfo);
if (d->cbCompletedLLM->findData(llmInfo.toVariant()) != -1)
d->cbCompletedLLM->removeItem(d->cbCompletedLLM->findData(llmInfo.toVariant()));

// add changed one
d->LLMModel->appendLLM(changedLLMInfo);
if (d->cbCompletedLLM->findData(changedLLMInfo.toVariant()) == -1)
d->cbCompletedLLM->addItem(changedLLMInfo.modelName, changedLLMInfo.toVariant());
}
}
dialog->deleteLater();
});
}

void DetailWidget::addDefaultLLM()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "addmodeldialog.h"
#include "modelconfigdialog.h"
#include "services/ai/aiservice.h"

Check warning on line 6 in src/plugins/aimanager/option/modelconfigdialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "services/ai/aiservice.h" not found.

Check warning on line 6 in src/plugins/aimanager/option/modelconfigdialog.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "services/ai/aiservice.h" not found.
#include "aimanager.h"

Check warning on line 7 in src/plugins/aimanager/option/modelconfigdialog.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "aimanager.h" not found.

Check warning on line 7 in src/plugins/aimanager/option/modelconfigdialog.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "aimanager.h" not found.

#include <DPasswordEdit>
#include <DStackedWidget>
Expand All @@ -21,8 +21,8 @@
class AddModelDialogPrivate
{
public:
friend class AddModelDialog;
AddModelDialogPrivate(AddModelDialog *qq) : q(qq) {}
friend class ModelConfigDialog;
AddModelDialogPrivate(ModelConfigDialog *qq) : q(qq) {}
void initUi();
void initConnection();

Expand All @@ -45,34 +45,34 @@

void showWaitingState(bool waiting);
void showErrorInfoDialog(const QString &error);
AddModelDialog *q;
ModelConfigDialog *q;
LLMInfo LLMAppended;
};

void AddModelDialogPrivate::initUi()
{
q->setFixedWidth(543);
QLabel *lbModelName = new QLabel(AddModelDialog::tr("Model Name"));
QLabel *lbModelName = new QLabel(ModelConfigDialog::tr("Model Name"));
lbModelName->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
leLLMName = new DLineEdit(q);
leLLMName->setPlaceholderText(AddModelDialog::tr("Required, please enter."));
leLLMName->setPlaceholderText(ModelConfigDialog::tr("Required, please enter."));

QLabel *lbLLMType = new QLabel(AddModelDialog::tr("Model Type"));
QLabel *lbLLMType = new QLabel(ModelConfigDialog::tr("Model Type"));
lbLLMType->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
cbLLMType = new DComboBox(q);
cbLLMType->addItem(AddModelDialog::tr("OpenAi(Compatible)"), LLMType::OPENAI);
cbLLMType->addItem(ModelConfigDialog::tr("OpenAi(Compatible)"), LLMType::OPENAI);

QLabel *lbApiUrl = new QLabel(AddModelDialog::tr("Api Path"));
QLabel *lbApiUrl = new QLabel(ModelConfigDialog::tr("Api Path"));
lbApiUrl->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
leApiUrl = new DLineEdit(q);
leApiUrl->setPlaceholderText(AddModelDialog::tr("Required, please enter."));
leApiUrl->setPlaceholderText(ModelConfigDialog::tr("Required, please enter."));

QLabel *lbApiKey = new QLabel(AddModelDialog::tr("Api Key"));
QLabel *lbApiKey = new QLabel(ModelConfigDialog::tr("Api Key"));
lbApiKey->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
leApiKey = new DPasswordEdit(q);
leApiKey->setPlaceholderText(AddModelDialog::tr("Optional, please enter."));
leApiKey->setPlaceholderText(ModelConfigDialog::tr("Optional, please enter."));

q->setWindowTitle(AddModelDialog::tr("Add Model"));
q->setWindowTitle(ModelConfigDialog::tr("Add Model"));

mainWidget = new QWidget(q);
QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
Expand All @@ -94,17 +94,17 @@
gridLayout->addWidget(leApiKey, 3, 1);

okButton = new DSuggestButton(q);
okButton->setText(AddModelDialog::tr("Confirm"));
okButton->setText(ModelConfigDialog::tr("Confirm"));
cancelButton = new DPushButton(q);
cancelButton->setText(AddModelDialog::tr("Cancel"));
cancelButton->setText(ModelConfigDialog::tr("Cancel"));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(cancelButton);
buttonLayout->addWidget(new DVerticalLine(q));
buttonLayout->addWidget(okButton);

mainLayout->addLayout(gridLayout);
mainLayout->addSpacing(10);
auto label = new DLabel(AddModelDialog::tr("To test if the model is available, the system will send a small amount of information, which will consume a small amount of tokens."), q);
auto label = new DLabel(ModelConfigDialog::tr("To test if the model is available, the system will send a small amount of information, which will consume a small amount of tokens."), q);
label->setWordWrap(true);
label->setForegroundRole(DPalette::PlaceholderText);
mainLayout->addWidget(label);
Expand All @@ -117,7 +117,7 @@
checkingLayout->setSpacing(20);
spinner = new DSpinner(checkingWidget);
spinner->setFixedSize(32, 32);
QLabel *lbCheck = new QLabel(AddModelDialog::tr("Checking... please wait."), checkingWidget);
QLabel *lbCheck = new QLabel(ModelConfigDialog::tr("Checking... please wait."), checkingWidget);
lbCheck->setAlignment(Qt::AlignCenter);
checkingLayout->addStretch(1);
checkingLayout->addWidget(spinner, 0, Qt::AlignCenter);
Expand All @@ -132,18 +132,18 @@

void AddModelDialogPrivate::initConnection()
{
AddModelDialog::connect(okButton, &DSuggestButton::clicked, q, [=](){
ModelConfigDialog::connect(okButton, &DSuggestButton::clicked, q, [=](){
slotAddModel();
});
AddModelDialog::connect(cancelButton, &DSuggestButton::clicked, q, &AddModelDialog::reject);
ModelConfigDialog::connect(cancelButton, &DSuggestButton::clicked, q, &ModelConfigDialog::reject);
}

void AddModelDialogPrivate::slotAddModel()
{
LLMInfo newLLMInfo;
newLLMInfo.modelName = leLLMName->text();
if (newLLMInfo.modelName.isEmpty()) {
leLLMName->showAlertMessage(AddModelDialog::tr("This field cannot be empty."));
leLLMName->showAlertMessage(ModelConfigDialog::tr("This field cannot be empty."));
return;
}
newLLMInfo.type = cbLLMType->currentData().value<LLMType>();
Expand All @@ -152,7 +152,7 @@

newLLMInfo.modelPath = leApiUrl->text();
if (newLLMInfo.modelPath.isEmpty()) {
leApiUrl->showAlertMessage(AddModelDialog::tr("This field cannot be empty."));
leApiUrl->showAlertMessage(ModelConfigDialog::tr("This field cannot be empty."));
return;
}
newLLMInfo.apikey = leApiKey->text();
Expand Down Expand Up @@ -184,7 +184,7 @@
{
DDialog dialog;
dialog.setIcon(QIcon::fromTheme("dialog-warning"));
dialog.setWindowTitle(AddModelDialog::tr("Error Information"));
dialog.setWindowTitle(ModelConfigDialog::tr("Error Information"));
auto warningText = new DLabel(error, q);
warningText->setTextInteractionFlags(Qt::TextSelectableByMouse);
warningText->setWordWrap(true);
Expand All @@ -195,26 +195,39 @@
mainLayout->addWidget(warningText, 0, Qt::AlignTop | Qt::AlignLeft);
dialog.addContent(mainWidget);

dialog.addButton(AddModelDialog::tr("Confirm"));
dialog.addButton(ModelConfigDialog::tr("Confirm"));
dialog.exec();
}

AddModelDialog::AddModelDialog(QWidget *parent)
ModelConfigDialog::ModelConfigDialog(QWidget *parent)
: DDialog(parent), d(new AddModelDialogPrivate(this))
{
d->initUi();
d->initConnection();
}

AddModelDialog::~AddModelDialog()
ModelConfigDialog::~ModelConfigDialog()
{
delete d;
}

LLMInfo AddModelDialog::getNewLLmInfo()
LLMInfo ModelConfigDialog::getLLmInfo()
{
if (!d->LLMAppended.modelName.isEmpty())
return d->LLMAppended;
else
return LLMInfo();
}

void ModelConfigDialog::setLLmInfo(const LLMInfo &llmInfo)
{
d->leLLMName->setText(llmInfo.modelName);

int index = d->cbLLMType->findData(QVariant::fromValue(llmInfo.type));
if (index != -1) {
d->cbLLMType->setCurrentIndex(index);
}

d->leApiUrl->setText(llmInfo.modelPath);
d->leApiKey->setText(llmInfo.apikey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

class LLMInfo;
class AddModelDialogPrivate;
class AddModelDialog : public Dtk::Widget::DDialog
class ModelConfigDialog : public Dtk::Widget::DDialog
{
Q_OBJECT
public:
AddModelDialog(QWidget *parent = nullptr);
~AddModelDialog();
LLMInfo getNewLLmInfo();
ModelConfigDialog(QWidget *parent = nullptr);
~ModelConfigDialog();
LLMInfo getLLmInfo();
void setLLmInfo(const LLMInfo &llmInfo);

private:
AddModelDialogPrivate *d;
Expand Down
Loading