Files
leadchat/enterprise/app/services/captain/llm/contact_attributes_service.rb
Pranav ecfa6bf6a2 feat: Add support for account abuse detection (#11001)
This PR adds service to automate account abuse detection. Currently
based on the signup name and URL, could potentially add more context
such as usage analysis, message metadata etc.
2025-02-28 15:28:19 -08:00

55 lines
1.3 KiB
Ruby

class Captain::Llm::ContactAttributesService < 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_attributes
generate_attributes
# to implement the update attributes
end
private
attr_reader :content
def generate_attributes
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
prompt = Captain::Llm::SystemPromptsService.attributes_generator
{
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('attributes', [])
rescue JSON::ParserError => e
Rails.logger.error "Error in parsing GPT processed response: #{e.message}"
[]
end
end