Skip to content

Commit

Permalink
Adding rewritten knowledge lookup functionality via CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Feb 11, 2024
1 parent 629b9be commit 86ec591
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 189 deletions.
27 changes: 13 additions & 14 deletions app/src/qt/cli_n_breadcrumbs_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void CliAndBreadcrumbsPresenter::handleCliTextChanged(const QString& text)
"</pre>"
"<br>Examples:"
"<pre>"
"<br>/notebook by tag TODO"
"<br>/ find notebook by tag TODO"
"<br>@arxiv LLM"
"<br>>emojis"
"<br>> emojis"
//"<br>: explain in simple terms SELECTED"
"</pre>"
)
Expand All @@ -91,7 +91,7 @@ void CliAndBreadcrumbsPresenter::handleCliTextChanged(const QString& text)
} else if(command.startsWith(CliAndBreadcrumbsView::CHAR_KNOW)) {
MF_DEBUG(" @ HELP knowledge" << endl);
if(command.size()<=2) {
view->updateCompleterModel(CliAndBreadcrumbsView::HELP_KNOW_CMDS);
view->updateCompleterModel(view->HELP_KNOW_CMDS);
}
return;
} else if(command.startsWith(CliAndBreadcrumbsView::CHAR_CMD)) {
Expand Down Expand Up @@ -183,28 +183,27 @@ void CliAndBreadcrumbsPresenter::executeCommand()
// mainPresenter->getStatusBar()->showInfo(tr("Notebook ")+QString::fromStdString(outlines->front()->getName()));
// mainPresenter->getStatusBar()->showInfo(tr("Notebook not found: ") += QString(name.c_str()));
return;
} else
} else
// knowledge lookup in the CLI:
// - @wikipedia ... opens tool dialog with Wikipedi SELECTED in the dropdown
// - @wikipedia llm ... opens directly https://wikipedia.org
if(command.startsWith(CliAndBreadcrumbsView::CHAR_KNOW)) {
for(auto c:CliAndBreadcrumbsView::HELP_KNOW_CMDS) {
QString toolName = command.mid(1, c.size()-1);
for(auto c:view->HELP_KNOW_CMDS) {
QString toolId = command.mid(1, c.size()-1);
if(command.startsWith(c)) {
QString phrase = command.mid(1, c.size());
QString phrase = command.mid(1 + c.size());
MF_DEBUG(
" executing: knowledge recherche of '"
<< phrase.toStdString()
" executing: knowledge recherche of phrase '"
<< phrase.toStdString()
<< "' using command '"
<< c.toStdString() << "' and tool '"
<< toolName.toStdString() << "'" << endl);
if(phrase.size() > 1 && phrase.startsWith(" ")) {
phrase = phrase.mid(1);
mainPresenter->doActionOpenRunToolDialog(phrase, toolName, false);
<< toolId.toStdString() << "'" << endl);
if(phrase.size() > 1) {
mainPresenter->doActionOpenRunToolDialog(phrase, toolId, false);
mainPresenter->handleRunTool();
} else {
// search phrase is empty
mainPresenter->doActionOpenRunToolDialog(phrase, toolName);
mainPresenter->doActionOpenRunToolDialog(phrase, toolId);
}
return;
}
Expand Down
25 changes: 5 additions & 20 deletions app/src/qt/cli_n_breadcrumbs_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,6 @@ const QStringList CliAndBreadcrumbsView::HELP_FIND_CMDS = QStringList()
const QString CliAndBreadcrumbsView::CHAR_KNOW
= "@";

// knowledge recherche uses current context / selected entity to lookup the knowledge
const QString CliAndBreadcrumbsView::CMD_KNOW_WIKIPEDIA
= "@wikipedia";
const QString CliAndBreadcrumbsView::CMD_KNOW_ARXIV
= "@arxiv";
const QString CliAndBreadcrumbsView::CMD_KNOW_STACK_OVERFLOW
= "@stackoverflow";
const QString CliAndBreadcrumbsView::CMD_KNOW_DUCK
= "@duckduckgo";
const QString CliAndBreadcrumbsView::CMD_KNOW_GITHUB
= "@github";

const QStringList CliAndBreadcrumbsView::HELP_KNOW_CMDS = QStringList()
<< CMD_KNOW_WIKIPEDIA
<< CMD_KNOW_ARXIV
<< CMD_KNOW_STACK_OVERFLOW
<< CMD_KNOW_DUCK
<< CMD_KNOW_GITHUB
;

const QString CliAndBreadcrumbsView::CHAR_CMD
= ">";

Expand Down Expand Up @@ -220,6 +200,11 @@ CliAndBreadcrumbsView::CliAndBreadcrumbsView(QWidget* parent, bool zenMode)
: QWidget(parent),
zenMode{zenMode}
{
HELP_KNOW_CMDS = QStringList();
for(const auto& toolName:KnowledgeTool::getToolIds()) {
HELP_KNOW_CMDS << (CHAR_KNOW + QString::fromStdString(toolName));
}

setFixedHeight(this->fontMetrics().height()*1.5);

QHBoxLayout* layout = new QHBoxLayout(this);
Expand Down
10 changes: 2 additions & 8 deletions app/src/qt/cli_n_breadcrumbs_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "look_n_feel.h"
#include "cli_n_breadcrumbs_presenter.h"
#include "../../lib/src/config/configuration.h"

namespace m8r {

Expand Down Expand Up @@ -103,14 +104,7 @@ class CliAndBreadcrumbsView : public QWidget

static const QString CHAR_KNOW;

static const QString CMD_KNOW_WIKIPEDIA;
static const QString CMD_KNOW_ARXIV;
static const QString CMD_KNOW_STACK_OVERFLOW;
static const QString CMD_KNOW_DUCK;
static const QString CMD_KNOW_GITHUB;
static const QString CMD_KNOW_BARD;

static const QStringList HELP_KNOW_CMDS;
QStringList HELP_KNOW_CMDS;

// command

Expand Down
121 changes: 51 additions & 70 deletions app/src/qt/dialogs/run_tool_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,18 @@ using namespace std;
RunToolDialog::RunToolDialog(QWidget* parent)
: QDialog(parent)
{
vector<QString> toolNames = {
QString{TOOL_ARXIV},
QString{TOOL_CHAT_GPT_WEB},
QString{TOOL_DEEPL},
QString{TOOL_DUCKDUCKGO},
QString{TOOL_GOOGLE_BARD},
QString{TOOL_GOOGLE_SEARCH},
QString{TOOL_GH_REPOS},
QString{TOOL_GH_TOPICS},
QString{TOOL_WIKIPEDIA}
};


// UI

phraseLabel = new QLabel{tr("Phrase:"), parent};
phraseEdit = new QLineEdit{parent};

toolLabel = new QLabel{tr("Knowledge source:"), parent};
toolCombo = new QComboBox{this};
for(QString toolName:toolNames) {
toolCombo->addItem(toolName);
for(string toolId: KnowledgeTool::getToolIds()) {
toolCombo->addItem(
getToolNameForToolId(toolId),
QString::fromStdString(toolId));
}

// TODO change of the tool changes the template

templateLabel = new QLabel{tr("Template:"), parent};
templateEdit = new QLineEdit{parent};

Expand Down Expand Up @@ -98,68 +84,63 @@ RunToolDialog::~RunToolDialog()
{
}

void RunToolDialog::show()
QString RunToolDialog::getToolNameForToolId(std::string toolId) const
{
QDialog::show();
if(KnowledgeTool::ARXIV == toolId) {
return tr("arXiv");
} else if(KnowledgeTool::WIKIPEDIA == toolId) {
return tr("Wikipedia");
} else if(KnowledgeTool::GH_REPOS == toolId) {
return tr("GitHub Repositories");
} else if(KnowledgeTool::GH_CODE == toolId) {
return tr("GitHub Code");
} else if(KnowledgeTool::DUCKDUCKGO == toolId) {
return tr("DuckDuckGo");
} else if(KnowledgeTool::GOOGLE == toolId) {
return tr("Google");
} else if(KnowledgeTool::STACK_OVERFLOW == toolId) {
return tr("StackOverflow");
} else if(KnowledgeTool::CPP == toolId) {
return tr("CPP reference");
} else if(KnowledgeTool::PYTHON == toolId) {
return tr("Python documentation");
}

// no suitable name available
return QString::fromStdString(toolId);
}

QString RunToolDialog::getTemplateTextForToolName(string selectedTool) const
{
if(selectedTool == TOOL_ARXIV) {
QString templateText{"https://arxiv.org/search/?query="};
templateText.append(TOOL_PHRASE);
return templateText;
} else if(selectedTool == TOOL_DEEPL) {
return QString{"https://www.deepl.com/en/translator"};
} else if(selectedTool == TOOL_STACK_OVERFLOW) {
QString templateText{"https://stackoverflow.com/search?q="};
templateText.append(TOOL_PHRASE);
return templateText;
} else if(selectedTool == TOOL_DUCKDUCKGO) {
QString templateText{"https://www.duckduckgo.com/?q="};
templateText.append(TOOL_PHRASE);
return templateText;
} else if(selectedTool == TOOL_GH_TOPICS) {
// TODO fix search URL
QString templateText{"https://www.github.com/search?q="};
templateText.append(TOOL_PHRASE);
return templateText;
} else if(selectedTool == TOOL_GH_REPOS) {
// TODO fix search URL
QString templateText{"https://www.github.com/search?q="};
templateText.append(TOOL_PHRASE);
return templateText;
} else if(selectedTool == TOOL_CHAT_GPT_WEB) {
return QString{"https://chat.openai.com/"};
} else if(selectedTool == TOOL_GOOGLE_BARD) {
return QString{"https://bard.google.com/chat"};
} else if(selectedTool == TOOL_GOOGLE_SEARCH) {
QString temlateText{"https://www.google.com/search?q="};
temlateText.append(TOOL_PHRASE);
return temlateText;
} else if(selectedTool == TOOL_WIKIPEDIA) {
// TODO: URL
QString temlateText{"https://en.wikipedia.org/w/index.php?search="};
temlateText.append(TOOL_PHRASE);
return temlateText;
bool RunToolDialog::selectToolById(std::string toolId) {
MF_DEBUG(
"RunToolDialog::selectToolById(" << toolId << ") in variantData: " << std::endl);

QVariant desiredValue = QVariant::fromValue<QString>(
QString::fromStdString(toolId));
int index = this->toolCombo->findData(desiredValue, Qt::UserRole);
if(index != -1) {
this->toolCombo->setCurrentIndex(index);
return true;
}

string msg{
"Tool '" + selectedTool + "' to search/explain/process "
"the phrase is not supported."};
QMessageBox msgBox{
QMessageBox::Critical,
QObject::tr("Unsupported Knowledge Tool"),
QObject::tr(msg.c_str()),
};
MF_DEBUG(
"ERROR: RunToolDialog::setSelectedToolId() - toolId '"
<< toolId << "' not found!" << std::endl);
return false;
}

return QString{};
void RunToolDialog::show()
{
QDialog::show();
}

void RunToolDialog::handleChangeToolCombo(const QString& text) {
void RunToolDialog::handleChangeToolCombo(const QString& text)
{
MF_DEBUG("Tool changed: " << text.toStdString() << endl);

this->templateEdit->setText(getTemplateTextForToolName(text.toStdString()));
this->templateEdit->setText(
QString::fromStdString(
KnowledgeTool::getUrlTemplateForToolId(
text.toStdString())));
}

} // m8r namespace
16 changes: 9 additions & 7 deletions app/src/qt/dialogs/run_tool_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "../../lib/src/config/configuration.h"

#include "../model_meta_definitions.h"

namespace m8r {

class RunToolDialog : public QDialog
Expand Down Expand Up @@ -51,22 +53,22 @@ class RunToolDialog : public QDialog
RunToolDialog& operator =(const RunToolDialog&&) = delete;
~RunToolDialog();

void show();
QString getToolNameForToolId(std::string toolId) const;

QString getTemplateTextForToolName(std::string selectedTool) const;
void show();

QPushButton* getRunButton() const { return runButton; }
QString getSelectedTool() const {
return this->toolCombo->itemText(
this->toolCombo->currentIndex()
);
}
void setSelectedTool(QString toolName) {
int index = this->toolCombo->findText(toolName);
if(index != -1) {
this->toolCombo->setCurrentIndex(index);
}
std::string getSelectedToolId() const {
int currentIndex = this->toolCombo->currentIndex();
QVariant variantData = this->toolCombo->itemData(currentIndex, Qt::UserRole);
return variantData.toString().toStdString();
}
bool selectToolById(std::string toolId);
QString getTemplateText() const {
return this->templateEdit->text();
}
Expand Down
Loading

0 comments on commit 86ec591

Please sign in to comment.