2023-11-28 15:05:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ::DiscourseAi
|
|
|
|
module Inference
|
|
|
|
class HuggingFaceTextEmbeddings
|
2024-11-25 11:12:43 -05:00
|
|
|
def initialize(endpoint, key, referer = Discourse.base_url)
|
|
|
|
@endpoint = endpoint
|
|
|
|
@key = key
|
|
|
|
@referer = referer
|
|
|
|
end
|
2024-03-08 11:02:50 -05:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
attr_reader :endpoint, :key, :referer
|
2024-03-08 11:02:50 -05:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
class << self
|
|
|
|
def instance
|
|
|
|
endpoint =
|
|
|
|
if SiteSetting.ai_hugging_face_tei_endpoint_srv.present?
|
|
|
|
service =
|
|
|
|
DiscourseAi::Utils::DnsSrv.lookup(SiteSetting.ai_hugging_face_tei_endpoint_srv)
|
|
|
|
"https://#{service.target}:#{service.port}"
|
|
|
|
else
|
|
|
|
SiteSetting.ai_hugging_face_tei_endpoint
|
|
|
|
end
|
|
|
|
|
|
|
|
new(endpoint, SiteSetting.ai_hugging_face_tei_api_key)
|
|
|
|
end
|
2024-03-08 11:02:50 -05:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
def configured?
|
|
|
|
SiteSetting.ai_hugging_face_tei_endpoint.present? ||
|
|
|
|
SiteSetting.ai_hugging_face_tei_endpoint_srv.present?
|
|
|
|
end
|
2024-03-08 11:02:50 -05:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
def reranker_configured?
|
|
|
|
SiteSetting.ai_hugging_face_tei_reranker_endpoint.present? ||
|
|
|
|
SiteSetting.ai_hugging_face_tei_reranker_endpoint_srv.present?
|
2024-01-10 17:23:07 -05:00
|
|
|
end
|
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
def rerank(content, candidates)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
|
|
|
body = { query: content, texts: candidates, truncate: true }.to_json
|
2023-11-28 15:05:26 -05:00
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
if SiteSetting.ai_hugging_face_tei_reranker_endpoint_srv.present?
|
|
|
|
service =
|
|
|
|
DiscourseAi::Utils::DnsSrv.lookup(
|
|
|
|
SiteSetting.ai_hugging_face_tei_reranker_endpoint_srv,
|
|
|
|
)
|
|
|
|
api_endpoint = "https://#{service.target}:#{service.port}"
|
|
|
|
else
|
|
|
|
api_endpoint = SiteSetting.ai_hugging_face_tei_reranker_endpoint
|
|
|
|
end
|
2023-11-28 15:05:26 -05:00
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
if SiteSetting.ai_hugging_face_tei_reranker_api_key.present?
|
|
|
|
headers["X-API-KEY"] = SiteSetting.ai_hugging_face_tei_reranker_api_key
|
2024-10-16 14:57:36 -04:00
|
|
|
headers["Authorization"] = "Bearer #{SiteSetting.ai_hugging_face_tei_reranker_api_key}"
|
2024-03-08 11:02:50 -05:00
|
|
|
end
|
2023-12-18 11:21:21 -05:00
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
response = conn.post("#{api_endpoint}/rerank", body, headers)
|
|
|
|
|
|
|
|
if response.status != 200
|
|
|
|
raise Net::HTTPBadResponse.new("Status: #{response.status}\n\n#{response.body}")
|
|
|
|
end
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
def classify(content, model_config)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
|
|
|
headers["X-API-KEY"] = model_config.api_key
|
|
|
|
headers["Authorization"] = "Bearer #{model_config.api_key}"
|
|
|
|
|
|
|
|
body = { inputs: content, truncate: true }.to_json
|
|
|
|
|
|
|
|
api_endpoint = model_config.endpoint
|
|
|
|
if api_endpoint.present? && api_endpoint.start_with?("srv://")
|
|
|
|
service = DiscourseAi::Utils::DnsSrv.lookup(api_endpoint.delete_prefix("srv://"))
|
|
|
|
api_endpoint = "https://#{service.target}:#{service.port}"
|
|
|
|
end
|
|
|
|
|
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
response = conn.post(api_endpoint, body, headers)
|
|
|
|
|
|
|
|
if response.status != 200
|
|
|
|
raise Net::HTTPBadResponse.new("Status: #{response.status}\n\n#{response.body}")
|
|
|
|
end
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
|
|
end
|
2024-11-25 11:12:43 -05:00
|
|
|
end
|
2024-11-04 07:14:34 -05:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
def perform!(content)
|
|
|
|
headers = { "Referer" => referer, "Content-Type" => "application/json" }
|
|
|
|
body = { inputs: content, truncate: true }.to_json
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
|
2024-11-25 11:12:43 -05:00
|
|
|
if key.present?
|
|
|
|
headers["X-API-KEY"] = key
|
|
|
|
headers["Authorization"] = "Bearer #{key}"
|
2024-03-08 11:02:50 -05:00
|
|
|
end
|
2024-11-25 11:12:43 -05:00
|
|
|
|
|
|
|
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
|
|
|
|
response = conn.post(endpoint, body, headers)
|
|
|
|
|
|
|
|
raise Net::HTTPBadResponse if ![200].include?(response.status)
|
|
|
|
|
|
|
|
JSON.parse(response.body, symbolize_names: true).first
|
2023-12-18 11:21:21 -05:00
|
|
|
end
|
2023-11-28 15:05:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|