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-11-25 13:12:43 -03:00
|
|
|
def initialize(endpoint, api_key, model, dimensions)
|
|
|
|
@endpoint = endpoint
|
|
|
|
@api_key = api_key
|
|
|
|
@model = model
|
|
|
|
@dimensions = dimensions
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :endpoint, :api_key, :model, :dimensions
|
|
|
|
|
|
|
|
def self.instance(model:, dimensions: nil)
|
|
|
|
new(SiteSetting.ai_openai_embeddings_url, SiteSetting.ai_openai_api_key, model, dimensions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform!(content)
|
2023-06-21 10:39:51 +10:00
|
|
|
headers = { "Content-Type" => "application/json" }
|
|
|
|
|
2024-11-25 13:12:43 -03:00
|
|
|
if endpoint.include?("azure")
|
|
|
|
headers["api-key"] = api_key
|
2023-06-21 10:39:51 +10:00
|
|
|
else
|
2024-11-25 13:12:43 -03:00
|
|
|
headers["Authorization"] = "Bearer #{api_key}"
|
2023-06-21 10:39:51 +10:00
|
|
|
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
|
2024-11-25 13:12:43 -03:00
|
|
|
JSON.parse(response.body, symbolize_names: true).dig(:data, 0, :embedding)
|
2024-01-24 15:57:18 -03:00
|
|
|
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
|