Skip to content

Commit

Permalink
Move custom promt to settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
Palm1r committed Sep 7, 2024
1 parent 384e07b commit 314ba06
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 106 deletions.
151 changes: 53 additions & 98 deletions QodeAssistSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,14 @@ QodeAssistSettings::QodeAssistSettings()
{
setAutoApply(false);

resetToDefaults.m_buttonText = Tr::tr("Reset to Defaults");

customJsonTemplate.setSettingsKey(Constants::CUSTOM_JSON_TEMPLATE);
customJsonTemplate.setLabelText("Custom JSON Template:");
customJsonTemplate.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
customJsonTemplate.setDefaultValue(R"({
"prompt": "{{QODE_INSTRUCTIONS}}<fim_prefix>{{QODE_PREFIX}}<fim_suffix>{{QODE_SUFFIX}}<fim_middle>",
"options": {
"temperature": 0.7,
"top_p": 0.95,
"top_k": 40,
"num_predict": 100,
"stop": [
"<|endoftext|>",
"<file_sep>",
"<fim_prefix>",
"<fim_suffix>",
"<fim_middle>"
],
"frequency_penalty": 0,
"presence_penalty": 0
},
"stream": true
})");

saveCustomTemplateButton.m_buttonText = (Tr::tr("Save Custom Template to JSON"));
loadCustomTemplateButton.m_buttonText = (Tr::tr("Load Custom Template from JSON"));

readSettings();

customJsonTemplate.setVisible(PromptTemplateManager::instance().getCurrentTemplate()->name()
== "Custom Template");

setLayouter([this]() {
using namespace Layouting;

return Column{Group{title(Tr::tr("FIM Prompt Settings")),
Form{Column{Column{customJsonTemplate,
Row{saveCustomTemplateButton,
loadCustomTemplateButton,
Stretch{1}}},
Form{Column{Column{
},
// readFullFile,
// maxFileThreshold,
// readStringsBeforeCursor,
Expand All @@ -110,19 +77,7 @@ QodeAssistSettings::QodeAssistSettings()

void QodeAssistSettings::setupConnections()
{
connect(&resetToDefaults,
&ButtonAspect::clicked,
this,
&QodeAssistSettings::resetSettingsToDefaults);

connect(&saveCustomTemplateButton,
&ButtonAspect::clicked,
this,
&QodeAssistSettings::saveCustomTemplate);
connect(&loadCustomTemplateButton,
&ButtonAspect::clicked,
this,
&QodeAssistSettings::loadCustomTemplate);
}

QStringList QodeAssistSettings::getInstalledModels()
Expand Down Expand Up @@ -173,7 +128,7 @@ void QodeAssistSettings::resetSettingsToDefaults()
// resetAspect(multiLineCompletion);
// resetAspect(useFilePathInContext);
// resetAspect(useSpecificInstructions);
resetAspect(customJsonTemplate);
// resetAspect(customJsonTemplate);

// fimPrompts.setStringValue("StarCoder2");
// llmProviders.setStringValue("Ollama");
Expand All @@ -188,60 +143,60 @@ void QodeAssistSettings::resetSettingsToDefaults()

void QodeAssistSettings::saveCustomTemplate()
{
QString fileName = QFileDialog::getSaveFileName(nullptr,
Tr::tr("Save JSON Template"),
QString(),
Tr::tr("JSON Files (*.json)"));
if (fileName.isEmpty())
return;

QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << customJsonTemplate.value();
file.close();
QMessageBox::information(nullptr,
Tr::tr("Save Successful"),
Tr::tr("JSON template has been saved successfully."));
} else {
QMessageBox::critical(nullptr,
Tr::tr("Save Failed"),
Tr::tr("Failed to save JSON template."));
}
// QString fileName = QFileDialog::getSaveFileName(nullptr,
// Tr::tr("Save JSON Template"),
// QString(),
// Tr::tr("JSON Files (*.json)"));
// if (fileName.isEmpty())
// return;

// QFile file(fileName);
// if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
// QTextStream out(&file);
// out << customJsonTemplate.value();
// file.close();
// QMessageBox::information(nullptr,
// Tr::tr("Save Successful"),
// Tr::tr("JSON template has been saved successfully."));
// } else {
// QMessageBox::critical(nullptr,
// Tr::tr("Save Failed"),
// Tr::tr("Failed to save JSON template."));
// }
}

void QodeAssistSettings::loadCustomTemplate()
{
QString fileName = QFileDialog::getOpenFileName(nullptr,
Tr::tr("Load JSON Template"),
QString(),
Tr::tr("JSON Files (*.json)"));
if (fileName.isEmpty())
return;

QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString jsonContent = in.readAll();
file.close();

QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonContent.toUtf8(), &parseError);
if (parseError.error == QJsonParseError::NoError) {
customJsonTemplate.setValue(jsonContent);
QMessageBox::information(nullptr,
Tr::tr("Load Successful"),
Tr::tr("JSON template has been loaded successfully."));
} else {
QMessageBox::critical(nullptr,
Tr::tr("Invalid JSON"),
Tr::tr("The selected file contains invalid JSON."));
}
} else {
QMessageBox::critical(nullptr,
Tr::tr("Load Failed"),
Tr::tr("Failed to load JSON template."));
}
// QString fileName = QFileDialog::getOpenFileName(nullptr,
// Tr::tr("Load JSON Template"),
// QString(),
// Tr::tr("JSON Files (*.json)"));
// if (fileName.isEmpty())
// return;

// QFile file(fileName);
// if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
// QTextStream in(&file);
// QString jsonContent = in.readAll();
// file.close();

// QJsonParseError parseError;
// QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonContent.toUtf8(), &parseError);
// if (parseError.error == QJsonParseError::NoError) {
// customJsonTemplate.setValue(jsonContent);
// QMessageBox::information(nullptr,
// Tr::tr("Load Successful"),
// Tr::tr("JSON template has been loaded successfully."));
// } else {
// QMessageBox::critical(nullptr,
// Tr::tr("Invalid JSON"),
// Tr::tr("The selected file contains invalid JSON."));
// }
// } else {
// QMessageBox::critical(nullptr,
// Tr::tr("Load Failed"),
// Tr::tr("Failed to load JSON template."));
// }
}

class QodeAssistSettingsPage : public Core::IOptionsPage
Expand Down
6 changes: 0 additions & 6 deletions QodeAssistSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ class QodeAssistSettings : public Utils::AspectContainer
public:
QodeAssistSettings();

Utils::StringAspect customJsonTemplate{this};
ButtonAspect saveCustomTemplateButton{this};
ButtonAspect loadCustomTemplateButton{this};

ButtonAspect resetToDefaults{this};

private:
void setupConnections();
QStringList getInstalledModels();
Expand Down
132 changes: 131 additions & 1 deletion settings/CustomPromptSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

#include "CustomPromptSettings.hpp"

#include <QFileDialog>
#include <QJsonParseError>
#include <QMessageBox>
#include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/icore.h>
#include <utils/layoutbuilder.h>

#include "QodeAssistConstants.hpp"
Expand All @@ -39,12 +43,138 @@ CustomPromptSettings::CustomPromptSettings()

setDisplayName(Tr::tr("Custom Prompt"));

customJsonLabel.setLabelText("Custom JSON Template:");
customJsonLabel.setDisplayStyle(Utils::StringAspect::LabelDisplay);

customJsonTemplate.setSettingsKey(Constants::CUSTOM_JSON_TEMPLATE);
customJsonTemplate.setDisplayStyle(Utils::StringAspect::TextEditDisplay);
customJsonTemplate.setDefaultValue(R"({
"prompt": "{{QODE_INSTRUCTIONS}}<fim_prefix>{{QODE_PREFIX}}<fim_suffix>{{QODE_SUFFIX}}<fim_middle>",
"options": {
"temperature": 0.7,
"top_p": 0.95,
"top_k": 40,
"num_predict": 100,
"stop": [
"<|endoftext|>",
"<file_sep>",
"<fim_prefix>",
"<fim_suffix>",
"<fim_middle>"
],
"frequency_penalty": 0,
"presence_penalty": 0
},
"stream": true
})");
saveCustomTemplateButton.m_buttonText = (Tr::tr("Save Custom Template to JSON"));
loadCustomTemplateButton.m_buttonText = (Tr::tr("Load Custom Template from JSON"));
resetToDefaults.m_buttonText = Tr::tr("Reset Page to Defaults");

readSettings();

setupConnection();

setLayouter([this]() {
using namespace Layouting;
return Column{Stretch{1}};
return Column{Row{customJsonLabel, Stretch{1}, resetToDefaults},
Row{customJsonTemplate,
Column{saveCustomTemplateButton, loadCustomTemplateButton, Stretch{1}}}};
});
}

void CustomPromptSettings::setupConnection()
{
connect(&resetToDefaults,
&ButtonAspect::clicked,
this,
&CustomPromptSettings::resetSettingsToDefaults);
connect(&saveCustomTemplateButton,
&ButtonAspect::clicked,
this,
&CustomPromptSettings::saveCustomTemplate);
connect(&loadCustomTemplateButton,
&ButtonAspect::clicked,
this,
&CustomPromptSettings::loadCustomTemplate);
}

void CustomPromptSettings::resetSettingsToDefaults()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(
Core::ICore::dialogParent(),
Tr::tr("Reset Settings"),
Tr::tr("Are you sure you want to reset all settings to default values?"),
QMessageBox::Yes | QMessageBox::No);

if (reply == QMessageBox::Yes) {
resetAspect(customJsonTemplate);
}

QMessageBox::information(Core::ICore::dialogParent(),
Tr::tr("Settings Reset"),
Tr::tr("All settings have been reset to their default values."));
}

void CustomPromptSettings::saveCustomTemplate()
{
QString fileName = QFileDialog::getSaveFileName(nullptr,
Tr::tr("Save JSON Template"),
QString(),
Tr::tr("JSON Files (*.json)"));
if (fileName.isEmpty())
return;

QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << customJsonTemplate.value();
file.close();
QMessageBox::information(nullptr,
Tr::tr("Save Successful"),
Tr::tr("JSON template has been saved successfully."));
} else {
QMessageBox::critical(nullptr,
Tr::tr("Save Failed"),
Tr::tr("Failed to save JSON template."));
}
}

void CustomPromptSettings::loadCustomTemplate()
{
QString fileName = QFileDialog::getOpenFileName(nullptr,
Tr::tr("Load JSON Template"),
QString(),
Tr::tr("JSON Files (*.json)"));
if (fileName.isEmpty())
return;

QFile file(fileName);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString jsonContent = in.readAll();
file.close();

QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonContent.toUtf8(), &parseError);
if (parseError.error == QJsonParseError::NoError) {
customJsonTemplate.setValue(jsonContent);
QMessageBox::information(nullptr,
Tr::tr("Load Successful"),
Tr::tr("JSON template has been loaded successfully."));
} else {
QMessageBox::critical(nullptr,
Tr::tr("Invalid JSON"),
Tr::tr("The selected file contains invalid JSON."));
}
} else {
QMessageBox::critical(nullptr,
Tr::tr("Load Failed"),
Tr::tr("Failed to load JSON template."));
}
}

class CustomPromptSettingsPage : public Core::IOptionsPage
{
public:
Expand Down
13 changes: 13 additions & 0 deletions settings/CustomPromptSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once

#include "settings/SettingsUtils.hpp"
#include <utils/aspects.h>

namespace QodeAssist::Settings {
Expand All @@ -27,6 +28,18 @@ class CustomPromptSettings : public Utils::AspectContainer
{
public:
CustomPromptSettings();

Utils::StringAspect customJsonLabel{this};
Utils::StringAspect customJsonTemplate{this};
ButtonAspect saveCustomTemplateButton{this};
ButtonAspect loadCustomTemplateButton{this};
ButtonAspect resetToDefaults{this};

private:
void setupConnection();
void resetSettingsToDefaults();
void saveCustomTemplate();
void loadCustomTemplate();
};

CustomPromptSettings &customPromptSettings();
Expand Down
Loading

0 comments on commit 314ba06

Please sign in to comment.