Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat: Restoring old chats in chat module #1344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions MAVProxy/modules/mavproxy_chat/chat_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def check_connection(self):
print("chat: failed to connect to OpenAI assistant")
return False

# To initialize existing thread id
assistant_thread_id_existing = None

# Check for existing thread
if assistant_thread_id_existing is not None :
self.assistant_thread = self.client.beta.threads.retrieve(assistant_thread_id_existing)
# create new thread
if self.assistant_thread is None:
self.assistant_thread = self.client.beta.threads.create()
Expand All @@ -99,6 +105,32 @@ def set_api_key(self, api_key_str):
self.assistant = None
self.assistant_thread = None

# get the old thread and messages
def get_old_msg(self):
with self.send_lock:

# check connection
if not self.check_connection():
return "chat: failed to connect to OpenAI"

# retrieve messages on the thread
reply = ""
reply_messages = self.client.beta.threads.messages.list(self.assistant_thread.id,
order="asc",
limit=20)

if reply_messages is None:
return "chat: failed to retrieve messages"

need_newline = False
for message in reply_messages.data:
reply = reply + message.content[0].text.value
if need_newline:
reply = reply + "\n"
need_newline = True

return reply

# send text to assistant
def send_to_assistant(self, text):
# get lock
Expand Down
7 changes: 7 additions & 0 deletions MAVProxy/modules/mavproxy_chat/chat_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ def apikey_set_button_click(self, event):
self.chat_voice_to_text.set_api_key(self.apikey_text_input.GetValue())
self.apikey_frame.Hide()

# initially place all old messages
orig_text_attr = self.text_reply.GetDefaultStyle()
old_msgs = self.chat_openai.get_old_msg()
if old_msgs :
wx.CallAfter(self.text_reply.SetDefaultStyle, orig_text_attr)
wx.CallAfter(self.text_reply.AppendText, old_msgs + "\n\n")

# API key close button clicked
def apikey_close_button_click(self, event):
self.apikey_frame.Hide()
Expand Down
Loading