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
|
2023-03-22 15:00:28 -04:00
|
|
|
CompletionFailed = Class.new(StandardError)
|
|
|
|
|
2023-04-04 10:24:09 -04:00
|
|
|
def self.perform!(messages, model = SiteSetting.ai_helper_model)
|
2023-03-07 14:14:39 -05:00
|
|
|
headers = {
|
|
|
|
"Authorization" => "Bearer #{SiteSetting.ai_openai_api_key}",
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:16:29 -04:00
|
|
|
connection_opts = { request: { write_timeout: 60, read_timeout: 60, open_timeout: 60 } }
|
2023-03-20 15:43:51 -04:00
|
|
|
|
2023-03-07 14:14:39 -05:00
|
|
|
response =
|
2023-03-20 15:43:51 -04:00
|
|
|
Faraday.new(nil, connection_opts).post(
|
2023-03-07 14:14:39 -05:00
|
|
|
"https://api.openai.com/v1/chat/completions",
|
2023-03-21 11:04:59 -04:00
|
|
|
{ model: model, messages: messages }.to_json,
|
2023-03-07 14:14:39 -05:00
|
|
|
headers,
|
|
|
|
)
|
|
|
|
|
2023-03-22 15:00:28 -04:00
|
|
|
if response.status != 200
|
|
|
|
Rails.logger.error(
|
|
|
|
"OpenAiCompletions: status: #{response.status} - body: #{response.body}",
|
|
|
|
)
|
|
|
|
raise CompletionFailed
|
|
|
|
end
|
2023-03-07 14:14:39 -05:00
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|