## Description Routes the remaining system-only and legacy-sensitive LLM jobs through feature-level model configuration, while preserving system credential usage and usage-accounting behavior. This adds dedicated defaults for help center article generation, onboarding content generation, query translation, transcription, and search embeddings so these flows can be configured per account without falling back to installation-wide model settings. Fixes https://linear.app/chatwoot/issue/CW-7425/test-new-models ## 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? Verified the feature routing defaults and account overrides for the touched Captain/system LLM paths, including the legacy OpenAI transcription and paginated FAQ services. - `eval "$(rbenv init -)" && bundle exec rspec spec/lib/captain/base_task_service_spec.rb spec/lib/llm/models_spec.rb spec/models/concerns/captain_featurable_spec.rb spec/controllers/api/v1/accounts/captain/preferences_controller_spec.rb spec/enterprise/services/captain/llm/paginated_faq_generator_service_spec.rb spec/enterprise/services/captain/llm/pdf_processing_service_spec.rb spec/enterprise/services/messages/audio_transcription_service_spec.rb spec/enterprise/services/onboarding/help_center_article_builder_spec.rb spec/enterprise/services/captain/onboarding/website_analyzer_service_spec.rb` - `eval "$(rbenv init -)" && bundle exec rubocop app/controllers/api/v1/accounts/captain/preferences_controller.rb app/models/concerns/account_settings_schema.rb lib/captain/base_task_service.rb enterprise/app/services/captain/llm/article_translation_service.rb enterprise/app/services/captain/llm/article_writer_service.rb enterprise/app/services/captain/llm/embedding_service.rb enterprise/app/services/captain/llm/help_center_curation_service.rb enterprise/app/services/captain/llm/paginated_faq_generator_service.rb enterprise/app/services/captain/llm/translate_query_service.rb enterprise/app/services/captain/llm/widget_tagline_service.rb enterprise/app/services/captain/onboarding/website_analyzer_service.rb enterprise/app/services/messages/audio_transcription_service.rb spec/enterprise/services/messages/audio_transcription_service_spec.rb spec/lib/captain/base_task_service_spec.rb` - `ruby -e "require 'yaml'; config = YAML.load_file('config/llm.yml'); %w[document_faq_generation help_center_article_generation onboarding_content_generation help_center_query_translation audio_transcription help_center_search].each { |feature| abort(%(missing #{feature})) unless config.dig('features', feature) }; abort('wrong article default') unless config.dig('features', 'help_center_article_generation', 'default') == 'gpt-5.2'; puts 'llm.yml ok'"` - `git diff --check` ## 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
58 lines
1.5 KiB
Ruby
58 lines
1.5 KiB
Ruby
class Captain::Llm::TranslateQueryService < Captain::BaseTaskService
|
|
pattr_initialize [:account!]
|
|
|
|
def translate(query, target_language:)
|
|
return query if query_in_target_language?(query)
|
|
|
|
messages = [
|
|
{ role: 'system', content: system_prompt(target_language) },
|
|
{ role: 'user', content: query }
|
|
]
|
|
|
|
response = make_api_call(feature: 'help_center_query_translation', messages: messages)
|
|
return query if response[:error]
|
|
|
|
response[:message].strip
|
|
rescue StandardError => e
|
|
Rails.logger.warn "TranslateQueryService failed: #{e.message}, falling back to original query"
|
|
query
|
|
end
|
|
|
|
private
|
|
|
|
def event_name
|
|
'translate_query'
|
|
end
|
|
|
|
# Translation is an internal operation, not customer-initiated.
|
|
# It should always use the installation key.
|
|
def llm_credential
|
|
@llm_credential ||= system_llm_credential
|
|
end
|
|
|
|
def counts_toward_usage?
|
|
false
|
|
end
|
|
|
|
def query_in_target_language?(query)
|
|
detector = CLD3::NNetLanguageIdentifier.new(0, 1000)
|
|
result = detector.find_language(query)
|
|
|
|
result.reliable? && result.language == account_language_code
|
|
rescue StandardError
|
|
false
|
|
end
|
|
|
|
def account_language_code
|
|
account.locale&.split('_')&.first
|
|
end
|
|
|
|
def system_prompt(target_language)
|
|
<<~SYSTEM_PROMPT_MESSAGE
|
|
You are a helpful assistant that translates queries from one language to another.
|
|
Translate the query to #{target_language}.
|
|
Return just the translated query, no other text.
|
|
SYSTEM_PROMPT_MESSAGE
|
|
end
|
|
end
|