-
Notifications
You must be signed in to change notification settings - Fork 15
/
api_requests.py
1026 lines (835 loc) · 42.7 KB
/
api_requests.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 abc import ABC, abstractmethod
import torch
import time
import re
import requests
from urllib.parse import urlparse, urlunparse
import openai
import anthropic
from .mng_json import json_manager, TroubleSgltn
from .fetch_models import RequestMode
from .utils import ImageUtils
class ImportedSgltn:
"""
This class is temporary to prevent circular imports
"""
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(ImportedSgltn, cls).__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if not self._initialized: #pylint: disable=access-member-before-definition
self._initialized = True
self._cfig = None
self._dalle = None
self._request_mode = None
self.get_imports()
def get_imports(self):
# Guard against re-importing if already done
if self._cfig is None or self._dalle is None:
from .style_prompt import cFigSingleton, DalleImage
self._cfig = cFigSingleton
self._dalle = DalleImage
self._request_mode = RequestMode
@property
def cfig(self):
if self._cfig is None:
self.get_imports()
return self._cfig()
@property
def dalle(self):
if self._dalle is None:
self.get_imports()
return self._dalle()
#Begin Strategy Pattern
class Request(ABC):
def __init__(self):
self.imps = ImportedSgltn()
self.utils = request_utils()
self.cFig = self.imps.cfig
self.mode = RequestMode
self.dalle = self.imps.dalle
self.j_mngr = json_manager()
@abstractmethod
def request_completion(self, **kwargs) -> None:
pass
class oai_object_request(Request): #Concrete class
def request_completion(self, **kwargs):
GPTmodel = kwargs.get('model')
creative_latitude = kwargs.get('creative_latitude', 0.7)
tokens = kwargs.get('tokens',500)
prompt = kwargs.get('prompt', "")
instruction = kwargs.get('instruction', "")
file = kwargs.get('file',"")
image = kwargs.get('image', None)
example_list = kwargs.get('example_list', [])
add_params = kwargs.get('add_params', None)
request_type = self.cFig.lm_request_mode
response = None
CGPT_response = ""
file += file.strip()
client = None
if request_type == self.mode.OPENSOURCE or request_type == self.mode.OLLAMA:
if self.cFig.lm_url:
self.j_mngr.log_events("Setting client to OpenAI Open Source LLM object",
is_trouble=True)
client = self.cFig.lm_client
#Force the correct url path
corrected_url = self.utils.validate_and_correct_url(self.cFig.lm_url,'/v1')
client.base_url = corrected_url
else:
self.j_mngr.log_events("Open Source api object is not ready for use, no URL provided. Aborting",
TroubleSgltn.Severity.WARNING,
is_trouble=True)
return CGPT_response
if request_type == self.mode.GROQ:
if self.cFig.lm_url:
self.j_mngr.log_events("Setting client to OpenAI Groq LLM object",
is_trouble=True)
client = self.cFig.lm_client
else:
self.j_mngr.log_events("Groq OpenAI api object is not ready for use, no URL provided. Aborting",
TroubleSgltn.Severity.WARNING,
is_trouble=True)
if request_type == self.mode.OPENAI:
if self.cFig.key:
self.j_mngr.log_events("Setting client to OpenAI ChatGPT object",
is_trouble=True)
client = self.cFig.openaiClient
else:
CGPT_response = "Invalid or missing OpenAI API key. Keys must be stored in an environment variable (see: ReadMe). ChatGPT request aborted"
self.j_mngr.log_events("Invalid or missing OpenAI API key. Keys must be stored in an environment variable (see: ReadMe). ChatGPT request aborted",
TroubleSgltn.Severity.WARNING,
is_trouble=True)
return CGPT_response
if not client:
if request_type == self.mode.OPENAI:
self.j_mngr.log_events("Invalid or missing OpenAI API key. Keys must be stored in an environment variable (see: ReadMe). ChatGPT request aborted",
TroubleSgltn.Severity.ERROR,
True)
CGPT_response = "Invalid or missing OpenAI API key. Keys must be stored in an environment variable (see: ReadMe). ChatGPT request aborted"
else:
self.j_mngr.log_events("LLM client not set. Make sure local Server is running if using a local LLM front-end",
TroubleSgltn.Severity.ERROR,
True)
CGPT_response = "Unable to process request, make sure local server is running"
return CGPT_response
#there's an image
if image:
# Use the user's selected vision model if it's what was chosen,
#otherwise use the last vision model in the list
#If the user is using a local LLM they're on their own to make
#the right model selection for handling an image
if isinstance(image, torch.Tensor): #just to be sure
image = self.dalle.tensor_to_base64(image)
if not isinstance(image,str):
image = None
self.j_mngr.log_events("Image file is invalid. Image will be disregarded in the generated output.",
TroubleSgltn.Severity.WARNING,
True)
messages = []
#Use basic data structure if there is no image
if not image:
messages = self.utils.build_data_basic(prompt, example_list, instruction)
else:
messages = self.utils.build_data_multi(prompt, instruction, example_list, image)
if not prompt and not image and not instruction and not example_list:
# User has provided no prompt, file or image
response = "Photograph of an stained empty box with 'NOTHING' printed on its side in bold letters, small flying moths, dingy, gloomy, dim light rundown warehouse"
self.j_mngr.log_events("No instruction and no prompt were provided, the node was only able to provide a 'Box of Nothing'",
TroubleSgltn.Severity.WARNING,
True)
return response
params = {
"model": GPTmodel,
"messages": messages,
"temperature": creative_latitude,
"max_tokens": tokens
}
# Add the parameter if it exists
if add_params:
add_keys =['param','value']
self.j_mngr.append_params(params, add_params, add_keys)
try:
response = client.chat.completions.create(**params)
except openai.APIConnectionError as e: # from httpx.
self.j_mngr.log_events(f"Server connection error: {e.__cause__}",
TroubleSgltn.Severity.ERROR,
True)
if request_type == self.mode.OPENSOURCE:
self.j_mngr.log_events(f"Local server is not responding to the URL: {self.cFig.lm_url}. Make sure your LLM Manager/Front-end app is running and its local server is live.",
TroubleSgltn.Severity.WARNING,
True)
except openai.RateLimitError as e:
error_message = e.body.get('message', "No error message provided") if isinstance(e.body, dict) else str(e.body or "No error message provided")
self.j_mngr.log_events(f"Server STATUS error {e.status_code}: {error_message}.",
TroubleSgltn.Severity.ERROR,
True)
except openai.APIStatusError as e:
error_message = e.body.get('message', "No error message provided") if isinstance(e.body, dict) else str(e.body or "No error message provided")
self.j_mngr.log_events(f"Server STATUS error {e.status_code}: {error_message}.",
TroubleSgltn.Severity.ERROR,
True)
except Exception as e:
self.j_mngr.log_events(f"An unexpected server error occurred.: {e}",
TroubleSgltn.Severity.ERROR,
True)
if response and response.choices and 'error' not in response:
rpt_model = ""
rpt_usage = ""
try:
rpt_model = response.model
rpt_usage = response.usage
except Exception as e:
self.j_mngr.log_events(f"Unable to report some completion information, error: {e}",
TroubleSgltn.Severity.INFO,
True)
if rpt_model:
self.j_mngr.log_events(f"Using LLM: {rpt_model}",
is_trouble=True)
if rpt_usage:
self.j_mngr.log_events(f"Tokens Used: {rpt_usage}",
TroubleSgltn.Severity.INFO,
True)
CGPT_response = response.choices[0].message.content
CGPT_response = self.utils.clean_response_text(CGPT_response)
else:
err_mess = getattr(response, 'error', "Error message missing")
CGPT_response = "Server was unable to process the request"
self.j_mngr.log_events(f"Server was unable to process this request. Error: {err_mess}",
TroubleSgltn.Severity.ERROR,
True)
return CGPT_response
class oai_web_request(Request):
def request_completion(self, **kwargs):
"""
Uses the incoming arguments to construct a JSON that contains the request for an LLM response.
Accesses an LLM via an http POST.
Sends the request via http. Handles the OpenAI return object and extacts the model and the response from it.
Args:
GPTmodel (str): The ChatGPT model to use in processing the request. Alternately this serves as a flag that the function will processing open source LLM data (GPTmodel = "LLM")
creative_latitude (float): A number setting the 'temperature' of the LLM
tokens (int): A number indicating the max number of tokens used to process the request and response
url (str): The url for the server the information is being sent to
request_:type (Enum): Specifies whether the function will be using a ChatGPT configured api object or an third party/url configured api object.
prompt (str): The users' request to action by the LLM
instruction (str): Text describing the conditions and specific requirements of the return value
image (b64 JSON/str): An image to be evaluated by the LLM in the context of the instruction
Return:
A string consisting of the LLM's response to the instruction and prompt in the context of any image and/or file
"""
GPTmodel = kwargs.get('model', "")
creative_latitude = kwargs.get('creative_latitude', 0.7)
url = kwargs.get('url',None)
tokens = kwargs.get('tokens', 500)
image = kwargs.get('image', None)
prompt = kwargs.get('prompt', None)
instruction = kwargs.get('instruction', "")
example_list = kwargs.get('example_list', [])
add_params = kwargs.get('add_params', None)
request_type = self.cFig.lm_request_mode
response = None
CGPT_response = ""
self.cFig.lm_url = url
if not self.cFig.is_lm_server_up:
self.j_mngr.log_events("Local or remote server is not responding, may be unable to send data.",
TroubleSgltn.Severity.WARNING,
True)
#if there's an image here
if image and request_type == self.mode.OSSIMPLE:
self.j_mngr.log_events("The AI Service using 'Simplfied Data' can't process an image. The image will be disregarded in generated output.",
TroubleSgltn.Severity.INFO,
True)
image = None
if image:
#The user is on their own to make
#the right model selection for handling an image
if isinstance(image, torch.Tensor): #just to be sure
image = self.dalle.tensor_to_base64(image)
if not isinstance(image,str):
image = None
self.j_mngr.log_events("Image file is invalid. Image will be disregarded in the generated output.",
TroubleSgltn.Severity.WARNING,
True)
key = ""
if request_type == self.mode.OPENAI:
key = self.cFig.key
elif request_type == self.mode.OPENSOURCE or request_type == self.mode.LMSTUDIO:
key = self.cFig.lm_key
elif request_type == self.mode.GROQ:
key = self.cFig.groq_key
else:
self.j_mngr.log_events("No LLM key value found",
TroubleSgltn.Severity.WARNING,
True)
headers = self.utils.build_web_header(key)
if request_type == self.mode.OSSIMPLE or not image:
messages = self.utils.build_data_basic(prompt, example_list, instruction) #Some apps can't handle an embedded list of role:user dicts
self.j_mngr.log_events("Using Basic data structure",
TroubleSgltn.Severity.INFO,
True)
else:
messages = self.utils.build_data_multi(prompt,instruction,example_list, image)
self.j_mngr.log_events("Using Complex data structure",
TroubleSgltn.Severity.INFO,
True)
params = {
"model": GPTmodel,
"messages": messages,
"temperature": creative_latitude,
"max_tokens": tokens
}
if add_params:
add_keys =['param','value']
self.j_mngr.append_params(params, add_params, add_keys)
post_success = False
response_json = ""
#payload = {**params}
try:
response = requests.post(url, headers=headers, json=params, timeout=(12,120))
if response.status_code in range(200, 300):
response_json = response.json()
if response_json and not 'error' in response_json:
CGPT_response = self.utils.clean_response_text(response_json['choices'][0]['message']['content'] )
post_success = True
else:
error_message = response_json.get('error', 'Unknown error')
self.j_mngr.log_events(f"Server was unable to process the response. Error: {error_message}",
TroubleSgltn.Severity.ERROR,
True)
else:
CGPT_response = 'Server was unable to process this request'
self.j_mngr.log_events(f"Server was unable to process the request. Status: {response.status_code}: {response.text}",
TroubleSgltn.Severity.ERROR,
True)
except Exception as e:
self.j_mngr.log_events(f"Unable to send data to server. Error: {e}",
TroubleSgltn.Severity.ERROR,
True)
if post_success:
try:
rpt_model = response_json['model']
rpt_usage = response_json['usage']
if rpt_model:
self.j_mngr.log_events(f"Using LLM: {rpt_model}",
is_trouble=True)
if rpt_usage:
self.j_mngr.log_events(f"Tokens Used: {rpt_usage}",
is_trouble=True)
except Exception as e:
self.j_mngr.log_events(f"Unable to report some completion information: model, usage. Error: {e}",
TroubleSgltn.Severity.INFO,
True)
return CGPT_response
class ooba_web_request(Request):
def request_completion(self, **kwargs):
"""
Accesses an OpenAI API client and uses the incoming arguments to construct a JSON that contains the request for an LLM response.
Sends the request via the client. Handles the OpenAI return object and extacts the model and the response from it.
Args:
GPTmodel (str): The ChatGPT model to use in processing the request. Alternately this serves as a flag that the function will processing open source LLM data (GPTmodel = "LLM")
creative_latitude (float): A number setting the 'temperature' of the LLM
tokens (int): A number indicating the max number of tokens used to process the request and response
url (str): The url for the server the information is being sent to
request_:type (Enum): Specifies whether the function will be using a ChatGPT configured api object or an third party/url configured api object.
prompt (str): The users' request to action by the LLM
instruction (str): Text describing the conditions and specific requirements of the return value
image (b64 JSON/str): An image to be evaluated by the LLM in the context of the instruction
Return:
A string consisting of the LLM's response to the instruction and prompt in the context of any image and/or file
"""
GPTmodel = kwargs.get('model', "")
creative_latitude = kwargs.get('creative_latitude', 0.7)
url = kwargs.get('url',None)
tokens = kwargs.get('tokens', 500)
image = kwargs.get('image', None)
prompt = kwargs.get('prompt', None)
instruction = kwargs.get('instruction', "")
example_list = kwargs.get('example_list', [])
request_type = self.cFig.lm_request_mode
add_params = kwargs.get('add_params', None)
response = None
CGPT_response = ""
url = self.utils.validate_and_correct_url(url) #validate v1/chat/completions path
self.cFig.lm_url = url
if not self.cFig.is_lm_server_up:
self.j_mngr.log_events("Local server is not responding, may be unable to send data.",
TroubleSgltn.Severity.WARNING,
True)
#image code is here, but right now none of the tested LLM front ends can handle them
#when using an http POST
if image:
image = None
self.j_mngr.log_events('Images not supported in this mode at this time. Image not transmitted',
TroubleSgltn.Severity.WARNING,
True)
key = ""
if request_type == self.mode.OPENAI:
key = self.cFig.key
else:
key = self.cFig.lm_key
headers = self.utils.build_web_header(key)
#messages = self.utils.build_data_basic(prompt, example_list, instruction)
messages = self.utils.build_data_ooba(prompt, example_list, instruction)
if request_type == self.mode.OOBABOOGA:
self.j_mngr.log_events(f"Processing Oobabooga http: POST request with url: {url}",
is_trouble=True)
params = {
"model": GPTmodel,
"messages": messages,
"temperature": creative_latitude,
"max_tokens": tokens,
"user_bio": "",
"user_name": ""
}
else:
params = {
"model": GPTmodel,
"messages": messages,
"temperature": creative_latitude,
"max_tokens": tokens
}
# Add the parameter if it exists
if add_params:
add_keys =['param','value']
self.j_mngr.append_params(params, add_params, add_keys)
post_success = False
response_json = ""
#payload = {**params}
try:
response = requests.post(url, headers=headers, json=params, timeout=(12,120))
if response.status_code in range(200, 300):
response_json = response.json()
if response_json and not 'error' in response_json:
CGPT_response = self.utils.clean_response_text(response_json['choices'][0]['message']['content'] )
post_success = True
else:
error_message = response_json.get('error', 'Unknown error')
self.j_mngr.log_events(f"Server was unable to process the response. Error: {error_message}",
TroubleSgltn.Severity.ERROR,
True)
else:
CGPT_response = 'Server was unable to process this request'
self.j_mngr.log_events(f"Server was unable to process the request. Status: {response.status_code}: {response.text}",
TroubleSgltn.Severity.ERROR,
True)
except Exception as e:
self.j_mngr.log_events(f"Unable to send data to server. Error: {e}",
TroubleSgltn.Severity.ERROR,
True)
if post_success:
try:
rpt_model = response_json['model']
rpt_usage = response_json['usage']
if rpt_model:
self.j_mngr.log_events(f"Using LLM: {rpt_model}",
is_trouble=True)
if rpt_usage:
self.j_mngr.log_events(f"Tokens Used: {rpt_usage}",
is_trouble=True)
except Exception as e:
self.j_mngr.log_events(f"Unable to report some completion information: model, usage. Error: {e}",
TroubleSgltn.Severity.INFO,
True)
return CGPT_response
class claude_request(Request):
def request_completion(self, **kwargs):
claude_model = kwargs.get('model')
creative_latitude = kwargs.get('creative_latitude', 0.7)
tokens = kwargs.get('tokens',500)
prompt = kwargs.get('prompt', "")
instruction = kwargs.get('instruction', "")
file = kwargs.get('file',"")
image = kwargs.get('image', None)
example_list = kwargs.get('example_list', [])
add_params = kwargs.get('add_params', None)
request_type = self.cFig.lm_request_mode
response = None
claude_response = ""
file += file.strip()
client = None
if request_type == self.mode.CLAUDE:
client = self.cFig.anthropic_client
if not client:
if request_type == self.mode.CLAUDE:
self.j_mngr.log_events("Invalid or missing anthropic API key (Claude). Keys must be stored in an environment variable (see: ReadMe). Claude request aborted",
TroubleSgltn.Severity.ERROR,
True)
claude_response = "Invalid or missing anthropic API key. Keys must be stored in an environment variable (see: ReadMe). Claude request aborted"
return claude_response
#there's an image
if image:
# Use the user's selected vision model if it's what was chosen,
#otherwise use the last vision model in the list
#If the user is using a local LLM they're on their own to make
#the right model selection for handling an image
if isinstance(image, torch.Tensor): #just to be sure
image = self.dalle.tensor_to_base64(image)
if not isinstance(image,str):
image = None
self.j_mngr.log_events("Image file is invalid. Image will be disregarded in the generated output.",
TroubleSgltn.Severity.WARNING,
True)
messages = []
messages = self.utils.build_data_claude(prompt, example_list, image)
if not prompt and not image and not instruction and not example_list:
# User has provided no prompt, file or image
claude_response = "Photograph of an stained empty box with 'NOTHING' printed on its side in bold letters, small flying moths, dingy, gloomy, dim light rundown warehouse"
self.j_mngr.log_events("No instruction and no prompt were provided, the node was only able to provide a 'Box of Nothing'",
TroubleSgltn.Severity.WARNING,
True)
return claude_response
params = {
"model": claude_model,
"messages": messages,
"temperature": creative_latitude,
"system": instruction,
"max_tokens": tokens
}
# Add the parameter if it exists
if add_params:
add_keys =['param','value']
self.j_mngr.append_params(params, add_params, add_keys)
try:
response = client.messages.create(**params)
except anthropic.AuthenticationError as e:
self.j_mngr.log_events(f"Authentication error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
except anthropic.PermissionDeniedError as e:
self.j_mngr.log_events(f"Permission denied error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
except anthropic.NotFoundError as e:
self.j_mngr.log_events(f"Not found error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
except anthropic.RateLimitError as e:
self.j_mngr.log_events(f"Rate limit exceeded error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.WARNING,
True)
except anthropic.BadRequestError as e:
self.j_mngr.log_events(f"Bad request error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
except anthropic.InternalServerError as e:
self.j_mngr.log_events(f"Internal server error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
except Exception as e:
self.j_mngr.log_events(f"Unexpected error: {request_utils.parse_anthropic_error(e)}",
TroubleSgltn.Severity.ERROR,
True)
if response and 'error' not in response:
rpt_model = ""
try:
rpt_model = response.model
rpt_usage = response.usage
if rpt_model:
self.j_mngr.log_events(f"Using LLM: {rpt_model}",
is_trouble=True)
if rpt_usage:
self.j_mngr.log_events(f"Tokens Used: {rpt_usage}",
TroubleSgltn.Severity.INFO,
True)
except Exception as e:
self.j_mngr.log_events(f"Unable to report some completion information, error: {e}",
TroubleSgltn.Severity.INFO,
True)
try:
claude_response = response.content[0].text
except (IndexError, AttributeError):
claude_response = "No data was returned"
self.j_mngr.log_events("Claude response was not valid data",
TroubleSgltn.Severity.WARNING,
True)
claude_response = self.utils.clean_response_text(claude_response)
else:
claude_response = "Server was unable to process the request"
self.j_mngr.log_events('Server was unable to process this request.',
TroubleSgltn.Severity.ERROR,
True)
return claude_response
class dall_e_request(Request):
def __init__(self):
super().__init__() # Ensures common setup from Request
self.trbl = TroubleSgltn()
self.iu = ImageUtils()
def request_completion(self, **kwargs)->tuple[torch.Tensor, str]:
GPTmodel = kwargs.get('model')
prompt = kwargs.get('prompt')
image_size = kwargs.get('image_size')
image_quality = kwargs.get('image_quality')
style = kwargs.get('style')
batch_size = kwargs.get('batch_size', 1)
self.trbl.set_process_header('Dall-e Request')
batched_images = torch.zeros(1, 1024, 1024, 3, dtype=torch.float32)
revised_prompt = "Image and mask could not be created" # Default prompt message
if not self.cFig.openaiClient:
self.j_mngr.log_events("OpenAI API key is missing or invalid. Key must be stored in an enviroment variable (see ReadMe). This node is not functional.",
TroubleSgltn.Severity.WARNING,
True)
return(batched_images, revised_prompt)
client = self.cFig.openaiClient
self.j_mngr.log_events(f"Talking to Dalle model: {GPTmodel}",
is_trouble=True)
have_rev_prompt = False
images_list = []
for _ in range(batch_size):
try:
response = client.images.generate(
model = GPTmodel,
prompt = prompt,
size = image_size,
quality = image_quality,
style = style,
n=1,
response_format = "b64_json",
)
# Get the revised_prompt
if response and not 'error' in response:
if not have_rev_prompt:
revised_prompt = response.data[0].revised_prompt
have_rev_prompt = True
#Convert the b64 json to a pytorch tensor
b64Json = response.data[0].b64_json
if b64Json:
png_image, _ = self.dalle.b64_to_tensor(b64Json)
images_list.append(png_image)
else:
self.j_mngr.log_events(f"Dalle-e could not process an image in your batch of: {batch_size} ",
TroubleSgltn.Severity.WARNING,
True)
else:
self.j_mngr.log_events(f"Dalle-e could not process an image in your batch of: {batch_size} ",
TroubleSgltn.Severity.WARNING,
True)
except openai.APIConnectionError as e:
self.j_mngr.log_events(f"ChatGPT server connection error in an image in your batch of {batch_size} Error: {e.__cause__}",
TroubleSgltn.Severity.ERROR,
True)
except openai.RateLimitError as e:
self.j_mngr.log_events(f"ChatGPT RATE LIMIT error in an image in your batch of {batch_size} Error: {e}: {e.response}",
TroubleSgltn.Severity.ERROR,
True)
time.sleep(0.5)
except openai.APIStatusError as e:
self.j_mngr.log_events(f"ChatGPT STATUS error in an image in your batch of {batch_size}; Error: {e.status_code}:{e.response}",
TroubleSgltn.Severity.ERROR,
True)
except Exception as e:
self.j_mngr.log_events(f"An unexpected error in an image in your batch of {batch_size}; Error:{e}",
TroubleSgltn.Severity.ERROR,
True)
if images_list:
count = len(images_list)
self.j_mngr.log_events(f'{count} images were processed successfully in your batch of: {batch_size}',
is_trouble=True)
batched_images = torch.cat(images_list, dim=0)
else:
self.j_mngr.log_events(f'No images were processed in your batch of: {batch_size}',
TroubleSgltn.Severity.WARNING,
is_trouble=True)
self.trbl.pop_header()
return(batched_images, revised_prompt)
def modify_image(self, client, model, image_bytes, prompt, image_size):
"""This is an unused stub to be used if Dall-e-3 ever implements image to image edits"""
image_bytes.seek(0) # Ensure the buffer is at the beginning
response = client.images.edit(
model=model,
image=image_bytes,
prompt=prompt,
n=1,
size=image_size,
response_format = "b64_json"
)
return response
class request_context:
def __init__(self)-> None:
self._request = None
self.j_mngr = json_manager()
@property
def request(self)-> Request:
return self._request
@request.setter
def request(self, request:Request)-> None:
self._request = request
def execute_request(self, **kwargs):
if self._request is not None:
return self._request.request_completion(**kwargs)
self.j_mngr.log_events("No request strategy object was set",
TroubleSgltn.Severity.ERROR,
True)
return None
class request_utils:
def __init__(self)-> None:
self.j_mngr = json_manager()
self.mode = RequestMode
def build_data_multi(self, prompt:str, instruction:str="", examples:list=None, image:str=None):
"""
Builds a list of message dicts, aggregating 'role:user' content into a list under 'content' key.
- image: Base64-encoded string or None. If string, included as 'image_url' type content.
- prompt: String to be included as 'text' type content under 'user' role.
- examples: List of additional example dicts to be included.
- instruction: Instruction string to be included under 'system' role.
"""
messages = []
user_role = {"role": "user", "content": None}
user_content = []
if instruction:
messages.append({"role": "system", "content": instruction})
if examples:
messages.extend(examples)
if prompt:
user_content.append({"type": "text", "text": prompt})
processed_image = self.process_image(image)
if processed_image:
user_content.append(processed_image)
if user_content:
user_role['content'] = user_content
messages.append(user_role)
return messages
def build_data_basic(self, prompt:str, examples:list=None, instruction:str=""):
"""
Builds a list of message dicts, presenting each 'role:user' item in its own dict.
- prompt: String to be included as 'text' type content under 'user' role.
- examples: List of additional example dicts to be included.
- instruction: Instruction string to be included under 'system' role.
"""
messages = []
if instruction:
messages.append({"role": "system", "content": instruction})
if examples:
messages.extend(examples)
if prompt:
messages.append({"role": "user", "content": prompt})
return messages
def build_data_ooba(self, prompt:str, examples:list=None, instruction:str="")-> list:
"""
Builds a list of message dicts, presenting each 'role:user' item in its own dict.
Since Oobabooga's system message is broken it includes it in the prompt
- prompt: String to be included as 'text' type content under 'user' role.
- examples: List of additional example dicts to be included.
- instruction: Instruction string to be included under 'system' role.
"""
messages = []
ooba_prompt = ""
if instruction:
ooba_prompt += f"INSTRUCTION: {instruction}\n\n"
if prompt:
ooba_prompt += f"PROMPT: {prompt}"
if examples:
messages.extend(examples)
if ooba_prompt:
messages.append({"role": "user", "content": ooba_prompt.strip()})
return messages
def build_data_claude(self, prompt:str, examples:list=None, image:str=None)-> list:
"""
Builds a list of message dicts, aggregating 'role:user' content into a list under 'content' key.
- image: Base64-encoded string or None. If string, included as 'image_url' type content.
- prompt: String to be included as 'text' type content under 'user' role.
- examples: List of additional example dicts to be included.
"""
messages = []
user_role = {"role": "user", "content": None}
user_content = []
if examples:
messages.extend(examples)
processed_image = self.process_image(image,RequestMode.CLAUDE)
if processed_image:
user_content.append(processed_image)
if prompt:
user_content.append({"type": "text", "text": prompt})
if user_content:
user_role['content'] = user_content
messages.append(user_role)
return messages
def process_image(self, image: str, request_type:RequestMode=RequestMode.OPENAI) :
if not image:
return None
if isinstance(image, str):
if request_type == self.mode.CLAUDE:
return {
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image
}
}
return {"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image}"
}
}
self.j_mngr.log_events("Image file is invalid.", TroubleSgltn.Severity.WARNING, True)
return None
def build_web_header(self, key:str=""):
if key:
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {key}"
}
else:
headers = {
"Content-Type": "application/json"
}
return headers
def validate_and_correct_url(self, user_url:str, required_path:str='/v1/chat/completions'):
"""
Takes the user's url and make sure it has the correct path for the connection
args:
user_url (str): The url to be validated and corrected if necessary
required_path (str): The correct path
return:
A string with either the original url if it was correct or the corrected url if it wasn't
"""
corrected_url = ""
parsed_url = urlparse(user_url)
# Check if the path is the required_path
if not parsed_url.path == required_path:
corrected_url = urlunparse((parsed_url.scheme,
parsed_url.netloc,
required_path,
'',
'',
''))
else:
corrected_url = user_url
self.j_mngr.log_events(f"URL was validated and is being presented as: {corrected_url}",
TroubleSgltn.Severity.INFO,
True)
return corrected_url
def clean_response_text(self, text: str)-> str:
# Replace multiple newlines or carriage returns with a single one
cleaned_text = re.sub(r'\n+', '\n', text).strip()
return cleaned_text
@staticmethod
def parse_anthropic_error(e):
"""
Parses error information from an exception object.
Args:
e (Exception): The exception from which to parse the error information.