diff --git a/lib/embeddings/semantic_search.rb b/lib/embeddings/semantic_search.rb index 5a40ec2c..36f64051 100644 --- a/lib/embeddings/semantic_search.rb +++ b/lib/embeddings/semantic_search.rb @@ -114,7 +114,7 @@ module DiscourseAi SiteSetting.ai_embeddings_semantic_search_hyde_model, ).completion!(prompt, @guardian.user) - Nokogiri::HTML5.fragment(llm_response).at("ai").text.presence || llm_response + Nokogiri::HTML5.fragment(llm_response).at("ai")&.text&.presence || llm_response end end end diff --git a/lib/embeddings/vector_representations/bge_large_en.rb b/lib/embeddings/vector_representations/bge_large_en.rb index dbdd377f..19a25ad1 100644 --- a/lib/embeddings/vector_representations/bge_large_en.rb +++ b/lib/embeddings/vector_representations/bge_large_en.rb @@ -11,7 +11,8 @@ module DiscourseAi .dig(:result, :data) .first elsif SiteSetting.ai_hugging_face_tei_endpoint.present? - DiscourseAi::Inference::HuggingFaceTextEmbeddings.perform!(text).first + truncated_text = tokenizer.truncate(text, max_sequence_length - 2) + DiscourseAi::Inference::HuggingFaceTextEmbeddings.perform!(truncated_text).first elsif SiteSetting.ai_embeddings_discourse_service_api_endpoint.present? DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_embeddings_discourse_service_api_endpoint}/api/v1/classify", diff --git a/lib/embeddings/vector_representations/multilingual_e5_large.rb b/lib/embeddings/vector_representations/multilingual_e5_large.rb index 9789f199..e968f060 100644 --- a/lib/embeddings/vector_representations/multilingual_e5_large.rb +++ b/lib/embeddings/vector_representations/multilingual_e5_large.rb @@ -6,7 +6,8 @@ module DiscourseAi class MultilingualE5Large < Base def vector_from(text) if SiteSetting.ai_hugging_face_tei_endpoint.present? - DiscourseAi::Inference::HuggingFaceTextEmbeddings.perform!(text).first + truncated_text = tokenizer.truncate(text, max_sequence_length - 2) + DiscourseAi::Inference::HuggingFaceTextEmbeddings.perform!(truncated_text).first elsif SiteSetting.ai_embeddings_discourse_service_api_endpoint.present? DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_embeddings_discourse_service_api_endpoint}/api/v1/classify", diff --git a/lib/inference/hugging_face_text_embeddings.rb b/lib/inference/hugging_face_text_embeddings.rb index e8968dde..47dd3f27 100644 --- a/lib/inference/hugging_face_text_embeddings.rb +++ b/lib/inference/hugging_face_text_embeddings.rb @@ -5,7 +5,7 @@ module ::DiscourseAi class HuggingFaceTextEmbeddings def self.perform!(content) headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" } - body = { inputs: content }.to_json + body = { inputs: content, truncate: true }.to_json api_endpoint = SiteSetting.ai_hugging_face_tei_endpoint