today_ai_weather/app/services/ai_service.rb
songtianlun af4ee3ee21 feat: enhance image generation prompt clarity
- Add requirements for clearer and brighter images
- Specify maintenance of brightness even in overcast conditions
- Include enhancements for sunny day imagery
- Revise output prompt to remove unnecessary prefix and suffix

These changes improve the clarity and effectiveness of prompts
used in AI-generated imagery. The additional specifications help
guide the AI in producing visually appealing and contextually
relevant images for various weather conditions.
2025-02-12 11:56:09 +08:00

95 lines
3.2 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class AiService
def initialize
@client = OpenAI::Client.new(
access_token: Rails.application.credentials.openai.token,
uri_base: Rails.application.credentials.openai.uri,
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",
n: 1
}
)
response.dig("data", 0, "url")
end
private
def ask_ai(system_message, user_message)
response = @client.chat(
parameters: {
model: "gpt-4",
message:
[ {
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
Create a DALL-E 3 prompt for a weather scene in #{city.name}, #{state}, #{country}, #{region}.
Location Desc: #{city_desc}
Weather conditions:
- Temperature: #{weather_data[:temperature]}°C
- Weather: #{weather_data[:description]}
- Cloud cover: #{weather_data[:cloud]}%
- Time: #{weather_data[:time]}
Requirements:
- Feature iconic landmarks or architecture from #{city.name}
- Realistic style
- Weather conditions should be clearly visible
- 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 its a sunny day, enhance the brightness and warmth of the scene.
- Atmospheric and artistic composition
Generate a detailed, creative prompt that will produce a beautiful and realistic image. Directly output prompt words, no prefix and suffix words
PROMPT
end
end