2023-03-07 16:14:39 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-14 16:03:50 -03:00
|
|
|
module ::DiscourseAi
|
2023-03-07 16:14:39 -03:00
|
|
|
module Inference
|
2023-03-15 17:02:20 -03:00
|
|
|
class OpenAiEmbeddings
|
2024-01-30 03:24:30 +11:00
|
|
|
def self.perform!(content, model:, dimensions: nil)
|
2023-06-21 10:39:51 +10:00
|
|
|
headers = { "Content-Type" => "application/json" }
|
|
|
|
|
2024-01-13 00:28:06 +01:00
|
|
|
if SiteSetting.ai_openai_embeddings_url.include?("azure")
|
2023-06-21 10:39:51 +10:00
|
|
|
headers["api-key"] = SiteSetting.ai_openai_api_key
|
|
|
|
else
|
|
|
|
headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}"
|
|
|
|
end
|
2023-03-07 16:14:39 -03:00
|
|
|
|
2024-01-30 03:24:30 +11:00
|
|
|
payload = { model: model, input: content }
|
|
|
|
payload[:dimensions] = dimensions if dimensions.present?
|
2023-03-07 16:14:39 -03:00
|
|
|
|
2024-02-21 17:14:50 -03:00
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
response = conn.post(SiteSetting.ai_openai_embeddings_url, payload.to_json, headers)
|
2024-01-24 15:57:18 -03:00
|
|
|
|
|
|
|
case response.status
|
|
|
|
when 200
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
when 429
|
|
|
|
# TODO add a AdminDashboard Problem?
|
|
|
|
else
|
2023-06-21 10:39:51 +10:00
|
|
|
Rails.logger.warn(
|
|
|
|
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
|
|
)
|
|
|
|
raise Net::HTTPBadResponse
|
|
|
|
end
|
2023-03-07 16:14:39 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|