@@ -18,12 +18,9 @@ def get_assistant_by_name(self, name):
18
18
raise OpenAIAssistantError (f"An error occurred while retrieving assistants: { e } " )
19
19
20
20
def get_assistant (self , assistant_id ):
21
- # Retrieve the assistant details by ID from the OpenAI API
22
21
try :
23
- assistant = self .client .beta .assistants .retrieve (assistant_id )
24
- return assistant
22
+ return self .client .beta .assistants .retrieve (assistant_id )
25
23
except Exception as e :
26
- print (f"An error occurred while retrieving the assistant: { e } " )
27
24
raise OpenAIAssistantError (f"An error occurred while retrieving the assistant: { e } " )
28
25
29
26
def create_assistant (self , name , model , description = None , tools = None , file_ids = None ):
@@ -56,18 +53,21 @@ def list_assistants(self):
56
53
except Exception as e :
57
54
raise OpenAIAssistantError (f"An error occurred while listing assistants: { e } " )
58
55
59
- def create_thread (self , assistant_id , initial_message , file_ids = None ):
56
+ # Adjusted the method signature to match expected OpenAI API behavior
57
+ def create_thread (self , assistant_id , initial_message , file_id = None ):
60
58
try :
61
- return self .client .beta .threads .create (
59
+ # Assuming that creating a thread is supported by the OpenAI API
60
+ thread = self .client .beta .threads .create (
62
61
assistant_id = assistant_id ,
63
62
messages = [
64
63
{
65
64
"role" : "user" ,
66
65
"content" : initial_message ,
67
- "file_ids" : file_ids or []
66
+ "file_ids" : [ file_id ] if file_id else []
68
67
}
69
68
]
70
69
)
70
+ return thread
71
71
except Exception as e :
72
72
raise OpenAIAssistantError (f"An error occurred while creating a thread: { e } " )
73
73
@@ -83,18 +83,16 @@ def list_threads(self, assistant_id):
83
83
except Exception as e :
84
84
raise OpenAIAssistantError (f"An error occurred while listing threads: { e } " )
85
85
86
- def chat_with_assistant (self , thread_id , message , file_ids = None ):
86
+ # Adjusted the method signature to match expected OpenAI API behavior
87
+ def chat_with_assistant (self , thread_id , assistant_id , message ):
87
88
try :
88
- return self .client .beta .threads .messages .create (
89
+ # Assuming that chatting with an assistant is supported by the OpenAI API
90
+ response = self .client .beta .threads .runs .create (
89
91
thread_id = thread_id ,
90
- messages = [
91
- {
92
- "role" : "user" ,
93
- "content" : message ,
94
- "file_ids" : file_ids or []
95
- }
96
- ]
92
+ assistant_id = assistant_id ,
93
+ messages = [{"role" : "user" , "content" : message }]
97
94
)
95
+ return response
98
96
except Exception as e :
99
97
raise OpenAIAssistantError (f"An error occurred while chatting with the assistant: { e } " )
100
98
@@ -110,8 +108,3 @@ def handle_command(self, command, thread_id=None, assistant_id=None):
110
108
return "Invalid command."
111
109
except OpenAIAssistantError as e :
112
110
return str (e )
113
-
114
- # Example usage to test:
115
- # client = openai.Client(api_key='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')
116
- # assistant = OpenAIAssistant(client)
117
- # assistant.create_or_get_assistant('MyAssistant', 'gpt-4-1106-preview')
0 commit comments