Skip to content

Commit

Permalink
Merge pull request #11 from Ojaybee/chat-memory
Browse files Browse the repository at this point in the history
 Adding context to conversations #10  adds context for last 6 interactions
  • Loading branch information
Mariosmsk committed Apr 1, 2023
2 parents 61e6669 + 99451c4 commit 8315c5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
23 changes: 21 additions & 2 deletions qchatqpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import base64
import sys
import requests
from collections import deque

from .install_packages.check_dependencies import check

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand Down Expand Up @@ -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])

Expand All @@ -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)
Expand Down

0 comments on commit 8315c5b

Please sign in to comment.