2023-03-07 14:14:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
module ::DiscourseAi
|
2023-03-07 14:14:39 -05:00
|
|
|
module Inference
|
2023-03-15 16:02:20 -04:00
|
|
|
class OpenAiEmbeddings
|
2024-11-25 11:12:43 -05: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-20 20:39:51 -04:00
|
|
|
headers = { "Content-Type" => "application/json" }
|
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
if endpoint.include?("azure")
|
|
|
|
headers["api-key"] = api_key
|
2023-06-20 20:39:51 -04:00
|
|
|
else
|
2024-11-25 11:12:43 -05:00
|
|
|
headers["Authorization"] = "Bearer #{api_key}"
|
2023-06-20 20:39:51 -04:00
|
|
|
end
|
2023-03-07 14:14:39 -05:00
|
|
|
|
2024-01-29 11:24:30 -05:00
|
|
|
payload = { model: model, input: content }
|
|
|
|
payload[:dimensions] = dimensions if dimensions.present?
|
2023-03-07 14:14:39 -05:00
|
|
|
|
2024-02-21 15:14:50 -05:00
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
response = conn.post(SiteSetting.ai_openai_embeddings_url, payload.to_json, headers)
|
2024-01-24 13:57:18 -05:00
|
|
|
|
|
|
|
case response.status
|
|
|
|
when 200
|
2024-11-25 11:12:43 -05:00
|
|
|
JSON.parse(response.body, symbolize_names: true).dig(:data, 0, :embedding)
|
2024-01-24 13:57:18 -05:00
|
|
|
when 429
|
|
|
|
# TODO add a AdminDashboard Problem?
|
|
|
|
else
|
2023-06-20 20:39:51 -04:00
|
|
|
Rails.logger.warn(
|
|
|
|
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
|
|
)
|
|
|
|
raise Net::HTTPBadResponse
|
|
|
|
end
|
2023-03-07 14:14:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|