add improve prompts

This commit is contained in:
aakashb95
2025-12-12 16:27:43 +05:30
parent 65136c90d8
commit baec4523d5
3 changed files with 68 additions and 1 deletions
+1 -1
View File
@@ -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!]
@@ -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 %}:
<tone_instruction>
{% 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 -%}
</tone_instruction>
{% 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.
@@ -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,