From baec4523d5d73045be62849e28f8483a6dabe6fc Mon Sep 17 00:00:00 2001 From: aakashb95 Date: Fri, 12 Dec 2025 16:27:43 +0530 Subject: [PATCH] add improve prompts --- lib/integrations/llm_base_service.rb | 2 +- .../openai/openai_prompts/improve.liquid | 42 +++++++++++++++++++ lib/integrations/openai/processor_service.rb | 25 +++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 lib/integrations/openai/openai_prompts/improve.liquid diff --git a/lib/integrations/llm_base_service.rb b/lib/integrations/llm_base_service.rb index 4652ad76f..3d38b6c23 100644 --- a/lib/integrations/llm_base_service.rb +++ b/lib/integrations/llm_base_service.rb @@ -8,7 +8,7 @@ class Integrations::LlmBaseService TOKEN_LIMIT = 400_000 GPT_MODEL = Llm::Config::DEFAULT_MODEL ALLOWED_EVENT_NAMES = %w[summarize reply_suggestion fix_spelling_grammar casual professional make_friendly make_formal confident - straightforward].freeze + straightforward improve].freeze CACHEABLE_EVENTS = %w[].freeze pattr_initialize [:hook!, :event!] diff --git a/lib/integrations/openai/openai_prompts/improve.liquid b/lib/integrations/openai/openai_prompts/improve.liquid new file mode 100644 index 000000000..74074463c --- /dev/null +++ b/lib/integrations/openai/openai_prompts/improve.liquid @@ -0,0 +1,42 @@ +You are an AI writing assistant integrated into Chatwoot, an omnichannel customer support platform. + +{% if selection -%} +# Task +Improve ONLY the selected portion of the message. Return the COMPLETE message with the improved selection seamlessly integrated. Keep all other parts exactly as they are. + +{% endif -%} +{% if tone -%} +# Tone +Apply the following tone to {% if selection %}the selected portion{% else %}the message{% endif %}: + +{% case tone -%} +{% when 'friendly' -%} +Warm, approachable, empathetic. Use conversational language and positive words. +{% when 'confident' -%} +Assertive and assured. Use definitive language, avoid hedging words like "maybe" or "I think". +{% when 'professional' -%} +Formal, polished, business-appropriate. Complete sentences, proper grammar, respectful distance. +{% when 'casual' -%} +Relaxed and informal. Use contractions, simpler words, conversational style. +{% when 'straightforward' -%} +Clear, direct, to-the-point. No fluff or extra pleasantries. +{% else -%} +{{ tone }} +{% endcase -%} + + +{% endif -%} +{% unless tone or selection -%} +# Task +Improve the message for clarity, grammar, spelling, and natural flow while maintaining the original tone and intent. + +{% endunless -%} +# Guidelines +- Preserve the core meaning and all important information +- Keep the message concise and appropriate for customer support +- Do not add information that wasn't in the original +- Do not remove critical details or instructions +- Reply in the same language as the original message + +Output only the final improved message without any preamble, tags or explanation. + diff --git a/lib/integrations/openai/processor_service.rb b/lib/integrations/openai/processor_service.rb index 0f648cf22..b15b30d0f 100644 --- a/lib/integrations/openai/processor_service.rb +++ b/lib/integrations/openai/processor_service.rb @@ -43,6 +43,17 @@ class Integrations::Openai::ProcessorService < Integrations::LlmBaseService make_api_call(build_api_call_body(tone_rewrite_prompt(tone_instruction))) end + def improve_message + content = event['data']['content'] + selection = event['data']['selection'].presence + tone = event['data']['tone'].presence + + system_prompt = improve_prompt(selection, tone) + user_content = selection.present? ? improve_user_content(content, selection) : content + + make_api_call(build_api_call_body(system_prompt, user_content)) + end + private def prompt_from_file(file_name, enterprise: false) @@ -54,6 +65,20 @@ class Integrations::Openai::ProcessorService < Integrations::LlmBaseService format(prompt_from_file('tone_rewrite'), tone_instruction) end + def improve_prompt(selection, tone) + render_liquid_prompt('improve', { selection: selection, tone: tone }) + end + + def improve_user_content(content, selection) + "Full message:\n#{content}\n\nSelected portion to improve:\n#{selection}" + end + + def render_liquid_prompt(template_name, context = {}) + template_path = Rails.root.join('lib/integrations/openai/openai_prompts', "#{template_name}.liquid") + template = Liquid::Template.parse(template_path.read) + template.render(context.deep_stringify_keys) + end + def build_api_call_body(system_content, user_content = event['data']['content']) { model: GPT_MODEL,