# 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
129 lines
4.2 KiB
Ruby
129 lines
4.2 KiB
Ruby
class Captain::Copilot::ChatService < Llm::BaseAiService
|
|
include Captain::ChatHelper
|
|
|
|
attr_reader :assistant, :account, :user, :copilot_thread, :previous_history, :messages
|
|
|
|
def initialize(assistant, config)
|
|
super(feature: 'copilot', account: assistant.account)
|
|
|
|
@assistant = assistant
|
|
@account = assistant.account
|
|
@user = nil
|
|
@copilot_thread = nil
|
|
@previous_history = []
|
|
@conversation = @account.conversations.find_by(display_id: config[:conversation_id])
|
|
@conversation_id = @conversation&.display_id
|
|
|
|
setup_user(config)
|
|
setup_message_history(config)
|
|
@tools = build_tools
|
|
@messages = build_messages(config)
|
|
end
|
|
|
|
def generate_response(input)
|
|
@messages << { role: 'user', content: input } if input.present?
|
|
response = request_chat_completion
|
|
|
|
Rails.logger.debug { "#{self.class.name} Assistant: #{@assistant.id}, Received response #{response}" }
|
|
Rails.logger.info(
|
|
"#{self.class.name} Assistant: #{@assistant.id}, Incrementing response usage for account #{@account.id}"
|
|
)
|
|
@account.increment_response_usage
|
|
|
|
response
|
|
end
|
|
|
|
private
|
|
|
|
def setup_user(config)
|
|
@user = @account.users.find_by(id: config[:user_id]) if config[:user_id].present?
|
|
end
|
|
|
|
def build_messages(config)
|
|
messages= [system_message]
|
|
messages << account_id_context
|
|
messages += @previous_history if @previous_history.present?
|
|
messages += current_viewing_history(config[:conversation_id]) if config[:conversation_id].present?
|
|
messages
|
|
end
|
|
|
|
def setup_message_history(config)
|
|
Rails.logger.info(
|
|
"#{self.class.name} Assistant: #{@assistant.id}, Previous History: #{config[:previous_history]&.length || 0}, Language: #{config[:language]}"
|
|
)
|
|
|
|
@copilot_thread = @account.copilot_threads.find_by(id: config[:copilot_thread_id]) if config[:copilot_thread_id].present?
|
|
@previous_history = if @copilot_thread.present?
|
|
@copilot_thread.previous_history
|
|
else
|
|
config[:previous_history].presence || []
|
|
end
|
|
end
|
|
|
|
def build_tools
|
|
tools = []
|
|
|
|
tools << Captain::Tools::SearchDocumentationService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetConversationService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchConversationsService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetContactService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetArticleService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchArticlesService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchContactsService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchLinearIssuesService.new(@assistant, user: @user)
|
|
|
|
tools.select(&:active?)
|
|
end
|
|
|
|
def system_message
|
|
{
|
|
role: 'system',
|
|
content: Captain::Llm::SystemPromptsService.copilot_response_generator(
|
|
@assistant.config['product_name'],
|
|
tools_summary,
|
|
@assistant.config
|
|
)
|
|
}
|
|
end
|
|
|
|
def tools_summary
|
|
@tools.map { |tool| "- #{tool.class.name}: #{tool.class.description}" }.join("\n")
|
|
end
|
|
|
|
def account_id_context
|
|
{
|
|
role: 'system',
|
|
content: "The current account id is #{@account.id}. The account is using #{@account.locale_english_name} as the language."
|
|
}
|
|
end
|
|
|
|
def current_viewing_history(conversation_id)
|
|
conversation = @account.conversations.find_by(display_id: conversation_id)
|
|
return [] unless conversation
|
|
|
|
Rails.logger.info("#{self.class.name} Assistant: #{@assistant.id}, Setting viewing history for conversation_id=#{conversation_id}")
|
|
contact_id = conversation.contact_id
|
|
[{
|
|
role: 'system',
|
|
content: <<~HISTORY.strip
|
|
You are currently viewing the conversation with the following details:
|
|
Conversation ID: #{conversation_id}
|
|
Contact ID: #{contact_id}
|
|
HISTORY
|
|
}]
|
|
end
|
|
|
|
def persist_message(message, message_type = 'assistant')
|
|
return if @copilot_thread.blank?
|
|
|
|
@copilot_thread.copilot_messages.create!(
|
|
message: message,
|
|
message_type: message_type
|
|
)
|
|
end
|
|
|
|
def feature_name
|
|
'copilot'
|
|
end
|
|
end
|