-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstyle_prompt.py
1939 lines (1518 loc) · 78.4 KB
/
style_prompt.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
# ------------------------
# Standard Library Imports
# ------------------------
import os
from typing import Optional
from enum import Enum
from urllib.parse import urlparse
# -------------------------
# Third-Party Library Imports
# -------------------------
from PIL import Image, TiffImagePlugin, UnidentifiedImageError
import requests
from requests.adapters import HTTPAdapter, Retry
from openai import OpenAI
import anthropic
from aiohttp import web
#import google.generativeai as genai
# -----------------------
# Local Module Imports
# -----------------------
try:
import folder_paths
import nodes
from server import PromptServer
from .mng_json import json_manager, helpSgltn, TroubleSgltn
from . import api_requests as rqst
from .fetch_models import FetchModels, ModelUtils, RequestMode, ModelContainer
except ImportError:
from mng_json import json_manager, helpSgltn, TroubleSgltn
import api_requests as rqst
from fetch_models import FetchModels, ModelUtils, RequestMode, ModelContainer
#pip install pillow
#pip install bytesio
#Enum for style_prompt user input modes
class InputMode(Enum):
IMAGE_PROMPT = 1
IMAGE_ONLY = 2
PROMPT_ONLY = 3
#Get information from the config.json file
class cFigSingleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._lm_client = None
cls._anthropic_client = None
cls._gemini_client = None
cls._oai_client = None
cls._lm_url = ""
cls._lm_request_mode = None
cls._lm_key = ""
cls._custom_key = ""
cls._custom_key_change = False #Flag only
cls._groq_key = ""
cls._claude_key = ""
cls._gemini_key = ""
cls._lm_models = None
cls._groq_models = None
cls._claude_models = None
cls._gemini_models = None
cls._ollama_models = None
cls._optional_models = None
cls._openrouter_models = None
cls._written_url = ""
cls.j_mngr = json_manager()
cls._model_fetch = FetchModels()
cls._model_prep = ModelUtils()
cls._pyexiv2 = None
cls._instance.get_file()
return cls._instance
def get_file(self):
#Get script working directory
#j_mngr = json_manager()
# Error handling is in the load_json method
# Errors will be raised since is_critical is set to True
config_data =self.j_mngr.load_json(self.j_mngr.config_file, True)
#Pyexiv2 seems to have trouble loading with some Python versions (it's misreading the vesrion number)
#So I'll open it in a try block so as not to stop the whole suite from loading
try:
import pyexiv2
self._pyexiv2 = pyexiv2
except Exception as e:
self._pyexiv2 = None
self.j_mngr.log_events(f"The Pyexiv2 library failed to load with Error: {e} ",
TroubleSgltn.Severity.ERROR)
#check if file is empty
if not config_data:
raise ValueError("Plush - Error: config.json contains no valid JSON data")
# Try getting API key from Plush environment variable
self._fig_key = os.getenv('OAI_KEY',"") or os.getenv('OPENAI_API_KEY',"")
if not self._fig_key:
#Let user know some nodes will not function
self.j_mngr.log_events("Open AI API key invalid or not found, some nodes will not be functional. See Read Me to install the key",
TroubleSgltn.Severity.WARNING)
self._lm_key = os.getenv("LLM_KEY","") #Fetch the LLM_KEY if the user has created one
self._groq_key = os.getenv("GROQ_API_KEY", "")
self._claude_key = os.getenv("ANTHROPIC_API_KEY", "")
self._gemini_key = os.getenv("GEMINI_API_KEY", "")
#Get user saved Open Source URL from the text file
#At this point all this does is pre-populate new instances of the node.
url_file =self.j_mngr.append_filename_to_path(self.j_mngr.script_dir, 'OpenSourceURL.txt')
lm_url_list =self.j_mngr.read_lines_of_file(url_file)
if lm_url_list:
self._lm_url = lm_url_list[0] #This call will set up the client if the local LLM server is running
self._written_url = self._lm_url
self.figInstruction = config_data.get('instruction', "")
self.figExample = config_data.get('example', "")
self.figExample2 = config_data.get('example2', "")
self.fig_n_example = config_data.get('n_example', "")
self.fig_n_example2 = config_data.get('n_example2', "")
self._use_examples = False
self.figStyle = config_data.get('style', "")
self.figImgInstruction = config_data.get('img_instruction', "")
self.figImgPromptInstruction = config_data.get('img_prompt_instruction', "")
self.fig_n_Instruction = config_data.get('n_instruction', "")
self.fig_n_ImgPromptInstruction = config_data.get('n_img_prompt_instruction', "")
self.fig_n_ImgInstruction = config_data.get('n_img_instruction', "")
self._fig_gpt_models = []
if self._fig_key:
try:
self._oai_client = OpenAI(api_key= self._fig_key)
except Exception as e:
self.j_mngr.log_events(f"Invalid or missing OpenAI API key. Please note, keys must now be kept in an environment variable (see: ReadMe) {e}",
severity=TroubleSgltn.Severity.ERROR)
if self._claude_key:
try:
self._anthropic_client = anthropic.Anthropic(api_key = self._claude_key)
except Exception as e:
self.j_mngr.log_events(f"Invalid or missing Anthropic API key. Please note, keys must be kept in an environment variable.{e}",
severity=TroubleSgltn.Severity.ERROR)
if not self._gemini_key:
try:
#genai.configure(api_key=self._gemini_key)
self._gemini_key = "" #Placeholder until gemini api is ready
except Exception as e:
self.j_mngr.log_events(f"Invalid or missing Gemini API key. Please note, keys must be kept in an environment variable.{e}",
severity=TroubleSgltn.Severity.ERROR)
self._fig_gpt_models = self._model_fetch.fetch_models(RequestMode.OPENAI, self._fig_key)
self._groq_models = self._model_fetch.fetch_models(RequestMode.GROQ, self._groq_key)
self._claude_models = self._model_fetch.fetch_models(RequestMode.CLAUDE, self._claude_key,api_obj=self.anthropic_client)
self._gemini_models = self._model_fetch.fetch_models(RequestMode.GEMINI, self._gemini_key)
self._ollama_models = self._model_fetch.fetch_models(RequestMode.OLLAMA, "")
self._optional_models = self._model_fetch.fetch_models(RequestMode.OPENSOURCE, "")
def get_chat_models(self, sort_it:bool=False, filter_str:tuple=())->list:
return self._model_prep.prep_models_list(self._fig_gpt_models, sort_it, filter_str)
def get_groq_models(self):
if self._groq_models is None:
# Initialize with an empty container if none exists
self._groq_models = ModelContainer([], RequestMode.GROQ)
return self._groq_models
def get_claude_models(self):
if self._claude_models is None:
# Initialize with an empty container if none exists
self._claude_models = ModelContainer([], RequestMode.CLAUDE)
return self._claude_models
def get_gemini_models(self)->ModelContainer:
if self._gemini_models is None:
# Initialize with an empty container if none exists
self._gemini_models = ModelContainer([], RequestMode.GEMINI)
return self._gemini_models
def get_ollama_models(self)->ModelContainer:
if self._ollama_models is None:
# Initialize with an empty container if none exists
self._ollama_models = ModelContainer([], RequestMode.OLLAMA)
return self._ollama_models
def get_optional_models(self, sort_it:bool=False, filter_str:tuple=())->list:
return self._model_prep.prep_models_list(self._optional_models, sort_it, filter_str)
def _set_llm_client(self, url:str, request_type:RequestMode=RequestMode.OPENSOURCE)-> bool:
if not self.is_lm_server_up() or not url:
self._lm_client = None
self._lm_url = url
self._lm_models = None
self.j_mngr.log_events("Local LLM server is not running; aborting client setup.",
TroubleSgltn.Severity.WARNING,
True)
return False
lm_object = OpenAI
key = "No key necessary" #Default value used in LLM front-ends that don't require a key
#Use the requested API
if request_type in (RequestMode.OOBABOOGA, RequestMode.OPENSOURCE):
key = self._custom_key or self._lm_key # key will be the first truthy value
message = (
"Setting Openai client with URL and key."
if key else
"Setting Openai client with URL, no key."
)
self.j_mngr.log_events(message, is_trouble=True)
elif request_type == RequestMode.GROQ:
key = self._groq_key
if not key:
self.j_mngr.log_events(
"Attempting to connect to Groq with no key",
TroubleSgltn.Severity.ERROR,
True
)
elif request_type == RequestMode.GEMINI:
key = self._gemini_key
if not key:
self.j_mngr.log_events(
"Attempting to connect to Gemini with no key",
TroubleSgltn.Severity.ERROR,
True
)
else:
key = self._groq_key
self.j_mngr.log_events(
"Setting Openai client with URL and Groq key.",
is_trouble=True
)
try:
self._lm_client = lm_object(base_url=url, api_key=key)
self._lm_url = url
except Exception as e:
self.j_mngr.log_events(f"Unable to create LLM client object using URL. Unable to communicate with LLM: {e}",
TroubleSgltn.Severity.ERROR,
True)
return False
return True
@property
def lm_client(self):
return self._lm_client
@property
def lm_url(self):
return self._lm_url
def write_url(self, url:str) -> bool:
# Save the current open source url for startup retrieval of models
url_result = False
if url and url != self._written_url:
url_file = self.j_mngr.append_filename_to_path(self.j_mngr.script_dir, 'OpenSourceURL.txt')
url_result = self.j_mngr.write_string_to_file(url, url_file)
self._written_url = url
self.j_mngr.log_events("Open source LLM URL saved to file.",
TroubleSgltn.Severity.INFO,
True)
return url_result
@lm_url.setter
def lm_url(self, url: str):
if url != self._lm_url or not self._lm_client or self._custom_key_change: # Check if the new URL is different to avoid unnecessary operations
self._lm_url = url
self._lm_client = None
if url: # If the new URL is not empty, update the client
self._set_llm_client(url, self._lm_request_mode)
def is_lm_server_up(self): #should be util in api_requests.py
session = requests.Session()
retries = Retry(total=2, backoff_factor=0, status_forcelist=[500, 502, 503, 504])
session.mount('http://', HTTPAdapter(max_retries=retries))
try:
response = session.head(self._lm_url, timeout=4) # Use HEAD to minimize data transfer
if 200 <= response.status_code <= 300:
self.write_url(self._lm_url) #Save url to a text file
self.j_mngr.log_events(f"Local LLM Server is running with status code: {response.status_code}",
TroubleSgltn.Severity.INFO,
True)
return True
else:
self.write_url(self._lm_url) #Save url to a text file
self.j_mngr.log_events(f"Server returned response code: {response.status_code}",
TroubleSgltn.Severity.INFO,
True)
return True
except requests.RequestException as e:
self.j_mngr.log_events(f"Local LLM Server is not running: {e}",
TroubleSgltn.Severity.WARNING,
True)
return False
@property
def use_examples(self)->bool:
return self._use_examples
@use_examples.setter
def use_examples(self, use_examples: bool):
#Write, sets internal flag
self._use_examples = use_examples
@property
def lm_request_mode(self)->RequestMode:
return self._lm_request_mode
@lm_request_mode.setter
def lm_request_mode(self, mode:RequestMode)-> None:
self._lm_request_mode = mode
@property
def key(self)-> str:
return self._fig_key
@property
def lm_key(self)-> str:
return self._lm_key
@property
def custom_key(self)->str:
return self._custom_key
@custom_key.setter
def custom_key(self, key:str)->None:
#Inject key value from User-defined Env. Variable
if key != self._custom_key:
self._custom_key_change = True
self._custom_key = key
else:
self._custom_key_change = False
@property
def groq_key(self)->str:
return self._groq_key
@property
def anthropic_key(self)->str:
return self._claude_key
@property
def gemini_key(self)->str:
return self._gemini_key
@property
def instruction(self):
return self.figInstruction
@property
def example(self):
if self._use_examples:
return self.figExample
return ""
@property
def example2(self):
if self._use_examples:
return self.figExample2
return ""
@property
def n_Example(self):
if self._use_examples:
return self.fig_n_example
return ""
@property
def n_example2(self):
if self._use_examples:
return self.fig_n_example2
return ""
@property
def style(self):
#make sure the designated default value is present in the list
if "Photograph" not in self.figStyle:
if not isinstance(self.figStyle, list):
self.figStyle = []
self.figStyle.append("Photograph")
return self.figStyle
@property
def ImgInstruction(self):
return self.figImgInstruction
@property
def ImgPromptInstruction(self):
return self.figImgPromptInstruction
@property
def n_Instruction(self):
return self.fig_n_Instruction
@property
def n_ImgPromptInstruction(self):
return self.fig_n_ImgPromptInstruction
@property
def n_ImgInstruction(self):
return self.fig_n_ImgInstruction
@property
def pyexiv2(self)-> Optional[object]:
return self._pyexiv2
@property
def anthropic_client(self)->Optional[object]:
if self._claude_key:
return self._anthropic_client
return None
@property
def openaiClient(self)-> Optional[object]:
if self._fig_key:
return self._oai_client
return None
#********************End Singleton*********************
#Module level functions to create binding endpoint and
#load drop down list values from file
def _load_env_var_list() -> list:
# Inactive Experimental Code
"""
Core function to load the environment variable list.
Returns a list with either the env vars or ['error'] if something goes wrong.
"""
j_mngr = json_manager()
envvar_list = ['error']
try:
envvar_file = j_mngr.append_filename_to_path(j_mngr.script_dir, "user_envvar.txt")
user_vars = j_mngr.read_lines_of_file(envvar_file, is_critical=True)
if user_vars:
envvar_list = user_vars
except Exception as e:
j_mngr.log_events(f"Unable to read 'user_envvar.txt' file. Error: {e}",
TroubleSgltn.Severity.ERROR,
True)
return envvar_list
@PromptServer.instance.routes.get("/plush_for_comfyui/envvar_list")
async def handle_envvar_list_request(request):
# Inactive Experimental Code
j_mngr = json_manager()
# Get any new variable from query params
new_var = request.query.get('new_var')
save_triggered = request.query.get('save_triggered', 'false') == 'true'
if new_var and save_triggered:
# Check if it exists in environment
key = os.getenv(new_var, "missing")
if key != "missing" and key: # Valid env var
# Read current list
envvar_file = j_mngr.append_filename_to_path(j_mngr.script_dir, "user_envvar.txt")
envvar_list = j_mngr.read_lines_of_file(envvar_file)
# Add if not duplicate
if new_var not in envvar_list:
success = j_mngr.write_string_to_file(new_var + "\n", envvar_file, append=True)
if success:
j_mngr.log_events("New environment variable saved.", is_trouble=True)
else:
j_mngr.log_events("Environment variable already exists in list.",
TroubleSgltn.Severity.WARNING,
True)
else:
j_mngr.log_events(f"Environment Variable '{new_var}' not found in system.",
TroubleSgltn.Severity.ERROR,
True)
# Return updated list
vars_list = _load_env_var_list()
return web.json_response(vars_list)
class CustomKeyVar:
#Active, pending success of experimental code
def __init__(self):
#instantiate Configuration and Help data classes
self.cFig = cFigSingleton()
self.help_data = helpSgltn()
self.j_mngr = json_manager()
self.trbl = TroubleSgltn()
@classmethod
def INPUT_TYPES(cls):# -> dict[str, dict[str, Any]]:
j_mngr = json_manager()
def get_envvar_list():
envvar_list =['error']
envvar_file = j_mngr.append_filename_to_path(j_mngr.script_dir,"user_envvar.txt")
try:
envvar_list = j_mngr.read_lines_of_file(envvar_file, is_critical=True) #Returns a list with any user entered env. variables
except Exception as e:
j_mngr.log_events(f"Unable to read 'user_envvar.txt' file. Error: {e}",
TroubleSgltn.Severity.ERROR,
True)
return envvar_list
return envvar_list
return {
"required": {
"Environment_Variable": (get_envvar_list(), {"default": "-New Env. Variable", "tooltip": "Choose to enter a new Env. Variable below, or choose an existing one from the list"}),
"New_Env_Variable": ("STRING", {"multiline": False, "default": "", "tooltip": "Enter the name of a new Environment Variable that contains your API Key."}),
}
}
RETURN_TYPES = ("KEY","STRING")
RETURN_NAMES = ("Custom_ApiKey", "troubleshooting")
FUNCTION = "gogo"
OUTPUT_NODE = False
CATEGORY = "Plush🧸/Prompt"
def gogo(self, Environment_Variable, New_Env_Variable)->tuple:
self.trbl.reset("Custom API Key")
env_var = ""
if Environment_Variable == "-New Env. Variable":
env_var = New_Env_Variable
if not New_Env_Variable:
self.j_mngr.log_events("You specified '-New Env Variable' but didn't provide one in the field.",
TroubleSgltn.Severity.ERROR,
True)
else:
env_var = Environment_Variable
key = os.getenv(env_var,"missing",) #Fetch the api Key from the User-def Env. Var.
if key == "missing":
self.j_mngr.log_events(f"Environment Variable missing or misspelled: {env_var}",
TroubleSgltn.Severity.ERROR,
True)
return ("", self.trbl.get_troubles())
if not key:
self.j_mngr.log_events(f"Environment Variable not found: {env_var}",
TroubleSgltn.Severity.ERROR,
True)
return ("", self.trbl.get_troubles() )
if Environment_Variable == "-New Env. Variable":
envvar_file = self.j_mngr.append_filename_to_path(self.j_mngr.script_dir, "user_envvar.txt")
envvar_list = self.j_mngr.read_lines_of_file(envvar_file)
if New_Env_Variable not in envvar_list: # Prevent duplicates
self.j_mngr.write_string_to_file(env_var + "\n", envvar_file, append=True)
self.j_mngr.log_events("API Key succesfully retrieved.", is_trouble=True)
return (key,self.trbl.get_troubles())
class AI_Chooser:
def __init__(self):
#instantiate Configuration and Help data classes
self.cFig = cFigSingleton()
self.help_data = helpSgltn()
self.j_mngr = json_manager()
self.trbl = TroubleSgltn()
@staticmethod
def select_request_mode(user_selection:str) -> RequestMode:
mode_map = {
"ChatGPT": RequestMode.OPENAI,
"Groq": RequestMode.GROQ,
"Anthropic": RequestMode.CLAUDE,
"Gemini": RequestMode.GEMINI,
"OpenAI API Connection (URL)": RequestMode.OPENSOURCE,
"Direct Web Connection (URL)": RequestMode.OPENSOURCE,
"LM_Studio (URL)": RequestMode.OPENSOURCE,
"Ollama (URL)": RequestMode.OPENSOURCE,
"Web Connection Simplified Data (URL)": RequestMode.OSSIMPLE,
"Oobabooga API (URL)": RequestMode.OOBABOOGA
}
return mode_map.get(user_selection)
@classmethod
def INPUT_TYPES(cls):
cFig=cFigSingleton()
gptfilter = ("gpt","o1","o3","o4")
#Floats have a problem, they go over the max value even when round and step are set, and the node fails. So I set max a little over the expected input value
return {
"required": {
"AI_Service": (["ChatGPT", "Groq", "Gemini","Anthropic"], {"default": "ChatGPT"}),
"ChatGPT_model": (cFig.get_chat_models(True,gptfilter), {"default": ""}),
"Groq_model": (cFig.get_groq_models().get_models(True), {"default": ""}),
"Google_Gemini_model": (cFig.get_gemini_models().get_models(True), {"default": ""}),
"Anthropic_model": (cFig.get_claude_models().get_models(False, exclude_filter=("2.0", "2.1")), {"default": ""}),
},
"hidden": {
"unique_id": "UNIQUE_ID",
}
}
RETURN_TYPES = ("DICTIONARY",)
RETURN_NAMES = ("AI_Selection",)
FUNCTION = "gogo"
OUTPUT_NODE = False
CATEGORY = "Plush🧸/Prompt"
def gogo(self, unique_id, AI_Service, ChatGPT_model, Groq_model, Google_Gemini_model, Anthropic_model):
ai_dict = {"service": AI_Chooser.select_request_mode(AI_Service), "model": None}
if ai_dict['service'] == RequestMode.OPENAI:
ai_dict['model'] = ChatGPT_model
elif ai_dict['service'] == RequestMode.GROQ :
ai_dict['model'] = Groq_model
elif ai_dict['service'] == RequestMode.GEMINI:
ai_dict['model'] = Google_Gemini_model
elif ai_dict['service'] == RequestMode.CLAUDE:
ai_dict['model'] = Anthropic_model
return (ai_dict,)
class Enhancer:
#Build a creative prompt using a ChatGPT model
def __init__(self):
#instantiate Configuration and Help data classes
self.cFig = cFigSingleton()
self.help_data = helpSgltn()
self.j_mngr = json_manager()
self.trbl = TroubleSgltn()
self.ctx = rqst.request_context()
def build_instruction(self, mode, style, prompt_style, elements, artist):
#build the instruction from user input
instruc = ""
if prompt_style == "Narrative":
if mode == InputMode.PROMPT_ONLY:
if self.cFig.n_Instruction:
instruc = self.cFig.n_Instruction
elif mode == InputMode.IMAGE_ONLY:
if self.cFig.n_ImgInstruction:
instruc = self.cFig.n_ImgInstruction
elif mode == InputMode.IMAGE_PROMPT:
if self.cFig.n_ImgPromptInstruction:
instruc = self.cFig.n_ImgPromptInstruction
else: #Prompt_style is Tags
if mode == InputMode.PROMPT_ONLY:
if self.cFig.instruction:
instruc = self.cFig.instruction
elif mode == InputMode.IMAGE_ONLY:
if self.cFig.ImgInstruction:
instruc = self.cFig.ImgInstruction
elif mode == InputMode.IMAGE_PROMPT:
if self.cFig.ImgPromptInstruction:
instruc = self.cFig.ImgPromptInstruction
if instruc.count("{}") >= 2:
instruc = instruc.format(style, elements)
elif instruc.count("{}") == 1:
instruc = instruc.format(style)
if artist >= 1:
art_instruc = " Include {} artist(s) who works in the specifed artistic style by placing the artists' name(s) at the end of the sentence prefaced by 'style of'."
instruc += art_instruc.format(str(artist))
return instruc
def translateModelName(self, model: str)-> str:
#Translate friendly model names to working model names
#Not in use right now, but new models typically go through a period where there's
#no pointer value for the latest models.
if model == "gpt-4 Turbo":
model = "gpt-4-1106-preview"
return model
@staticmethod
def undefined_to_none( sus_var):
"""
Converts the string "undefined" to a None.
Note: ComfyUI returns unconnected UI elements as "undefined"
which causes problems when the node expects these to be handled as falsey
Args:
sus_var(any): The variable that might containt "undefined"
Returns:
None if the variable is set to the string "undefined" or unchanged (any) if not.
"""
return None if sus_var == "undefined" else sus_var
@classmethod
def INPUT_TYPES(cls):
cFig=cFigSingleton()
#Floats have a problem, they go over the max value even when round and step are set, and the node fails. So I set max a little over the expected input value
return {
"required": {
#"GPTmodel": (cFig.get_chat_models(True, 'gpt'),{"default": ""} ),
"creative_latitude" : ("FLOAT", {"max": 1.201, "min": 0.1, "step": 0.1, "display": "number", "round": 0.1, "default": 0.7}),
"tokens" : ("INT", {"max": 8000, "min": 20, "step": 10, "default": 500, "display": "number"}),
"style": (cFig.style,{"default": "Photograph"}),
"artist" : ("INT", {"max": 3, "min": 0, "step": 1, "default": 1, "display": "number"}),
"prompt_style": (["Tags", "Narrative"],{"default": "Tags"}),
"max_elements" : ("INT", {"max": 25, "min": 3, "step": 1, "default": 10, "display": "number"}),
"style_info" : ("BOOLEAN", {"default": False})
},
"hidden": {
"unique_id": "UNIQUE_ID",
},
"optional": {
"AI_Selection":("DICTIONARY", {"default": None}),
"prompt": ("STRING",{"multiline": True, "default": ""}),
"image" : ("IMAGE", {"default": None})
}
}
RETURN_TYPES = ("STRING", "STRING", "STRING", "STRING","STRING")
RETURN_NAMES = ("AI_prompt", "AI_instruction","Style Info", "Help","troubleshooting")
FUNCTION = "gogo"
OUTPUT_NODE = False
CATEGORY = "Plush🧸/Prompt"
def gogo(self, creative_latitude, tokens, style, artist, prompt_style, max_elements, style_info, AI_Selection=None, prompt="", image=None, unique_id=None):
if unique_id:
self.trbl.reset('Style Prompt, Node #'+unique_id)
else:
self.trbl.reset('Style Prompt')
_help = self.help_data.style_prompt_help
CGPT_prompt = ""
instruction = ""
CGPT_styleInfo = ""
if AI_Selection:
ais_model = AI_Selection['model']
else:
self.j_mngr.log_events("You must connect the Plush AI_Chooser to the AI_Selection Input and choose an AI_Service and model to use",
TroubleSgltn.Severity.ERROR,
True)
CGPT_prompt = "Plush AI_Chooser not connected to AI_Selection input, or missing input values"
return(CGPT_prompt, instruction, CGPT_styleInfo, _help, self.trbl.get_troubles())
# unconnected UI elements get passed in as the string "undefined" by ComfyUI
image = self.undefined_to_none(image)
prompt = self.undefined_to_none(prompt)
#Translate any friendly model names
#build instruction based on user input
mode = 0
if image is not None and prompt:
mode = InputMode.IMAGE_PROMPT
elif image is not None:
mode = InputMode.IMAGE_ONLY
elif prompt:
mode = InputMode.PROMPT_ONLY
instruction = self.build_instruction(mode, style, prompt_style, max_elements, artist)
self.cFig.lm_request_mode = AI_Selection['service']
if AI_Selection['service'] == RequestMode.OPENAI:
self.ctx.request = rqst.oai_object_request()
elif AI_Selection['service'] == RequestMode.GROQ:
self.ctx.request = rqst.oai_object_request()
# set the url so the function making the request will have a properly initialized object.
self.cFig.lm_url = "https://api.groq.com/openai/v1" # Ugh! I've embedded a 'magic value' URL here for the OPENAI API Object because the GROQ API object looks flakey...
elif AI_Selection['service'] == RequestMode.CLAUDE:
self.ctx.request = rqst.claude_request()
elif AI_Selection['service'] == RequestMode.GEMINI:
self.ctx.request = rqst.oai_object_request()
self.cFig.lm_url = "https://generativelanguage.googleapis.com/v1beta/openai/"
if style_info:
self.trbl.set_process_header("Art Style Info:")
#User has request information about the art style. GPT will provide it
sty_prompt = f"Give an 150 word backgrounder on the art style: {style}. Starting with describing what it is, include information about its history and which artists represent the style."
kwargs = { "model": ais_model,
"creative_latitude": creative_latitude,
"tokens": tokens,
"prompt": sty_prompt,
}
CGPT_styleInfo = self.ctx.execute_request(**kwargs)
self.trbl.pop_header()
kwargs = { "model": ais_model,
"creative_latitude": creative_latitude,
"tokens": tokens,
"prompt": prompt,
"instruction": instruction,
"image": image,
}
CGPT_prompt = self.ctx.execute_request(**kwargs)
return (CGPT_prompt, instruction, CGPT_styleInfo, _help, self.trbl.get_troubles())
class addParameters:
##New##
def __init__(self):
#instantiate Configuration and Help data classes
self.cFig = cFigSingleton()
self.help_data = helpSgltn()
self.j_mngr = json_manager()
self.trbl = TroubleSgltn()
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Parameters": ("STRING", {"multiline": True, "default": ""}),
"Save_to_file": ("BOOLEAN", {"default": False}),
"File_name": ("STRING", {"default": ""})
},
# "optional": {
# "text": ("STRING", {"default": None, "forceinput": True})
# },
"hidden": {
"unique_id": "UNIQUE_ID",
}
}
RETURN_TYPES = ("LIST", "STRING", "STRING")
RETURN_NAMES = ("Add_Parameter","Help","Troubleshooting")
FUNCTION = "gogo"
OUTPUT_NODE = False
CATEGORY = "Plush🧸/Prompt"
def gogo(self, Parameters, Save_to_file, File_name:bool, unique_id=None):
self.trbl.reset(f"Add Parameters, Node: {unique_id}")
_help = self.help_data.add_params_help
param_list = []
#Create path and dir for saved .txt files
write_dir = ''
comfy_dir = self.j_mngr.comfy_dir
if comfy_dir:
output_dir = self.j_mngr.find_child_directory(comfy_dir,'output')
if output_dir:
write_dir = self.j_mngr.find_child_directory(output_dir, 'PlushFiles',True) #Setting argument to True means dir will be created if not present
if not write_dir:
self.j_mngr.log_events('Unable to find or create PlushFiles directory. Unable to write files',
TroubleSgltn.Severity.WARNING,
True)
else:
self.j_mngr.log_events('Unable to find output directory, Unable to write files',
TroubleSgltn.Severity.WARNING,
True)
else:
self.j_mngr.log_events('Unable to find ComfyUI directory. Unable to write files.',
TroubleSgltn.Severity.WARNING,
True)
if Save_to_file and write_dir:
working_file_name = self.j_mngr.generate_unique_filename("txt", File_name + '_param_')
working_file_path = self.j_mngr.append_filename_to_path(write_dir,working_file_name)
self.j_mngr.write_string_to_file(Parameters,working_file_path)
self.j_mngr.log_events(f"Parameter file: '{working_file_name}' successfully written to directory [{write_dir}]",
is_trouble=True)
if Parameters:
template_dict = {"param": None, "value": None}
param_list = self.j_mngr.positional_str_to_dict(Parameters,template_dict,"#","::") #Parses the Parameters data and puts the result in a list of dicts
if not param_list:
self.j_mngr.log_events("Parameters not processed",
TroubleSgltn.Severity.ERROR,
True)
return(param_list,_help,self.trbl.get_troubles())
else:
self.j_mngr.log_events("No paramter data provided.",
TroubleSgltn.Severity.INFO,
True)
return(param_list,_help,self.trbl.get_troubles())
self.j_mngr.log_events(f"Parameters added: {str(param_list)}",
is_trouble=True)
return(param_list,_help,self.trbl.get_troubles())
class addParams:
##Depricated##
def __init__(self):
#instantiate Configuration and Help data classes
self.cFig = cFigSingleton()
self.help_data = helpSgltn()
self.j_mngr = json_manager()
self.trbl = TroubleSgltn()
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Parameter_type": (["none", "OpenAI JSON Format", "User Defined"], {"default": "none"}),
"Param_Name": ("STRING", {"default": ""}),
"Param_Value": ("STRING", {"multiline": True}),
"Is_JSON": ("BOOLEAN", {"default": False})
},
"hidden": {
"unique_id": "UNIQUE_ID",
},
"optional": {
"Add_Parameters": ("LIST", {"default": None, "forceInput": True})
}
}
RETURN_TYPES = ("LIST", "STRING", "STRING")
RETURN_NAMES = ("Add_Parameter","Help","Troubleshooting")
FUNCTION = "gogo"
OUTPUT_NODE = False
CATEGORY = "Plush🧸/Prompt"
def gogo(self, Parameter_type, Param_Name, Param_Value, Is_JSON: bool, Add_Parameters=None, unique_id=None):