2023-11-28 15:05:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ::DiscourseAi
|
|
|
|
module Inference
|
|
|
|
class HuggingFaceTextEmbeddings
|
|
|
|
def self.perform!(content)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
2023-12-07 08:36:56 -05:00
|
|
|
body = { inputs: content, truncate: true }.to_json
|
2023-11-28 15:05:26 -05:00
|
|
|
|
2023-12-18 11:21:21 -05:00
|
|
|
if SiteSetting.ai_hugging_face_tei_endpoint_srv.present?
|
|
|
|
service = DiscourseAi::Utils::DnsSrv.lookup(SiteSetting.ai_hugging_face_tei_endpoint_srv)
|
|
|
|
api_endpoint = "https://#{service.target}:#{service.port}"
|
|
|
|
else
|
|
|
|
api_endpoint = SiteSetting.ai_hugging_face_tei_endpoint
|
|
|
|
end
|
2023-11-28 15:05:26 -05:00
|
|
|
|
2024-01-10 17:23:07 -05:00
|
|
|
if SiteSetting.ai_hugging_face_tei_api_key.present?
|
|
|
|
headers["X-API-KEY"] = SiteSetting.ai_hugging_face_tei_api_key
|
|
|
|
end
|
|
|
|
|
2023-11-28 15:05:26 -05:00
|
|
|
response = Faraday.post(api_endpoint, body, headers)
|
|
|
|
|
|
|
|
raise Net::HTTPBadResponse if ![200].include?(response.status)
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
2023-12-18 11:21:21 -05:00
|
|
|
|
|
|
|
def self.configured?
|
|
|
|
SiteSetting.ai_hugging_face_tei_endpoint.present? ||
|
|
|
|
SiteSetting.ai_hugging_face_tei_endpoint_srv.present?
|
|
|
|
end
|
2023-11-28 15:05:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|