2023-10-04 12:47:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
|
|
|
module VectorRepresentations
|
|
|
|
class BgeLargeEn < Base
|
2024-02-01 14:54:09 -05:00
|
|
|
class << self
|
|
|
|
def name
|
|
|
|
"bge-large-en"
|
|
|
|
end
|
|
|
|
|
|
|
|
def correctly_configured?
|
|
|
|
SiteSetting.ai_cloudflare_workers_api_token.present? ||
|
|
|
|
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_cloudflare_workers_api_token
|
|
|
|
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)
|
|
|
|
text = "#{asymmetric_query_prefix} #{text}" if asymetric
|
|
|
|
|
2023-11-28 15:05:26 -05:00
|
|
|
if SiteSetting.ai_cloudflare_workers_api_token.present?
|
|
|
|
DiscourseAi::Inference::CloudflareWorkersAi
|
|
|
|
.perform!(inference_model_name, { text: text })
|
|
|
|
.dig(:result, :data)
|
|
|
|
.first
|
2023-12-18 11:21:21 -05:00
|
|
|
elsif DiscourseAi::Inference::HuggingFaceTextEmbeddings.configured?
|
2023-12-07 08:36:56 -05:00
|
|
|
truncated_text = tokenizer.truncate(text, max_sequence_length - 2)
|
|
|
|
DiscourseAi::Inference::HuggingFaceTextEmbeddings.perform!(truncated_text).first
|
2024-01-10 17:23:07 -05:00
|
|
|
elsif discourse_embeddings_endpoint.present?
|
2023-11-28 15:05:26 -05:00
|
|
|
DiscourseAi::Inference::DiscourseClassifier.perform!(
|
2024-01-10 17:23:07 -05:00
|
|
|
"#{discourse_embeddings_endpoint}/api/v1/classify",
|
2023-11-28 15:05:26 -05:00
|
|
|
inference_model_name.split("/").last,
|
|
|
|
text,
|
|
|
|
SiteSetting.ai_embeddings_discourse_service_api_key,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
raise "No inference endpoint configured"
|
|
|
|
end
|
2023-10-04 12:47:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inference_model_name
|
|
|
|
"baai/bge-large-en-v1.5"
|
|
|
|
end
|
|
|
|
|
|
|
|
def dimensions
|
|
|
|
1024
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_sequence_length
|
|
|
|
512
|
|
|
|
end
|
|
|
|
|
|
|
|
def id
|
|
|
|
4
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
def pg_function
|
|
|
|
"<#>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pg_index_type
|
2024-08-08 10:55:20 -04:00
|
|
|
"halfvec_ip_ops"
|
2023-10-04 12:47:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::BgeLargeEnTokenizer
|
|
|
|
end
|
2024-03-08 11:02:50 -05:00
|
|
|
|
|
|
|
def asymmetric_query_prefix
|
|
|
|
"Represent this sentence for searching relevant passages:"
|
|
|
|
end
|
2023-10-04 12:47:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|