Skip to content

Commit

Permalink
Merge pull request #261 from VesnaT/chat_fix
Browse files Browse the repository at this point in the history
ChatGPT Summarize: Updates
  • Loading branch information
lanzagar authored Aug 25, 2023
2 parents 5ef0d45 + 9703bde commit 5486901
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions orangecontrib/prototypes/widgets/owchatgpt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from AnyQt.QtCore import Signal
from AnyQt.QtCore import Signal, Qt
from AnyQt.QtGui import QFocusEvent
from AnyQt.QtWidgets import QPlainTextEdit, QLineEdit, QTextEdit

Expand All @@ -9,6 +9,7 @@

from Orange.data import Table, StringVariable
from Orange.widgets import gui
from Orange.widgets.credentials import CredentialManager
from Orange.widgets.utils.itemmodels import DomainModel
from Orange.widgets.settings import Setting, DomainContextHandler, \
ContextSetting
Expand All @@ -31,7 +32,10 @@ def run_gpt(
content = f"{prompt_start}\n{text}.\n{prompt_end}"
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": content}]
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": content},
]
)
return response.choices[0].message.content

Expand All @@ -45,14 +49,14 @@ def focusOutEvent(self, ev: QFocusEvent):


class OWChatGPT(OWWidget):
name = "Chat GPT"
description = "Chat GPT."
name = "ChatGPT Summarize"
description = "Summarize content using a ChatGPT."
icon = "icons/chatgpt.svg"
priority = 10
keywords = ["text", "gpt"]

settingsHandler = DomainContextHandler()
access_key = Setting("") # TODO
access_key = ""
model_index = Setting(0)
text_var = ContextSetting(None)
prompt_start = Setting("")
Expand All @@ -76,19 +80,25 @@ def __init__(self):
self.__start_text_edit: QTextEdit = None
self.__end_text_edit: QTextEdit = None
self.__answer_text_edit: QPlainTextEdit = None

self.__cm = CredentialManager("Ask")
self.access_key = self.__cm.access_key or ""

self.setup_gui()

def setup_gui(self):
box = gui.vBox(self.controlArea, "Chat GPT")
edit: QLineEdit = gui.lineEdit(box, self, "access_key", "Access key:",
callback=self.commit.deferred)
box = gui.vBox(self.controlArea, "Model")
edit: QLineEdit = gui.lineEdit(box, self, "access_key", "API Key:",
orientation=Qt.Horizontal,
callback=self.__on_access_key_changed)
edit.setEchoMode(QLineEdit.Password)
gui.comboBox(box, self, "model_index", label="Model:",
orientation=Qt.Horizontal,
items=MODELS, callback=self.commit.deferred)

gui.comboBox(self.controlArea, self, "text_var", "Options",
"Text field:", model=self.__text_var_model,
callback=self.commit.deferred)
gui.comboBox(self.controlArea, self, "text_var", "Data",
"Text variable:", model=self.__text_var_model,
orientation=Qt.Horizontal, callback=self.commit.deferred)

box = gui.vBox(self.controlArea, "Prompt")
gui.label(box, self, "Start:")
Expand All @@ -112,6 +122,10 @@ def setup_gui(self):
self.__answer_text_edit = QPlainTextEdit(readOnly=True)
box.layout().addWidget(self.__answer_text_edit)

def __on_access_key_changed(self):
self.__cm.access_key = self.access_key
self.commit.deferred()

def __on_start_text_edit_changed(self):
prompt_start = self.__start_text_edit.toPlainText()
if self.prompt_start != prompt_start:
Expand Down

0 comments on commit 5486901

Please sign in to comment.