Add files via upload
This commit is contained in:
+18
-3
@@ -1,5 +1,20 @@
|
||||
from .chat_OpenAI import OmixChatPro
|
||||
#from .chat_Grok import ChatGrok coming soon
|
||||
# 👾 OmiXDev/chat Chat Nodes for ComfyUI
|
||||
# Includes Gemini and OpenAI Chat APIs
|
||||
|
||||
|
||||
|
||||
from .chat_OpenAI import NODE_CLASS_MAPPINGS as OPENAI_NODES, NODE_DISPLAY_NAME_MAPPINGS as OPENAI_DISPLAY
|
||||
from .chat_Gemini import NODE_CLASS_MAPPINGS as GEMINI_NODES, NODE_DISPLAY_NAME_MAPPINGS as GEMINI_DISPLAY
|
||||
|
||||
# Merge both into one unified export for ComfyUI
|
||||
NODE_CLASS_MAPPINGS = {}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {}
|
||||
|
||||
NODE_CLASS_MAPPINGS.update(OPENAI_NODES)
|
||||
NODE_CLASS_MAPPINGS.update(GEMINI_NODES)
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS.update(OPENAI_DISPLAY)
|
||||
NODE_DISPLAY_NAME_MAPPINGS.update(GEMINI_DISPLAY)
|
||||
|
||||
|
||||
#from .chat_helper import *
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import google.generativeai as genai
|
||||
|
||||
class OmixGeminiPro:
|
||||
"""
|
||||
ComfyUI Node for chatting with Google Gemini models (Gemini 1.5 Pro, 1.5 Flash, etc.)
|
||||
Supports chat history, model selection, and reset control.
|
||||
"""
|
||||
|
||||
chat_history = [] # Static chat history for persistence
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"api_key": ("STRING", {"multiline": False, "default": "🔑 AIzaSy..."}),
|
||||
"user_message": ("STRING", {"multiline": True, "default": "Hello Gemini 👋"}),
|
||||
"model": (
|
||||
["gemini-2.5-flash-lite", "gemini-exp-1206"],
|
||||
{"default": "gemini-2.5-flash-lite"},
|
||||
),
|
||||
},
|
||||
"optional": {
|
||||
"reset": ("BOOLEAN", {"default": False}),
|
||||
"send_history": ("BOOLEAN", {"default": True}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING", "STRING")
|
||||
RETURN_NAMES = ("last_reply", "chat_history")
|
||||
FUNCTION = "chat"
|
||||
CATEGORY = "👾 OmiXDev/Chat"
|
||||
|
||||
def chat(self, api_key, user_message, model, reset=False, send_history=True):
|
||||
"""
|
||||
Chat with Gemini API using GenerativeModel interface.
|
||||
Maintains a conversation if send_history=True.
|
||||
"""
|
||||
# --- 1. Reset history if needed ---
|
||||
if reset:
|
||||
self.chat_history.clear()
|
||||
return ("🧹 Chat history cleared.", "")
|
||||
|
||||
# --- 2. Validate API key ---
|
||||
if not api_key or not api_key.startswith("AI"):
|
||||
reply = "❌ Invalid or missing Gemini API Key."
|
||||
return reply, "\n".join(self.chat_history)
|
||||
|
||||
# --- 3. Skip if message empty ---
|
||||
if not user_message.strip():
|
||||
return ("", "\n".join(self.chat_history))
|
||||
|
||||
try:
|
||||
# --- 4. Configure Gemini ---
|
||||
genai.configure(api_key=api_key)
|
||||
model_instance = genai.GenerativeModel(model)
|
||||
|
||||
# --- 5. Prepare chat session ---
|
||||
if send_history and self.chat_history:
|
||||
history = []
|
||||
for line in self.chat_history:
|
||||
if line.startswith("👤"):
|
||||
history.append({"role": "user", "parts": [line[2:].strip()]})
|
||||
elif line.startswith("🤖"):
|
||||
history.append({"role": "model", "parts": [line[2:].strip()]})
|
||||
chat = model_instance.start_chat(history=history)
|
||||
else:
|
||||
chat = model_instance.start_chat(history=[])
|
||||
|
||||
# --- 6. Send message ---
|
||||
response = chat.send_message(user_message)
|
||||
reply = response.text.strip()
|
||||
|
||||
except Exception as e:
|
||||
reply = f"❌ Error: {e}"
|
||||
|
||||
# --- 7. Update history ---
|
||||
self.chat_history.append(f"👤 {user_message}")
|
||||
self.chat_history.append(f"🤖 {reply}")
|
||||
|
||||
return reply, "\n".join(self.chat_history)
|
||||
|
||||
|
||||
# ✅ Register node
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"OmixGeminiPro": OmixGeminiPro
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"OmixGeminiPro": "👾 Gemini Chat API 🔑"
|
||||
}
|
||||
+15
-52
@@ -1,81 +1,44 @@
|
||||
from openai import OpenAI
|
||||
|
||||
class OmixChatPro:
|
||||
"""
|
||||
Node for chatting with OpenAI (API key input + history + model selection)
|
||||
"""
|
||||
|
||||
chat_history = [] # Store chat history for this node
|
||||
chat_history = []
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"api_key": ("STRING", {"multiline": False, "default": "🔑 sk-proj-xxxxxxxxx"}),
|
||||
"user_message": ("STRING", {"multiline": True, "default": "Start Chat"}),
|
||||
"model": (["gpt-4.1-mini", "gpt-4.1", "gpt-4o-mini", "gpt-4o"], {"default": "gpt-4.1-mini"}),
|
||||
"api_key": ("STRING", {"default": "", "multiline": False}),
|
||||
"user_message": ("STRING", {"default": "Hello!", "multiline": True}),
|
||||
"model": (["gpt-4.1-mini","gpt-4.1","gpt-4o-mini"], {"default":"gpt-4.1-mini"}),
|
||||
},
|
||||
"optional": {
|
||||
"reset": ("BOOLEAN", {"default": False}),
|
||||
"send_history": ("BOOLEAN", {"default": True}),
|
||||
"send_history": ("BOOLEAN", {"default": True})
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING", "STRING")
|
||||
RETURN_NAMES = ("last_reply", "chat_history")
|
||||
RETURN_TYPES = ("STRING","STRING")
|
||||
RETURN_NAMES = ("last_reply","chat_history")
|
||||
FUNCTION = "chat"
|
||||
CATEGORY = "👾 OmiX/Chat"
|
||||
CATEGORY = "👾 OmiXDev/Chat"
|
||||
|
||||
def chat(self, api_key, user_message, model, reset=False, send_history=True):
|
||||
if reset:
|
||||
self.chat_history.clear()
|
||||
return ("[History cleared]", "")
|
||||
|
||||
if not user_message.strip():
|
||||
return ("", "\n".join(self.chat_history))
|
||||
|
||||
if not api_key or not api_key.startswith("sk-"):
|
||||
reply = "❌ Please enter a valid API Key."
|
||||
return reply, "\n".join(self.chat_history)
|
||||
|
||||
if not user_message.strip() or not api_key:
|
||||
return ("❌ Enter valid message and API key", "\n".join(self.chat_history))
|
||||
try:
|
||||
client = OpenAI(api_key=api_key)
|
||||
|
||||
# Build messages
|
||||
if send_history and self.chat_history:
|
||||
messages = [{"role": "system", "content": "You are a helpful assistant and respond in Persian."}]
|
||||
for line in self.chat_history:
|
||||
if line.startswith("👤"):
|
||||
messages.append({"role": "user", "content": line[2:].strip()})
|
||||
elif line.startswith("🤖"):
|
||||
messages.append({"role": "assistant", "content": line[2:].strip()})
|
||||
messages.append({"role": "user", "content": user_message})
|
||||
else:
|
||||
messages = [
|
||||
{"role": "system", "content": "You are a helpful assistant and respond in English."},
|
||||
{"role": "user", "content": user_message},
|
||||
]
|
||||
|
||||
resp = client.chat.completions.create(
|
||||
model=model,
|
||||
messages=messages,
|
||||
)
|
||||
messages = [{"role":"user","content":user_message}]
|
||||
resp = client.chat.completions.create(model=model,messages=messages)
|
||||
reply = resp.choices[0].message.content.strip()
|
||||
except Exception as e:
|
||||
reply = f"❌ Error: {e}"
|
||||
|
||||
# Add to history
|
||||
self.chat_history.append(f"👤 {user_message}")
|
||||
self.chat_history.append(f"🤖 {reply}")
|
||||
return (reply, "\n".join(self.chat_history))
|
||||
|
||||
return reply, "\n".join(self.chat_history)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"OmixChatPro": OmixChatPro
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"OmixChatPro": "👾 OpenAI Chat API 🔑"
|
||||
}
|
||||
|
||||
NODE_CLASS_MAPPINGS = {"OmixChatPro": OmixChatPro}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {"OmixChatPro":"👾 OpenAI Chat API 🔑"}
|
||||
|
||||
Reference in New Issue
Block a user