diff --git a/.env.example b/.env.example index 69b1b9cde..c9f3c855c 100644 --- a/.env.example +++ b/.env.example @@ -272,9 +272,9 @@ AZURE_APP_SECRET= # ENABLE_SIDEKIQ_DEQUEUE_LOGGER=false -# AI powered features -## OpenAI key -# OPENAI_API_KEY= +# AI powered features (Captain) +# The OpenAI API key and endpoint for Captain are not configured via .env. +# Set them at Super Admin > App Configs > Captain (CAPTAIN_OPEN_AI_API_KEY, CAPTAIN_OPEN_AI_ENDPOINT). # Housekeeping/Performance related configurations # Set to true if you want to remove stale contact inboxes diff --git a/enterprise/app/models/enterprise/concerns/article.rb b/enterprise/app/models/enterprise/concerns/article.rb index 9482313fd..6be262fef 100644 --- a/enterprise/app/models/enterprise/concerns/article.rb +++ b/enterprise/app/models/enterprise/concerns/article.rb @@ -67,7 +67,7 @@ module Enterprise::Concerns::Article { role: 'system', content: article_to_search_terms_prompt }, { role: 'user', content: "title: #{title} \n description: #{description} \n content: #{content}" } ] - headers = { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{ENV.fetch('OPENAI_API_KEY', nil)}" } + headers = { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{openai_api_key}" } body = { model: 'gpt-4o', messages: messages, response_format: { type: 'json_object' } }.to_json Rails.logger.info "Requesting Chat GPT with body: #{body}" response = HTTParty.post(openai_api_url, headers: headers, body: body) @@ -77,8 +77,12 @@ module Enterprise::Concerns::Article private + def openai_api_key + InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_API_KEY')&.value.presence || raise(I18n.t('captain.api_key_missing')) + end + def openai_api_url - endpoint = InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_ENDPOINT')&.value || 'https://api.openai.com/' + endpoint = InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_ENDPOINT')&.value.presence || 'https://api.openai.com/' endpoint = endpoint.chomp('/') "#{endpoint}/v1/chat/completions" end diff --git a/spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb index 4187adfea..e4b2bfe70 100644 --- a/spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/applied_slas_controller_spec.rb @@ -144,7 +144,8 @@ RSpec.describe 'Applied SLAs API', type: :request do csv_data = CSV.parse(response.body) csv_data.reject! { |row| row.all?(&:nil?) } expect(csv_data.size).to eq(3) - expect(csv_data[1][0].to_i).to eq(conversation1.display_id) + conversation_ids = csv_data.drop(1).map { |row| row[0].to_i } + expect(conversation_ids).to contain_exactly(conversation1.display_id, conversation2.display_id) end it 'excludes conversations with blocked contacts from the CSV file' do