@@ -1,42 +1,34 @@
|
||||
import yaml
|
||||
import openai
|
||||
from .settings import load_settings
|
||||
|
||||
import openai
|
||||
import threading
|
||||
import time
|
||||
class GPT4_WWL:
|
||||
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
all_settings = load_settings()
|
||||
default_user_prompt = all_settings['example_user_prompt']
|
||||
available_apis = [a for a in all_settings['openai_compatible']]
|
||||
default_model = all_settings['openai_compatible']['default']['model']
|
||||
|
||||
return {
|
||||
"required": {
|
||||
"api": (available_apis, {
|
||||
"default": "default"
|
||||
"app_id": ("STRING", {"default": ""}),
|
||||
"api_key": ("STRING", {"default": ""}),
|
||||
"base_url": ("STRING", {"default": ""}),
|
||||
"model": ("STRING", {"default": "gpt-4o-2024-05-13"}),
|
||||
"system_prompt": ("STRING", {
|
||||
"default": ("your user prompt hereact as prompt generator"),
|
||||
"multiline": True, "dynamicPrompts": False
|
||||
}),
|
||||
"model": (default_model,
|
||||
{"default": "gpt-4-1106-preview"}),
|
||||
"system_prompt": ("STRING",
|
||||
{
|
||||
"default": ("act as prompt generator, I will give you text and you describe an image that matches that text in details, "
|
||||
"answer with one response only.if I input in Chinese to communicate with you, but it is crucial that your response be in English."),
|
||||
"multiline": True, "dynamicPrompts": False
|
||||
}),
|
||||
|
||||
"user_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": default_user_prompt
|
||||
"default": "ator, I will give you text and you describe an image that matches that text in details, "
|
||||
"answer with one response only.if I input in Chinese to communicate with you, but it is crucial that your response be in English."
|
||||
}),
|
||||
"temperature": ("FLOAT", {
|
||||
"default": 0.7, "min": 0.0, "max": 2.0, "step": 0.01,
|
||||
"default": 0.7, "min": 0.0, "max": 2.0, "step": 0.01
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"top_p": ("FLOAT", {
|
||||
"default": 1.0, "min": 0.001, "max": 1.0, "step": 0.01,
|
||||
"default": 1.0, "min": 0.001, "max": 1.0, "step": 0.01
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -46,7 +38,7 @@ class GPT4_WWL:
|
||||
FUNCTION = "generate"
|
||||
CATEGORY = "WWL"
|
||||
|
||||
def generate(self, api, model: str, temperature: float | None = None,
|
||||
def generate(self, app_id, api_key, base_url, model: str, temperature: float | None = None,
|
||||
top_p: float | None = None, user_prompt="", system_prompt=""):
|
||||
try:
|
||||
system_content = system_prompt
|
||||
@@ -57,29 +49,39 @@ class GPT4_WWL:
|
||||
{'role': 'system', 'content': system_content}
|
||||
]
|
||||
|
||||
|
||||
response = self.gpt_openai(
|
||||
messages=messages,
|
||||
model=model,
|
||||
temperature=temperature,
|
||||
)
|
||||
return (response,)
|
||||
# 使用线程来实现超时功能
|
||||
result = {"response": None}
|
||||
thread = threading.Thread(target=self.gpt_openai_with_timeout, args=(result, app_id, api_key, base_url, messages, model, temperature))
|
||||
thread.start()
|
||||
thread.join(timeout=10) # 等待10秒
|
||||
|
||||
if thread.is_alive():
|
||||
# 如果线程仍在运行,说明超时了
|
||||
print("Request timed out after 10 seconds.")
|
||||
return (system_content,)
|
||||
|
||||
if result["response"] is None:
|
||||
# 如果响应为None,可能是由于其他错误
|
||||
print("Failed to get response from GPT-4.")
|
||||
return (system_content,)
|
||||
|
||||
return (result["response"],)
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
return (system_content,)
|
||||
|
||||
def gpt_openai(self, messages, model, max_tokens=None, temperature=1, silent=False):
|
||||
settings = load_settings()
|
||||
api_settings = settings['openai_compatible']['default']
|
||||
|
||||
APP_ID = api_settings['app_id']
|
||||
APP_KEY = api_settings['api_key']
|
||||
END_POINT = api_settings['base_url']
|
||||
|
||||
def gpt_openai_with_timeout(self, result, app_id, api_key, base_url, messages, model, temperature):
|
||||
try:
|
||||
response = self.gpt_openai(app_id, api_key, base_url, messages, model, temperature=temperature)
|
||||
result["response"] = response[0] # 存储响应
|
||||
except Exception as e:
|
||||
print(f"Error in gpt_openai: {e}")
|
||||
result["response"] = None
|
||||
|
||||
def gpt_openai(self, app_id, api_key, base_url, messages, model, max_tokens=None, temperature=1, silent=False):
|
||||
client = openai.OpenAI(
|
||||
api_key=f'{APP_ID}.{APP_KEY}',
|
||||
base_url=END_POINT,
|
||||
api_key=f'{app_id}.{api_key}',
|
||||
base_url=base_url,
|
||||
)
|
||||
|
||||
data = {
|
||||
@@ -95,5 +97,4 @@ class GPT4_WWL:
|
||||
|
||||
response = client.chat.completions.create(**data)
|
||||
|
||||
return (f'{response.choices[0].message.content}',)
|
||||
|
||||
return (f'{response.choices[0].message.content}',)
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
import yaml
|
||||
|
||||
DEFAULT_SETTINGS = {
|
||||
"cyberdolphin": {
|
||||
"openai": {
|
||||
"app_id": "NO ID",
|
||||
"api_key": "NO API KEY",
|
||||
"model": "gpt-4-1106-preview"
|
||||
},
|
||||
"openai_compatible": {
|
||||
"app_id": "NO ID",
|
||||
"api_key": "your_key",
|
||||
"api_base": 'http://ip:3200'
|
||||
},
|
||||
"prompts": {
|
||||
"example_user_prompt": "{Camel|goldfish|glowing orb},{moss|tree|fern|balloon},{space station|garden shed|glowing laser sword|bowl of petunias|orange taxi|neon sign}",
|
||||
"default_prompt": {
|
||||
"system": "You are deeply artistic, understanding of concepts like composition, pallete and color theory, and image psychology.",
|
||||
"prefix": "Do use objective language. Do not add narrative. Do describe objects visually and in context. \
|
||||
Do not describe the purpose of the objects, or any other explanations \"",
|
||||
"suffix": '"'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def load_settings():
|
||||
path = os.path.join(os.path.dirname(__file__), "settings.yaml")
|
||||
file_path = pathlib.Path(path)
|
||||
if not file_path.exists():
|
||||
return DEFAULT_SETTINGS['cyberdolphin']
|
||||
|
||||
with open(path) as settings:
|
||||
the_yaml = yaml.safe_load(settings)
|
||||
# print(f'LOADED: {the_yaml["cyberdolphin"]}')
|
||||
return the_yaml['cyberdolphin']
|
||||
|
||||
|
||||
def api_settings(section: str = "openai"):
|
||||
openai_settings = load_settings()['openai_compatible'][section]
|
||||
return openai_settings['api_base'], openai_settings['api_key'], openai_settings['organisation']
|
||||
@@ -1,45 +0,0 @@
|
||||
cyberdolphin:
|
||||
openai_compatible:
|
||||
default:
|
||||
organisation: "NONE"
|
||||
app_id: "xxxxxxx"
|
||||
api_key: "xxxxxxxx"
|
||||
base_url: "https://xxxxxxx.com"
|
||||
model:
|
||||
- "gpt-4-1106-preview"
|
||||
- "gpt-4-0125-preview"
|
||||
- "gpt-4"
|
||||
|
||||
|
||||
example_user_prompt: "your user prompt here"
|
||||
prompt_templates:
|
||||
# the user prompt is by default a list of objects, such as what might be returned from a resnet node
|
||||
|
||||
default:
|
||||
system: >-
|
||||
act as prompt generator ,i will give you text and you describe an image that match that text in details, answer with one response only.I input in Chinese to communicate with you, but it is crucial that your response be in English.
|
||||
prefix: >-
|
||||
make a list of the things you see. do not explain why you see them.
|
||||
do explain what the things you see are doing. do explain where they are.
|
||||
do not explain their purpose,
|
||||
do not say anything other than the list of things you see.
|
||||
do use purely objective language: do not say "I see a..." but instead say "there is a..."
|
||||
SO:
|
||||
When I provide the list "
|
||||
suffix: >-
|
||||
",
|
||||
what is this scene -
|
||||
do not simply repeat the list, do not say "when I provide the list" or similar just project the description.
|
||||
describe what you see in your mind.
|
||||
|
||||
|
||||
prompt_two_example:
|
||||
system: >-
|
||||
You are deeply artistic, understanding of concepts like composition, palette and color theory, and image psychology.
|
||||
prefix: >-
|
||||
describe a scene using the following list of objects: "
|
||||
suffix: >-
|
||||
" - there are many things
|
||||
and you must list them using a maximum of 25 words. be clear and specific.
|
||||
use present tense, objective language: do not say, "I see ..." rather say, "there is ..."
|
||||
|
||||
Reference in New Issue
Block a user