-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssistant2.py
1122 lines (872 loc) · 33 KB
/
Assistant2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from openai import OpenAI
from openai import AssistantEventHandler
from openai.types.beta import Assistant, AssistantDeleted
from openai.types.beta import Thread, ThreadDeleted
from openai.types.beta import VectorStore, VectorStoreDeleted
from openai.types.beta.vector_stores import VectorStoreFile, VectorStoreFileDeleted
from openai.types.beta.threads import Message, Run
from enum import Enum
from typing_extensions import override
from os import path
class Assistant_Error(Exception):
"""
Exception class for assistant errors.
"""
def __init__(self, message: str, code: int = 0):
"""
An error that occurs during an assistant's processes.
Parameters:
message (str): The error message.
code (int): The error code.
"""
self.message = message
self.code = code
super().__init__(self.message)
def __str__(self):
return f"(Code: {self.code}) {self.message}"
class Language_Model(Enum):
"""
Enum class for language models.
"""
GPT_3_5_TURBO: str = "gpt-3.5-turbo-0125"
GPT_4O_MINI: str = "gpt-4o-mini"
class Stream_Handler(AssistantEventHandler):
def __init__(self, client: OpenAI, assistantName: str = 'Assistant'):
super().__init__()
self.client = client
self.assistantName = assistantName
@override
def on_exception(self, exception) -> None:
print(f"|| Stream failed to complete: {exception} ||", flush=True)
raise Assistant_Error(
message=f"Stream failed to complete. {exception}",
code=303
)
@override
def on_event(self, event):
"""
**[ DO NOT OVERRIDE ]**
"""
if event.event == 'thread.run.requires_action':
self.Handle_Required_Actions(data=event.data)
def Handle_Required_Actions(self, data: Run) -> None:
return None
def _Submit_Tool_Outputs(self, toolOutputs: list[dict]) -> None:
"""
**[ DO NOT OVERRIDE ]**
"""
with self.client.beta.threads.runs.submit_tool_outputs_stream(
thread_id=self.current_run.thread_id,
run_id=self.current_run.id,
tool_outputs=toolOutputs,
event_handler=Stream_Handler(
client=self.client,
assistantName=self.assistantName
)
) as stream:
for text in stream.text_deltas:
print(text, end="", flush=True)
print()
@override
def on_text_created(self, text) -> None:
print(f"{self.assistantName} > ", end="", flush=True)
@override
def on_text_delta(self, delta, snapshot) -> None:
print(delta.value, end="", flush=True)
@override
def on_text_done(self, text) -> None:
return super().on_text_done(text)
@override
def on_tool_call_created(self, tool_call) -> None:
print(f"\n{self.assistantName} > Using the {tool_call.type.replace('_', ' ')} tool.", flush=True)
@override
def on_message_done(self, message) -> None:
# Get message annotations
content = message.content[0].text
annotations: list = content.annotations
# Build citations
citations: list = []
for index, annotation in enumerate(annotations):
content.value = content.value.replace(
annotation.text, f"[{index}]"
)
if file_citation := getattr(annotation, "file_citation", None):
citedFile = self.client.files.retrieve(file_citation.file_id)
citations.append(f"{citedFile.filename}")
if (len(citations) > 0):
print(f"\nSources: ", end="", flush=True)
for i, x in enumerate(citations):
print(f"[{i}] {x}, ", end="", flush=True)
print("", end="\n", flush=True)
class Vector_Store:
def __init__(
self,
client: OpenAI,
id: str | None = None,
name: str | None = 'Vector_Store',
lifeTime: int | None = 1
):
# User defined attributes
self.client = client
self.id = id
self.name = name
self.lifeTime = lifeTime
# Default attributes
self.files: dict[str, str] = {}
# Retrieve the vector store
self.instance = self.Retrieve_Vector_Store()
self.id = self.instance.id
# # # #
#
# Vector Store Creation and Deletion Methods
#
# # # #
def Retrieve_Vector_Store(self) -> VectorStore:
"""
Retrieves a vector store with the given ID.
If an ID was not provided, this method creates a new vector store and assigns it to the instance.
Returns:
VectorStore: The retrieved vector store
Raises:
Assistant_Error: If the vector store could not be retrieved
"""
# Create a new vector store if an ID was not provided
if self.id is None:
return self.Create_Vector_Store()
try:
# Retrieve the vector store
return self.client.beta.vector_stores.retrieve(vector_store_id=self.id)
except Exception as e:
raise Assistant_Error(
message=f"Failed to retrieve vector store: {e}",
code=400
)
def Create_Vector_Store(self) -> VectorStore:
"""
Creates a new vector store and assigns it to the instance.
Returns:
VectorStore: The created vector store
Raises:
Assistant_Error: If the vector store could not be created
"""
try:
# Create the vector store
return self.client.beta.vector_stores.create(
name=self.name,
expires_after={
"anchor": "last_active_at",
"days": self.lifeTime
}
)
except Exception as e:
raise Assistant_Error(
message=f"Failed to create vector store: {e}",
code=401
)
def Delete_Vector_Store(self, deleteFiles: bool = True) -> bool:
# Delete attached files
if deleteFiles:
if not self.Delete_All_Files():
print("Failed to delete attached files")
# Delete the vector store
deletionResponse: VectorStoreDeleted = self.client.beta.vector_stores.delete(
vector_store_id=self.id
)
# Check if the vector store was deleted
if deletionResponse.deleted:
self.instance = None
self.id = None
return True
else:
return False
# # # #
#
# File Management Methods
#
# # # #
def Add_File_By_Id(self, fileName: str, fileID: str) -> None:
raise NotImplementedError
def Add_File_By_Path(self, fileName: str, filePath: str) -> None:
"""
Adds a file to the vector store by path.
Parameters:
fileName (str): The name of the file to add.
filePath (str): The path of the file to add.
Raises:
FileNotFoundError: If the file path does not exist
Assistant_Error: If the file could not be added to the vector store
"""
# Check if the file exists
if path.exists(filePath) == False:
raise FileNotFoundError("File not found")
try:
try:
# Open the file into a file stream
fileStream = open(filePath, "rb")
except Exception as e:
raise Assistant_Error(
message=f"Failed to open file: {e}",
code=403
)
# Upload and poll the file
vsFile: VectorStoreFile = self.client.beta.vector_stores.files.upload_and_poll(
vector_store_id=self.id,
file=fileStream
)
# Add the file to the files dictionary
self.files[fileName] = vsFile.id
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to upload file: {e}",
code=402
)
def _Delete_File_By_Id(self, fileID: str) -> bool:
"""
Deletes a file from the vector store by its ID.
This method does not delete the reference to the file's name in the files dictionary.
Parameters:
fileID (str): The ID of the file to delete.
Returns:
bool: True if the file was deleted, False otherwise.
Raises:
Assistant_Error: If the file could not be deleted.
"""
try:
# Delete the file
deletionResponse: VectorStoreFileDeleted = self.client.files.delete(
file_id=fileID
)
if deletionResponse.deleted:
return True
return False
except Exception as e:
raise Assistant_Error(
message=f"Failed to delete file: {e}",
code=404
)
def Delete_File_By_Name(self, fileName: str) -> bool:
"""
Deletes a file from the vector store by its name.
Parameters:
fileName (str): The name of the file to delete.
Returns:
bool: True if the file was successfully deleted or if no files exist, False otherwise.
Raises:
Assistant_Error: If the file could not be found or deleted.
"""
# Check if there are any files
if self.files == {}:
return True
try:
fileID: str = self.files[fileName]
except KeyError:
raise KeyError("File name does not exist")
finally:
if self._Delete_File_By_Id(fileID):
del self.files[fileName]
return True
def Delete_All_Files(self) -> bool:
# Get file names
fileNames: list[str] = list(self.files.keys())
# Check if there are any files
if len(fileNames) == 0:
return True
# Delete all files
for fileName in fileNames:
self.Delete_File_By_Name(fileName)
# Check status
if self.files == {}:
return True
else:
return False
class Assistant_V2:
def __init__(
self,
client: OpenAI,
id: str | None = None,
name: str | None = 'Assistant',
instructionPrompt: str | None = 'You are a simple chat bot.',
languageModel: Language_Model | None = Language_Model.GPT_3_5_TURBO,
):
# Set user defined attributes
self.client = client
self.id = id
self.name = name
self.instructionPrompt = instructionPrompt
self.languageModel = languageModel
# Set default attributes
self.threads: dict[str, str] = {}
self.tools: list[dict[str, any]] = [
{"type": "file_search"}
]
self.vectorStores: list[Vector_Store] = []
# Retrieve the assistant
self.instance: Assistant = self.Retrieve_Assistant()
# # # #
#
# General Maintenance Methods
#
# # # #
def _Verify_Existing_Thread_Name(self, threadName: str) -> bool:
"""
Verifies that the given thread name exists in the threads dictionary.
Parameters:
threadName (str): The name of the thread to verify.
Returns:
bool: True if the thread name exists.
Raises:
Assistant_Error: If the thread name does not exist.
"""
if threadName not in self.threads:
raise Assistant_Error(
message=f"No thread with alias: {threadName}",
code=103
)
return True
def _Verify_Unique_Thread_Name(self, threadName: str) -> bool:
"""
Verifies that the given thread name does not already exist in the threads dictionary.
Parameters:
threadName (str): The name of the thread to verify.
Returns:
bool: True if the thread name does not already exist, False otherwise.
Raises:
Assistant_Error: If the thread name already exists.
"""
if threadName in self.threads:
raise Assistant_Error(
message=f"Thread name '{threadName}' already exists.",
code=100
)
return True
# # # #
#
# Assistant Creation and Deletion Methods
#
# # # #
def Retrieve_Assistant(self) -> Assistant:
"""
Retrieves an assistant instance from OpenAI.
If the assistant ID is None, it will create a new assistant first.
Returns:
Assistant: The retrieved assistant instance.
Raises:
Assistant_Error: If the assistant ID is None and the Create_Assistant method fails.
Assistant_Error: If the retrieval request fails.
"""
# Create a new assistant if an ID was not provided
if self.id is None:
self.Create_Assistant()
# Retrieve the assistant
try:
return self.client.beta.assistants.retrieve(
assistant_id=self.id
)
except Exception as e:
raise Assistant_Error(
message=f"Failed to retrieve assistant: {e}",
code=205
)
def Create_Assistant(self) -> None:
"""
Creates a new assistant instance.
If an existing assistant ID is present, the existing assistant is deleted before creating a new one.
Raises:
Assistant_Error: If there is an error during the assistant creation.
"""
# Check if there is an existing assistant
if self.id is not None:
self.Delete_Assistant()
# Create a new assistant
self.instance = self.client.beta.assistants.create(
name=self.name,
instructions=self.instructionPrompt,
model=self.languageModel.value,
tools=self.tools
)
# Set the assistant ID
try:
self.id = self.instance.id
# Raise an exception if the assistant could not be created
except Exception as e:
raise Assistant_Error(
message=f"Failed to create assistant: {e}",
code=204
)
def Delete_Assistant(self) -> bool:
"""
Deletes the assistant. Call this method once you are done using the assistant.
Returns:
bool: True if the assistant is successfully deleted.
Raises:
Assistant_Error: If the assistant could not be deleted.
"""
# If there is no assistant instance, return True
if self.instance is None:
return True
# Delete the assistant
deletionResponse: AssistantDeleted = self.client.beta.assistants.delete(
assistant_id=self.id
)
# Check if the assistant was successfully deleted
if deletionResponse.deleted:
# Set the assistant instance to None
self.instance = None
self.id = None
# Delete the threads associated with the assistant
self.threads.clear()
return True
raise Assistant_Error(
message="Failed to delete assistant.",
code=203
)
# # # #
#
# Assistant Parameter Modification Methods
#
# # # #
def Update_Assistant_Name(self, name: str) -> bool:
"""
Updates the name of the assistant.
Parameters:
name (str): The new name of the assistant.
Returns:
bool: True if the assistant was successfully updated, False if not.
Raises:
Assistant_Error: If the assistant could not be updated.
"""
try:
# Update the assistant
self.instance = self.client.beta.assistants.update(
assistant_id=self.id,
name=name
)
# Reset the assistant name
self.name = name
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to update assistant: {e}",
code=200
)
def Update_Assistant_Instruction_Prompt(self, instructionPrompt: str) -> bool:
"""
Updates the instruction prompt of the assistant.
Parameters:
instructionPrompt (str): The new instruction prompt of the assistant.
Returns:
bool: True if the assistant was successfully updated, False if not.
Raises:
Assistant_Error: If the assistant could not be updated.
"""
try:
# Update the assistant
self.instance = self.client.beta.assistants.update(
assistant_id=self.id,
instructions=instructionPrompt
)
# Reset the assistant instruction prompt
self.instructionPrompt = instructionPrompt
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to update assistant: {e}",
code=201
)
def Update_Assistant_Language_Model(self, languageModel: Language_Model) -> bool:
"""
Updates the language model of the assistant.
Parameters:
languageModel (Language_Model): The new language model of the assistant.
Returns:
bool: True if the assistant was successfully updated, False if not.
Raises:
Assistant_Error: If the assistant could not be updated.
"""
try:
# Update the assistant
self.instance = self.client.beta.assistants.update(
assistant_id=self.id,
model=languageModel.value
)
# Reset the assistant language model
self.languageModel = languageModel
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to update assistant: {e}",
code=202
)
def Update_Assistant_Tools(self, tools: list[dict[str, any]]) -> bool:
# Maintain the file search tool
tools.append({"type": "file_search"})
try:
# Update the assistant
self.instance = self.client.beta.assistants.update(
assistant_id=self.id,
tools=tools
)
# Reset the assistant tools
self.tools = tools
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to update assistant: {e}",
code=206
)
# # # #
#
# Assistant Thread Handling Methods
#
# # # #
def Create_Thread(self, threadName: str) -> str:
"""
Creates a new thread with the given name.
Parameters:
threadName (str): The name of the thread to create.
Returns:
str: The ID of the created thread.
Raises:
Thread_Error: If the thread name already exists or if the thread could not be created.
"""
# Verify that the thread name is unique
self._Verify_Unique_Thread_Name(threadName)
# Variable initialization
threadInstance: Thread = None
# Create a new thread
threadInstance = self.client.beta.threads.create()
# Exception handling
if threadInstance is None:
raise Assistant_Error(
message="Failed to create thread.",
code=101
)
# Add the thread to the threads dictionary
self.threads[threadName] = threadInstance.id
# Return the thread ID
return threadInstance.id
def Delete_Thread_By_Id(self, threadID: str) -> bool:
raise NotImplementedError
def Delete_Thread_By_Name(self, threadName: str) -> bool:
"""
Deletes a thread with the given name.
Parameters:
threadName (str): The name of the thread to delete.
Returns:
bool: True if the thread was successfully deleted, False otherwise.
Raises:
Thread_Error: If the thread name does not exist or if the thread could not be deleted.
"""
# Verify that the thread name exists
self._Verify_Existing_Thread_Name(threadName)
# Retrieve the thread
threadID: str = self.threads[threadName]
# Delete the thread
deletionResponse: ThreadDeleted = self.client.beta.threads.delete(
thread_id=threadID
)
# Check if the thread was successfully deleted
if deletionResponse.deleted:
# Remove the thread from the threads dictionary
del self.threads[threadName]
return True
# Raise an exception if the thread could not be deleted
raise Assistant_Error(
message="Failed to delete thread.",
code=105
)
def Retrieve_Thread_By_Id(self, threadID: str) -> Thread:
"""
Retrieves a thread by its id.
Parameters:
threadID (str): The ID of the thread to retrieve.
Returns:
Thread: The retrieved thread.
Raises:
Thread_Error: If the thread could not be retrieved.
"""
try:
# Retrieve the thread
return self.client.beta.threads.retrieve(
thread_id=threadID
)
except Exception as e:
raise Assistant_Error(
message=f"Failed to retrieve thread. | {e}",
code=102
)
def Retrieve_Thread_By_Name(self, threadName: str) -> Thread:
"""
Retrieves a thread by its name.
Parameters:
threadName (str): The name of the thread to retrieve.
Returns:
Thread: The retrieved thread.
Raises:
Thread_Error: If the thread could not be retrieved.
"""
# Verify that the thread exists
self._Verify_Existing_Thread_Name(threadName)
# Retrieve the thread
return self.Retrieve_Thread_By_Id(self.threads[threadName])
def Update_Thread_Name(self, threadName: str, newName: str) -> bool:
"""
Updates the name of an existing thread.
Parameters:
threadName (str): The current name of the thread to update.
newName (str): The new name to assign to the thread.
Returns:
bool: True if the update was successful, False otherwise.
Raises:
Thread_Error: If the thread name could not be updated.
"""
# Verify that the thread exists
self._Verify_Existing_Thread_Name(threadName)
# Verify that the new thread name is unique
self._Verify_Unique_Thread_Name(newName)
try:
# Add the new thread
self.threads[newName] = self.threads[threadName]
# Remove the old thread
del self.threads[threadName]
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to update thread name. | {e}",
code=104
)
# # # #
#
# Assistant Vector Store Methods
#
# # # #
def _Link_VS_To_Thread(self, threadName: str, vectorStore: Vector_Store) -> bool:
"""
Links a vector store to a thread.
Parameters:
threadName (str): The name of the thread to which the vector store should be linked.
vectorStore (Vector_Store): The vector store to link to the thread.
Returns:
bool: True if the linking was successful, False otherwise.
Raises:
Assistant_Error: If the vector store could not be linked to the thread.
"""
self._Verify_Existing_Thread_Name(threadName)
threadId: str = self.threads[threadName]
try:
self.client.beta.threads.update(
thread_id=threadId,
tool_resources={"file_search":{
"vector_store_ids":[vectorStore.id]
}}
)
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to link vector store to thread. | {e}",
code=407
)
def _Link_VS_To_Assistant(self, vectorStore: Vector_Store) -> bool:
"""
Links a vector store to an assistant.
Parameters:
vectorStore (Vector_Store): The vector store to link to the assistant.
Returns:
bool: True if the linking was successful, False otherwise.
Raises:
Assistant_Error: If the vector store could not be linked to the assistant.
"""
try:
self.instance = self.client.beta.assistants.update(
assistant_id=self.id,
tool_resources={"file_search":{
"vector_store_ids":[vectorStore.id]
}}
)
return True
except Exception as e:
raise Assistant_Error(
message=f"Failed to link vector store to assistant. | {e}",
code=406
)
def Link_Vector_Store(self, vectorStore: Vector_Store, threadName: str | None = None) -> bool:
"""
Links a vector store to an assistant or a thread.
Parameters:
vectorStore (Vector_Store): The vector store to link to the assistant or thread.
threadName (str | None): The name of the thread to link the vector store to. If None, the vector store is linked to the assistant.
Returns:
bool: True if the linking was successful, False otherwise.
Raises:
Assistant_Error: If the vector store could not be linked to the assistant or thread.
"""
# Verify that the vector store exists
vectorStoreID: str = vectorStore.id
if vectorStoreID is None:
raise Assistant_Error(
message="Vector store does not exist.",
code=405
)
if threadName is None:
# Link the vector store to the assistant
return self._Link_VS_To_Assistant(vectorStore=vectorStore)
else:
# Link the vector store to the thread
return self._Link_VS_To_Thread(threadName=threadName, vectorStore=vectorStore)
# # # #
#
# Assistant Message Handling Methods
#
# # # #
def _Filter_Assistant_Response(self, messages: list[Message]) -> list[Message]:
"""
This method filters a list of messages into a list of only messages from the
assistant, after the user's most recent message.
Parameters
----------
messages (list[Message]): A list of messages to filter.
Returns
-------
list[Message]: A list of only messages from the assistant.
"""
# Variable initialization
filteredMessages: list[Message] = []
# Check if there are no messages
if len(messages) == 0:
return filteredMessages
# Iterate over the messages
for message in messages:
# Check if the message is from the assistant
if message.role == "assistant":
# Add the message to the filtered messages
filteredMessages.append(message)
else: break
# Return the filtered messages
return filteredMessages
def _Filter_Message_Strings(self, messages: list[Message]) -> list[str]:
"""
This method extracts and returns a list of strings from a list of Message objects.
Parameters:
messages (list[Message]): A list of Message objects whose content strings are to be extracted.
Returns:
list[str]: A list of strings extracted from the Message objects.
"""
# Variable initialization
messageStrings: list[str] = []
# Iterate over the messages
for message in messages:
# Add the message content to the message strings
messageStrings.append(
message.content[0].text.value
)
return messageStrings
def _Create_Run_Instance(self, threadID: str, assistantID: str) -> Run:
"""
Creates a new run instance in the specified thread.
Parameters:
threadID (str): The ID of the thread to create the run in.
assistantID (str): The ID of the assistant to associate with the run.
Returns:
Run: The created run instance.
Raises
Assistant_Error: If the run creation fails.
"""