today_ai_weather/app/services/ai_service.rb
songtianlun 4d38dad1cf feat: improve DALL-E prompt generation for weather scenes
- Update the prompt to specify generating a highly detailed and
  photorealistic image for a specific weather moment.
- Enhance clarity regarding the scene's inspiration by retaining
  context about the city, state, country, and region.

This change refines the AI generation process by focusing on
particular weather moments, improving the output's relevance and
detail, which could lead to a better user experience.
2025-04-09 15:51:56 +08:00

99 lines
4.7 KiB
Ruby

class AiService
def initialize
@client = OpenAI::Client.new(
access_token: Rails.application.credentials.openai.token,
uri_base: Rails.application.credentials.openai.uri,
log_errors: Rails.env.development?, # 只在开发环境下启用
request_timeout: 240
)
end
def generate_prompt(city, weather_data)
city_desc = generate_location_desc(city)
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)
end
def generate_image(prompt)
response = @client.images.generate(
parameters: {
model: "dall-e-3",
prompt: prompt,
size: "1792x1024",
# quality: "standard",
quality: "hd",
n: 1
}
)
response.dig("data", 0, "url")
end
private
def ask_ai(system_message, user_message)
response = @client.chat(
parameters: {
model: "gpt-4o",
messages:
[ {
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)
region = city.country.region&.name || ""
country = city.country.name || ""
state = city.state&.name || ""
<<~PROMPT
Generate a highly detailed and photorealistic DALL-E 3 prompt capturing a specific weather moment in #{city.name}, #{state}, #{country}, #{region}.
**Scene Inspiration:**
#{city_desc}
(Use this description to inform the specific elements and mood of the scene).
**Weather Conditions to Depict:**
- Temperature Feel: Convey the atmosphere associated with #{weather_data[:temperature]}°C.
- Visible Weather: #{weather_data[:description]}. Show its effects on the environment (e.g., rain slicking streets, sun casting sharp shadows, fog softening distant objects).
- Sky & Light: Sky is #{weather_data[:cloud]}% cloud covered. Render the lighting accurately for the time: #{weather_data[:time]} (e.g., warm morning light, bright midday sun, soft afternoon light, dramatic sunset colors, twilight ambiance, or night scene lighting like streetlights).
**Mandatory Requirements for Realism and Aesthetics:**
- **Style:** Photorealistic photograph, hyperrealistic, cinematic quality. Capture fine details, textures (e.g., wet pavement, brickwork, foliage), and accurate lighting.
- **Composition:** Atmospheric and artistic composition (e.g., rule of thirds, leading lines), creating depth and visual interest. Consider a natural eye-level perspective or a slightly elevated viewpoint for landscape/cityscape.
- **Landmarks/Architecture:** Feature iconic landmarks or characteristic architecture from #{city.name} as naturally integrated elements within the scene.
- **Clarity & Brightness:** Ensure the image is sharp, clear, and well-exposed. Avoid excessive blur or motion artifacts unless intentionally used for artistic effect (like long exposure for rain).
- **Lighting Enhancement:**
- Even in overcast conditions (high #{weather_data[:cloud]}), maintain visual appeal with soft, diffused light, ensuring details in shadows are visible and colors remain natural yet engaging.
- On sunny days (low #{weather_data[:cloud]}%), emphasize the brightness, warmth, contrast, and potentially lens flare for added realism.
- **Avoid:** Do NOT produce illustrations, paintings, sketches, anime, cartoons, or overly stylized digital art. The final image must look like a real photograph.
**Output Format:** Directly output the complete, detailed DALL-E prompt string only. No introductory text, labels, or explanations.
PROMPT
end
end