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 DiscourseClassifier
|
|
|
|
def self.perform!(endpoint, model, content, api_key)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
|
|
|
|
|
|
|
headers["X-API-KEY"] = api_key if api_key.present?
|
|
|
|
|
|
|
|
response = Faraday.post(endpoint, { model: model, content: content }.to_json, headers)
|
|
|
|
|
2023-05-11 14:35:39 -04:00
|
|
|
raise Net::HTTPBadResponse if ![200, 415].include?(response.status)
|
2023-03-07 14:14:39 -05:00
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|