88from typing import Dict , List , Union
99from openai import OpenAI
1010from dotenv import load_dotenv
11+ from sanic .request .form import File
12+ import socketio
1113
1214load_dotenv ()
1315
@@ -221,7 +223,7 @@ def build_feature_functions(
221223 return experiments_function_call
222224
223225
224- def upload_file_to_vector_store (client : OpenAI , file_path : str ) -> str :
226+ def upload_file_to_vector_store (client : OpenAI , file : File , sid : str ) -> str :
225227 """
226228 Uploads a file to the vector store.
227229
@@ -232,6 +234,12 @@ def upload_file_to_vector_store(client: OpenAI, file_path: str) -> str:
232234 - URL to the uploaded file.
233235 """
234236
237+ file_path = f"paper/{ sid } { file .name } "
238+
239+ with open (file_path , "wb" ) as f :
240+ f .write (file .body )
241+ f .close ()
242+
235243 file_info = client .files .create (file = open (file_path , "rb" ), purpose = "assistants" )
236244
237245 now = datetime .now ()
@@ -311,44 +319,44 @@ def create_temporary_assistant(client: OpenAI):
311319 return my_temporary_assistant
312320
313321
314- def call_asssistant_api (file_path : str , sid : str , sio ):
322+ async def call_asssistant_api (file : File , sid : str , sio : socketio . AsyncServer ):
315323
316324 client = OpenAI ()
317325
318326 try :
319- sio .emit (
327+ await sio .emit (
320328 "status" ,
321329 {"status" : "Fetching all features..." , "progress" : 0 },
322330 to = sid ,
323331 namespace = "/home" ,
324332 )
325333 feature_list = get_all_features ()
326334
327- sio .emit (
335+ await sio .emit (
328336 "status" ,
329337 {"status" : "Building feature functions..." , "progress" : 5 },
330338 to = sid ,
331339 namespace = "/home" ,
332340 )
333341 functions = build_feature_functions (feature_list )
334342
335- sio .emit (
343+ await sio .emit (
336344 "status" ,
337345 {"status" : "Uploading file to vector store..." , "progress" : 10 },
338346 to = sid ,
339347 namespace = "/home" ,
340348 )
341- vector_store = upload_file_to_vector_store (client , file_path )
349+ vector_store = upload_file_to_vector_store (client = client , file = file , sid = sid )
342350
343- sio .emit (
351+ await sio .emit (
344352 "status" ,
345353 {"status" : "Creating an assistant for your task..." , "progress" : 12 },
346354 to = sid ,
347355 namespace = "/home" ,
348356 )
349357 my_temporary_assistant = create_temporary_assistant (client )
350358
351- sio .emit (
359+ await sio .emit (
352360 "status" ,
353361 {"status" : "Updating assistant..." , "progress" : 15 },
354362 to = sid ,
@@ -358,7 +366,7 @@ def call_asssistant_api(file_path: str, sid: str, sio):
358366 client , my_temporary_assistant .id , vector_store , functions
359367 )
360368
361- sio .emit (
369+ await sio .emit (
362370 "status" ,
363371 {"status" : "Creating thread message..." , "progress" : 30 },
364372 to = sid ,
@@ -368,12 +376,12 @@ def call_asssistant_api(file_path: str, sid: str, sio):
368376 messages = [
369377 {
370378 "role" : "user" ,
371- "content" : "define_experiments_conditions_and_behaviors" ,
379+ "content" : "run function define_experiments_conditions_and_behaviors" ,
372380 }
373381 ],
374382 )
375383
376- sio .emit (
384+ await sio .emit (
377385 "status" ,
378386 {"status" : "Running assistant..." , "progress" : 40 },
379387 to = sid ,
@@ -384,7 +392,7 @@ def call_asssistant_api(file_path: str, sid: str, sio):
384392 thread_id = thread_message .id , assistant_id = updated_assistant .id
385393 )
386394
387- sio .emit (
395+ await sio .emit (
388396 "status" ,
389397 {"status" : "Getting tool outputs..." , "progress" : 50 },
390398 to = sid ,
@@ -395,7 +403,7 @@ def call_asssistant_api(file_path: str, sid: str, sio):
395403 run .required_action .submit_tool_outputs .tool_calls [0 ].function .arguments
396404 )
397405
398- sio .emit (
406+ await sio .emit (
399407 "status" ,
400408 {"status" : "Checking output format..." , "progress" : 60 },
401409 to = sid ,
@@ -413,7 +421,7 @@ def call_asssistant_api(file_path: str, sid: str, sio):
413421 print (e )
414422 raise AssistantException ("Assistant run failed" ) from e
415423 finally :
416- sio .emit (
424+ await sio .emit (
417425 "status" ,
418426 {"status" : "Cleaning up resources..." , "progress" : 70 },
419427 to = sid ,
0 commit comments