Add Lesson 8 Task 1

This commit is contained in:
Stanislav Mykhailenko 2024-07-30 02:17:43 +03:00
parent 0e6e992e05
commit 1842b50139
GPG key ID: 1E95E66A9C9D6A36
7 changed files with 406 additions and 0 deletions

74
Lesson_8/Task_1/.gitignore vendored Normal file
View file

@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View file

@ -0,0 +1,24 @@
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
llmmathquiz.cpp
HEADERS += \
llmmathquiz.h
FORMS += \
llmmathquiz.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

View file

@ -0,0 +1,95 @@
#include "llmmathquiz.h"
#include "ui_llmmathquiz.h"
LlmMathQuiz::LlmMathQuiz(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::LlmMathQuiz)
{
ui->setupUi(this);
connect(ui->b_submit, &QPushButton::clicked, this, &LlmMathQuiz::submit);
connect(ui->b_generate, &QPushButton::clicked, this, &LlmMathQuiz::generate);
connect (m_manager, &QNetworkAccessManager::finished, this, &LlmMathQuiz::newAnswer);
}
LlmMathQuiz::~LlmMathQuiz()
{
delete ui;
}
void LlmMathQuiz::submit()
{
if (ui->e_user->text().isEmpty())
return;
ui->e_llm->setText(m_llmAnswer);
ui->b_submit->setEnabled(false);
ui->e_user->setReadOnly(true);
ui->l_opinion->setText("Generating…");
sendPrompt("The two following lines will contain two answers. Respond with \"true\" if they are the same, and \"false\" if they are not. Always use lowercase."
+ QString("\n") + ui->e_user->text()
+ QString("\n") + m_llmAnswer);
}
void LlmMathQuiz::generate()
{
if (m_taskReceived) {
ui->l_number->setText(QString::number(ui->l_number->text().toInt() + 1));
m_taskReceived = false;
}
m_taskReceived = false;
ui->b_generate->setEnabled(false);
ui->e_task->setText("Generating…");
ui->e_user->clear();
ui->e_llm->clear();
ui->l_opinion->setText("Not checked");
sendPrompt("Generate a mathematical task and a correct answer for it. Both the task and the answer need to take only one line. Provide the task on the first line, and the answer on the second line.");
}
void LlmMathQuiz::newAnswer(QNetworkReply *reply)
{
if (!m_taskReceived) {
if (reply->error() == QNetworkReply::NoError) {
QByteArray textReply = reply->readAll();
QStringList data = QJsonDocument::fromJson(textReply).object().value("response").toString().split('\n');
ui->e_task->setText(data[0]);
m_llmAnswer = data[1].isEmpty() ? data[2] : data[1];
m_taskReceived = true;
ui->b_submit->setEnabled(true);
ui->e_user->setReadOnly(false);
} else {
ui->e_task->setText("Error!");
ui->b_generate->setEnabled(true);
}
} else {
if (reply->error() == QNetworkReply::NoError) {
QByteArray textReply = reply->readAll();
QString data = QJsonDocument::fromJson(textReply).object().value("response").toString();
if (data == "true") {
ui->l_matched->setText(QString::number(ui->l_matched->text().toInt() + 1));
ui->l_opinion->setText("Match");
} else if (data == "false")
ui->l_opinion->setText("Mismatch");
else
ui->l_opinion->setText("Not match nor mismatch");
ui->b_generate->setEnabled(true);
} else {
ui->e_llm->setText("Error!");
ui->b_submit->setEnabled(true);
}
}
}
void LlmMathQuiz::sendPrompt(QString prompt)
{
QNetworkRequest request(QUrl("http://127.0.0.1:11434/api/generate"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject json;
json["model"] = "llama3.1:8b";
json["prompt"] = prompt;
json["stream"] = false;
m_manager->post(request, QJsonDocument(json).toJson());
}

View file

@ -0,0 +1,36 @@
#ifndef LLMMATHQUIZ_H
#define LLMMATHQUIZ_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>
QT_BEGIN_NAMESPACE
namespace Ui {
class LlmMathQuiz;
}
QT_END_NAMESPACE
class LlmMathQuiz : public QMainWindow
{
Q_OBJECT
public:
LlmMathQuiz(QWidget *parent = nullptr);
~LlmMathQuiz();
private slots:
void submit();
void generate();
void newAnswer(QNetworkReply *);
private:
void sendPrompt(QString);
Ui::LlmMathQuiz *ui;
QNetworkAccessManager *m_manager = new QNetworkAccessManager();
bool m_taskReceived = false;
QString m_llmAnswer;
};
#endif // LLMMATHQUIZ_H

View file

@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LlmMathQuiz</class>
<widget class="QMainWindow" name="LlmMathQuiz">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>LlmMathQuiz</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Task:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_task">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>User answer:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_user">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>LLM answer:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_llm">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>LLM opinion:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="l_opinion">
<property name="text">
<string>Not checked</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="b_submit">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Submit</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_generate">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Task #</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="l_number">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Answers matched:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="l_matched">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

11
Lesson_8/Task_1/main.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "llmmathquiz.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LlmMathQuiz w;
w.show();
return a.exec();
}

View file

@ -35,3 +35,7 @@ Checked means the task is Done.
### Lesson 6 ### Lesson 6
- [ ] Task 1 - [ ] Task 1
### Lesson 8
- [ ] Task 1