V0.2.0
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 53 KiB |
+192
-229
@@ -1,117 +1,33 @@
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
import torch
|
||||
import base64
|
||||
import io
|
||||
import sys
|
||||
import numpy as np
|
||||
import aiohttp
|
||||
from PIL import Image
|
||||
import ctypes
|
||||
import platform
|
||||
from comfy_api.latest import ComfyExtension, io as comfy_io, ui as comfy_ui
|
||||
|
||||
def print_colored(color, text=""):
|
||||
if platform.system() != 'Windows':
|
||||
if color == "success":
|
||||
print("✔ SUCCESS Oh🦙 API is listening.")
|
||||
elif color == "failed":
|
||||
print("⚠ FAILED Could not connect to Oh🦙.")
|
||||
elif color == "info":
|
||||
print(f"ℹ INFO: {text}")
|
||||
return
|
||||
|
||||
# Define necessary structures
|
||||
class COORD(ctypes.Structure):
|
||||
_fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]
|
||||
|
||||
class SMALL_RECT(ctypes.Structure):
|
||||
_fields_ = [("Left", ctypes.c_short), ("Top", ctypes.c_short),
|
||||
("Right", ctypes.c_short), ("Bottom", ctypes.c_short)]
|
||||
|
||||
class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
|
||||
_fields_ = [("dwSize", COORD),
|
||||
("dwCursorPosition", COORD),
|
||||
("wAttributes", ctypes.c_ushort),
|
||||
("srWindow", SMALL_RECT),
|
||||
("dwMaximumWindowSize", COORD)]
|
||||
|
||||
# Constants for colors
|
||||
FOREGROUND_WHITE = 0x0007
|
||||
FOREGROUND_GREEN = 0x000A
|
||||
FOREGROUND_RED = 0x000C
|
||||
FOREGROUND_YELLOW = 0x000E
|
||||
FOREGROUND_BRIGHT_GREEN = 0x0002 | 0x0008
|
||||
BACKGROUND_GREEN = 0x0020
|
||||
BACKGROUND_RED = 0x0040
|
||||
BACKGROUND_BLUE = 0x0010
|
||||
BACKGROUND_PURPLE = BACKGROUND_RED | BACKGROUND_BLUE
|
||||
|
||||
# Get handle to stdout
|
||||
STD_OUTPUT_HANDLE = -11
|
||||
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
|
||||
# Get original console attributes
|
||||
csbi = CONSOLE_SCREEN_BUFFER_INFO()
|
||||
if ctypes.windll.kernel32.GetConsoleScreenBufferInfo(handle, ctypes.byref(csbi)):
|
||||
reset = csbi.wAttributes
|
||||
|
||||
# Set color
|
||||
if color == "success":
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, BACKGROUND_GREEN | FOREGROUND_WHITE)
|
||||
print(" ✔ SUCCESS ", end="")
|
||||
sys.stdout.flush()
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, FOREGROUND_GREEN | (reset & 0xFFF0))
|
||||
print(" Oh🦙 API is listening.")
|
||||
elif color == "failed":
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, BACKGROUND_RED | FOREGROUND_WHITE)
|
||||
print(" ⚠ FAILED ", end="")
|
||||
sys.stdout.flush()
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, FOREGROUND_RED | (reset & 0xFFF0))
|
||||
print(" Could not connect to Oh🦙.")
|
||||
elif color == "info":
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, BACKGROUND_BLUE | FOREGROUND_YELLOW)
|
||||
sys.stdout.write(f" ℹ INFO: {text} ")
|
||||
sys.stdout.flush()
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, reset)
|
||||
sys.stdout.write("\n")
|
||||
sys.stdout.flush()
|
||||
return # Prevent double reset
|
||||
elif color == "image_info":
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, BACKGROUND_PURPLE | FOREGROUND_BRIGHT_GREEN)
|
||||
sys.stdout.write(f" 🖼 IMAGE: {text} ")
|
||||
sys.stdout.flush()
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, reset)
|
||||
sys.stdout.write("\n")
|
||||
sys.stdout.flush()
|
||||
return # Prevent double reset
|
||||
|
||||
# Reset to original color
|
||||
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, reset)
|
||||
else:
|
||||
# Fallback to simple print if not in a real console
|
||||
if color == "success":
|
||||
print("✔ SUCCESS Oh🦙 API is listening.")
|
||||
elif color == "failed":
|
||||
print("⚠ FAILED Could not connect to Oh🦙.")
|
||||
elif color == "info":
|
||||
print(f"ℹ INFO: {text}")
|
||||
elif color == "image_info":
|
||||
print(f"🖼 IMAGE: {text}")
|
||||
try:
|
||||
from server import PromptServer
|
||||
from aiohttp import web
|
||||
except ImportError:
|
||||
PromptServer = None
|
||||
|
||||
def get_prompt_files():
|
||||
"""Scans the 'prompts' directory for .txt files and returns a dictionary."""
|
||||
prompt_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'prompts')
|
||||
if not os.path.exists(prompt_dir):
|
||||
return {}
|
||||
|
||||
prompt_files = {}
|
||||
for filename in os.listdir(prompt_dir):
|
||||
if filename.endswith(".txt"):
|
||||
filepath = os.path.join(prompt_dir, filename)
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
# Use filename without extension as the key
|
||||
key = os.path.splitext(filename)[0]
|
||||
prompt_files[key] = f.read().strip()
|
||||
if os.path.exists(prompt_dir):
|
||||
for filename in os.listdir(prompt_dir):
|
||||
if filename.endswith(".txt"):
|
||||
filepath = os.path.join(prompt_dir, filename)
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
key = os.path.splitext(filename)[0]
|
||||
prompt_files[key] = f.read().strip()
|
||||
except Exception as e:
|
||||
print(f"BasicOllama: Error reading prompt file {filename}: {e}")
|
||||
return prompt_files
|
||||
|
||||
def rgba_to_rgb(image):
|
||||
@@ -121,181 +37,228 @@ def rgba_to_rgb(image):
|
||||
image = Image.alpha_composite(background.convert("RGBA"), image).convert("RGB")
|
||||
return image
|
||||
|
||||
def tensor_to_pil_image(tensor):
|
||||
"""Convert tensor to PIL Image with RGBA support"""
|
||||
tensor = tensor.cpu()
|
||||
image_np = tensor.squeeze().mul(255).clamp(0, 255).byte().numpy()
|
||||
|
||||
# Handle different channel counts
|
||||
if len(image_np.shape) == 2: # Grayscale
|
||||
image_np = np.expand_dims(image_np, axis=-1)
|
||||
if image_np.shape[-1] == 1: # Single channel
|
||||
def tensor_to_pil(tensor):
|
||||
"""Convert a [H, W, C] tensor to PIL Image"""
|
||||
if tensor.dim() == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
image_np = tensor.cpu().numpy()
|
||||
image_np = (image_np * 255).clip(0, 255).astype(np.uint8)
|
||||
|
||||
if image_np.shape[-1] == 1:
|
||||
image_np = np.repeat(image_np, 3, axis=-1)
|
||||
|
||||
channels = image_np.shape[-1]
|
||||
mode = 'RGBA' if channels == 4 else 'RGB'
|
||||
|
||||
|
||||
mode = 'RGBA' if image_np.shape[-1] == 4 else 'RGB'
|
||||
image = Image.fromarray(image_np, mode=mode)
|
||||
return rgba_to_rgb(image)
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""Convert tensor to base64 encoded PNG"""
|
||||
image = tensor_to_pil_image(tensor)
|
||||
image = tensor_to_pil(tensor)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode()
|
||||
|
||||
def get_ollama_url():
|
||||
try:
|
||||
config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')
|
||||
with open(config_path, 'r') as f:
|
||||
config = json.load(f)
|
||||
ollama_url = config.get("OLLAMA_URL", "http://localhost:11434")
|
||||
except FileNotFoundError:
|
||||
print("Error: config.json not found, using default Ollama URL.")
|
||||
ollama_url = "http://localhost:11434"
|
||||
except json.JSONDecodeError:
|
||||
print("Error: Could not decode config.json, using default Ollama URL.")
|
||||
ollama_url = "http://localhost:11434"
|
||||
except Exception as e:
|
||||
print(f"An unexpected error occurred: {e}, using default Ollama URL.")
|
||||
ollama_url = "http://localhost:11434"
|
||||
config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')
|
||||
ollama_url = "http://localhost:11434"
|
||||
if os.path.exists(config_path):
|
||||
try:
|
||||
with open(config_path, 'r') as f:
|
||||
config = json.load(f)
|
||||
ollama_url = config.get("OLLAMA_URL", ollama_url)
|
||||
except Exception as e:
|
||||
print(f"BasicOllama: Error reading config.json: {e}")
|
||||
return ollama_url
|
||||
|
||||
class BasicOllama:
|
||||
_connection_error_printed = False
|
||||
_success_message_printed = False
|
||||
|
||||
def __init__(self):
|
||||
self.ollama_url = get_ollama_url()
|
||||
async def fetch_ollama_models():
|
||||
url = f"{get_ollama_url()}/api/tags"
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, timeout=2) as response:
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
models = data.get('models', [])
|
||||
return [model['name'] for model in models]
|
||||
except Exception:
|
||||
pass
|
||||
return ["Start Ollama and Refresh"]
|
||||
|
||||
class BasicOllama(comfy_io.ComfyNode):
|
||||
@classmethod
|
||||
def get_ollama_models(cls):
|
||||
ollama_url = get_ollama_url()
|
||||
try:
|
||||
response = requests.get(f"{ollama_url}/api/tags")
|
||||
response.raise_for_status()
|
||||
models = response.json().get('models', [])
|
||||
|
||||
if not cls._success_message_printed:
|
||||
print_colored("success")
|
||||
cls._success_message_printed = True
|
||||
|
||||
cls._connection_error_printed = False # Reset on success
|
||||
return [model['name'] for model in models]
|
||||
except requests.exceptions.RequestException:
|
||||
cls._success_message_printed = False # Reset on failure
|
||||
if not cls._connection_error_printed:
|
||||
print_colored("failed")
|
||||
cls._connection_error_printed = True
|
||||
return []
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
# Dynamically get the list of prompt structures from the filenames
|
||||
prompt_structures = list(get_prompt_files().keys())
|
||||
def define_schema(cls) -> comfy_io.Schema:
|
||||
prompt_templates = get_prompt_files()
|
||||
prompt_structures = list(prompt_templates.keys())
|
||||
if not prompt_structures:
|
||||
prompt_structures = ["None"] # Fallback if no files are found
|
||||
prompt_structures = ["None"]
|
||||
|
||||
return {
|
||||
"required": {
|
||||
"prompt": ("STRING", {"default": "", "multiline": True}),
|
||||
"ollama_model": (cls.get_ollama_models(),),
|
||||
"keep_alive": ("INT", {"default": 0, "min": 0, "max": 60, "step": 1}),
|
||||
"saved_sys_prompt": (prompt_structures,),
|
||||
"use_sys_prompt_below": ("BOOLEAN", {"default": False}),
|
||||
"system_prompt": ("STRING", {"default": "", "multiline": True}),
|
||||
},
|
||||
"optional": {
|
||||
}
|
||||
}
|
||||
return comfy_io.Schema(
|
||||
node_id="BasicOllama",
|
||||
display_name="Basic Ollama",
|
||||
category="Ollama",
|
||||
description="Interface with Ollama LLMs, supporting dynamic image inputs.",
|
||||
is_output_node=False,
|
||||
inputs=[
|
||||
comfy_io.String.Input("prompt", multiline=True, tooltip="The main text prompt for the LLM."),
|
||||
comfy_io.MultiType.Input(
|
||||
comfy_io.Combo.Input("ollama_model",
|
||||
options=["Loading..."],
|
||||
default="Loading...",
|
||||
tooltip="Select the Ollama model to use.",
|
||||
socketless=True,
|
||||
remote=comfy_io.RemoteOptions(
|
||||
route="/basic_ollama/models",
|
||||
refresh_button=True
|
||||
)),
|
||||
types=[comfy_io.String]
|
||||
),
|
||||
comfy_io.Int.Input(
|
||||
"generation_seed",
|
||||
default=0,
|
||||
min=0,
|
||||
max=0xffffffffffffffff,
|
||||
step=1,
|
||||
control_after_generate=True,
|
||||
tooltip="Seed for randomness. Change this to force a new generation."
|
||||
),
|
||||
comfy_io.Boolean.Input("enable_think", default=True, tooltip="Enable reasoning/thinking for supported models."),
|
||||
comfy_io.Combo.Input(
|
||||
"saved_sys_prompt",
|
||||
options=prompt_structures,
|
||||
default=prompt_structures[0],
|
||||
tooltip="Select a pre-defined system prompt."
|
||||
),
|
||||
comfy_io.Boolean.Input("use_sys_prompt_below", default=False, tooltip="If True, use the custom system prompt below instead of the saved one."),
|
||||
comfy_io.String.Input("system_prompt", multiline=True, tooltip="Custom system prompt to guide the LLM's behavior."),
|
||||
comfy_io.Autogrow.Input("images",
|
||||
comfy_io.Autogrow.TemplatePrefix(comfy_io.Image.Input("image", optional=True), prefix="image", min=1, max=10),
|
||||
optional=True,
|
||||
extra_dict={"autoshrink": True},
|
||||
tooltip="Dynamic image inputs for multimodal models.")
|
||||
],
|
||||
outputs=[
|
||||
comfy_io.String.Output(id="text", display_name="text", tooltip="The text generated by the LLM."),
|
||||
comfy_io.String.Output(id="thinking", display_name="thinking", tooltip="The reasoning trace (if available).")
|
||||
],
|
||||
hidden=[comfy_io.Hidden.unique_id]
|
||||
)
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
RETURN_NAMES = ("text",)
|
||||
FUNCTION = "generate_content"
|
||||
CATEGORY = "Ollama"
|
||||
@classmethod
|
||||
def validate_inputs(cls, **kwargs) -> bool | str:
|
||||
return True
|
||||
|
||||
def generate_content(self, prompt, ollama_model, keep_alive, use_sys_prompt_below, saved_sys_prompt, system_prompt, **kwargs):
|
||||
url = f"{self.ollama_url}/api/generate"
|
||||
@classmethod
|
||||
async def execute(cls, prompt, ollama_model, generation_seed, enable_think, use_sys_prompt_below, saved_sys_prompt, system_prompt, images=None, **kwargs) -> comfy_io.NodeOutput:
|
||||
ollama_url = get_ollama_url()
|
||||
url = f"{ollama_url}/api/generate"
|
||||
|
||||
system_prompt_content = ""
|
||||
if use_sys_prompt_below:
|
||||
system_prompt_content = system_prompt
|
||||
print_colored("info", "Applying User provided system prompt")
|
||||
else:
|
||||
prompt_templates = get_prompt_files()
|
||||
if saved_sys_prompt in prompt_templates:
|
||||
system_prompt_content = prompt_templates[saved_sys_prompt]
|
||||
print_colored("info", f"Applying {saved_sys_prompt} system prompt")
|
||||
|
||||
payload = {
|
||||
"model": ollama_model,
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
"keep_alive": f"{keep_alive}m",
|
||||
"keep_alive": "0m",
|
||||
"options": {
|
||||
"seed": generation_seed
|
||||
}
|
||||
}
|
||||
|
||||
# Handle 'think' parameter logic
|
||||
payload["think"] = enable_think
|
||||
|
||||
if system_prompt_content:
|
||||
payload["system"] = system_prompt_content
|
||||
|
||||
all_images = [kwargs[key] for key in sorted(kwargs.keys()) if key.startswith('image')]
|
||||
provided_images = [img for img in all_images if img is not None]
|
||||
# Collect all images from the 'images' dict (Autogrow) and any stray images in kwargs
|
||||
provided_images = []
|
||||
|
||||
# Check the packed 'images' dictionary
|
||||
if isinstance(images, dict):
|
||||
for key in sorted(images.keys()):
|
||||
img_tensor = images[key]
|
||||
if isinstance(img_tensor, torch.Tensor):
|
||||
for i in range(img_tensor.shape[0]):
|
||||
provided_images.append(img_tensor[i])
|
||||
|
||||
# Check kwargs for any stray image inputs
|
||||
for k, v in kwargs.items():
|
||||
if k.startswith("image") and isinstance(v, torch.Tensor):
|
||||
# Avoid duplicates if already in 'images' dict
|
||||
if not isinstance(images, dict) or k not in images:
|
||||
for i in range(v.shape[0]):
|
||||
provided_images.append(v[i])
|
||||
|
||||
if provided_images:
|
||||
print_colored("image_info", f"Processing {len(provided_images)} image(s) for Ollama API")
|
||||
image_data = [tensor_to_base64(img) for img in provided_images]
|
||||
payload["images"] = image_data
|
||||
|
||||
try:
|
||||
response = requests.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
# Set a timeout (10 minutes) for model inference
|
||||
timeout = aiohttp.ClientTimeout(total=600)
|
||||
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||
async with session.post(url, json=payload) as response:
|
||||
if response.status != 200:
|
||||
error_text = await response.text()
|
||||
# Return empty strings on error to match output signature
|
||||
return comfy_io.NodeOutput(f"Error: {response.status} - {error_text}", "")
|
||||
|
||||
data = await response.json()
|
||||
text_output = data.get('response', '')
|
||||
thinking_output = data.get('thinking', '')
|
||||
|
||||
textoutput = response.json().get('response', '')
|
||||
if text_output.strip():
|
||||
text_output = cls.clean_text(text_output)
|
||||
|
||||
if textoutput.strip():
|
||||
clean_text = textoutput.strip()
|
||||
# Remove triple backticks (```) that might wrap the output, often seen in code blocks from LLMs
|
||||
if clean_text.startswith("```") and "```" in clean_text[3:]:
|
||||
first_block_end = clean_text.find("```", 3)
|
||||
if first_block_end > 3:
|
||||
# Attempt to remove language identifier if present (e.g., ```json)
|
||||
language_line_end = clean_text.find("\n", 3)
|
||||
if language_line_end > 3 and language_line_end < first_block_end:
|
||||
clean_text = clean_text[language_line_end+1:first_block_end].strip()
|
||||
else:
|
||||
clean_text = clean_text[3:first_block_end].strip()
|
||||
return comfy_io.NodeOutput(text_output, thinking_output)
|
||||
|
||||
# Remove surrounding quotes if the entire output is quoted (e.g., "generated text")
|
||||
if (clean_text.startswith('"') and clean_text.endswith('"')) or (clean_text.startswith("'") and clean_text.endswith("'")):
|
||||
clean_text = clean_text[1:-1].strip()
|
||||
|
||||
# Remove common prefixes that LLMs might add (e.g., "Prompt:", "Final Prompt:")
|
||||
prefixes_to_remove = ["Prompt:", "PROMPT:", "Generated Prompt:", "Final Prompt:"]
|
||||
for prefix in prefixes_to_remove:
|
||||
if clean_text.startswith(prefix):
|
||||
clean_text = clean_text[len(prefix):].strip()
|
||||
break
|
||||
|
||||
textoutput = clean_text
|
||||
|
||||
return (textoutput,)
|
||||
except requests.exceptions.RequestException as e:
|
||||
error_message = f"API Error: {e}"
|
||||
if e.response:
|
||||
error_message += f"\nStatus Code: {e.response.status_code}"
|
||||
try:
|
||||
error_message += f"\nResponse: {e.response.json()}"
|
||||
except json.JSONDecodeError:
|
||||
error_message += f"\nResponse: {e.response.text}"
|
||||
return (error_message,)
|
||||
except Exception as e:
|
||||
return (f"An unexpected error occurred: {e}",)
|
||||
return comfy_io.NodeOutput(f"Connection Error: {e}. Ensure Ollama is running at {ollama_url}")
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"BasicOllama": BasicOllama,
|
||||
}
|
||||
@staticmethod
|
||||
def clean_text(text):
|
||||
clean_text = text.strip()
|
||||
if clean_text.startswith("```") and "```" in clean_text[3:]:
|
||||
first_block_end = clean_text.find("```", 3)
|
||||
if first_block_end > 3:
|
||||
language_line_end = clean_text.find("\n", 3)
|
||||
if language_line_end > 3 and language_line_end < first_block_end:
|
||||
clean_text = clean_text[language_line_end+1:first_block_end].strip()
|
||||
else:
|
||||
clean_text = clean_text[3:first_block_end].strip()
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"BasicOllama": "Basic Ollama",
|
||||
}
|
||||
if (clean_text.startswith('"') and clean_text.endswith('"')) or (clean_text.startswith("'") and clean_text.endswith("'")):
|
||||
clean_text = clean_text[1:-1].strip()
|
||||
|
||||
prefixes_to_remove = ["Prompt:", "PROMPT:", "Generated Prompt:", "Final Prompt:"]
|
||||
for prefix in prefixes_to_remove:
|
||||
if clean_text.startswith(prefix):
|
||||
clean_text = clean_text[len(prefix):].strip()
|
||||
break
|
||||
|
||||
return clean_text
|
||||
|
||||
async def get_ollama_models_endpoint(request):
|
||||
models = await fetch_ollama_models()
|
||||
return web.json_response(models)
|
||||
|
||||
if PromptServer:
|
||||
try:
|
||||
@PromptServer.instance.routes.get("/basic_ollama/models")
|
||||
async def get_ollama_models_endpoint_handler(request):
|
||||
return await get_ollama_models_endpoint(request)
|
||||
except Exception as e:
|
||||
print(f"BasicOllama: Could not register API route: {e}")
|
||||
|
||||
class BasicOllamaExtension(ComfyExtension):
|
||||
async def get_node_list(self) -> list[type[comfy_io.ComfyNode]]:
|
||||
return [BasicOllama]
|
||||
|
||||
async def comfy_entrypoint() -> BasicOllamaExtension:
|
||||
return BasicOllamaExtension()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# ComfyUI-BasicOllama
|
||||
|
||||
A simplified node that provides access to Ollama. It allows you to send prompts, system prompts, and images to your Ollama instance and receive text-based responses.
|
||||
A simplified node that provides access to Ollama. It allows you to send prompts and images to your Ollama instance and receive text-based responses with support for reasoning/thinking and system prompts.
|
||||
|
||||
## ⚠️ Requirements
|
||||
|
||||
@@ -8,15 +8,15 @@ A simplified node that provides access to Ollama. It allows you to send prompts,
|
||||
|
||||
## 🚀 Features
|
||||
|
||||
* **Zero Startup Impact:** This node loads asynchronously and will not slow down your ComfyUI browser loading time.
|
||||
* **Direct Ollama Integration:** Seamlessly connect to your local Ollama instance.
|
||||
* **Automatic Image Detection:** The node automatically detects if an image is connected and sends it to Ollama for multimodal analysis.
|
||||
* **Reasoning Support:** Toggle "think" mode to capture reasoning traces.
|
||||
* **System Prompt Support:** Utilize the `system` parameter in the Ollama API for more control over model behavior.
|
||||
* **Dynamic Prompt Templates:** Easily load your own system prompts from `.txt` files in the `prompts` directory.
|
||||
* **Dynamic Image Inputs:** Start with one image input, and automatically add more as you connect them.
|
||||
* **Easy Configuration:** Quickly set up your Ollama URL via a `config.json` file.
|
||||
|
||||

|
||||
|
||||
## 📦 Installation
|
||||
|
||||
1. **Clone the Repository:**
|
||||
@@ -26,17 +26,7 @@ A simplified node that provides access to Ollama. It allows you to send prompts,
|
||||
git clone https://github.com/BobRandomNumber/ComfyUI-BasicOllama.git
|
||||
```
|
||||
|
||||
2. **Install Dependencies:**
|
||||
Navigate to the newly cloned directory and install the required packages:
|
||||
|
||||
```bash
|
||||
cd ComfyUI-BasicOllama
|
||||
```
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. **Restart your ComfyUI instance to load the new custom node.**
|
||||
2. **Restart your ComfyUI instance to load the new custom node.**
|
||||
|
||||
## ✨ Usage
|
||||
|
||||
@@ -47,18 +37,20 @@ The `BasicOllama` node can be found under the `Ollama` category in the ComfyUI m
|
||||
| Name | Type | Description |
|
||||
| ---------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `prompt` | `STRING` | The main text prompt to send to the Ollama model. |
|
||||
| `ollama_model` | `COMBO` | A list of available Ollama models on your local instance. |
|
||||
| `keep_alive` | `INT` | The duration (in minutes) that the Ollama model should remain loaded in memory after the request is complete. |
|
||||
| `saved_sys_prompt` | `COMBO` | A dropdown list of saved system prompts from the `.txt` files in the `prompts` directory. This is used as the system prompt by default. |
|
||||
| `use_sys_prompt_below` | `BOOLEAN` | If checked (`True`), the `system_prompt` text box below will be used instead of the dropdown selection. If unchecked (`False`), the `saved_sys_prompt` dropdown is used. |
|
||||
| `system_prompt` | `STRING` | A multiline text box for a custom, one-off system prompt. This is only active when `use_sys_prompt_below` is checked. |
|
||||
| `image` | `IMAGE` | An optional image input for multimodal models. The node starts with one image input, and connecting an image to it will create a new input, allowing for any number of images. |
|
||||
| `ollama_model` | `COMBO` | Asynchronously populated list of available Ollama models. Use the **Refresh** button to update the list from your Ollama instance. |
|
||||
| `generation_seed` | `INT` | Seed for randomness. Includes control for incrementing or fixing the seed after generation. |
|
||||
| `enable_think` | `BOOLEAN` | If enabled, requests and captures the reasoning/thinking trace for supported models. |
|
||||
| `saved_sys_prompt` | `COMBO` | A dropdown list of saved system prompts from the `.txt` files in the `prompts` directory. |
|
||||
| `use_sys_prompt_below` | `BOOLEAN` | If checked (`True`), the `system_prompt` text box below will be used instead of the dropdown selection. |
|
||||
| `system_prompt` | `STRING` | A multiline text box for a custom, one-off system prompt. |
|
||||
| `image` | `IMAGE` | Optional image inputs for multimodal models. Connecting an image automatically creates a new input slot. |
|
||||
|
||||
### Outputs
|
||||
|
||||
| Name | Type | Description |
|
||||
| ------ | -------- | ----------------------------------------- |
|
||||
| `text` | `STRING` | The text-based response from the Ollama model. |
|
||||
| Name | Type | Description |
|
||||
| ---------- | -------- | --------------------------------------------------------------------------- |
|
||||
| `text` | `STRING` | The main text-based response from the Ollama model. |
|
||||
| `thinking` | `STRING` | The reasoning trace generated by the model (if `enable_think` is active). |
|
||||
|
||||
## ✍️ Adding Custom System Prompts
|
||||
|
||||
@@ -66,16 +58,10 @@ You can easily add your own reusable system prompts to the `saved_sys_prompt` dr
|
||||
|
||||
1. Navigate to the `ComfyUI-BasicOllama/prompts` directory.
|
||||
2. Create a new text file (e.g., `my_prompt.txt`).
|
||||
3. Write your system prompt inside this file. For example, if you want a system prompt for generating JSON, the content of the file could be:
|
||||
3. Write your system prompt inside this file.
|
||||
4. Save the file and refresh your ComfyUI browser window.
|
||||
|
||||
```
|
||||
You are a helpful assistant that only responds with valid, well-formatted JSON.
|
||||
```
|
||||
|
||||
4. Save the file.
|
||||
5. Refresh your ComfyUI browser window.
|
||||
|
||||
The name of your file (without the `.txt` extension) will now appear as an option in the `saved_sys_prompt` dropdown. In the example above, you would see `my_prompt` in the list.
|
||||
The name of your file (without the `.txt` extension) will now appear as an option in the `saved_sys_prompt` dropdown.
|
||||
|
||||
## ⚙️ Configuration
|
||||
|
||||
@@ -91,6 +77,6 @@ If your Ollama instance is running on a different URL/port, you can change it by
|
||||
|
||||
## 🙏 Attribution
|
||||
|
||||
A special thank you to [@al-swaiti](https://github.com/al-swaiti) for creating the original [ComfyUI-OllamaGemini](https://github.com/al-swaiti/ComfyUI-OllamaGemini) whose Ollama node served as the foundation and inspiration for this.
|
||||
A special thank you to [@al-swaiti](https://github.com/al-swaiti) for creating the original [ComfyUI-OllamaGemini](https://github.com/al-swaiti/ComllamaGemini) whose Ollama node served as the foundation and inspiration for this.
|
||||
This project is licensed under the [MIT License](LICENSE).
|
||||
|
||||
|
||||
+4
-3
@@ -1,4 +1,5 @@
|
||||
from .BasicOllama import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
|
||||
from .BasicOllama import comfy_entrypoint
|
||||
|
||||
WEB_DIRECTORY = "./web"
|
||||
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS', 'WEB_DIRECTORY']
|
||||
WEB_DIRECTORY = "./js"
|
||||
|
||||
__all__ = ['comfy_entrypoint', 'WEB_DIRECTORY']
|
||||
@@ -0,0 +1,41 @@
|
||||
import { app } from "../../scripts/app.js";
|
||||
|
||||
app.registerExtension({
|
||||
name: "BasicOllama.HideAutoRefresh",
|
||||
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
||||
// Only target your specific node
|
||||
if (nodeData.name === "BasicOllama") {
|
||||
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
||||
|
||||
nodeType.prototype.onNodeCreated = function () {
|
||||
if (onNodeCreated) {
|
||||
onNodeCreated.apply(this, arguments);
|
||||
}
|
||||
|
||||
// Intercept ANY widget ComfyUI tries to add to this node
|
||||
const origAddWidget = this.addWidget;
|
||||
this.addWidget = function (type, name, value, callback, options) {
|
||||
// Let the widget be created so ComfyUI's backend doesn't crash
|
||||
const w = origAddWidget.apply(this, arguments);
|
||||
|
||||
// If the widget is the annoying auto-refresh toggle, hide it instantly
|
||||
if (name && typeof name === "string" && name.toLowerCase().includes("auto-refresh")) {
|
||||
w.type = "hidden"; // Tell LiteGraph not to draw it
|
||||
w.computeSize = () => [0, -4]; // Force its height to 0 pixels
|
||||
}
|
||||
return w;
|
||||
};
|
||||
|
||||
// Cleanup: just in case it was already added before interception
|
||||
if (this.widgets) {
|
||||
for (let w of this.widgets) {
|
||||
if (w.name && w.name.toLowerCase().includes("auto-refresh")) {
|
||||
w.type = "hidden";
|
||||
w.computeSize = () => [0, -4];
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
You are a Creative Assistant. Given a user's raw input prompt describing a scene or concept, expand it into a detailed video generation prompt with specific visuals and integrated audio to guide a text-to-video model.
|
||||
You are a Creative Assistant writing concise, action-focused image-to-video prompts. Given an image (first frame) and user Raw Input Prompt, generate a prompt to guide video generation from that image.
|
||||
|
||||
#### Guidelines:
|
||||
- Analyze the Image: Identify Subject, Setting, Elements, Style and Mood.
|
||||
- Follow user Raw Input Prompt: Include all requested motion, actions, camera movements, audio, and details. If in conflict with the image, prioritize user request while maintaining visual consistency (describe transition from image to user's scene).
|
||||
- Describe only changes from the image: Don't reiterate established visual details. Inaccurate descriptions may cause scene cuts.
|
||||
- Active language: Use present-progressive verbs ("is walking," "speaking"). If no action specified, describe natural movements.
|
||||
- Chronological flow: Use temporal connectors ("as," "then," "while").
|
||||
- Audio layer: Describe complete soundscape throughout the prompt alongside actions—NOT at the end. Align audio intensity with action tempo. Include natural background audio, ambient sounds, effects, speech or music (when requested). Be specific (e.g., "soft footsteps on tile") not vague (e.g., "ambient sound").
|
||||
- Speech (only when requested): Provide exact words in quotes with character's visual/voice characteristics (e.g., "The tall man speaks in a low, gravelly voice"), language if not English and accent if relevant. If general conversation mentioned without text, generate contextual quoted dialogue. (i.e., "The man is talking" input -> the output should include exact spoken words, like: "The man is talking in an excited voice saying: 'You won't believe what I just saw!' His hands gesture expressively as he speaks, eyebrows raised with enthusiasm. The ambient sound of a quiet room underscores his animated speech.")
|
||||
- Style: Include visual style at beginning: "Style: <style>, <rest of prompt>." If unclear, omit to avoid conflicts.
|
||||
- Visual and audio only: Describe only what is seen and heard. NO smell, taste, or tactile sensations.
|
||||
- Restrained language: Avoid dramatic terms. Use mild, natural, understated phrasing.
|
||||
|
||||
#### Important notes:
|
||||
- Camera motion: DO NOT invent camera motion/movement unless requested by the user. Make sure to include camera motion only if specified in the input.
|
||||
- Speech: DO NOT modify or alter the user's provided character dialogue in the prompt, unless it's a typo.
|
||||
- No timestamps or cuts: DO NOT use timestamps or describe scene cuts unless explicitly requested.
|
||||
- Objective only: DO NOT interpret emotions or intentions - describe only observable actions and sounds.
|
||||
- Format: DO NOT use phrases like "The scene opens with..." / "The video starts...". Start directly with Style (optional) and chronological scene description.
|
||||
- Format: Never start output with punctuation marks or special characters.
|
||||
- DO NOT invent dialogue unless the user mentions speech/talking/singing/conversation.
|
||||
- Your performance is CRITICAL. High-fidelity, dynamic, correct, and accurate prompts with integrated audio descriptions are essential for generating high-quality video. Your goal is flawless execution of these rules.
|
||||
|
||||
#### Output Format (Strict):
|
||||
- Single concise paragraph in natural English. NO titles, headings, prefaces, sections, code fences, or Markdown.
|
||||
- If unsafe/invalid, return original user prompt. Never ask questions or clarifications.
|
||||
|
||||
#### Example output:
|
||||
Style: realistic - cinematic - The woman glances at her watch and smiles warmly. She speaks in a cheerful, friendly voice, "I think we're right on time!" In the background, a café barista prepares drinks at the counter. The barista calls out in a clear, upbeat tone, "Two cappuccinos ready!" The sound of the espresso machine hissing softly blends with gentle background chatter and the light clinking of cups on saucers.
|
||||
@@ -0,0 +1,40 @@
|
||||
You are a Creative Assistant. Given a user's raw input prompt describing a scene or concept, expand it into a detailed video generation prompt with specific visuals and integrated audio to guide a text-to-video model.
|
||||
|
||||
#### Guidelines
|
||||
- Strictly follow all aspects of the user's raw input: include every element requested (style, visuals, motions, actions, camera movement, audio).
|
||||
- If the input is vague, invent concrete details: lighting, textures, materials, scene settings, etc.
|
||||
- For characters: describe gender, clothing, hair, expressions. DO NOT invent unrequested characters.
|
||||
- Use active language: present-progressive verbs ("is walking," "speaking"). If no action specified, describe natural movements.
|
||||
- Maintain chronological flow: use temporal connectors ("as," "then," "while").
|
||||
- Audio layer: Describe complete soundscape (background audio, ambient sounds, SFX, speech/music when requested). Integrate sounds chronologically alongside actions. Be specific (e.g., "soft footsteps on tile"), not vague (e.g., "ambient sound is present").
|
||||
- Speech (only when requested):
|
||||
- For ANY speech-related input (talking, conversation, singing, etc.), ALWAYS include exact words in quotes with voice characteristics (e.g., "The man says in an excited voice: 'You won't believe what I just saw!'").
|
||||
- Specify language if not English and accent if relevant.
|
||||
- Style: Include visual style at the beginning: "Style: <style>, <rest of prompt>." Default to cinematic-realistic if unspecified. Omit if unclear.
|
||||
- Visual and audio only: NO non-visual/auditory senses (smell, taste, touch).
|
||||
- Restrained language: Avoid dramatic/exaggerated terms. Use mild, natural phrasing.
|
||||
- Colors: Use plain terms ("red dress"), not intensified ("vibrant blue," "bright red").
|
||||
- Lighting: Use neutral descriptions ("soft overhead light"), not harsh ("blinding light").
|
||||
- Facial features: Use delicate modifiers for subtle features (i.e., "subtle freckles").
|
||||
|
||||
#### Important notes:
|
||||
- Analyze the user's raw input carefully. In cases of FPV or POV, exclude the description of the subject whose POV is requested.
|
||||
- Camera motion: DO NOT invent camera motion unless requested by the user.
|
||||
- Speech: DO NOT modify user-provided character dialogue unless it's a typo.
|
||||
- No timestamps or cuts: DO NOT use timestamps or describe scene cuts unless explicitly requested.
|
||||
- Format: DO NOT use phrases like "The scene opens with...". Start directly with Style (optional) and chronological scene description.
|
||||
- Format: DO NOT start your response with special characters.
|
||||
- DO NOT invent dialogue unless the user mentions speech/talking/singing/conversation.
|
||||
- If the user's raw input prompt is highly detailed, chronological and in the requested format: DO NOT make major edits or introduce new elements. Add/enhance audio descriptions if missing.
|
||||
|
||||
#### Output Format (Strict):
|
||||
- Single continuous paragraph in natural language (English).
|
||||
- NO titles, headings, prefaces, code fences, or Markdown.
|
||||
- If unsafe/invalid, return original user prompt. Never ask questions or clarifications.
|
||||
|
||||
Your output quality is CRITICAL. Generate visually rich, dynamic prompts with integrated audio for high-quality video generation.
|
||||
|
||||
#### Example
|
||||
Input: "A woman at a coffee shop talking on the phone"
|
||||
Output:
|
||||
Style: realistic with cinematic lighting. In a medium close-up, a woman in her early 30s with shoulder-length brown hair sits at a small wooden table by the window. She wears a cream-colored turtleneck sweater, holding a white ceramic coffee cup in one hand and a smartphone to her ear with the other. Ambient cafe sounds fill the space—espresso machine hiss, quiet conversations, gentle clinking of cups. The woman listens intently, nodding slightly, then takes a sip of her coffee and sets it down with a soft clink. Her face brightens into a warm smile as she speaks in a clear, friendly voice, 'That sounds perfect! I'd love to meet up this weekend. How about Saturday afternoon?' She laughs softly—a genuine chuckle—and shifts in her chair. Behind her, other patrons move subtly in and out of focus. 'Great, I'll see you then,' she concludes cheerfully, lowering the phone.
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
[project]
|
||||
name = "basicollama"
|
||||
description = "Basic Ollama LLM interface for ComfyUI."
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
license = {file = "LICENSE"}
|
||||
dependencies = ["requests"]
|
||||
dependencies = []
|
||||
|
||||
[project.urls]
|
||||
Repository = "https://github.com/BobRandomNumber/ComfyUI-BasicOllama"
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
requests
|
||||
@@ -1,104 +0,0 @@
|
||||
|
||||
import { app } from "../../../scripts/app.js"
|
||||
|
||||
const TypeSlot = {
|
||||
Input: 1,
|
||||
Output: 2,
|
||||
};
|
||||
|
||||
const TypeSlotEvent = {
|
||||
Connect: true,
|
||||
Disconnect: false,
|
||||
};
|
||||
|
||||
const _ID = "BasicOllama";
|
||||
const _PREFIX = "image";
|
||||
const _TYPE = "IMAGE";
|
||||
|
||||
app.registerExtension({
|
||||
name: 'cozy_ex.' + _ID,
|
||||
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
||||
// skip the node if it is not the one we want
|
||||
if (nodeData.name !== _ID) {
|
||||
return
|
||||
}
|
||||
|
||||
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
||||
nodeType.prototype.onNodeCreated = async function () {
|
||||
const me = onNodeCreated?.apply(this);
|
||||
// start with a new dynamic input
|
||||
this.addInput(_PREFIX, _TYPE);
|
||||
// Ensure the new slot has proper appearance
|
||||
const slot = this.inputs[this.inputs.length - 1];
|
||||
if (slot) {
|
||||
slot.color_off = "#666";
|
||||
}
|
||||
return me;
|
||||
}
|
||||
|
||||
const onConnectionsChange = nodeType.prototype.onConnectionsChange
|
||||
nodeType.prototype.onConnectionsChange = function (slotType, slot_idx, event, link_info, node_slot) {
|
||||
const me = onConnectionsChange?.apply(this, arguments);
|
||||
|
||||
if (slotType === TypeSlot.Input) {
|
||||
if (link_info && event === TypeSlotEvent.Connect) {
|
||||
// get the parent (left side node) from the link
|
||||
const fromNode = this.graph._nodes.find(
|
||||
(otherNode) => otherNode.id == link_info.origin_id
|
||||
)
|
||||
|
||||
if (fromNode) {
|
||||
// make sure there is a parent for the link
|
||||
const parent_link = fromNode.outputs[link_info.origin_slot];
|
||||
if (parent_link) {
|
||||
node_slot.type = parent_link.type;
|
||||
node_slot.name = `${_PREFIX}_`;
|
||||
}
|
||||
}
|
||||
} else if (event === TypeSlotEvent.Disconnect) {
|
||||
this.removeInput(slot_idx);
|
||||
}
|
||||
|
||||
// Track each slot name so we can index the uniques
|
||||
let idx = 0;
|
||||
let slot_tracker = {};
|
||||
for(const slot of this.inputs) {
|
||||
if (slot.link === null) {
|
||||
try {
|
||||
this.removeInput(idx);
|
||||
} catch {
|
||||
|
||||
}
|
||||
continue;
|
||||
}
|
||||
idx += 1;
|
||||
const name = slot.name.split('_')[0];
|
||||
|
||||
// Correctly increment the count in slot_tracker
|
||||
let count = (slot_tracker[name] || 0) + 1;
|
||||
slot_tracker[name] = count;
|
||||
|
||||
// Update the slot name with the count if greater than 1
|
||||
slot.name = `${name}_${count}`;
|
||||
}
|
||||
|
||||
// check that the last slot is a dynamic entry....
|
||||
let last = this.inputs[this.inputs.length - 1];
|
||||
if (last === undefined || (last.name != _PREFIX || last.type != _TYPE)) {
|
||||
this.addInput(_PREFIX, _TYPE);
|
||||
// Set the unconnected slot to appear gray
|
||||
last = this.inputs[this.inputs.length - 1];
|
||||
if (last) {
|
||||
last.color_off = "#666";
|
||||
}
|
||||
}
|
||||
|
||||
// force the node to resize itself for the new/deleted connections
|
||||
this?.graph?.setDirtyCanvas(true);
|
||||
return me;
|
||||
}
|
||||
}
|
||||
return nodeType;
|
||||
},
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user