Fixed node and added retry to OpenAi Int

This commit is contained in:
Victor Naranjo
2023-12-15 20:38:07 -08:00
parent a6d3fd865c
commit 394f3e299a
4 changed files with 2880 additions and 29 deletions
+6
View File
@@ -2,6 +2,7 @@ import json
import os
from .chat_gpt_integration import NODE_CLASS_MAPPINGS
from .chat_gpt_integration import NODE_DISPLAY_NAME_MAPPINGS
from datetime import datetime
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
@@ -14,3 +15,8 @@ if not os.path.isfile(os.path.join(os.path.dirname(os.path.realpath(__file__)),"
}
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"config.json"), "w") as f:
json.dump(config, f, indent=4)
#check if config file exists
if not os.path.isfile(os.path.join(os.path.dirname(os.path.realpath(__file__)),"prompt_log.txt")):
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"prompt_log.txt"), "a") as f:
f.write(f"File Creation Date {datetime.now().strftime('%Y%m%d%H%M%S')}")
+79 -28
View File
@@ -2,14 +2,20 @@ import importlib
import json
import os
import random
import time
from openai import OpenAI
from datetime import datetime
openAI_models = None
NODE_FILE = os.path.abspath(__file__)
CONFIG_FILE = f"{os.path.dirname(NODE_FILE)}\\config.json"
ROLES_FILE = f"{os.path.dirname(NODE_FILE)}\\roles.json"
def save_prompt_to_file(log):
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"prompt_log.txt"), "a") as f:
f.write(f"Logged {datetime.now().strftime('%Y%m%d%H%M%S')}\n")
f.write(f"{log}\n\n\n")
def get_roles():
try:
# open config file
@@ -89,38 +95,47 @@ def get_gpt_roles():
roles = ["AI Assistant", "Alejandro Jodorowsky", "H.R. Giger"]
return roles
def GetPrompt(role, prompt, input_model, max_words,custom_formatting) -> str:
def GetPrompt(role, prompt, input_model, max_words,append_string) -> str:
client = OpenAI(
api_key=get_api_key(),
)
print("submitting prompt to OpenAi")
sb = prompt.strip()
if len(custom_formatting) > 0 :
sb += f' in the format: {custom_formatting}'
sb += f' using at most {max_words} words'
completion = client.chat.completions.create(
model=input_model,
messages=[
{
"role": "system",
"content": f"You are {role}",
},
{
"role": "user",
"content": f"{prompt}",
},
],
)
print(f'recieved {len(completion.choices[0].message.content)} from OpenAi')
return completion.choices[0].message.content
if len(append_string) > 0 :
sb += f' - {append_string}'
sb += f' - return no more than {max_words} words'
save_prompt_to_file(sb)
retryCounter = 0
while retryCounter < 3:
try:
completion = client.chat.completions.create(
model=input_model,
messages=[
{
"role": "system",
"content": f"You are a {role} imitator",
},
{
"role": "user",
"content": f"{prompt}",
},
],
)
resp = f"Goal: Produce an image in the style of {role}. Content: " + completion.choices[0].message.content + f' Style: {role}'
save_prompt_to_file(resp)
return resp
except:
print('Failed to talk to OpenAi. sleeping for 10 seconds and trying again')
retryCounter += 1
time.sleep(10)
class ChatGptPrompt:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
def INPUT_TYPES(s):
return {
"required": {
# Multiline string input for the prompt
@@ -130,12 +145,12 @@ class ChatGptPrompt:
"max_words": ("INT", {"default": 400,"min":1,"max":1000})
},
"optional": {
"custom_formatting": ("STRING", {"multiline": True}),
"append_string": ("STRING", {"multiline": True}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("Prompt",)
FUNCTION = "process"
@@ -143,16 +158,52 @@ class ChatGptPrompt:
CATEGORY = "OpenAI" # Define the category for the node
def process(self, prompt,model,role,max_words,custom_formatting):
return GetPrompt(role,prompt,model,max_words,custom_formatting)
EXECUTE='process'
@staticmethod
def process(prompt,model,role,max_words,append_string,seed) -> str:
text = GetPrompt(role,prompt,model,max_words,append_string)
print(f'Returning Text: {text}')
return (text,)
class TextConcat:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
# Multiline string input for the prompt
"text1": ("STRING", {"multiline": True, "defaultBehavior": "input"}),
"text2": ("STRING", {"multiline": True, "defaultBehavior": "input"}),
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "process"
OUTPUT_NODE = True
CATEGORY = "Text" # Define the category for the node
EXECUTE='process'
@staticmethod
def process(text1, text2) -> str:
print(f'Text from previous node {text1}')
return (text1 + ' ' + text2)
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"ChatGptPrompt": ChatGptPrompt
"ChatGptPrompt": ChatGptPrompt,
"ChatGptTextConcat": TextConcat
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"ChatGptPrompt": "ChatGPT Prompt Node"
"ChatGptPrompt": "ChatGPT Prompt Node",
"ChatGptTextConcat": "ChatGpt Text Container"
}
+2751
View File
File diff suppressed because it is too large Load Diff
+44 -1
View File
@@ -1,5 +1,6 @@
{
"creativeThinkers": [
{"name": "AI Assistant", "field": "Generic", "style": "Generic"},
{"name": "Salvador Dalí", "field": "Painter", "style": "Surrealist"},
{"name": "Frida Kahlo", "field": "Painter", "style": "Symbolic"},
{"name": "David Lynch", "field": "Filmmaker", "style": "Surreal, Unsettling"},
@@ -10,6 +11,48 @@
{"name": "Hieronymus Bosch", "field": "Painter", "style": "Fantastical, Symbolic"},
{"name": "Philip K. Dick", "field": "Author", "style": "Exploratory, Sci-Fi"},
{"name": "Terry Gilliam", "field": "Filmmaker", "style": "Visually Imaginative, Eccentric"},
{"name": "H.R. Giger", "field": "Artist", "style": "Biochemical, Gothic"}
{"name": "H.R. Giger", "field": "Artist", "style": "Biomechanical, Surrealist"},
{"name": "Alejandro Jodorowsky", "field": "Filmmaker", "style": "Surreal, Symbolic"},
{"name": "Georgia O'Keeffe", "field": "Painter", "style": "Abstract, Modernist"},
{"name": "Stanley Kubrick", "field": "Filmmaker", "style": "Visionary, Intense"},
{"name": "Isaac Asimov", "field": "Author", "style": "Scientific, Thoughtful"},
{"name": "Dali Lama", "field": "Spiritual Leader", "style": "Philosophical, Compassionate"},
{"name": "J.R.R. Tolkien", "field": "Author", "style": "Fantasy, Epic"},
{"name": "Grant Morrison", "field": "Comic Book Writer", "style": "Mind-Bending, Metafiction"},
{"name": "Federico Fellini", "field": "Filmmaker", "style": "Whimsical, Fantasy"},
{"name": "Jean-Michel Basquiat", "field": "Painter", "style": "Street Art, Neo-Expressionism"},
{"name": "Aldous Huxley", "field": "Author", "style": "Dystopian, Philosophical"},
{"name": "Guillermo del Toro", "field": "Filmmaker", "style": "Fantasy, Gothic"},
{"name": "Yayoi Kusama", "field": "Artist", "style": "Polka Dots, Infinity Rooms"},
{"name": "Roger Zelazny", "field": "Author", "style": "Fantasy, Mythopoeic"},
{"name": "Quentin Tarantino", "field": "Filmmaker", "style": "Quirky, Violent"},
{"name": "René Magritte", "field": "Painter", "style": "Surrealist, Conceptual"},
{"name": "Neil Gaiman", "field": "Author", "style": "Fantasy, Mythic"},
{"name": "Satoshi Kon", "field": "Animator/Filmmaker", "style": "Psychological, Surreal"},
{"name": "Angela Carter", "field": "Author", "style": "Feminist, Fairy Tales"},
{"name": "Federico García Lorca", "field": "Poet/Playwright", "style": "Surreal, Symbolic"},
{"name": "James Cameron", "field": "Filmmaker", "style": "Sci-Fi, Blockbuster"},
{"name": "MagicalRealism Bot", "field": "Twitter Bot", "style": "Magical Realism, Botanical"},
{"name": "Jean Giraud (Moebius)", "field": "Artist", "style": "Sci-Fi, Fantasy"},
{"name": "Miyuki Miyabe", "field": "Author", "style": "Fantasy, Mystery"},
{"name": "Jean-Pierre Jeunet", "field": "Filmmaker", "style": "Whimsical, Romantic"},
{"name": "J.K. Rowling", "field": "Author", "style": "Fantasy, Wizardry"},
{"name": "Janelle Monáe", "field": "Musician", "style": "Afrofuturism, Sci-Fi"},
{"name": "David Bowie", "field": "Musician", "style": "Chameleon, Experimental"},
{"name": "Gu Cheng", "field": "Poet", "style": "Surreal, Modernist"},
{"name": "Masamune Shirow", "field": "Mangaka", "style": "Cyberpunk, Mecha"},
{"name": "Ray Bradbury", "field": "Author", "style": "Sci-Fi, Dystopian"},
{"name": "Akira Kurosawa", "field": "Filmmaker", "style": "Epic, Samurai"},
{"name": "Katsuhiro Otomo", "field": "Mangaka", "style": "Dystopian, Cyberpunk"},
{"name": "Gabriel García Márquez", "field": "Author", "style": "Magical Realism, Surreal"},
{"name": "David Fincher", "field": "Filmmaker", "style": "Dark, Intense"},
{"name": "M.C. Escher", "field": "Artist", "style": "Impossible, Mathematical"},
{"name": "Hideo Kojima", "field": "Video Game Designer", "style": "Meta, Kojima-esque"},
{"name": "Italo Calvino", "field": "Author", "style": "Fantasy, Postmodern"},
{"name": "Luis Buñuel", "field": "Filmmaker", "style": "Surreal, Subversive"},
{"name": "Clive Barker", "field": "Author", "style": "Horror, Dark Fantasy"},
{"name": "Shigeru Miyamoto", "field": "Video Game Designer", "style": "Whimsical, Playful"},
{"name": "Diana Wynne Jones", "field": "Author", "style": "Fantasy, Whimsical"},
{"name": "Aleksandr Solzhenitsyn", "field": "Author", "style": "Dissident, Historical"}
]
}