From 5a6fd7eb269f97769d49fa38276a8212aa6cdd73 Mon Sep 17 00:00:00 2001 From: Olivier Halligon Date: Mon, 2 Dec 2024 18:27:17 +0100 Subject: [PATCH] Refactor predefined prompts (allow Symbols) --- .../actions/common/openai_generate_action.rb | 32 +++++++++++++------ spec/openai_generate_action_spec.rb | 4 +-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_generate_action.rb b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_generate_action.rb index a95fe6635..bd704e740 100644 --- a/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_generate_action.rb +++ b/lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_generate_action.rb @@ -7,18 +7,20 @@ module Actions class OpenaiGenerateAction < Action OPENAI_API_ENDPOINT = URI('https://api.openai.com/v1/chat/completions').freeze - RELEASE_NOTES_PROMPT = <<~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. + 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 + 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 def self.run(params) api_token = params[:api_token] prompt = params[:prompt] - prompt = RELEASE_NOTES_PROMPT if prompt == ':release_notes' + prompt = PREDEFINED_PROMPTS[prompt] if PREDEFINED_PROMPTS.key?(prompt) question = params[:question] headers = { @@ -82,13 +84,25 @@ def self.details DETAILS end + def self.available_prompt_symbols + PREDEFINED_PROMPTS.keys.map { |v| "`:#{v}`" }.join(',') + end + def self.available_options [ FastlaneCore::ConfigItem.new(key: :prompt, - description: 'The internal top-level instructions to give to the model to tell it how to behave. Use `:release_notes` to use a predefined prompt to ask to write release notes from a changelog list', + 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), + 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, diff --git a/spec/openai_generate_action_spec.rb b/spec/openai_generate_action_spec.rb index e60c73bb8..1a5c0cd5e 100644 --- a/spec/openai_generate_action_spec.rb +++ b/spec/openai_generate_action_spec.rb @@ -3,7 +3,7 @@ describe Fastlane::Actions::OpenaiGenerateAction do let(:fake_token) { 'sk-proj-faketok' } let(:endpoint) { Fastlane::Actions::OpenaiGenerateAction::OPENAI_API_ENDPOINT } - let(:release_notes_prompt) { Fastlane::Actions::OpenaiGenerateAction::RELEASE_NOTES_PROMPT } + let(:release_notes_prompt) { Fastlane::Actions::OpenaiGenerateAction::PREDEFINED_PROMPTS[:release_notes] } def stubbed_response(text) <<~JSON @@ -106,7 +106,7 @@ def run_test(prompt_param:, question_param:, expected_prompt:, expected_response TEXT result = run_test( - prompt_param: ':release_notes', + prompt_param: :release_notes, question_param: "Help me write release notes for the following items:\n#{changelog}", expected_prompt: release_notes_prompt, expected_response: expected_response