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 OpenAiEmbeddings
|
2023-03-07 14:14:39 -05:00
|
|
|
def self.perform!(content, model = nil)
|
2023-06-20 20:39:51 -04:00
|
|
|
headers = { "Content-Type" => "application/json" }
|
|
|
|
|
|
|
|
if SiteSetting.ai_openai_embeddings_url.include? ("azure")
|
|
|
|
headers["api-key"] = SiteSetting.ai_openai_api_key
|
|
|
|
else
|
|
|
|
headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}"
|
|
|
|
end
|
2023-03-07 14:14:39 -05:00
|
|
|
|
|
|
|
model ||= "text-embedding-ada-002"
|
|
|
|
|
|
|
|
response =
|
|
|
|
Faraday.post(
|
2023-06-20 20:39:51 -04:00
|
|
|
SiteSetting.ai_openai_embeddings_url,
|
2023-03-07 14:14:39 -05:00
|
|
|
{ model: model, input: content }.to_json,
|
|
|
|
headers,
|
|
|
|
)
|
2023-06-20 20:39:51 -04:00
|
|
|
if response.status != 200
|
|
|
|
Rails.logger.warn(
|
|
|
|
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
|
|
)
|
|
|
|
raise Net::HTTPBadResponse
|
|
|
|
end
|
2023-03-07 14:14:39 -05:00
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|