Spaces:
Sleeping
Sleeping
File size: 11,452 Bytes
d5fb696 27c79bc d5fb696 27c79bc d5fb696 27c79bc 760ff9a 27c79bc 53e55d4 27c79bc d5fb696 27c79bc 9999d91 27c79bc 9999d91 27c79bc 9999d91 27c79bc 9999d91 27c79bc 9999d91 27c79bc 9999d91 27c79bc 9999d91 760ff9a 53e55d4 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 53e55d4 27c79bc 760ff9a 53e55d4 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
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):
prompt_instructions = f"{initial_prompt_instructions} {store_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.
### System prompt :
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".
store_front_sd_prompt should be from the exterior, emphasize the store's architecture, style, and atmosphere and exclude descriptions of the owner.
storefront_sd_prompt Examples :
"A highly detailed fantasy painting of the exterior of a bustling magic shop. The shop is adorned with glowing runes, enchanted crystals, and mystical artifacts. The shop sign reads "Arcane Emporium" in elegant script. The street is filled with magical creatures, floating lanterns, and colorful stalls. "
"A highly detailed fantasy drawing of the entrance to a mysterious alchemy shop. The shop is hidden down a narrow alley, with a flickering lantern above the door. The shop sign reads "Elixir Emporium" in faded letters. The alley is filled with shadows, strange smells, and the distant sound of bubbling potions. "
"A highly detailed fantasy photo of the front of a grand weapon shop. The shop is decorated with suits of armor, hanging banners, and a large sword embedded in the stone facade. The shop sign reads "Ironclad Armory" in bold letters. The street is filled with armored guards, clashing swords, and the sound of ringing anvils. "
Owner and Employee Generation Prompt Examples :
"A highly detailed fantasy oil painting portrait of a wise and mysterious shop owner. The owner is an elderly elf with silver hair, a long beard, and piercing blue eyes. They wear flowing robes, a jeweled amulet, and a crown of thorns. The owner is surrounded by magical artifacts, ancient tomes, and glowing crystals."
"A highly detailed fantasy charcoal sketch of a quirky and eccentric shop employee. The employee is a young gnome with wild hair, mismatched eyes, and a mischievous grin. They wear patched clothes, a bandolier of potions, and a pet dragon perched on their shoulder. The employee is surrounded by alchemical ingredients, bubbling cauldrons, and floating spellbooks."
"A highly detailed fantasy watercolor painting of a stoic and intimidating shop security guard. The guard is a massive half-orc with a shaved head, a scarred face, and a stern expression. They wear heavy armor, a massive sword, and a shield emblazoned with a roaring lion. The guard is surrounded by chained beasts, locked chests, and glowing runes."
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!
Inventory Instructions:
1. Core Inventory should include at least five items.
2.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.
3.. ALL items have "properties :"
"""
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": {
"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": []
}
]
}
}
inventory_prompt_instructions = """
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": []
}
]
}
}
|