Update openai_helper.py
This commit is contained in:
@@ -1,50 +1,10 @@
|
||||
import hashlib
|
||||
import random
|
||||
import os
|
||||
|
||||
from inopyutils import InoJsonHelper
|
||||
from inopyutils import InoJsonHelper, InoOpenAIHelper
|
||||
from openai import OpenAI
|
||||
|
||||
from custom_nodes.comfyui_ino_nodes.src.comfyui_ino_nodes.node_helper import ino_print_log
|
||||
|
||||
openai_client = None
|
||||
|
||||
def get_openai_client(config):
|
||||
global openai_client
|
||||
if openai_client is None:
|
||||
openai_client = OpenAI(
|
||||
api_key=config["api_key"],
|
||||
timeout=config["timeout"],
|
||||
max_retries=config["max_retries"],
|
||||
)
|
||||
return openai_client
|
||||
|
||||
|
||||
class InoOpenaiConfig:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"openai_api_key": ("STRING", {"default": "sk-proj-xxxxxxx"}),
|
||||
"timeout": ("FLOAT", {"default": 300}),
|
||||
"max_retries": ("INT", {"default": 3}),
|
||||
}
|
||||
}
|
||||
|
||||
CATEGORY = "InoOpenaiHelper"
|
||||
RETURN_TYPES = ("BOOLEAN", "STRING",)
|
||||
RETURN_NAMES = ("success", "config",)
|
||||
FUNCTION = "function"
|
||||
|
||||
async def function(self, openai_api_key, timeout, max_retries):
|
||||
openai_api_key = os.getenv('OPENAI_TOKEN', openai_api_key)
|
||||
config = {
|
||||
"api_key": openai_api_key,
|
||||
"timeout": timeout,
|
||||
"max_retries": max_retries,
|
||||
}
|
||||
return (True, config)
|
||||
|
||||
class InoOpenaiResponses:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
@@ -64,7 +24,9 @@ class InoOpenaiResponses:
|
||||
"image_url": ("STRING", {"default": ""}),
|
||||
},
|
||||
"optional": {
|
||||
"config": ("STRING", {"default": ""}),
|
||||
"openai_api_key": ("STRING", {"default": ""}),
|
||||
"timeout": ("FLOAT", {"default": 300}),
|
||||
"max_retries": ("INT", {"default": 3}),
|
||||
"model": (["gpt-5", "gpt-4.1"], {}),
|
||||
}
|
||||
}
|
||||
@@ -74,13 +36,18 @@ class InoOpenaiResponses:
|
||||
RETURN_NAMES = ("success", "id", "status", "error", "output_text", "output",)
|
||||
FUNCTION = "function"
|
||||
|
||||
async def function(self, enabled, seed, response_type, text, image_url, config, model):
|
||||
async def function(self, enabled, seed, response_type, text, image_url, openai_api_key="", timeout=300, max_retries=3, model="gpt-5"):
|
||||
if not enabled:
|
||||
ino_print_log("InoOpenaiResponses","Node is disabled")
|
||||
return (False, -1, "not enabled", "", "", "", )
|
||||
return (False, -1, "not enabled", "", "", "")
|
||||
|
||||
try:
|
||||
client = get_openai_client(config)
|
||||
api_key = openai_api_key if openai_api_key else os.getenv('OPENAI_TOKEN', '')
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
timeout=timeout,
|
||||
max_retries=max_retries,
|
||||
)
|
||||
|
||||
response_input = text
|
||||
if response_type == "text":
|
||||
@@ -125,10 +92,8 @@ class InoOpenaiResponses:
|
||||
|
||||
|
||||
LOCAL_NODE_CLASS = {
|
||||
"InoOpenaiConfig": InoOpenaiConfig,
|
||||
"InoOpenaiResponses": InoOpenaiResponses,
|
||||
}
|
||||
LOCAL_NODE_NAME = {
|
||||
"InoOpenaiConfig": "Ino Openai Config",
|
||||
"InoOpenaiResponses": "Ino Openai Responses",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user