From d045f532ec211998ed12f588b7992fdb03c8c3f5 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 12 Feb 2025 11:33:11 +0800 Subject: [PATCH] feat: enhance AI prompt generation logic - Refactor prompt generation to separate location description functionality - Update user prompt generation for DALL-E 3 - Improve AI service's response handling This change enhances the AiService by creating a more modular approach to generating prompts. The introduction of a `generate_location_desc` method improves the detail and context provided in the generated prompts. Furthermore, the `ask_ai` method centralizes AI request logic, allowing for cleaner and more organized code. These adjustments should improve the overall robustness and readability of the prompt generation process. --- app/services/ai_service.rb | 55 +++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/app/services/ai_service.rb b/app/services/ai_service.rb index 9f86b40..568915c 100644 --- a/app/services/ai_service.rb +++ b/app/services/ai_service.rb @@ -8,22 +8,24 @@ class AiService 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 - } - ) + city_desc = generate_location_desc(city) - response.dig("choices", 0, "message", "content") + 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) @@ -42,7 +44,26 @@ class AiService private - def generate_prompt_request(city, weather_data) + 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 || "" @@ -50,6 +71,8 @@ class AiService <<~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]}