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 OpenAiCompletions
|
|
|
|
def self.perform!(content, model = "gpt-3.5-turbo")
|
2023-03-07 14:14:39 -05:00
|
|
|
headers = {
|
|
|
|
"Authorization" => "Bearer #{SiteSetting.ai_openai_api_key}",
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
response =
|
|
|
|
Faraday.post(
|
|
|
|
"https://api.openai.com/v1/chat/completions",
|
|
|
|
{ model: model, messages: content }.to_json,
|
|
|
|
headers,
|
|
|
|
)
|
|
|
|
|
|
|
|
raise Net::HTTPBadResponse unless response.status == 200
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|