-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathIFChatPromptNode.py
1151 lines (1010 loc) · 53.2 KB
/
IFChatPromptNode.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
# IFChatPromptNode.py
import os
import sys
import json
import torch
import shutil
import base64
import platform
import importlib
import subprocess
import numpy as np
import folder_paths
from PIL import Image
import yaml
from io import BytesIO
import asyncio
from typing import List, Union, Dict, Any, Tuple, Optional
from .agent_tool import AgentTool
from .send_request import send_request
#from .transformers_api import TransformersModelManager
import tempfile
import threading
import codecs
from aiohttp import web
from .graphRAG_module import GraphRAGapp
from .colpaliRAG_module import colpaliRAGapp
from .superflorence import FlorenceModule
from .utils import get_api_key, get_models, validate_models, clean_text, process_mask, load_placeholder_image, process_images_for_comfy
#from byaldi import RAGMultiModalModel
# Set up logging
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Add the ComfyUI directory to the Python path
comfy_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.insert(0, comfy_path)
ifchat_prompt_node = None
try:
from server import PromptServer
@PromptServer.instance.routes.post("/IF_ChatPrompt/get_llm_models")
async def get_llm_models_endpoint(request):
data = await request.json()
llm_provider = data.get("llm_provider")
engine = llm_provider
base_ip = data.get("base_ip")
port = data.get("port")
external_api_key = data.get("external_api_key")
logger.debug(f"Received request for LLM models. Provider: {llm_provider}, External API key provided: {bool(external_api_key)}")
if external_api_key:
api_key = external_api_key
logger.debug("Using provided external LLM API key")
else:
api_key_name = f"{llm_provider.upper()}_API_KEY"
try:
api_key = get_api_key(api_key_name, engine)
logger.debug("Using API key from environment or .env file")
except ValueError:
logger.warning(f"No API key found for {llm_provider}. Attempting to proceed without an API key.")
api_key = None
models = get_models(engine, base_ip, port, api_key)
logger.debug(f"Fetched {len(models)} models for {llm_provider}")
return web.json_response(models)
@PromptServer.instance.routes.post("/IF_ChatPrompt/get_embedding_models")
async def get_embedding_models_endpoint(request):
data = await request.json()
embedding_provider = data.get("embedding_provider")
engine = embedding_provider
base_ip = data.get("base_ip")
port = data.get("port")
external_api_key = data.get("external_api_key")
logger.debug(f"Received request for LLM models. Provider: {embedding_provider}, External API key provided: {bool(external_api_key)}")
if external_api_key:
api_key = external_api_key
logger.debug("Using provided external LLM API key")
else:
api_key_name = f"{embedding_provider.upper()}_API_KEY"
try:
api_key = get_api_key(api_key_name, engine)
logger.debug("Using API key from environment or .env file")
except ValueError:
logger.warning(f"No API key found for {embedding_provider}. Attempting to proceed without an API key.")
api_key = None
models = get_models(engine, base_ip, port, api_key)
logger.debug(f"Fetched {len(models)} models for {embedding_provider}")
return web.json_response(models)
@PromptServer.instance.routes.post("/IF_ChatPrompt/upload_file")
async def upload_file_route(request):
try:
reader = await request.multipart()
rag_folder_name = None
file_content = None
filename = None
# Process all parts of the multipart request
while True:
part = await reader.next()
if part is None:
break
if part.name == "rag_root_dir":
rag_folder_name = await part.text()
elif part.filename:
filename = part.filename
file_content = await part.read()
if not filename or not file_content or not rag_folder_name:
return web.json_response({"status": "error", "message": "Missing file, filename, or RAG folder name"})
node = IFChatPrompt()
input_dir = os.path.join(node.rag_dir, rag_folder_name, "input")
if not os.path.exists(input_dir):
os.makedirs(input_dir, exist_ok=True)
file_path = os.path.join(input_dir, filename)
with open(file_path, 'wb') as f:
f.write(file_content)
logger.info(f"File uploaded to: {file_path}")
return web.json_response({"status": "success", "message": f"File uploaded to: {file_path}"})
except Exception as e:
logger.error(f"Error in upload_file_route: {str(e)}")
return web.json_response({"status": "error", "message": f"Error uploading file: {str(e)}"})
@PromptServer.instance.routes.post("/IF_ChatPrompt/setup_and_initialize")
async def setup_and_initialize(request):
global ifchat_prompt_node
data = await request.json()
folder_name = data.get('folder_name', 'rag_data')
if ifchat_prompt_node is None:
ifchat_prompt_node = IFChatPrompt()
init_result = await ifchat_prompt_node.graphrag_app.setup_and_initialize_folder(folder_name, data)
ifchat_prompt_node.rag_folder_name = folder_name
ifchat_prompt_node.colpali_app.set_rag_root_dir(folder_name)
return web.json_response(init_result)
@PromptServer.instance.routes.post("/IF_ChatPrompt/run_indexer")
async def run_indexer_endpoint(request):
try:
data = await request.json()
logger.debug(f"Received indexing request with data: {data}")
global ifchat_prompt_node # Access the global instance
# Set the rag_root_dir in both modules using the global instance
ifchat_prompt_node.graphrag_app.set_rag_root_dir(data.get('rag_folder_name'))
ifchat_prompt_node.colpali_app.set_rag_root_dir(data.get('rag_folder_name'))
query_type = data.get('mode_type')
logger.debug(f"Query type: {query_type}")
logger.debug(f"Starting indexing process for query type: {query_type}")
# Initialize the colpali_model before calling insert, using the global instance
if query_type == 'colpali' or query_type == 'colqwen2' or query_type == 'colpali-v1.2':
_ = ifchat_prompt_node.colpali_app.get_colpali_model(query_type) # This will load or retrieve the cached model
result = await ifchat_prompt_node.colpali_app.insert()
else:
result = await ifchat_prompt_node.graphrag_app.insert()
logger.debug(f"Indexing process completed with result: {result}")
if result:
return web.json_response({"status": "success", "message": f"Indexing complete for {query_type}"})
else:
return web.json_response({"status": "error", "message": "Indexing failed. Check server logs."}, status=500)
except Exception as e:
logger.error(f"Error in run_indexer_endpoint: {str(e)}")
return web.json_response({"status": "error", "message": f"Error during indexing: {str(e)}"}, status=500)
@PromptServer.instance.routes.post("/IF_ChatPrompt/process_chat")
async def process_chat_endpoint(request):
try:
data = await request.json()
# Set default values for required arguments if not provided
defaults = {
"prompt": "",
"assistant": "Cortana", # Default assistant
"neg_prompt": "Default", # Default negative prompt
"embellish_prompt": "Default", # Default embellishment
"style_prompt": "Default", # Default style
"llm_provider": "ollama",
"llm_model": "",
"base_ip": "localhost",
"port": "11434",
"embedding_model": "",
"embedding_provider": "sentence_transformers"
}
# Update data with defaults for missing keys
for key, default_value in defaults.items():
if key not in data:
data[key] = default_value
global ifchat_prompt_node
result = await ifchat_prompt_node.process_chat(**data)
return web.json_response(result)
except Exception as e:
logger.error(f"Error in process_chat_endpoint: {str(e)}")
return web.json_response({
"status": "error",
"message": f"Error processing chat: {str(e)}",
"Question": data.get("prompt", ""),
"Response": f"Error: {str(e)}",
"Negative": "",
"Tool_Output": None,
"Retrieved_Image": None,
"Mask": None
}, status=500)
@PromptServer.instance.routes.post("/IF_ChatPrompt/load_index")
async def load_index_route(request):
try:
data = await request.json()
index_name = data.get('rag_folder_name')
query_type = data.get('query_type')
if not index_name:
logger.error("No index name provided in the request.")
return web.json_response({
"status": "error",
"message": "No index name provided"
})
# Check if index exists in .byaldi directory
byaldi_index_path = os.path.join(".byaldi", index_name)
if not os.path.exists(byaldi_index_path):
logger.error(f"Index not found in .byaldi: {byaldi_index_path}")
return web.json_response({
"status": "error",
"message": f"Index {index_name} does not exist"
})
try:
global ifchat_prompt_node
if ifchat_prompt_node is None:
logger.debug("Initializing IFChatPrompt instance.")
ifchat_prompt_node = IFChatPrompt()
if query_type in ['colpali', 'colqwen2', 'colpali-v1.2']:
logger.debug(f"Loading model for query type: {query_type}")
# Clear any existing cached index
ifchat_prompt_node.colpali_app.cleanup_index()
# First get the base model
colpali_model = ifchat_prompt_node.colpali_app.get_colpali_model(query_type)
if colpali_model:
# Load and cache the new index
model = await ifchat_prompt_node.colpali_app._prepare_model(query_type, index_name)
if not model:
raise ValueError("Failed to load and cache index")
# Set the RAG root directory
ifchat_prompt_node.colpali_app.set_rag_root_dir(index_name)
logger.info(f"Successfully loaded and cached index: {index_name}")
return web.json_response({
"status": "success",
"message": f"Successfully loaded index: {index_name}",
"rag_root_dir": index_name
})
else:
logger.error("Failed to initialize ColPali model.")
raise ValueError("Failed to initialize ColPali model")
else:
logger.error(f"Unsupported query type: {query_type}")
return web.json_response({
"status": "error",
"message": f"Query type {query_type} not supported for loading indexes"
})
except Exception as e:
logger.error(f"Error loading index {index_name}: {str(e)}")
return web.json_response({
"status": "error",
"message": f"Error loading index: {str(e)}"
})
except Exception as e:
logger.error(f"Error in load_index_route: {str(e)}")
return web.json_response({
"status": "error",
"message": f"Error processing request: {str(e)}"
})
# Add this with the other routes
@PromptServer.instance.routes.post("/IF_ChatPrompt/delete_index")
async def delete_index_route(request):
try:
data = await request.json()
index_name = data.get('rag_folder_name')
if not index_name:
return web.json_response({
"status": "error",
"message": "No index name provided"
})
# Path to the index
index_path = os.path.join(".byaldi", index_name)
if not os.path.exists(index_path):
return web.json_response({
"status": "error",
"message": f"Index {index_name} does not exist"
})
# Delete the index directory
try:
shutil.rmtree(index_path)
logger.info(f"Successfully deleted index: {index_name}")
return web.json_response({
"status": "success",
"message": f"Successfully deleted index: {index_name}"
})
except Exception as e:
logger.error(f"Error deleting index {index_name}: {str(e)}")
return web.json_response({
"status": "error",
"message": f"Error deleting index: {str(e)}"
})
except Exception as e:
logger.error(f"Error in delete_index_route: {str(e)}")
return web.json_response({
"status": "error",
"message": f"Error processing request: {str(e)}"
})
except AttributeError:
print("PromptServer.instance not available. Skipping route decoration for IF_ChatPrompt.")
class IFChatPrompt:
def __init__(self):
self.base_ip = "localhost"
self.port = "11434"
self.llm_provider = "ollama"
self.embedding_provider = "sentence_transformers"
self.llm_model = ""
self.embedding_model = ""
self.assistant = "None"
self.random = False
self.comfy_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
self.rag_dir = os.path.join(folder_paths.base_path, "custom_nodes", "ComfyUI-IF_AI_tools", "IF_AI", "rag")
self.presets_dir = os.path.join(folder_paths.base_path, "custom_nodes", "ComfyUI-IF_AI_tools", "IF_AI", "presets")
self.stop_file = os.path.join(self.presets_dir, "stop_strings.json")
self.assistants_file = os.path.join(self.presets_dir, "assistants.json")
self.neg_prompts_file = os.path.join(self.presets_dir, "neg_prompts.json")
self.embellish_prompts_file = os.path.join(self.presets_dir, "embellishments.json")
self.style_prompts_file = os.path.join(self.presets_dir, "style_prompts.json")
self.tasks_file = os.path.join(self.presets_dir, "florence_prompts.json")
self.agents_dir = os.path.join(self.presets_dir, "agents")
self.agent_tools = self.load_agent_tools()
self.stop_strings = self.load_presets(self.stop_file)
self.assistants = self.load_presets(self.assistants_file)
self.neg_prompts = self.load_presets(self.neg_prompts_file)
self.embellish_prompts = self.load_presets(self.embellish_prompts_file)
self.style_prompts = self.load_presets(self.style_prompts_file)
self.florence_prompts = self.load_presets(self.tasks_file)
self.keep_alive = False
self.seed = 94687328150
self.messages = []
self.history_steps = 10
self.external_api_key = ""
self.tool_input = ""
self.prime_directives = None
self.rag_folder_name = "rag_data"
self.graphrag_app = GraphRAGapp()
self.colpali_app = colpaliRAGapp()
self.fix_json = True
self.cached_colpali_model = None
self.florence_app = FlorenceModule()
self.florence_models = {}
self.query_type = "global"
self.enable_RAG = False
self.clear_history = False
self.mode = False
self.tool = "None"
self.preset = "Default"
self.precision = "fp16"
self.task = None
self.attention = "sdpa"
self.aspect_ratio = "16:9"
self.top_k_search = 3
self.placeholder_image_path = os.path.join(folder_paths.base_path, "custom_nodes", "ComfyUI-IF_AI_tools", "IF_AI", "placeholder.png")
if not os.path.exists(self.placeholder_image_path):
placeholder = Image.new('RGB', (512, 512), color=(73, 109, 137))
os.makedirs(os.path.dirname(self.placeholder_image_path), exist_ok=True)
placeholder.save(self.placeholder_image_path)
def load_presets(self, file_path: str) -> Dict[str, Any]:
"""
Load JSON presets with support for multiple encodings.
Args:
file_path (str): Path to the JSON preset file
Returns:
Dict[str, Any]: Loaded JSON data or empty dict if loading fails
"""
# List of encodings to try
encodings = ['utf-8', 'utf-8-sig', 'latin1', 'cp1252', 'gbk']
for encoding in encodings:
try:
with codecs.open(file_path, 'r', encoding=encoding) as f:
data = json.load(f)
# If successful, write back with UTF-8 encoding to prevent future issues
try:
with codecs.open(file_path, 'w', encoding='utf-8') as out_f:
json.dump(data, out_f, ensure_ascii=False, indent=2)
except Exception as write_err:
print(f"Warning: Could not write back UTF-8 encoded file: {write_err}")
return data
except UnicodeDecodeError:
continue
except json.JSONDecodeError as e:
print(f"JSON parsing error with {encoding} encoding: {str(e)}")
continue
except Exception as e:
print(f"Error loading presets from {file_path} with {encoding} encoding: {e}")
continue
print(f"Error: Failed to load {file_path} with any supported encoding")
return {}
def load_agent_tools(self):
os.makedirs(self.agents_dir, exist_ok=True)
agent_tools = {}
try:
for filename in os.listdir(self.agents_dir):
if filename.endswith('.json'):
full_path = os.path.join(self.agents_dir, filename)
with open(full_path, 'r') as f:
try:
data = json.load(f)
if 'output_type' not in data:
data['output_type'] = None
agent_tool = AgentTool(**data)
agent_tool.load()
if agent_tool._class_instance is not None:
if agent_tool.python_function:
agent_tools[agent_tool.name] = agent_tool
else:
print(f"Warning: Agent tool {agent_tool.name} in {filename} does not have a python_function defined.")
else:
print(f"Failed to create class instance for {filename}")
except json.JSONDecodeError:
print(f"Error: Invalid JSON in {filename}")
except Exception as e:
print(f"Error loading {filename}: {str(e)}")
return agent_tools
except Exception as e:
print(f"Warning: Error accessing agent tools directory: {str(e)}")
return {}
async def process_chat(
self,
prompt,
llm_provider,
llm_model,
base_ip,
port,
assistant,
neg_prompt,
embellish_prompt,
style_prompt,
embedding_model,
embedding_provider,
external_api_key="",
temperature=0.7,
max_tokens=2048,
seed=0,
random=False,
history_steps=10,
keep_alive=False,
top_k=40,
top_p=0.2,
repeat_penalty=1.1,
stop_string=None,
images=None,
mode=True,
clear_history=False,
text_cleanup=True,
tool=None,
tool_input=None,
prime_directives=None,
enable_RAG=False,
query_type="global",
preset="Default",
rag_folder_name=None,
task=None,
fill_mask=False,
output_mask_select="",
precision="fp16",
attention="sdpa",
aspect_ratio="16:9",
top_k_search=3
):
if external_api_key != "":
llm_api_key = external_api_key
else:
llm_api_key = get_api_key(f"{llm_provider.upper()}_API_KEY", llm_provider)
print(f"LLM API key: {llm_api_key[:5]}...")
if prime_directives is not None:
system_message_str = prime_directives
else:
system_message = self.assistants.get(assistant, "")
system_message_str = json.dumps(system_message)
# Validate LLM model
validate_models(llm_model, llm_provider, "LLM", base_ip, port, llm_api_key)
# Validate embedding model
validate_models(embedding_model, embedding_provider, "embedding", base_ip, port, llm_api_key)
# Handle history
if clear_history:
self.messages = []
elif history_steps > 0:
self.messages = self.messages[-history_steps:]
messages = self.messages
# Handle stop
if stop_string is None or stop_string == "None":
stop_content = None
else:
stop_content = self.stop_strings.get(stop_string, None)
stop = stop_content
if llm_provider not in ["ollama", "llamacpp", "vllm", "lmstudio", "gemeni"]:
if llm_provider == "kobold":
stop = stop_content + \
["\n\n\n\n\n"] if stop_content else ["\n\n\n\n\n"]
elif llm_provider == "mistral":
stop = stop_content + \
["\n\n"] if stop_content else ["\n\n"]
else:
stop = stop_content if stop_content else None
# Handle tools
try:
if tool and tool != "None":
selected_tool = self.agent_tools.get(tool)
if not selected_tool:
raise ValueError(f"Invalid agent tool selected: {tool}")
# Prepare tool execution message
tool_message = f"Execute the {tool} tool with the following input: {prompt}"
system_prompt = json.dumps(selected_tool.system_prompt)
# Send request to LLM for tool execution
generated_text =await send_request(
llm_provider=llm_provider,
base_ip=base_ip,
port=port,
images=images,
model=llm_model,
system_message=system_prompt,
user_message=tool_message,
messages=messages,
seed=seed,
temperature=temperature,
max_tokens=max_tokens,
random=random,
top_k=top_k,
top_p=top_p,
repeat_penalty=repeat_penalty,
stop=stop,
keep_alive=keep_alive,
llm_api_key=llm_api_key,
)
# Parse the generated text for function calls
function_call = None
try:
response_data = json.loads(generated_text)
if 'function_call' in response_data:
function_call = response_data['function_call']
generated_text = response_data['content']
except json.JSONDecodeError:
pass # The response wasn't JSON, so it's just the generated text
# Execute the tool with the LLM's response
tool_args = {
"input": prompt,
"llm_response": generated_text,
"function_call": function_call,
"omni_input": tool_input,
"name": selected_tool.name,
"description": selected_tool.description,
"system_prompt": selected_tool.system_prompt
}
tool_result = selected_tool.execute(tool_args)
# Update messages
messages.append({"role": "user", "content": prompt})
messages.append({
"role": "assistant",
"content": json.dumps(tool_result) if isinstance(tool_result, dict) else str(tool_result)
})
# Process the tool output
if isinstance(tool_result, dict):
if "error" in tool_result:
generated_text = f"Error in {tool}: {tool_result['error']}"
tool_output = None
elif selected_tool.output_type and selected_tool.output_type in tool_result:
tool_output = tool_result[selected_tool.output_type]
generated_text = f"Agent {tool} executed successfully. Output generated."
else:
tool_output = tool_result
generated_text = str(tool_output)
else:
tool_output = tool_result
generated_text = str(tool_output)
return {
"Question": prompt,
"Response": generated_text,
"Negative": self.neg_prompts.get(neg_prompt, ""),
"Tool_Output": tool_output,
"Retrieved_Image": None # No image retrieved in tool execution
}
else:
response = await self.generate_response(
enable_RAG,
query_type,
prompt,
preset,
llm_provider,
base_ip,
port,
images,
llm_model,
system_message_str,
messages,
temperature,
max_tokens,
random,
top_k,
top_p,
repeat_penalty,
stop,
seed,
keep_alive,
llm_api_key,
task,
fill_mask,
output_mask_select,
precision,
attention
)
generated_text = response.get("Response")
selected_neg_prompt_name = neg_prompt
omni = response.get("Tool_Output")
retrieved_image = response.get("Retrieved_Image")
retrieved_mask = response.get("Mask")
# Update messages
messages.append({"role": "user", "content": prompt})
messages.append({"role": "assistant", "content": generated_text})
text_result = str(generated_text).strip()
if mode:
embellish_content = self.embellish_prompts.get(embellish_prompt, "").strip()
style_content = self.style_prompts.get(style_prompt, "").strip()
lines = [line.strip() for line in text_result.split('\n') if line.strip()]
combined_prompts = []
for line in lines:
if text_cleanup:
line = clean_text(line)
formatted_line = f"{embellish_content} {line} {style_content}".strip()
combined_prompts.append(formatted_line)
combined_prompt = "\n".join(formatted_line for formatted_line in combined_prompts)
# Handle negative prompts
if selected_neg_prompt_name == "AI_Fill":
try:
neg_system_message = self.assistants.get("NegativePromptEngineer")
if not neg_system_message:
logger.error("NegativePromptEngineer not found in assistants configuration")
negative_prompt = "Error: NegativePromptEngineer not configured"
else:
user_message = f"Generate negative prompts for the following prompt:\n{text_result}"
system_message_str = json.dumps(neg_system_message)
logger.info(f"Requesting negative prompts for prompt: {text_result[:100]}...")
neg_response = await send_request(
llm_provider=llm_provider,
base_ip=base_ip,
port=port,
images=None,
llm_model=llm_model,
system_message=system_message_str,
user_message=user_message,
messages=[], # Fresh context for negative generation
seed=seed,
temperature=temperature,
max_tokens=max_tokens,
random=random,
top_k=top_k,
top_p=top_p,
repeat_penalty=repeat_penalty,
stop=stop,
keep_alive=keep_alive,
llm_api_key=llm_api_key
)
logger.debug(f"Received negative prompt response: {neg_response}")
if neg_response:
negative_lines = []
for line in neg_response.split('\n'):
line = line.strip()
if line:
negative_lines.append(line)
while len(negative_lines) < len(lines):
negative_lines.append(negative_lines[-1] if negative_lines else "")
negative_lines = negative_lines[:len(lines)]
negative_prompt = "\n".join(negative_lines)
else:
negative_prompt = "Error: Empty response from LLM"
except Exception as e:
logger.error(f"Error generating negative prompts: {str(e)}", exc_info=True)
negative_prompt = f"Error generating negative prompts: {str(e)}"
elif neg_prompt != "None":
neg_content = self.neg_prompts.get(neg_prompt, "").strip()
negative_lines = [neg_content for _ in range(len(lines))]
negative_prompt = "\n".join(negative_lines)
else:
negative_prompt = ""
else:
combined_prompt = text_result
negative_prompt = ""
try:
if isinstance(retrieved_image, torch.Tensor):
# Ensure it's in the correct format (B, C, H, W)
if retrieved_image.dim() == 3: # Single image (C, H, W)
image_tensor = retrieved_image.unsqueeze(0) # Add batch dimension
else:
image_tensor = retrieved_image # Already batched
# Create matching batch masks
batch_size = image_tensor.shape[0]
height = image_tensor.shape[2]
width = image_tensor.shape[3]
# Create white masks (all ones) for each image in batch
mask_tensor = torch.ones((batch_size, 1, height, width),
dtype=torch.float32,
device=image_tensor.device)
if retrieved_mask is not None:
# If we have masks, process them to match the batch
if isinstance(retrieved_mask, torch.Tensor):
if retrieved_mask.dim() == 3: # Single mask
mask_tensor = retrieved_mask.unsqueeze(0)
else:
mask_tensor = retrieved_mask
else:
# Process retrieved_mask if it's not a tensor
mask_tensor = process_mask(retrieved_mask, image_tensor)
else:
image_tensor, default_mask_tensor = process_images_for_comfy(
retrieved_image,
self.placeholder_image_path,
response_key=None,
field_name=None
)
mask_tensor = default_mask_tensor
if retrieved_mask is not None:
mask_tensor = process_mask(retrieved_mask, image_tensor)
return (
prompt,
combined_prompt,
negative_prompt,
omni,
image_tensor,
mask_tensor,
)
except Exception as e:
logger.error(f"Exception in image processing: {str(e)}", exc_info=True)
placeholder_image, placeholder_mask = load_placeholder_image(self.placeholder_image_path)
return (
prompt,
f"Error: {str(e)}",
"",
None,
placeholder_image,
placeholder_mask
)
except Exception as e:
logger.error(f"Exception occurred in process_chat: {str(e)}", exc_info=True)
placeholder_image, placeholder_mask = load_placeholder_image(self.placeholder_image_path)
return (
prompt,
f"Error: {str(e)}",
"",
None,
placeholder_image,
placeholder_mask
)
async def generate_response(
self,
enable_RAG,
query_type,
prompt,
preset,
llm_provider,
base_ip,
port,
images,
llm_model,
system_message_str,
messages,
temperature,
max_tokens,
random,
top_k,
top_p,
repeat_penalty,
stop,
seed,
keep_alive,
llm_api_key,
task=None,
fill_mask=False,
output_mask_select="",
precision="fp16",
attention="sdpa",
):
response_strategies = {
"graphrag": self.graphrag_app.query,
"colpali": self.colpali_app.query,
"florence": self.florence_app.run_florence,
"normal": lambda: send_request(
llm_provider=llm_provider,
base_ip=base_ip,
port=port,
images=images,
llm_model=llm_model,
system_message=system_message_str,
user_message=prompt,
messages=messages,
seed=seed,
temperature=temperature,
max_tokens=max_tokens,
random=random,
top_k=top_k,
top_p=top_p,
repeat_penalty=repeat_penalty,
stop=stop,
keep_alive=keep_alive,
llm_api_key=llm_api_key,
tools=None,
tool_choice=None,
precision=precision,
attention=attention
),
}
florence_tasks = list(self.florence_prompts.keys())
if enable_RAG:
if query_type == "colpali" or query_type == "colpali-v1.2" or query_type == "colqwen2":
strategy = "colpali"
else: # For "global", "local", and "naive" query types
strategy = "graphrag"
elif task and task.lower() != 'none' and task in florence_tasks:
strategy = "florence"
else:
strategy = "normal"
print(f"Strategy: {strategy}")
try:
if strategy == "colpali":
# Ensure the model is loaded before querying
if self.cached_colpali_model is None:
self.cached_colpali_model = self.colpali_app.get_colpali_model(query_type)
response = await response_strategies[strategy](prompt=prompt, query_type=query_type, system_message_str=system_message_str)
return response
elif strategy == "graphrag":
response = await response_strategies[strategy](prompt=prompt, query_type=query_type, preset=preset)
return {
"Question": prompt,
"Response": response[0],
"Negative": "",
"Tool_Output": response[1],
"Retrieved_Image": None,
"Mask": None
}
elif strategy == "florence":
task_content = self.florence_prompts.get(task, "")
response = await response_strategies[strategy](
images=images,
task=task,
task_prompt=task_content,
llm_model=llm_model,
precision=precision,
attention=attention,
fill_mask=fill_mask,
output_mask_select=output_mask_select,
keep_alive=keep_alive,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
top_k=top_k,
repetition_penalty=repeat_penalty,
seed=seed,
text_input=prompt,
)
print("Florence response:", response)
return response
else:
response = await response_strategies[strategy]()
print("Normal response:", response)
return {
"Question": prompt,
"Response": response,
"Negative": "",
"Tool_Output": None,
"Retrieved_Image": None,
"Mask": None
}
except Exception as e:
logger.error(f"Error processing strategy: {strategy}")
return {
"Question": prompt,
"Response": f"Error processing task: {str(e)}",
"Negative": "",
"Tool_Output": {"error": str(e)},
"Retrieved_Image": None,
"Mask": None
}
def process_chat_wrapper(self, *args, **kwargs):
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logger.debug(f"process_chat_wrapper kwargs: {kwargs}")
logger.debug(f"External LLM API Key: {kwargs.get('external_api_key', 'Not provided')}")
return loop.run_until_complete(self.process_chat(*args, **kwargs))
@classmethod