mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-10-28 13:08:38 +00:00
* start summary module * chat channel summarization * FEATURE: modal for channel summarization --------- Co-authored-by: Roman Rizzi <rizziromanalejandro@gmail.com>
35 lines
982 B
Ruby
35 lines
982 B
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class OpenAiCompletions
|
|
CompletionFailed = Class.new(StandardError)
|
|
|
|
def self.perform!(messages, model = SiteSetting.ai_helper_model)
|
|
headers = {
|
|
"Authorization" => "Bearer #{SiteSetting.ai_openai_api_key}",
|
|
"Content-Type" => "application/json",
|
|
}
|
|
|
|
connection_opts = { request: { write_timeout: 60, read_timeout: 60, open_timeout: 60 } }
|
|
|
|
response =
|
|
Faraday.new(nil, connection_opts).post(
|
|
"https://api.openai.com/v1/chat/completions",
|
|
{ model: model, messages: messages }.to_json,
|
|
headers,
|
|
)
|
|
|
|
if response.status != 200
|
|
Rails.logger.error(
|
|
"OpenAiCompletions: status: #{response.status} - body: #{response.body}",
|
|
)
|
|
raise CompletionFailed
|
|
end
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
end
|
|
end
|
|
end
|
|
end
|