today_ai_weather/app/services/ai_service.rb
songtianlun e39c87ac5c feat: improve prompt generation with location details
- Include state, country, and region in the DALL-E prompt
- Enhance context for the generated weather scene

This change improves the specificity of the prompts generated
for the AI, providing more contextual information such as
state, country, and region alongside the city name. This
enhancement can lead to more accurate and relevant outputs
from the DALL-E 3 model for weather scenes.
2025-02-12 10:02:47 +08:00

69 lines
1.9 KiB
Ruby

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)
response = @client.chat(
parameters: {
model: "gpt-4",
messages: [ {
role: "system",
content: "You are a professional artist creating prompts for DALL-E 3. Create realistic, artistic weather scenes featuring iconic landmarks."
}, {
role: "user",
content: generate_prompt_request(city, weather_data)
} ],
temperature: 0.7,
max_tokens: 300
}
)
response.dig("choices", 0, "message", "content")
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 generate_prompt_request(city, weather_data)
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}.
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
- Atmospheric and artistic composition
Generate a detailed, creative prompt that will produce a beautiful and realistic image.
PROMPT
end
end