mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
feat: Ability to improve drafts in the editor using GPT integration (#6957)
ref: https://github.com/chatwoot/chatwoot/issues/6436 fixes: https://linear.app/chatwoot/issue/CW-1552/ability-to-rephrase-text-in-the-editor-using-gpt-integration --------- Co-authored-by: Sojan <sojan@pepalo.com> Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
This commit is contained in:
39
lib/integrations/openai/processor_service.rb
Normal file
39
lib/integrations/openai/processor_service.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
class Integrations::Openai::ProcessorService
|
||||
pattr_initialize [:hook!, :event!]
|
||||
|
||||
def perform
|
||||
rephrase_message if event['name'] == 'rephrase'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def rephrase_body(tone, message)
|
||||
{
|
||||
model: 'gpt-3.5-turbo',
|
||||
messages: [
|
||||
{ role: 'system', content: "You are a helpful support agent. Please rephrase the following response to a more #{tone} tone." },
|
||||
{ role: 'user', content: message }
|
||||
]
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def rephrase_message
|
||||
response = make_api_call(rephrase_body(event['data']['tone'], event['data']['content']))
|
||||
JSON.parse(response)['choices'].first['message']['content']
|
||||
end
|
||||
|
||||
def make_api_call(body)
|
||||
headers = {
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => "Bearer #{hook.settings['api_key']}"
|
||||
}
|
||||
|
||||
response = HTTParty.post(
|
||||
'https://api.openai.com/v1/chat/completions',
|
||||
headers: headers,
|
||||
body: body
|
||||
)
|
||||
|
||||
response.body
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user