2025-01-22 16:50:00 +08:00
class AiService
def initialize
@client = OpenAI :: Client . new (
access_token : Rails . application . credentials . openai . token ,
uri_base : Rails . application . credentials . openai . uri ,
2025-02-13 17:21:41 +08:00
log_errors : Rails . env . development? , # 只在开发环境下启用
2025-01-22 16:50:00 +08:00
request_timeout : 240
)
end
def generate_prompt ( city , weather_data )
2025-02-12 11:33:11 +08:00
city_desc = generate_location_desc ( city )
2025-01-22 16:50:00 +08:00
2025-02-12 11:33:11 +08:00
system_message =
" You are a professional artist creating prompts for DALL-E 3. Create realistic, artistic weather scenes featuring iconic landmarks. "
user_message = generate_dall_e_3_prompt_request ( city , weather_data , city_desc )
ask_ai ( system_message , user_message )
end
def generate_location_desc ( city )
region = city . country . region & . name || " "
country = city . country . name || " "
state = city . state & . name || " "
system_message =
" You are a global geography master, you understand the culture, geography, architecture, customs and other information of all cities around the world. Describe this city based on the city I gave you. Include details about its culture, climate, landmarks, and any unique features that make this place special. Condense the keyword into a description of about 50 words "
user_message =
" Describe the characteristics of the city of #{ city . name } , located in the #{ state } , #{ country } , #{ region } "
ask_ai ( system_message , user_message )
2025-01-22 16:50:00 +08:00
end
def generate_image ( prompt )
response = @client . images . generate (
parameters : {
model : " dall-e-3 " ,
prompt : prompt ,
size : " 1792x1024 " ,
2025-02-16 13:27:25 +08:00
# quality: "standard",
quality : " hd " ,
2025-01-22 16:50:00 +08:00
n : 1
}
)
response . dig ( " data " , 0 , " url " )
end
private
2025-02-12 11:33:11 +08:00
def ask_ai ( system_message , user_message )
response = @client . chat (
parameters : {
2025-02-13 17:21:41 +08:00
model : " gpt-4o " ,
messages :
2025-02-12 11:33:11 +08:00
[ {
role : " System " ,
content : system_message
} , {
role : " User " ,
content : user_message
} ] ,
temperature : 0 . 7 ,
max_tokens : 300
}
)
response . dig ( " choices " , 0 , " message " , " content " )
end
def generate_dall_e_3_prompt_request ( city , weather_data , city_desc )
2025-02-12 10:02:09 +08:00
region = city . country . region & . name || " "
country = city . country . name || " "
state = city . state & . name || " "
2025-01-22 16:50:00 +08:00
<< ~ PROMPT
2025-02-12 10:02:09 +08:00
Create a DALL - E 3 prompt for a weather scene in #{city.name}, #{state}, #{country}, #{region}.
2025-01-22 16:50:21 +08:00
2025-02-12 11:33:11 +08:00
Location Desc : #{city_desc}
2025-01-22 16:50:00 +08:00
Weather conditions :
- Temperature : #{weather_data[:temperature]}°C
- Weather : #{weather_data[:description]}
- Cloud cover : #{weather_data[:cloud]}%
2025-01-31 10:46:32 +08:00
- Time : #{weather_data[:time]}
2025-01-22 16:50:21 +08:00
2025-01-22 16:50:00 +08:00
Requirements :
- Feature iconic landmarks or architecture from #{city.name}
- Realistic style
- Weather conditions should be clearly visible
2025-02-12 11:56:09 +08:00
- Keep the picture clear and bright and avoid blurring
- Ensure the overall brightness is maintained , even in overcast conditions , by incorporating vibrant colors and soft lighting effects .
- If it ’ s a sunny day , enhance the brightness and warmth of the scene .
2025-01-22 16:50:00 +08:00
- Atmospheric and artistic composition
2025-01-22 16:50:21 +08:00
2025-02-12 11:56:09 +08:00
Generate a detailed , creative prompt that will produce a beautiful and realistic image . Directly output prompt words , no prefix and suffix words
2025-01-22 16:50:00 +08:00
PROMPT
end
2025-01-22 16:50:21 +08:00
end