Motivation: LLM API calls frequently failed with HTTP 429 and 50x errors, crashing the workflow. Code redundancy existed between different LLM nodes. Non-standard payload fields caused 400 errors with some providers. Implementation: 1. Extracted api_client.py for centralized HTTP handling with smart retries (Retry-After, backoff, jitter) and structured error logging. 2. Implemented graceful degradation to return error text instead of raising exceptions. 3. Cleaned up non-standard OpenAI payload fields. 4. Added input validation to fail fast on empty prompts or API keys. 5. Refactored both openai_compatible.py and llm_translator.py to reuse LLMClient. 6. Handled dual import registration in __init__.py for robust dynamic loading.
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""
|
|
ComfyUI-LLMs-Toolkit: Lightweight LLM API integration for ComfyUI.
|
|
|
|
Auto-registration mechanism for all nodes.
|
|
"""
|
|
from pathlib import Path
|
|
import importlib.util
|
|
import sys
|
|
|
|
# Initialize mappings
|
|
NODE_CLASS_MAPPINGS = {}
|
|
NODE_DISPLAY_NAME_MAPPINGS = {}
|
|
|
|
# Auto-load all nodes from nodes/ directory
|
|
nodes_dir = Path(__file__).parent / "nodes"
|
|
|
|
if nodes_dir.exists():
|
|
for py_file in sorted(nodes_dir.glob("*.py")):
|
|
if py_file.stem.startswith("_"):
|
|
continue
|
|
|
|
try:
|
|
# Load module
|
|
module_name = f"ComfyUI-LLMs-Toolkit.nodes.{py_file.stem}"
|
|
spec = importlib.util.spec_from_file_location(module_name, py_file)
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[module_name] = module
|
|
# Also register under simple name for inter-module imports
|
|
sys.modules[py_file.stem] = module
|
|
spec.loader.exec_module(module)
|
|
|
|
# Register node mappings
|
|
if hasattr(module, "NODE_CLASS_MAPPINGS"):
|
|
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
|
|
|
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
|
|
NODE_DISPLAY_NAME_MAPPINGS.update(module.NODE_DISPLAY_NAME_MAPPINGS)
|
|
|
|
except Exception as e:
|
|
print(f"[LLMs_Toolkit] Failed to load {py_file.stem}: {e}")
|
|
|
|
WEB_DIRECTORY = "./web"
|
|
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
|