# 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
85 lines
2.6 KiB
Ruby
85 lines
2.6 KiB
Ruby
class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
|
include Captain::ChatHelper
|
|
|
|
def initialize(assistant: nil, conversation: nil, source: nil)
|
|
super(feature: 'assistant', account: assistant&.account || conversation&.account)
|
|
|
|
@assistant = assistant
|
|
@conversation = conversation
|
|
@conversation_id = conversation&.display_id
|
|
@source = source
|
|
|
|
@messages = [system_message]
|
|
@response = ''
|
|
@tools = build_tools
|
|
end
|
|
|
|
# additional_message: A single message (String) from the user that should be appended to the chat.
|
|
# It can be an empty String or nil when you only want to supply historical messages.
|
|
# message_history: An Array of already formatted messages that provide the previous context.
|
|
# role: The role for the additional_message (defaults to `user`).
|
|
#
|
|
# NOTE: Parameters are provided as keyword arguments to improve clarity and avoid relying on
|
|
# positional ordering.
|
|
def generate_response(additional_message: nil, message_history: [], role: 'user')
|
|
@messages += message_history
|
|
@messages << { role: role, content: additional_message } if additional_message.present?
|
|
request_chat_completion
|
|
end
|
|
|
|
private
|
|
|
|
def build_tools
|
|
tools = [Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
|
|
return tools unless custom_tools_enabled?
|
|
|
|
tools + @assistant.account.captain_custom_tools.enabled.map do |ct|
|
|
ct.tool(@assistant, base_class: Captain::Tools::CustomHttpTool, conversation: @conversation)
|
|
end
|
|
end
|
|
|
|
def system_message
|
|
{
|
|
role: 'system',
|
|
content: Captain::Llm::SystemPromptsService.assistant_response_generator(
|
|
@assistant.name, @assistant.config['product_name'], @assistant.config.merge('timezone' => inbox_timezone),
|
|
contact: contact_attributes,
|
|
custom_tools: custom_tools_metadata
|
|
)
|
|
}
|
|
end
|
|
|
|
def custom_tools_metadata
|
|
return [] unless custom_tools_enabled?
|
|
|
|
@assistant.account.captain_custom_tools.enabled.map do |ct|
|
|
{ name: ct.slug, description: ct.description }
|
|
end
|
|
end
|
|
|
|
def custom_tools_enabled?
|
|
@assistant.account.feature_enabled?('custom_tools')
|
|
end
|
|
|
|
def contact_attributes
|
|
return nil unless @conversation&.contact
|
|
return nil unless @assistant&.feature_contact_attributes
|
|
|
|
@conversation.contact.attributes.symbolize_keys.slice(
|
|
:id, :name, :email, :phone_number, :identifier, :custom_attributes
|
|
)
|
|
end
|
|
|
|
def inbox_timezone
|
|
@conversation&.inbox&.timezone.presence || 'UTC'
|
|
end
|
|
|
|
def persist_message(message, message_type = 'assistant')
|
|
# No need to implement
|
|
end
|
|
|
|
def feature_name
|
|
'assistant'
|
|
end
|
|
end
|