mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-08 20:44:42 +00:00
The Faraday adapter and `FinalDestionation::HTTP` will protect us from admin-initiated SSRF attacks when interacting with the external services powering this plugin features.:
26 lines
728 B
Ruby
26 lines
728 B
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
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?
|
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
response =
|
|
conn.post(
|
|
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
|