Skip to content

Commit

Permalink
Added editable example entry in settings page
Browse files Browse the repository at this point in the history
New example format
save files now have examples
  • Loading branch information
Fus3n committed Feb 26, 2024
1 parent ffa1d9a commit e91de70
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 18 deletions.
17 changes: 16 additions & 1 deletion src/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def __init__(self) -> None:
api_key="ollama",
)

self.final_examples = []

def simple_check(self):
if not self.base_url.endswith("/v1"):
if self.base_url[-1] == "/":
Expand All @@ -46,6 +48,17 @@ def reload_settings(self):
self.system_msg = conf["system_msg"]
self.model = conf["model"]

def convert_examples(self):
self.final_examples.clear()
for example in self.examples:
self.final_examples.extend(
[
{"role": "user", "content": f'"{example["from_str"]}"'},
{"role": "assistant", "content": example["result_str"]},
]
)


def generate_result(self, first: str, second: str) -> tuple[str | None, str | None]:
if not first or not second:
return None, "Invalid Input"
Expand All @@ -55,11 +68,13 @@ def generate_result(self, first: str, second: str) -> tuple[str | None, str | No

result = f'"{first} + {second}"'

self.convert_examples()
messages = [
{"role": "system", "content": self.system_msg},
]
messages.extend(self.examples)
messages.extend(self.final_examples)
messages.append({"role": "user", "content": result})
print(messages)

try:
response = self.__client.chat.completions.create(
Expand Down
6 changes: 4 additions & 2 deletions src/configmanager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json, os

from consts import DEFAULT_BASE_URL, DEFAULT_SYSTEM_MSG, DEFAULT_MODEL
from consts import DEFAULT_BASE_URL, DEFAULT_SYSTEM_MSG, DEFAULT_MODEL, DEFAULT_EXAMPLES

class ConfigManger:

Expand All @@ -25,6 +25,8 @@ def init_file(self):
config["system_msg"] = DEFAULT_SYSTEM_MSG
if "model" not in config:
config["model"] = DEFAULT_MODEL
if "examples" not in config:
config["examples"] = DEFAULT_EXAMPLES
self.set_config(config)

def get_config(self):
Expand All @@ -33,7 +35,7 @@ def get_config(self):

def set_config(self, config):
with open(self.file, "w") as f:
json.dump(config, f, indent=4)
json.dump(config, f, indent=4, )

def get_key(self, key):
config = self.get_config()
Expand Down
14 changes: 10 additions & 4 deletions src/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@
"""

class ExampleEntry(TypedDict):
role: str
content: str
from_str: str
result_str: str

ExampleType = List[ExampleEntry]


DEFAULT_BASE_URL = "http://localhost:11434/v1"
DEFAULT_EXAMPLES: ExampleType = [
{"role": "user", "content": '"🌍 Earth + 💧 Water"'},
{"role": "assistant", "content": '🌱 Plant'},
{"from_str": "🌍 Earth + 💧 Water", "result_str": "🌱 Plant"},
{"from_str": "🌱 Oak Saplings + 🦴 Bone Meal", "result_str": "🌳 Oak Tree"},
{"from_str": "🌿 Wheat + 🌾 Wheat", "result_str": "🍞 Bread"},
{"from_str": "🥚 Egg + 🥚 Egg", "result_str": "🐣 Chick"},
{"from_str": "🧊 Ice Block + 🔥 Torch", "result_str": "💧 Water Source"},
{"from_str": "🧱 Brick + 🍶 Water Bottle", "result_str": "🏺 Clay"},
{"from_str": "🏹 Bow + 🎣 Fishing Rod" , "result_str": "🛶 Trident"}
]

MODELS = [
Expand Down
35 changes: 35 additions & 0 deletions src/example_entry_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from PySide6.QtWidgets import (
QFrame,
QHBoxLayout,
QLineEdit,
QPushButton
)

class ExampleEntry(QFrame):

def __init__(self, from_text: str, result_text: str, item) -> None:
super().__init__()
self.item = item
self.from_text = from_text
self.result_text = result_text


lay = QHBoxLayout()

self.from_input = QLineEdit(self.from_text)
self.result_input = QLineEdit(self.result_text)
self.result_input.setMaximumWidth(100)
dlt_btn = QPushButton("X")
dlt_btn.setMinimumWidth(20)
dlt_btn.clicked.connect(self.remove_item)

lay.addWidget(self.from_input, stretch=2)
lay.addWidget(self.result_input, stretch=1)
lay.addWidget(dlt_btn)

self.setLayout(lay)

def remove_item(self):
l = self.item.listWidget()
it = l.row(self.item)
l.takeItem(it)
131 changes: 120 additions & 11 deletions src/settings.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
from PySide6.QtWidgets import QDialog, QWidget, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QLabel, QLineEdit, QCheckBox, QTextEdit, QComboBox
from PySide6.QtWidgets import (
QDialog,
QWidget,
QPushButton,
QHBoxLayout,
QVBoxLayout,
QLabel,
QLineEdit,
QTextEdit,
QComboBox,
QListWidget,
QListWidgetItem
)
from PySide6.QtCore import Qt
from configmanager import ConfigManger
from consts import MODELS, DEFAULT_SYSTEM_MSG, DEFAULT_BASE_URL, DEFAULT_MODEL
from consts import MODELS, DEFAULT_SYSTEM_MSG, DEFAULT_BASE_URL, DEFAULT_MODEL, DEFAULT_EXAMPLES
from example_entry_widget import ExampleEntry

class Settings(QDialog):

def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(parent)
self.conf_manager = ConfigManger()
self.setWindowTitle("Settings")
self.setMinimumSize(600, 500)
self.setMinimumSize(980, 650)

self.heading_layout = QHBoxLayout()
self.heading_layout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -24,6 +37,23 @@ def __init__(self, parent: QWidget | None = None) -> None:
reset_btn.clicked.connect(self.reset_settings)
self.heading_layout.addWidget(reset_btn, stretch=1)

self.section_layout = QHBoxLayout()

self.examples_lay = QVBoxLayout()
self.examples_list = QListWidget()
self.examples_list.setFixedWidth(280)

add_example_btn = QPushButton("Add Example")
add_example_btn.setMinimumHeight(30)
add_example_btn.clicked.connect(self.add_example)

self.examples_lay.addWidget(add_example_btn)
self.examples_lay.addWidget(self.examples_list)

conf = self.conf_manager.get_config()

for example in conf["examples"]:
self.add_example_entry(example["from_str"], example["result_str"])

self.lay = QVBoxLayout()
self.lay.setSpacing(5)
Expand All @@ -32,14 +62,14 @@ def __init__(self, parent: QWidget | None = None) -> None:

self.models_choice = QComboBox()
self.models_choice.addItems(MODELS)
self.models_choice.setCurrentText(self.conf_manager.get_key("model"))
self.models_choice.setCurrentText(conf["model"])
self.lay.addWidget(QLabel("Model"))
self.lay.addWidget(self.models_choice)

self.lay.addWidget(QLabel("Base URL"))
self.base_url_input = QLineEdit()
self.base_url_input.setMinimumHeight(30)
self.base_url_input.setText(self.conf_manager.get_key("base_url"))
self.base_url_input.setText(conf["base_url"])
self.base_url_input.setPlaceholderText("Base URL")
self.lay.addWidget(self.base_url_input)

Expand All @@ -49,7 +79,7 @@ def __init__(self, parent: QWidget | None = None) -> None:
font.setPointSize(14)
self.system_prompt_input.setFont(font)
self.system_prompt_input.setPlaceholderText("System prompt")
self.system_prompt_input.setText(self.conf_manager.get_key("system_msg"))
self.system_prompt_input.setText(conf["system_msg"])
self.lay.addWidget(self.system_prompt_input)


Expand All @@ -65,15 +95,34 @@ def __init__(self, parent: QWidget | None = None) -> None:
cancel_button.clicked.connect(self.reject)
buttons_layout.addWidget(ok_button)
buttons_layout.addWidget(cancel_button)
self.lay.addLayout(buttons_layout)

self.setLayout(self.lay)
self.section_layout.addLayout(self.lay)
self.section_layout.addLayout(self.examples_lay)

self.dialog_lay = QVBoxLayout()

self.dialog_lay.addLayout(self.section_layout)
self.dialog_lay.addLayout(buttons_layout)

self.setLayout(self.dialog_lay)

def save_settings(self):
conf = self.conf_manager.get_config()
conf["model"] = self.models_choice.currentText()
conf["base_url"] = self.base_url_input.text()
conf["system_msg"] = self.system_prompt_input.toPlainText()

conf["examples"] = []
for i in range(self.examples_list.count()):
item = self.examples_list.item(i)
example_entry: ExampleEntry = self.examples_list.itemWidget(item)
conf["examples"].append(
{
"from_str": example_entry.from_input.text(),
"result_str": example_entry.result_input.text()
}
)

self.conf_manager.set_config(conf)
self.accept()

Expand All @@ -82,6 +131,66 @@ def reset_settings(self):
self.base_url_input.setText(DEFAULT_BASE_URL)
self.system_prompt_input.setText(DEFAULT_SYSTEM_MSG)




def add_example_entry(self, from_txt: str, result_txt: str):
item = QListWidgetItem()
item.setFlags(Qt.NoItemFlags)
item_edit = ExampleEntry(from_txt, result_txt, item)
item.setSizeHint(item_edit.sizeHint())
self.examples_list.addItem(item)
self.examples_list.setItemWidget(item, item_edit)

def remove_item(self, item: QListWidgetItem):
self.examples_list.takeItem(self.examples_list.row(item))
self.conf_manager.remove_example(item.text())
item.setHidden(True)
self.examples_list.update()

def add_example(self):
dialog = QDialog(self)
dialog.setWindowTitle("Add Example")
dialog.setFixedWidth(400)

dialog_lay = QVBoxLayout()
dialog_lay.setSpacing(5)
dialog.setLayout(dialog_lay)

from_label = QLabel("From")
from_input = QLineEdit()
from_input.setMinimumHeight(30)
from_input.setPlaceholderText("\ud83c\udf0d Earth + \ud83d\udca7 Water")
from_lay = QHBoxLayout()
from_lay.addWidget(from_label)
from_lay.addWidget(from_input)

result_label = QLabel("Result:")
result_input = QLineEdit()
result_input.setMinimumHeight(30)
result_input.setPlaceholderText("\ud83c\udf31 Plant")
result_lay = QHBoxLayout()
result_lay.addWidget(result_label)
result_lay.addWidget(result_input)

dialog_lay.addLayout(from_lay)
dialog_lay.addLayout(result_lay)


buttons_lay = QHBoxLayout()
buttons_lay.setAlignment(Qt.AlignmentFlag.AlignTop)
ok_button = QPushButton("Save")
ok_button.setMinimumHeight(30)
ok_button.clicked.connect(dialog.accept)
cancel_button = QPushButton("Cancel")
cancel_button.setMinimumHeight(30)
cancel_button.clicked.connect(dialog.reject)
buttons_lay.addWidget(ok_button)
buttons_lay.addWidget(cancel_button)

dialog_lay.addLayout(buttons_lay)
dialog.setFixedHeight(dialog.sizeHint().height())



if dialog.exec() == QDialog.DialogCode.Accepted:
from_txt = from_input.text()
result_txt = result_input.text()
self.add_example_entry(from_txt, result_txt)

0 comments on commit e91de70

Please sign in to comment.