From 132cf802d5b5cbf898bf9446ce9e31d754aa15b5 Mon Sep 17 00:00:00 2001 From: steffenhaak Date: Thu, 18 May 2023 15:20:48 +0200 Subject: [PATCH] fix: Open AI Integration should respond in conversation language (#7092) Appended "Reply in the user's language." to obtain the answer in the user's language. Fixes: https://linear.app/chatwoot/issue/CW-1735/chore-use-account-language-for-the-prompts Co-authored-by: Sojan --- lib/integrations/openai/processor_service.rb | 8 +++++--- .../integrations/openai/processor_service_spec.rb | 13 +++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/integrations/openai/processor_service.rb b/lib/integrations/openai/processor_service.rb index 84afad27a..17a34e23f 100644 --- a/lib/integrations/openai/processor_service.rb +++ b/lib/integrations/openai/processor_service.rb @@ -28,7 +28,8 @@ class Integrations::Openai::ProcessorService model: GPT_MODEL, messages: [ { role: 'system', - content: "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone." }, + content: "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone. " \ + "Reply in the user's language." }, { role: 'user', content: event['data']['content'] } ] }.to_json @@ -96,7 +97,7 @@ class Integrations::Openai::ProcessorService messages: [ { role: 'system', content: 'Please summarize the key points from the following conversation between support agents and ' \ - 'customer as bullet points for the next support agent looking into the conversation' }, + 'customer as bullet points for the next support agent looking into the conversation. Reply in the user\'s language.' }, { role: 'user', content: conversation_messages } ] }.to_json @@ -106,7 +107,8 @@ class Integrations::Openai::ProcessorService { model: GPT_MODEL, messages: [ - { role: 'system', content: 'Please suggest a reply to the following conversation between support agents and customer' } + { role: 'system', + content: 'Please suggest a reply to the following conversation between support agents and customer. Reply in the user\'s language.' } ].concat(conversation_messages(in_array_format: true)) }.to_json end diff --git a/spec/lib/integrations/openai/processor_service_spec.rb b/spec/lib/integrations/openai/processor_service_spec.rb index 2accf1070..686fa564f 100644 --- a/spec/lib/integrations/openai/processor_service_spec.rb +++ b/spec/lib/integrations/openai/processor_service_spec.rb @@ -29,8 +29,12 @@ RSpec.describe Integrations::Openai::ProcessorService do request_body = { 'model' => 'gpt-3.5-turbo', 'messages' => [ - { 'role' => 'system', - 'content' => "You are a helpful support agent. Please rephrase the following response to a more #{event['data']['tone']} tone." }, + { + 'role' => 'system', + 'content' => 'You are a helpful support agent. ' \ + 'Please rephrase the following response to a more ' \ + "#{event['data']['tone']} tone. Reply in the user's language." + }, { 'role' => 'user', 'content' => event['data']['content'] } ] }.to_json @@ -51,7 +55,8 @@ RSpec.describe Integrations::Openai::ProcessorService do request_body = { 'model' => 'gpt-3.5-turbo', 'messages' => [ - { role: 'system', content: 'Please suggest a reply to the following conversation between support agents and customer' }, + { role: 'system', + content: 'Please suggest a reply to the following conversation between support agents and customer. Reply in the user\'s language.' }, { role: 'user', content: customer_message.content }, { role: 'assistant', content: agent_message.content } ] @@ -79,7 +84,7 @@ RSpec.describe Integrations::Openai::ProcessorService do 'messages' => [ { 'role' => 'system', 'content' => 'Please summarize the key points from the following conversation between support agents and customer ' \ - 'as bullet points for the next support agent looking into the conversation' }, + 'as bullet points for the next support agent looking into the conversation. Reply in the user\'s language.' }, { 'role' => 'user', 'content' => conversation_messages } ] }.to_json