Skip to content

Commit 8de8816

Browse files
committed
feat!: Add advanced mode support
1 parent df200dd commit 8de8816

File tree

3 files changed

+116
-12
lines changed

3 files changed

+116
-12
lines changed

console.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ def generate_plugin(description):
3737

3838
return schem
3939

40+
def get_schematic_advanced(description):
41+
print("(Advanced Mode) Generating programme...")
42+
programme = core.askgpt(config.BTR_DESC_SYS_GEN, config.BTR_DESC_USR_GEN.replace("%DESCRIPTION%", description), config.GENERATE_MODEL, disable_json_mode=True)
43+
44+
print("(Advanced Mode) Generating image tag...")
45+
image_tag = core.askgpt(config.IMG_TAG_SYS_GEN, config.IMG_TAG_USR_GEN.replace("%PROGRAMME%", programme), config.GENERATE_MODEL, disable_json_mode=True)
46+
47+
print("(Advanced Mode) Generating image...")
48+
tag = image_tag + ", minecraft)"
49+
image_url = core.ask_dall_e(tag)
50+
51+
print("(Advanced Mode) Generating schematic...")
52+
response = core.askgpt(config.SYS_GEN_ADV, config.USR_GEN_ADV.replace("%DESCRIPTION%", description), config.VISION_MODEL, image_url=image_url)
53+
54+
schem = core.text_to_schem(response)
55+
56+
return schem
57+
4058
if __name__ == "__main__":
4159
core.initialize()
4260

@@ -54,7 +72,11 @@ def generate_plugin(description):
5472

5573
print("Generating...")
5674

57-
schem = generate_plugin(description)
75+
if config.ADVANCED_MODE:
76+
print("Advanced mode is enabled. Generating a schematic with advanced features.")
77+
schem = get_schematic_advanced(description)
78+
else:
79+
schem = generate_plugin(description)
5880

5981
logger(f"console: Saving {name}.schem to generated/ folder.")
6082
version_tag = core.input_version_to_mcs_tag(version)

core.py

+66-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import mcschematic
33
import sys
44
import json
5+
import requests
6+
import base64
7+
import uuid
58

69
from log_writer import logger
710
import config
@@ -31,24 +34,46 @@ def askgpt(system_prompt: str, user_prompt: str, model_name: str):
3134
Returns:
3235
str: The response from ChatGPT.
3336
"""
34-
client = OpenAI(api_key=config.API_KEY, base_url=config.BASE_URL)
37+
if image_url is not None and config.USE_DIFFERENT_APIKEY_FOR_VISION_MODEL:
38+
logger("Using different API key for vision model.")
39+
client = OpenAI(api_key=config.VISION_API_KEY, base_url=config.VISION_BASE_URL)
40+
else:
41+
client = OpenAI(api_key=config.API_KEY, base_url=config.BASE_URL)
42+
3543
logger("Initialized the OpenAI client.")
3644

3745
# Define the messages for the conversation
38-
messages = [
39-
{"role": "system", "content": system_prompt},
40-
{"role": "user", "content": user_prompt}
41-
]
46+
if image_url is not None:
47+
messages = [
48+
{"role": "system", "content": system_prompt},
49+
{"role": "user", "content": [
50+
{"type": "text", "text": user_prompt},
51+
{"type": "image_url", "image_url": {"url": image_url}}
52+
]
53+
}
54+
]
55+
else:
56+
messages = [
57+
{"role": "system", "content": system_prompt},
58+
{"role": "user", "content": user_prompt}
59+
]
60+
4261

4362
logger(f"askgpt: system {system_prompt}")
4463
logger(f"askgpt: user {user_prompt}")
4564

4665
# Create a chat completion
47-
response = client.chat.completions.create(
48-
model=model_name,
49-
response_format={"type": "json_object"},
50-
messages=messages
51-
)
66+
if disable_json_mode:
67+
response = client.chat.completions.create(
68+
model=model_name,
69+
messages=messages
70+
)
71+
else:
72+
response = client.chat.completions.create(
73+
model=model_name,
74+
response_format={"type": "json_object"},
75+
messages=messages
76+
)
5277

5378
logger(f"askgpt: response {response}")
5479

@@ -57,6 +82,37 @@ def askgpt(system_prompt: str, user_prompt: str, model_name: str):
5782
logger(f"askgpt: extracted reply {assistant_reply}")
5883
return assistant_reply
5984

85+
def ask_dall_e(description: str):
86+
"""
87+
Generates a design image using the DALL-E API.
88+
89+
Args:
90+
description (str): The prompt or description for generating the image.
91+
92+
Returns:
93+
str: The URL of the generated image.
94+
"""
95+
if config.USE_DIFFERENT_APIKEY_FOR_DALLE_MODEL:
96+
client = OpenAI(api_key=config.DALLE_API_KEY, base_url=config.DALLE_BASE_URL)
97+
else:
98+
client = OpenAI(api_key=config.API_KEY, base_url=config.BASE_URL)
99+
100+
logger("ask_dall_e: Generating design image using DALL-E API.")
101+
102+
response = client.images.generate(
103+
model=config.IMAGE_GENERATION_MODEL,
104+
prompt=description,
105+
size=config.IMAGE_SIZE,
106+
quality="standard",
107+
n=1,
108+
)
109+
110+
image_url = response.data[0].url
111+
112+
logger(f"ask_dall_e: Generated image URL {image_url}")
113+
114+
return image_url
115+
60116
def text_to_schem(text: str):
61117
"""
62118
Converts a JSON string to a Minecraft schematic.

ui.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ def get_schematic(description):
2929

3030
return schem
3131

32+
def get_schematic_advanced(description):
33+
print("(Advanced Mode) Generating programme...")
34+
programme = core.askgpt(config.BTR_DESC_SYS_GEN, config.BTR_DESC_USR_GEN.replace("%DESCRIPTION%", description), config.GENERATE_MODEL, disable_json_mode=True)
35+
36+
print("(Advanced Mode) Generating image tag...")
37+
image_tag = core.askgpt(config.IMG_TAG_SYS_GEN, config.IMG_TAG_USR_GEN.replace("%PROGRAMME%", programme), config.GENERATE_MODEL, disable_json_mode=True)
38+
39+
print("(Advanced Mode) Generating image...")
40+
tag = image_tag + ", minecraft)"
41+
image_url = core.ask_dall_e(tag)
42+
43+
print("(Advanced Mode) Generating schematic...")
44+
response = core.askgpt(config.SYS_GEN_ADV, config.USR_GEN_ADV.replace("%DESCRIPTION%", description), config.VISION_MODEL, image_url=image_url)
45+
46+
schem = core.text_to_schem(response)
47+
48+
return schem
49+
3250
def generate_schematic():
3351
"""
3452
Generates a schematic file based on user input.
@@ -42,6 +60,11 @@ def generate_schematic():
4260
"""
4361
generate_button.config(state=tk.DISABLED, text="Generating...")
4462

63+
if config.ADVANCED_MODE:
64+
msgbox.showwarning("Warning", "You are using advanced mode. This mode will generate schematic with higher quality, but it may take longer to generate.")
65+
66+
msgbox.showinfo("Info", "It is expected to take 30 seconds to 5 minutes. The programme may \"not responding\", this is normal, just be patient. DO NOT CLOSE THE PROGRAM. Click the button below to start generating.")
67+
4568
version = version_entry.get()
4669
name = name_entry.get()
4770
description = description_entry.get()
@@ -50,7 +73,10 @@ def generate_schematic():
5073
logger(f"console: input name {name}")
5174
logger(f"console: input description {description}")
5275

53-
schem = get_schematic(description)
76+
if config.ADVANCED_MODE:
77+
schem = get_schematic_advanced(description)
78+
else:
79+
schem = get_schematic(description)
5480

5581
logger(f"console: Saving {name}.schem to generated/ folder.")
5682
version_tag = core.input_version_to_mcs_tag(version)

0 commit comments

Comments
 (0)