1
+ # chat_interface.py
2
+
1
3
class TerminalChatInterface :
2
4
"""A simple terminal interface to interact with an OpenAI Assistant."""
3
5
4
6
def __init__ (self , assistant_manager , assistant_id = None , assistant_name = None , file_ids = None ):
5
7
"""
6
8
Initialize the interface with an instance of OpenAIAssistant.
7
9
If both assistant ID and name are provided, verify they match.
10
+ If file IDs are provided, store them for use in creating threads.
8
11
"""
9
12
self .assistant_manager = assistant_manager
10
13
self .assistant_id = assistant_id
11
14
self .assistant_name = assistant_name
12
- self .file_ids = file_ids
15
+ self .file_ids = file_ids # Store the provided file IDs
13
16
self .thread_id = None
14
17
15
18
if assistant_id and assistant_name :
@@ -32,22 +35,22 @@ def start_chat(self):
32
35
return
33
36
34
37
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 } '." )
36
42
self .start_thread ()
37
43
38
44
def start_thread (self ):
39
45
"""Start a new thread for conversation with the assistant."""
46
+ initial_message = "Hello, how can I assist you today?"
40
47
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 )
44
49
self .thread_id = thread .get ('id' )
45
50
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
49
52
except Exception as e :
50
- print (f"An error occurred: { e } " )
53
+ print (f"An error occurred while creating a thread : { e } " )
51
54
exit ()
52
55
53
56
def chat_loop (self ):
@@ -60,11 +63,15 @@ def chat_loop(self):
60
63
elif user_message .startswith ('/' ):
61
64
self .handle_command (user_message )
62
65
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 } " )
68
75
69
76
def handle_command (self , command ):
70
77
"""Handle special commands in the chat interface."""
0 commit comments