Skip to content

Commit 7315780

Browse files
committed
fix: null file ids chat interfacte
1 parent d2897ac commit 7315780

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

chat_interface.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
# chat_interface.py
2+
13
class TerminalChatInterface:
24
"""A simple terminal interface to interact with an OpenAI Assistant."""
35

46
def __init__(self, assistant_manager, assistant_id=None, assistant_name=None, file_ids=None):
57
"""
68
Initialize the interface with an instance of OpenAIAssistant.
79
If both assistant ID and name are provided, verify they match.
10+
If file IDs are provided, store them for use in creating threads.
811
"""
912
self.assistant_manager = assistant_manager
1013
self.assistant_id = assistant_id
1114
self.assistant_name = assistant_name
12-
self.file_ids = file_ids
15+
self.file_ids = file_ids # Store the provided file IDs
1316
self.thread_id = None
1417

1518
if assistant_id and assistant_name:
@@ -32,22 +35,22 @@ def start_chat(self):
3235
return
3336

3437
print(f"Welcome to the OpenAI Chatbot Interface.")
35-
print(f"You are now chatting with assistant ID '{self.assistant_id}'.")
38+
if self.assistant_name:
39+
print(f"You are now chatting with assistant '{self.assistant_name}' (ID: '{self.assistant_id}').")
40+
else:
41+
print(f"You are now chatting with assistant ID '{self.assistant_id}'.")
3642
self.start_thread()
3743

3844
def start_thread(self):
3945
"""Start a new thread for conversation with the assistant."""
46+
initial_message = "Hello, how can I assist you today?"
4047
try:
41-
initial_message = "Hello, how can I assist you today?"
42-
thread = self.assistant_manager.create_thread(self.assistant_id, initial_message,
43-
file_ids=self.file_ids) # Updated
48+
thread = self.assistant_manager.create_thread(self.assistant_id, initial_message, file_ids=self.file_ids)
4449
self.thread_id = thread.get('id')
4550
print(f"Bot: {initial_message}")
46-
47-
# Chat loop
48-
self.chat_loop()
51+
self.chat_loop() # Start the chat loop after initializing the thread
4952
except Exception as e:
50-
print(f"An error occurred: {e}")
53+
print(f"An error occurred while creating a thread: {e}")
5154
exit()
5255

5356
def chat_loop(self):
@@ -60,11 +63,15 @@ def chat_loop(self):
6063
elif user_message.startswith('/'):
6164
self.handle_command(user_message)
6265
else:
63-
try:
64-
response = self.assistant_manager.chat_with_assistant(self.thread_id, user_message)
65-
print(f"Bot: {response['choices'][0]['message']['content']}")
66-
except Exception as e:
67-
print(f"An error occurred while sending the message: {e}")
66+
self.send_message(user_message)
67+
68+
def send_message(self, message):
69+
"""Send a message to the assistant and handle the response."""
70+
try:
71+
response = self.assistant_manager.chat_with_assistant(self.thread_id, self.assistant_id, message)
72+
print(f"Bot: {response['choices'][0]['message']['content']}")
73+
except Exception as e:
74+
print(f"An error occurred while sending the message: {e}")
6875

6976
def handle_command(self, command):
7077
"""Handle special commands in the chat interface."""

0 commit comments

Comments
 (0)