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,
|
|
|
|
request_timeout: 240
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_prompt(city, weather_data)
|
|
|
|
response = @client.chat(
|
|
|
|
parameters: {
|
|
|
|
model: "gpt-4",
|
2025-01-22 16:50:21 +08:00
|
|
|
messages: [ {
|
2025-01-22 16:50:00 +08:00
|
|
|
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)
|
2025-01-22 16:50:21 +08:00
|
|
|
} ],
|
2025-01-22 16:50:00 +08:00
|
|
|
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)
|
|
|
|
<<~PROMPT
|
|
|
|
Create a DALL-E 3 prompt for a weather scene in #{city.name}, #{city.country.name}.
|
2025-01-22 16:50:21 +08:00
|
|
|
|
2025-01-22 16:50:00 +08:00
|
|
|
Weather conditions:
|
|
|
|
- Temperature: #{weather_data[:temperature]}°C
|
|
|
|
- Weather: #{weather_data[:description]}
|
|
|
|
- Cloud cover: #{weather_data[:cloud]}%
|
|
|
|
- Time: Early morning
|
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
|
|
|
|
- Atmospheric and artistic composition
|
2025-01-22 16:50:21 +08:00
|
|
|
|
2025-01-22 16:50:00 +08:00
|
|
|
Generate a detailed, creative prompt that will produce a beautiful and realistic image.
|
|
|
|
PROMPT
|
|
|
|
end
|
2025-01-22 16:50:21 +08:00
|
|
|
end
|