This PR implements the following features - FAQs from conversations will be generated in account language - Contact notes will be generated in account language - Copilot chat will respond in user language, unless the agent asks the question in a different language ## Changes ### Copilot Chat - Update the prompt to include an instruction for the language, the bot will reply in asked language, but will default to account language - Update the `ChatService` class to include pass the language to `SystemPromptsService` ### FAQ and Contact note generation - Update contact note generator and conversation generator to include account locale - Pass the account locale to `SystemPromptsService` <details><summary>Screenshots</summary> #### FAQs being generated in system langauge  #### Copilot responding in system language  </details> --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com> Co-authored-by: Pranav <pranav@chatwoot.com>
58 lines
1.4 KiB
Ruby
58 lines
1.4 KiB
Ruby
class Captain::Llm::ContactNotesService < Llm::BaseOpenAiService
|
|
def initialize(assistant, conversation)
|
|
super()
|
|
@assistant = assistant
|
|
@conversation = conversation
|
|
@contact = conversation.contact
|
|
@content = "#Contact\n\n#{@contact.to_llm_text} \n\n#Conversation\n\n#{@conversation.to_llm_text}"
|
|
end
|
|
|
|
def generate_and_update_notes
|
|
generate_notes.each do |note|
|
|
@contact.notes.create!(content: note)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :content
|
|
|
|
def generate_notes
|
|
response = @client.chat(parameters: chat_parameters)
|
|
parse_response(response)
|
|
rescue OpenAI::Error => e
|
|
Rails.logger.error "OpenAI API Error: #{e.message}"
|
|
[]
|
|
end
|
|
|
|
def chat_parameters
|
|
account_language = @conversation.account.locale_english_name
|
|
prompt = Captain::Llm::SystemPromptsService.notes_generator(account_language)
|
|
|
|
{
|
|
model: @model,
|
|
response_format: { type: 'json_object' },
|
|
messages: [
|
|
{
|
|
role: 'system',
|
|
content: prompt
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: content
|
|
}
|
|
]
|
|
}
|
|
end
|
|
|
|
def parse_response(response)
|
|
content = response.dig('choices', 0, 'message', 'content')
|
|
return [] if content.nil?
|
|
|
|
JSON.parse(content.strip).fetch('notes', [])
|
|
rescue JSON::ParserError => e
|
|
Rails.logger.error "Error in parsing GPT processed response: #{e.message}"
|
|
[]
|
|
end
|
|
end
|