Spaces:
Sleeping
Sleeping
File size: 10,091 Bytes
d5fb696 27c79bc d5fb696 27c79bc d5fb696 27c79bc 760ff9a 27c79bc d5fb696 27c79bc 760ff9a 27c79bc d5fb696 27c79bc d5fb696 27c79bc 760ff9a 27c79bc d5fb696 27c79bc 760ff9a 27c79bc 760ff9a d5fb696 27c79bc 760ff9a d5fb696 27c79bc d5fb696 27c79bc d5fb696 27c79bc d5fb696 27c79bc d5fb696 27c79bc 760ff9a 27c79bc 760ff9a 27c79bc d5fb696 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
from flask import Flask, request, jsonify
from flask_cors import CORS
import ast
import gc
from openai import OpenAI
client = OpenAI()
def load_llm(user_input, prompt_instructions):
prompt = f"{user_input}"
print(prompt)
response = client.chat.completions.create(
model="gpt-4o-2024-05-13",
messages=[
{
"role": "user",
"content": f"{prompt_instructions} {prompt}"
}
],
temperature=1,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print(f"Model : {response.model}")
return response.choices[0].message.content
# Call the LLM and store its output
def call_llm_and_cleanup(user_input, inventory = False):
if not inventory :
prompt_instructions = f"{initial_prompt_instructions} {store_description}"
else : prompt_instructions = f"{inventory_prompt_instructions} {inventory_description}"
llm_output = load_llm(user_input, prompt_instructions)
llm_output = "".join(llm_output)
print(llm_output)
llm_output = convert_to_dict(llm_output)
gc.collect()
# llm_output is still available for use here
return llm_output
def convert_to_dict(string):
# Check if the input is already a dictionary
if isinstance(string, dict):
print("Input is already a dictionary.")
return string
# Function to try parsing the string to a dictionary
def try_parse(s):
try:
result = ast.literal_eval(s)
if isinstance(result, dict):
print("Item dictionary is valid")
return result
except SyntaxError as e:
error_message = str(e)
print("Syntax Error:", error_message)
# Check if the error message indicates an unclosed '{'
if "'{' was never closed" in error_message:
return try_parse(s + '}') # Attempt to fix by adding a closing '}'
except ValueError as e:
print("Value Error:", e)
return None
# First, try parsing the original string
result = try_parse(string)
if result is not None:
return result
# Check if braces are missing
if not string.startswith('{'):
string = '{' + string
if not string.endswith('}'):
string = string + '}'
# Try parsing again with added braces
return try_parse(string) or "Dictionary not valid"
# Instructions past 4 are not time tested and may need to be removed.
### Meta prompted :
initial_prompt_instructions = """ **Purpose**: ONLY Generate a structured json following the provided format. The job is to generate a store with character, style, detail, and a healthy splash of fun, fantasy, and weird. You do not need to stick strictly to the rules and mechanics of the game, if it fits the style and flavor of the user input, get weird, scary, or silly with the details. You will also be writing interesting flavor text and description of the location and it's atmopshere, and a brief one sentence image generation prompts. Us a wide range of words, you have certain words you use too often, avoid them ex : "whimsical", "unwavering".
Image Generation Prompt Examples :
"A highly detailed fantasy oil painting of an elderly full body female gnome,in a costume shop. The gnome is wearing a costume with wings, with a costume hat . The gnome has distinct fantasy features, such as pointed ears and a small, sturdy build. "
"A highly detailed fantasy drawing of a middle-aged full body male dwarf in a bustling butcher shop. The dwarf is wearing a bloodstained apron and a butcher's hat. The shop is filled with hanging meats, freshly cut steaks, and various sausages. The dwarf has distinct fantasy features, such as a long braided beard and a stout, muscular build. The background shows the hustle and bustle of Market Square outside the shop window."
"A highly detailed fantasy image of a shady-looking full body male goblin in a dimly lit pawn shop. The goblin is wearing a patched vest and a tattered hat. The shop is cluttered with various items, including old weapons, dusty artifacts, and strange trinkets. The goblin has distinct fantasy features, such as green skin, sharp teeth, and pointed ears. The background is filled with shadows and the glint of hidden treasures."
"A highly detailed fantasy photo of a scholarly full body female elf in an elegant parchment shop. The elf is wearing a flowing robe and a delicate circlet. The shop is filled with scrolls, quills, and ancient tomes. The elf has distinct fantasy features, such as pointed ears and a slender, graceful build. The background shows the interior of the shop with shelves lined with parchment and ink bottles, and a large window letting in natural light."
"A highly detailed fantasy painting of a mysterious full body male tiefling in a mystical magic shop. The tiefling is wearing a long cloak and a hood, with glowing runes on his hands. The shop is filled with potions, spellbooks, and enchanted artifacts. The tiefling has distinct fantasy features, such as red skin, horns, and a tail. The background is filled with a magical aura, with various mystical items floating in the air and a crystal ball on the counter."
1. Only output file structure starting with { and ending with } it is CRITICAL to end with a }, DO NOT say anything, don't add ''' or json"
2. DO NOT use null, use "".
3. All keys and values MUST be enclosed in double quotes. ""
4. Services and specialties should have name, description, and prices.
5. sd_prompts should specify race or species
6. quests MUST be detailed, and interesting, preferably unexpected, delightful and memorable.
7. The reward for the quest MUST be specific and detailed!
"""
store_description = {
"store_name": "",
"store_description": "",
"store_reputation": "",
"store_backstory": "",
"storefront_sd_prompt": "",
"store_type": "",
"store_size": "",
"store_hours": "",
"store_location": {
"town": "",
"district": "",
"street": ""
},
"store_owners": [
{
"name": "",
"species": "",
"class": "",
"description": "",
"personality": "",
"secrets": [],
"sd_prompt":""
}
],
"store_employees": [
{
"name": "",
"role": "",
"species": "",
"description": "",
"personality": "",
"sd_prompt":""
}
],
"store_quests": [
{
"name": "",
"description": "",
"reward": ""
}
],
"store_customers": [
{
"name": "",
"description": "",
"influence": ""
}
],
"store_rumors": [],
"store_security": [
{
"name": "",
"description": "",
"mechanics": ""
}
],
"store_services": [
{
"name": "",
"description": "",
"price": ""
}
],
"store_specialties": [
{
"name": "",
"description": "",
"price": ""
}
]
}
inventory_prompt_instructions = """
ONLY Generate a structured json following the provided format. The job is to generate a store inventory of about 10 items. How mundane or extravagent they are is influenced by the shop and merchant. It is always okay to have style, detail, and a healthy splash of fun, fantasy, and weird. You do not need to stick strictly to the rules and mechanics of the game, if it fits the style and flavor of the store or merchant, get weird, scary, or silly with the details.
Core Inventory is a simple list of the very standard things a shop might carry.
1. Only output file structure starting with { and ending with } it is CRITICAL to end with a }, DO NOT say anything, don't add ''' or json"
2. DO NOT use null, use "".
3. All keys and values MUST be enclosed in double quotes. ""
4. Many categories of items wont make sense for most stores. IE Butchers Shops do not need magic items and potions. They might have them, but they would be very specific and probably not for sale at a reasonable price. Similarly a weapon shop does not need to sell basic traveling goods.
5. ALL items have "properties :"
"""
inventory_description = {
"inventory": {
"core_inventory":[
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"weapons": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"armor": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"potions": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"scrolls": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"magical_items": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"mundane_items": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
],
"miscellaneous_items": [
{
"name": "",
"type": "",
"cost": "",
"properties": []
}
]
}
}
|