2023-09-05 10:08:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
|
|
|
module VectorRepresentations
|
|
|
|
class MultilingualE5Large < Base
|
2024-02-01 14:54:09 -05:00
|
|
|
class << self
|
|
|
|
def name
|
|
|
|
"multilingual-e5-large"
|
|
|
|
end
|
|
|
|
|
|
|
|
def correctly_configured?
|
|
|
|
DiscourseAi::Inference::HuggingFaceTextEmbeddings.configured? ||
|
|
|
|
(
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint_srv.present? ||
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint.present?
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependant_setting_names
|
|
|
|
%w[
|
|
|
|
ai_hugging_face_tei_endpoint_srv
|
|
|
|
ai_hugging_face_tei_endpoint
|
|
|
|
ai_embeddings_discourse_service_api_key
|
|
|
|
ai_embeddings_discourse_service_api_endpoint_srv
|
|
|
|
ai_embeddings_discourse_service_api_endpoint
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
def vector_from(text, asymetric: false)
|
2024-11-25 11:12:43 -05:00
|
|
|
client = inference_client
|
|
|
|
|
|
|
|
needs_truncation = client.class.name.include?("HuggingFaceTextEmbeddings")
|
|
|
|
if needs_truncation
|
|
|
|
text = tokenizer.truncate(text, max_sequence_length - 2)
|
2024-11-26 12:12:32 -05:00
|
|
|
elsif !text.starts_with?("query:")
|
2024-11-25 11:12:43 -05:00
|
|
|
text = "query: #{text}"
|
2023-11-28 15:05:26 -05:00
|
|
|
end
|
2024-11-25 11:12:43 -05:00
|
|
|
|
|
|
|
client.perform!(text)
|
2023-09-05 10:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def id
|
|
|
|
3
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
def dimensions
|
|
|
|
1024
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_sequence_length
|
|
|
|
512
|
|
|
|
end
|
|
|
|
|
|
|
|
def pg_function
|
|
|
|
"<=>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pg_index_type
|
2024-08-08 10:55:20 -04:00
|
|
|
"halfvec_cosine_ops"
|
2023-09-05 10:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::MultilingualE5LargeTokenizer
|
|
|
|
end
|
2024-11-25 11:12:43 -05:00
|
|
|
|
|
|
|
def inference_client
|
|
|
|
if DiscourseAi::Inference::HuggingFaceTextEmbeddings.configured?
|
|
|
|
DiscourseAi::Inference::HuggingFaceTextEmbeddings.instance
|
|
|
|
elsif SiteSetting.ai_embeddings_discourse_service_api_endpoint_srv.present? ||
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint.present?
|
|
|
|
DiscourseAi::Inference::DiscourseClassifier.instance(self.class.name)
|
|
|
|
else
|
|
|
|
raise "No inference endpoint configured"
|
|
|
|
end
|
|
|
|
end
|
2024-11-26 12:12:32 -05:00
|
|
|
|
|
|
|
def prepare_text(record)
|
2024-11-26 13:54:20 -05:00
|
|
|
prepared_text = super(record)
|
|
|
|
|
|
|
|
if prepared_text.present? && inference_client.class.name.include?("DiscourseClassifier")
|
|
|
|
return "query: #{prepared_text}"
|
2024-11-26 12:12:32 -05:00
|
|
|
end
|
|
|
|
|
2024-11-26 13:54:20 -05:00
|
|
|
prepared_text
|
2024-11-26 12:12:32 -05:00
|
|
|
end
|
2023-09-05 10:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|