discourse-ai/lib/shared/inference_manager.rb

18 lines
514 B
Ruby
Raw Normal View History

2023-02-22 18:46:53 -05:00
# frozen_string_literal: true
module ::DiscourseAI
class InferenceManager
def self.perform!(endpoint, model, content, api_key)
2023-02-22 18:48:51 -05:00
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
2023-02-22 18:46:53 -05:00
2023-02-22 18:48:51 -05:00
headers["X-API-KEY"] = api_key if api_key.present?
2023-02-22 18:46:53 -05:00
2023-02-22 18:48:51 -05:00
response = Faraday.post(endpoint, { model: model, content: content }.to_json, headers)
2023-02-22 18:46:53 -05:00
raise Net::HTTPBadResponse unless response.status == 200
JSON.parse(response.body, symbolize_names: true)
2023-02-22 18:46:53 -05:00
end
end
end