diff --git a/README.md b/README.md index 5408bbc..51ba0c9 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,12 @@ Source: https://openai.com/ ![https://github.com/Mariosmsk/QChatGPT/blob/main/example.png](https://github.com/Mariosmsk/QChatGPT/blob/main/example2.png) +## Installation notes + +### macOS + QGIS 3 + +* in a macOS shell window, run + +``` +/Applications/QGIS.app/Contents/MacOS/bin/python3 -m pip install openai +``` diff --git a/qchatqpt.py b/qchatqpt.py index 57b3ccf..d71479d 100644 --- a/qchatqpt.py +++ b/qchatqpt.py @@ -37,6 +37,7 @@ import base64 import sys import requests +from collections import deque from .install_packages.check_dependencies import check @@ -61,6 +62,8 @@ def __init__(self, iface): :type iface: QgsInterface """ # Save reference to the QGIS interface + + self.history = deque(maxlen=6) self.resp = None self.last_ans = None self.dlg = None @@ -264,6 +267,18 @@ def send_message(self): self.dlg.chatgpt_ans.verticalScrollBar().maximum()) finally: if ask: + try: + question_history = " ".join(self.history) + " " + self.question + self.response = openai.Completion.create( + engine=model, + prompt=question_history, + temperature=temperature, + max_tokens=max_tokens-len(question_history), + top_p=1, + frequency_penalty=0.0, + presence_penalty=0.6, + ) + try: if model == "gpt-3.5-turbo": self.response = openai.ChatCompletion.create( @@ -290,9 +305,12 @@ def send_message(self): level=Qgis.Warning, duration=3) self.dlg.send_chat.setEnabled(True) self.dlg.question.setEnabled(True) + return self.last_ans = self.response['choices'][0]['text'] + conversation_pair = self.question + " " + self.last_ans + self.history.append(conversation_pair) last_ans = "AI: " + self.last_ans self.answers.append(last_ans) @@ -347,7 +365,8 @@ def add_on_map(self): def clear_ans_fun(self): self.questions = [] - self.answers = ['Welcome to the QChatGPT.'] + self.history = deque(maxlen=5) + self.answers = ['Welcome to the QChatGPT development version.'] self.dlg.chatgpt_ans.clear() self.dlg.chatgpt_ans.append(self.answers[0]) @@ -368,7 +387,7 @@ def run(self): self.read_tok() self.questions = [] - self.answers = ['Welcome to the QChatGPT.'] + self.answers = ['Welcome to the QChatGPT development version.'] # self.dlg.setWindowFlags(Qt.Dialog | Qt.WindowStaysOnTopHint | Qt.WindowMinMaxButtonsHint | # Qt.WindowCloseButtonHint)