Upload comfyui-claude-node-final.py
Browse files- comfyui-claude-node-final.py +84 -0
comfyui-claude-node-final.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import anthropic
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
|
| 6 |
+
class ClaudeCustomPrompt:
|
| 7 |
+
"""
|
| 8 |
+
ComfyUI node that generates prompts using Claude API
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.api_key = os.getenv("ANTHROPIC_API_KEY", "")
|
| 13 |
+
self.client = None
|
| 14 |
+
|
| 15 |
+
@classmethod
|
| 16 |
+
def INPUT_TYPES(cls) -> Dict[str, Any]:
|
| 17 |
+
return {
|
| 18 |
+
"required": {
|
| 19 |
+
"system_prompt": ("STRING", {
|
| 20 |
+
"default": "You are an AI art prompt generator. Reply only with a prompt for image generation, no explanations.",
|
| 21 |
+
"multiline": True
|
| 22 |
+
}),
|
| 23 |
+
"user_input": ("STRING", {
|
| 24 |
+
"default": "Generate a prompt for: space cat",
|
| 25 |
+
"multiline": True
|
| 26 |
+
}),
|
| 27 |
+
},
|
| 28 |
+
"optional": {
|
| 29 |
+
"api_key": ("STRING", {
|
| 30 |
+
"default": "",
|
| 31 |
+
"multiline": False
|
| 32 |
+
}),
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
RETURN_TYPES = ("STRING",)
|
| 37 |
+
FUNCTION = "generate_prompt"
|
| 38 |
+
CATEGORY = "prompt generation"
|
| 39 |
+
|
| 40 |
+
def generate_prompt(self, system_prompt: str, user_input: str, api_key: str = "") -> tuple[str]:
|
| 41 |
+
# Use provided API key or fallback to environment variable
|
| 42 |
+
key_to_use = api_key if api_key else self.api_key
|
| 43 |
+
if not key_to_use:
|
| 44 |
+
raise ValueError("No API key provided. Please set ANTHROPIC_API_KEY environment variable or provide it as input.")
|
| 45 |
+
|
| 46 |
+
# Initialize client if needed
|
| 47 |
+
if not self.client or api_key:
|
| 48 |
+
self.client = anthropic.Anthropic(api_key=key_to_use)
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
# Make API call to Claude
|
| 52 |
+
message = self.client.messages.create(
|
| 53 |
+
model="claude-3-opus-20240229",
|
| 54 |
+
max_tokens=1000,
|
| 55 |
+
temperature=0,
|
| 56 |
+
system=system_prompt,
|
| 57 |
+
messages=[
|
| 58 |
+
{
|
| 59 |
+
"role": "user",
|
| 60 |
+
"content": [
|
| 61 |
+
{
|
| 62 |
+
"type": "text",
|
| 63 |
+
"text": user_input
|
| 64 |
+
}
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Extract the generated prompt
|
| 71 |
+
generated_prompt = message.content[0].text.strip()
|
| 72 |
+
return (generated_prompt,)
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise RuntimeError(f"Error generating prompt: {str(e)}")
|
| 76 |
+
|
| 77 |
+
# Node registration
|
| 78 |
+
NODE_CLASS_MAPPINGS = {
|
| 79 |
+
"ClaudeCustomPrompt": ClaudeCustomPrompt
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 83 |
+
"ClaudeCustomPrompt": "Claude Prompt Generator"
|
| 84 |
+
}
|