mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-08 18:29:32 +00:00
This allows admins to configure services with multiple backends using DNS SRV records. This PR also adds support for shared secret auth via headers for TEI and vLLM endpoints, so they are inline with the other ones.
35 lines
1.1 KiB
Ruby
35 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class HuggingFaceTextEmbeddings
|
|
def self.perform!(content)
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
|
body = { inputs: content, truncate: true }.to_json
|
|
|
|
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
|
|
|
|
if SiteSetting.ai_hugging_face_tei_api_key.present?
|
|
headers["X-API-KEY"] = SiteSetting.ai_hugging_face_tei_api_key
|
|
end
|
|
|
|
response = Faraday.post(api_endpoint, body, headers)
|
|
|
|
raise Net::HTTPBadResponse if ![200].include?(response.status)
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
end
|
|
|
|
def self.configured?
|
|
SiteSetting.ai_hugging_face_tei_endpoint.present? ||
|
|
SiteSetting.ai_hugging_face_tei_endpoint_srv.present?
|
|
end
|
|
end
|
|
end
|
|
end
|