From 99b65b41722d236102be15993e824f6e85f4d16c Mon Sep 17 00:00:00 2001 From: Gilles Morain <67698956+gilles-morain@users.noreply.github.com> Date: Fri, 10 Mar 2023 10:13:33 +0100 Subject: [PATCH 1/2] Adding hint to install openai module on macOS --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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 +``` From 9d5562e5f7c0acf217fcdb20513b89fadbc2d491 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 17 Mar 2023 08:55:03 +0000 Subject: [PATCH 2/2] adds context for last 6 interactions --- qchatqpt.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/qchatqpt.py b/qchatqpt.py index ac2296f..946e0e0 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 @@ -64,6 +65,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 @@ -267,16 +270,18 @@ def send_message(self): self.dlg.chatgpt_ans.verticalScrollBar().maximum()) finally: if ask: - try: + try: + question_history = " ".join(self.history) + " " + self.question self.response = openai.Completion.create( engine=model, - prompt=self.question, + prompt=question_history, temperature=temperature, - max_tokens=max_tokens-len(self.question), + max_tokens=max_tokens-len(question_history), top_p=1, frequency_penalty=0.0, presence_penalty=0.6, ) + except: self.iface.messageBar().pushMessage('QChatGPT', f'openai.error.AuthenticationError: ' @@ -286,9 +291,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) @@ -343,7 +351,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]) @@ -364,7 +373,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)