Skip to content

Commit

Permalink
#3064 add: list of AI backends from scripts and test buttons in AI se…
Browse files Browse the repository at this point in the history
…ttings dialog

Signed-off-by: Patrizio Bekerle <[email protected]>
  • Loading branch information
pbek committed Jul 31, 2024
1 parent 284529f commit ed41ce8
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 136 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 24.8.0
- In the *AI settings* there now are buttons to test the connection to the AI services
(for [#3062](https://github.com/pbek/QOwnNotes/issues/3062))
- In the *AI settings* there now is a list of all AI backends from scripts and buttons
to test the connection to those AI services (for [#3064](https://github.com/pbek/QOwnNotes/issues/3064))

## 24.7.3
- `gpt-4o-mini` was added to the list of available OpenAI models
Expand Down
89 changes: 77 additions & 12 deletions src/dialogs/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ SettingsDialog::SettingsDialog(int page, QWidget *parent)

ui->groqApiTestButton->setDisabled(true);
ui->openAiApiTestButton->setDisabled(true);
ui->aiScriptingGroupBox->setHidden(true);
ui->loginFlowCancelButton->hide();
ui->qrCodeWidget->hide();
ui->connectionTestLabel->hide();
Expand Down Expand Up @@ -3322,6 +3323,10 @@ void SettingsDialog::on_settingsStackedWidget_currentChanged(int index) {
if (connectionTestCanBeStarted()) {
on_connectButton_clicked();
}
} else if (index == AiPage) {
if (connectionTestCanBeStarted()) {
buildAiScriptingTreeWidget();
}
}

// turn off the tasks page if no ownCloud settings are available
Expand Down Expand Up @@ -4371,21 +4376,11 @@ void SettingsDialog::on_showStatusBarNotePathCheckBox_toggled(bool checked) {
}

void SettingsDialog::on_groqApiTestButton_clicked() {
OpenAiService *openAiService = OpenAiService::instance();
openAiService->setBackendId(QStringLiteral("groq"));
openAiService->setModelId(QStringLiteral("llama3-8b-8192"));
openAiService->setApiKeyForCurrentBackend(ui->groqApiKeyLineEdit->text());
QString result = openAiService->complete("Test");
QMessageBox::information(this, tr("Groq API test result"), result);
runAiApiTest(QStringLiteral("groq"), QStringLiteral("llama3-8b-8192"), ui->groqApiKeyLineEdit->text());
}

void SettingsDialog::on_openAiApiTestButton_clicked() {
OpenAiService *openAiService = OpenAiService::instance();
openAiService->setBackendId(QStringLiteral("openai"));
openAiService->setModelId(QStringLiteral("gpt-4o"));
openAiService->setApiKeyForCurrentBackend(ui->openAiApiKeyLineEdit->text());
QString result = openAiService->complete("Test");
QMessageBox::information(this, tr("OpenAI API test result"), result);
runAiApiTest(QStringLiteral("openai"), QStringLiteral("gpt-4o"), ui->openAiApiKeyLineEdit->text());
}

void SettingsDialog::on_groqApiKeyLineEdit_textChanged(const QString &arg1) {
Expand All @@ -4395,3 +4390,73 @@ void SettingsDialog::on_groqApiKeyLineEdit_textChanged(const QString &arg1) {
void SettingsDialog::on_openAiApiKeyLineEdit_textChanged(const QString &arg1) {
ui->openAiApiTestButton->setDisabled(arg1.isEmpty());
}

void SettingsDialog::runAiApiTest(QString backend, QString model, QString apiKey) {
OpenAiService *openAiService = OpenAiService::instance();
openAiService->setBackendId(backend);
openAiService->setModelId(model);
if (!apiKey.isEmpty()) {
openAiService->setApiKeyForCurrentBackend(apiKey);
}
QString result = openAiService->complete("Test");
QMessageBox::information(this, tr("API test result for %1 (%2)").arg(backend, model), result);
}

void SettingsDialog::buildAiScriptingTreeWidget() {
OpenAiService *openAiService = OpenAiService::instance();
auto backendNames = openAiService->getBackendNames();

if (backendNames.count() > 2) {
ui->aiScriptingTreeWidget->clear();
ui->aiScriptingGroupBox->setVisible(true);
} else {
ui->aiScriptingGroupBox->setVisible(false);
return;
}

for (const auto &key : backendNames.keys()) {
// Continue on groq and openai
if (key == QStringLiteral("groq") || key == QStringLiteral("openai")) {
continue;
}

const QString &name = backendNames.value(key);

auto backendItem = new QTreeWidgetItem(ui->aiScriptingTreeWidget);
backendItem->setText(0, name);
backendItem->setToolTip(0, tr("AI backend: %1").arg(key));
backendItem->setData(0, Qt::UserRole, key);
backendItem->setFlags(backendItem->flags() & ~Qt::ItemIsSelectable);

auto models = openAiService->getModelsForBackend(key);
for (const auto &model : models) {
auto modelItem = new QTreeWidgetItem(backendItem);
modelItem->setText(0, model);
modelItem->setToolTip(0, tr("AI model: %1").arg(model));
modelItem->setData(0, Qt::UserRole, model);
modelItem->setFlags(modelItem->flags() | Qt::ItemIsSelectable);

// Add test button in new column
auto testButton = new QPushButton();
testButton->setText(tr("Test"));
testButton->setToolTip(tr("Test connection to %1 (%2)").arg(name, model));
testButton->setIcon(QIcon::fromTheme(QStringLiteral("network-connect"),
QIcon(":/icons/breeze-qownnotes/16x16/network-connect.svg")));
testButton->setProperty("backend", key);
testButton->setProperty("model", model);
connect(testButton, &QPushButton::clicked, this, [this, testButton]() {
QString backend = testButton->property("backend").toString();
QString model = testButton->property("model").toString();
runAiApiTest(backend, model);
});

ui->aiScriptingTreeWidget->setItemWidget(modelItem, 1, testButton);
}

}

ui->aiScriptingTreeWidget->setRootIsDecorated(false);
ui->aiScriptingTreeWidget->expandAll();
ui->aiScriptingTreeWidget->resizeColumnToContents(0);
ui->aiScriptingTreeWidget->resizeColumnToContents(1);
}
4 changes: 4 additions & 0 deletions src/dialogs/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ class SettingsDialog : public MasterDialog {
void resetOKLabelData();

void loadNextcloudDeckStackTreeWidget();

void buildAiScriptingTreeWidget();

void runAiApiTest(QString backend, QString model, QString apiKey = QString());
};

#endif // SETTINGSDIALOG_H
46 changes: 46 additions & 0 deletions src/dialogs/settingsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6880,6 +6880,12 @@ Just test yourself if you get sync conflicts and set a higher value if so.</stri
</layout>
</widget>
<widget class="QWidget" name="aiPage">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout_32">
<item>
<widget class="QGroupBox" name="groupBox_40">
Expand Down Expand Up @@ -6980,6 +6986,46 @@ Just test yourself if you get sync conflicts and set a higher value if so.</stri
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="aiScriptingGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>AI backends added via the scripting engine</string>
</property>
<layout class="QGridLayout" name="gridLayout_87">
<item row="0" column="0">
<widget class="QTreeWidget" name="aiScriptingTreeWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<column>
<property name="text">
<string notr="true">Name</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">Test</string>
</property>
</column>
<column>
<property name="text">
<string></string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="aiVerticalSpacer1">
<property name="orientation">
Expand Down
Loading

0 comments on commit ed41ce8

Please sign in to comment.