# Pull Request Template ## Description Routes Enterprise assistant, copilot, FAQ, contact memory, action-classifier, and false-promise detector LLM paths through feature-specific model resolution. `Llm::BaseAiService` now accepts feature/account context and uses `Llm::FeatureRouter` when that context is present, while retaining the installation-model fallback for unmigrated callers. This also adds a `document_faq_generation` feature default for generative FAQ/document content. Linear: https://linear.app/chatwoot/issue/CW-7425/test-new-models Depends on #14840 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? - `bundle exec rspec spec/lib/llm/models_spec.rb spec/enterprise/services/llm/base_ai_service_spec.rb spec/enterprise/services/captain/copilot/chat_service_spec.rb spec/enterprise/services/captain/llm/assistant_chat_service_spec.rb spec/enterprise/services/captain/llm/faq_generator_service_spec.rb spec/enterprise/services/captain/llm/conversation_faq_service_spec.rb spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb spec/enterprise/services/captain/llm/assistant_false_promise_service_spec.rb spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb` passed with 112 examples, 0 failures. - `bundle exec rspec spec/models/concerns/captain_featurable_spec.rb spec/models/account_spec.rb spec/controllers/api/v1/accounts/captain/preferences_controller_spec.rb spec/lib/llm/feature_router_spec.rb` passed with 87 examples, 0 failures. - `bundle exec rubocop enterprise/app/services/llm/base_ai_service.rb enterprise/app/services/captain/copilot/chat_service.rb enterprise/app/services/captain/llm/assistant_chat_service.rb enterprise/app/services/captain/llm/faq_generator_service.rb enterprise/app/services/captain/llm/conversation_faq_service.rb enterprise/app/services/captain/llm/contact_notes_service.rb enterprise/app/services/captain/llm/contact_attributes_service.rb enterprise/app/services/captain/llm/assistant_action_classifier_service.rb enterprise/app/services/captain/llm/assistant_false_promise_service.rb spec/enterprise/services/llm/base_ai_service_spec.rb spec/enterprise/services/captain/copilot/chat_service_spec.rb spec/enterprise/services/captain/llm/assistant_chat_service_spec.rb spec/enterprise/services/captain/llm/faq_generator_service_spec.rb spec/enterprise/services/captain/llm/conversation_faq_service_spec.rb spec/enterprise/services/captain/llm/assistant_action_classifier_service_spec.rb spec/enterprise/services/captain/llm/assistant_false_promise_service_spec.rb spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb` passed with no offenses. - `bundle exec ruby -e "require 'yaml'; config = YAML.load_file('config/llm.yml'); abort('missing document_faq_generation') unless config.dig('features', 'document_faq_generation'); abort('missing default') unless config.dig('features', 'document_faq_generation', 'default'); puts 'llm.yml ok'"` passed. - `git diff --check` passed. ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
128 lines
3.7 KiB
Ruby
128 lines
3.7 KiB
Ruby
class Captain::Llm::ConversationFaqService < Llm::BaseAiService
|
|
include Integrations::LlmInstrumentation
|
|
|
|
DISTANCE_THRESHOLD = 0.3
|
|
|
|
def initialize(assistant, conversation)
|
|
super(feature: 'document_faq_generation', account: conversation.account)
|
|
@assistant = assistant
|
|
@conversation = conversation
|
|
@content = conversation.to_llm_text
|
|
end
|
|
|
|
# Generates and deduplicates FAQs from conversation content
|
|
# Skips processing if there was no human interaction
|
|
def generate_and_deduplicate
|
|
return [] if no_human_interaction?
|
|
|
|
new_faqs = generate
|
|
return [] if new_faqs.empty?
|
|
|
|
duplicate_faqs, unique_faqs = find_and_separate_duplicates(new_faqs)
|
|
save_new_faqs(unique_faqs)
|
|
log_duplicate_faqs(duplicate_faqs) if Rails.env.development?
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :content, :conversation, :assistant
|
|
|
|
def no_human_interaction?
|
|
conversation.first_reply_created_at.nil?
|
|
end
|
|
|
|
def find_and_separate_duplicates(faqs)
|
|
duplicate_faqs = []
|
|
unique_faqs = []
|
|
|
|
faqs.each do |faq|
|
|
combined_text = "#{faq['question']}: #{faq['answer']}"
|
|
embedding = Captain::Llm::EmbeddingService.new(account_id: @conversation.account_id).get_embedding(combined_text)
|
|
similar_faqs = find_similar_faqs(embedding)
|
|
|
|
if similar_faqs.any?
|
|
duplicate_faqs << { faq: faq, similar_faqs: similar_faqs }
|
|
else
|
|
unique_faqs << faq
|
|
end
|
|
end
|
|
|
|
[duplicate_faqs, unique_faqs]
|
|
end
|
|
|
|
def find_similar_faqs(embedding)
|
|
similar_faqs = assistant
|
|
.responses
|
|
.nearest_neighbors(:embedding, embedding, distance: 'cosine')
|
|
Rails.logger.debug(similar_faqs.map { |faq| [faq.question, faq.neighbor_distance] })
|
|
similar_faqs.select { |record| record.neighbor_distance < DISTANCE_THRESHOLD }
|
|
end
|
|
|
|
def save_new_faqs(faqs)
|
|
faqs.map do |faq|
|
|
assistant.responses.create!(
|
|
question: faq['question'],
|
|
answer: faq['answer'],
|
|
status: 'pending',
|
|
documentable: conversation
|
|
)
|
|
end
|
|
end
|
|
|
|
def log_duplicate_faqs(duplicate_faqs)
|
|
return if duplicate_faqs.empty?
|
|
|
|
Rails.logger.info "Found #{duplicate_faqs.length} duplicate FAQs:"
|
|
duplicate_faqs.each do |duplicate|
|
|
Rails.logger.info(
|
|
"Q: #{duplicate[:faq]['question']}\n" \
|
|
"A: #{duplicate[:faq]['answer']}\n\n" \
|
|
"Similar existing FAQs: #{duplicate[:similar_faqs].map { |f| "Q: #{f.question} A: #{f.answer}" }.join(', ')}"
|
|
)
|
|
end
|
|
end
|
|
|
|
def generate
|
|
response = instrument_llm_call(instrumentation_params) do
|
|
chat
|
|
.with_params(response_format: { type: 'json_object' })
|
|
.with_instructions(system_prompt)
|
|
.ask(@content)
|
|
end
|
|
parse_response(response.content)
|
|
rescue RubyLLM::Error => e
|
|
Rails.logger.error "LLM API Error: #{e.message}"
|
|
[]
|
|
end
|
|
|
|
def instrumentation_params
|
|
{
|
|
span_name: 'llm.captain.conversation_faq',
|
|
model: @model,
|
|
temperature: @temperature,
|
|
account_id: @conversation.account_id,
|
|
conversation_id: @conversation.display_id,
|
|
feature_name: 'conversation_faq',
|
|
messages: [
|
|
{ role: 'system', content: system_prompt },
|
|
{ role: 'user', content: @content }
|
|
],
|
|
metadata: { assistant_id: @assistant.id }
|
|
}
|
|
end
|
|
|
|
def system_prompt
|
|
account_language = @conversation.account.locale_english_name
|
|
Captain::Llm::SystemPromptsService.conversation_faq_generator(account_language)
|
|
end
|
|
|
|
def parse_response(response)
|
|
return [] if response.nil?
|
|
|
|
JSON.parse(sanitize_json_response(response)).fetch('faqs', [])
|
|
rescue JSON::ParserError => e
|
|
Rails.logger.error "Error in parsing GPT processed response: #{e.message}"
|
|
[]
|
|
end
|
|
end
|