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
|
|
|
|
class DiscourseReranker
|
|
|
|
def self.perform!(endpoint, model, content, candidates, api_key)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
|
|
|
|
|
|
|
headers["X-API-KEY"] = api_key if api_key.present?
|
|
|
|
|
2024-02-21 15:14:50 -05:00
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
2023-03-07 14:14:39 -05:00
|
|
|
response =
|
2024-02-21 15:14:50 -05:00
|
|
|
conn.post(
|
2023-03-07 14:14:39 -05:00
|
|
|
endpoint,
|
|
|
|
{ model: model, content: content, candidates: candidates }.to_json,
|
|
|
|
headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
raise Net::HTTPBadResponse unless response.status == 200
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|