# Pull Request Template ## Description Fixes: https://linear.app/chatwoot/issue/CW-7167/label-suggestions-bad-ux ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
64 lines
1.7 KiB
Ruby
64 lines
1.7 KiB
Ruby
class Captain::RewriteService < Captain::BaseTaskService
|
|
pattr_initialize [:account!, :content!, :operation!, { conversation_display_id: nil }]
|
|
|
|
TONE_OPERATIONS = %i[casual professional friendly confident straightforward].freeze
|
|
ALLOWED_OPERATIONS = (%i[fix_spelling_grammar improve] + TONE_OPERATIONS).freeze
|
|
|
|
def perform
|
|
operation_sym = operation.to_sym
|
|
raise ArgumentError, "Invalid operation: #{operation}" unless ALLOWED_OPERATIONS.include?(operation_sym)
|
|
|
|
send(operation_sym)
|
|
end
|
|
|
|
TONE_OPERATIONS.each do |tone|
|
|
define_method(tone) do
|
|
call_llm_with_prompt(tone_rewrite_prompt(tone.to_s))
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def fix_spelling_grammar
|
|
call_llm_with_prompt(prompt_from_file('fix_spelling_grammar'))
|
|
end
|
|
|
|
def improve
|
|
template = prompt_from_file('improve')
|
|
|
|
system_prompt = render_liquid_template(template, {
|
|
'conversation_context' => conversation.to_llm_text(include_contact_details: true),
|
|
'draft_message' => content
|
|
})
|
|
|
|
call_llm_with_prompt(system_prompt, content)
|
|
end
|
|
|
|
def call_llm_with_prompt(system_content, user_content = content)
|
|
make_api_call(
|
|
model: GPT_MODEL,
|
|
messages: [
|
|
{ role: 'system', content: system_content },
|
|
{ role: 'user', content: user_content }
|
|
]
|
|
)
|
|
end
|
|
|
|
def render_liquid_template(template_content, variables = {})
|
|
Liquid::Template.parse(template_content).render(variables)
|
|
end
|
|
|
|
def tone_rewrite_prompt(tone)
|
|
template = prompt_from_file('tone_rewrite')
|
|
render_liquid_template(template, 'tone' => tone)
|
|
end
|
|
|
|
def event_name
|
|
operation
|
|
end
|
|
|
|
def use_account_openai_hook?
|
|
true
|
|
end
|
|
end
|