2
2
import mcschematic
3
3
import sys
4
4
import json
5
+ import requests
6
+ import base64
7
+ import uuid
5
8
6
9
from log_writer import logger
7
10
import config
@@ -31,24 +34,46 @@ def askgpt(system_prompt: str, user_prompt: str, model_name: str):
31
34
Returns:
32
35
str: The response from ChatGPT.
33
36
"""
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
+
35
43
logger ("Initialized the OpenAI client." )
36
44
37
45
# 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
+
42
61
43
62
logger (f"askgpt: system { system_prompt } " )
44
63
logger (f"askgpt: user { user_prompt } " )
45
64
46
65
# 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
+ )
52
77
53
78
logger (f"askgpt: response { response } " )
54
79
@@ -57,6 +82,37 @@ def askgpt(system_prompt: str, user_prompt: str, model_name: str):
57
82
logger (f"askgpt: extracted reply { assistant_reply } " )
58
83
return assistant_reply
59
84
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
+
60
116
def text_to_schem (text : str ):
61
117
"""
62
118
Converts a JSON string to a Minecraft schematic.
0 commit comments