Class: Fastlane::Actions::OpenaiAskAction
- Inherits:
-
Action
- Object
- Action
- Fastlane::Actions::OpenaiAskAction
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb
Constant Summary collapse
- OPENAI_API_ENDPOINT =
URI('https://api.openai.com/v1/chat/completions').freeze
- PREDEFINED_PROMPTS =
{ release_notes: <<~PROMPT.freeze Act like a mobile app marketer who wants to prepare release notes for Google Play and App Store. Do not write it point by point and keep it under 350 characters. It should be a unique paragraph. When provided a list, use the number of any potential "*" in brackets at the start of each item as indicator of importance. Ignore items starting with "[Internal]", and ignore links to GitHub. PROMPT }.freeze
Documentation collapse
- .authors ⇒ Object
- .available_options ⇒ Object
- .available_prompt_symbols ⇒ Object
- .description ⇒ Object
- .details ⇒ Object
- .examples ⇒ Object
- .is_supported?(_platform) ⇒ Boolean
- .return_value ⇒ Object
Class Method Summary collapse
- .format_message(role:, text:) ⇒ Object
- .request_body(prompt:, question:) ⇒ Object
- .run(params) ⇒ Object
Class Method Details
.authors ⇒ Object
74 75 76 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 74 def self. ['Automattic'] end |
.available_options ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 107 def self. [ FastlaneCore::ConfigItem.new(key: :prompt, description: 'The internal top-level instructions to give to the model to tell it how to behave. ' \ + "Use a Ruby Symbol from one of [#{available_prompt_symbols}] to use a predefined prompt instead of writing your own", optional: true, default_value: nil, type: String, skip_type_validation: true, verify_block: proc do |value| next if value.is_a?(String) next if PREDEFINED_PROMPTS.include?(value) UI.user_error!("Parameter `prompt` can only be a String or one of the following Symbols: [#{available_prompt_symbols}]") end), FastlaneCore::ConfigItem.new(key: :question, description: 'The user message to ask the question to the OpenAI model', optional: false, default_value: nil, type: String), FastlaneCore::ConfigItem.new(key: :api_token, description: 'The OpenAI API Token to use for the request', env_name: 'OPENAI_API_TOKEN', optional: false, sensitive: true, type: String), ] end |
.available_prompt_symbols ⇒ Object
103 104 105 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 103 def self.available_prompt_symbols PREDEFINED_PROMPTS.keys.map { |v| "`:#{v}`" }.join(',') end |
.description ⇒ Object
70 71 72 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 70 def self.description 'Use OpenAI API to generate response to a prompt' end |
.details ⇒ Object
82 83 84 85 86 87 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 82 def self.details <<~DETAILS Uses the OpenAI API to generate response to a prompt. Can be used to e.g. ask it to generate Release Notes based on a bullet point technical changelog or similar. DETAILS end |
.examples ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 89 def self.examples [ <<~EXEMPLE, items = extract_release_notes_for_version(version: app_version, release_notes_file_path: 'RELEASE-NOTES.txt') nice_changelog = openai_ask( prompt: :release_notes, # Uses the pre-crafted prompt for App Store / Play Store release notes question: "Help me write release notes for the following items:\n#{items}", api_token: get_required_env('OPENAI_API_TOKEN') ) File.write(File.join('fastlane', 'metadata', 'android', 'en-US', 'changelogs', 'default.txt'), nice_changelog) EXEMPLE ] end |
.format_message(role:, text:) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 57 def self.(role:, text:) return nil if text.nil? || text.empty? { role: role, content: [{ type: 'text', text: text }] } end |
.is_supported?(_platform) ⇒ Boolean
136 137 138 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 136 def self.is_supported?(_platform) true end |
.request_body(prompt:, question:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 43 def self.request_body(prompt:, question:) { model: 'gpt-4o', response_format: { type: 'text' }, temperature: 1, max_tokens: 2048, top_p: 1, messages: [ (role: 'system', text: prompt), (role: 'user', text: question), ].compact }.to_json end |
.return_value ⇒ Object
78 79 80 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 78 def self.return_value 'The response text from the prompt as returned by OpenAI API' end |
.run(params) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 20 def self.run(params) api_token = params[:api_token] prompt = params[:prompt] prompt = PREDEFINED_PROMPTS[prompt] if PREDEFINED_PROMPTS.key?(prompt) question = params[:question] headers = { 'Content-Type': 'application/json', Authorization: "Bearer #{api_token}" } body = request_body(prompt: prompt, question: question) response = Net::HTTP.post(OPENAI_API_ENDPOINT, body, headers) case response when Net::HTTPOK json = JSON.parse(response.body) json['choices']&.first&.dig('message', 'content') else UI.user_error!("Error in OpenAI API response: #{response}. #{response.body}") end end |