|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class EmbeddingDefinition < ActiveRecord::Base |
| 4 | + CLOUDFLARE = "cloudflare" |
| 5 | + DISCOURSE = "discourse" |
| 6 | + HUGGING_FACE = "hugging_face" |
| 7 | + OPEN_AI = "open_ai" |
| 8 | + GEMINI = "gemini" |
| 9 | + |
| 10 | + class << self |
| 11 | + def provider_names |
| 12 | + [CLOUDFLARE, DISCOURSE, HUGGING_FACE, OPEN_AI, GEMINI] |
| 13 | + end |
| 14 | + |
| 15 | + def tokenizer_names |
| 16 | + [ |
| 17 | + DiscourseAi::Tokenizer::AllMpnetBaseV2Tokenizer, |
| 18 | + DiscourseAi::Tokenizer::BgeLargeEnTokenizer, |
| 19 | + DiscourseAi::Tokenizer::BgeM3Tokenizer, |
| 20 | + DiscourseAi::Tokenizer::OpenAiTokenizer, |
| 21 | + DiscourseAi::Tokenizer::MultilingualE5LargeTokenizer, |
| 22 | + DiscourseAi::Tokenizer::OpenAiTokenizer, |
| 23 | + ].map(&:name) |
| 24 | + end |
| 25 | + |
| 26 | + def self.provider_params |
| 27 | + { discourse: { model_name: :text }, open_ai: { model_name: :text } } |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + validates :provider, presence: true, inclusion: provider_names |
| 32 | + validates :display_name, presence: true, length: { maximum: 100 } |
| 33 | + validates :tokenizer_class, presence: true, inclusion: tokenizer_names |
| 34 | + |
| 35 | + def tokenizer |
| 36 | + tokenizer_class.constantize |
| 37 | + end |
| 38 | + |
| 39 | + def inference_client |
| 40 | + case provider |
| 41 | + when CLOUDFLARE |
| 42 | + cloudflare_client |
| 43 | + when DISCOURSE |
| 44 | + discourse_client |
| 45 | + when HUGGING_FACE |
| 46 | + hugging_face_client |
| 47 | + when OPEN_AI |
| 48 | + open_ai_client |
| 49 | + when GEMINI |
| 50 | + gemini_client |
| 51 | + else |
| 52 | + raise "Uknown embeddings provider" |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def lookup_custom_param(key) |
| 57 | + provider_params&.dig(key) |
| 58 | + end |
| 59 | + |
| 60 | + def endpoint_url |
| 61 | + return url if !url.starts_with?("srv://") |
| 62 | + |
| 63 | + service = DiscourseAi::Utils::DnsSrv.lookup(url) |
| 64 | + "https://#{service.target}:#{service.port}" |
| 65 | + end |
| 66 | + |
| 67 | + def prepare_query_text(text, asymetric: false) |
| 68 | + strategy.prepare_query_text(text, self, asymetric: asymetric) |
| 69 | + end |
| 70 | + |
| 71 | + def prepare_target_text(target) |
| 72 | + strategy.prepare_target_text(target, self) |
| 73 | + end |
| 74 | + |
| 75 | + def strategy_id |
| 76 | + strategy.id |
| 77 | + end |
| 78 | + |
| 79 | + def strategy_version |
| 80 | + strategy.version |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def strategy |
| 86 | + @strategy ||= DiscourseAi::Embeddings::Strategies::Truncation.new |
| 87 | + end |
| 88 | + |
| 89 | + def cloudflare_client |
| 90 | + DiscourseAi::Inference::CloudflareWorkersAi.new(endpoint_url, api_key) |
| 91 | + end |
| 92 | + |
| 93 | + def discourse_client |
| 94 | + client_url = endpoint_url |
| 95 | + client_url = "#{client_url}/api/v1/classify" if url.starts_with?("srv://") |
| 96 | + |
| 97 | + DiscourseAi::Inference::DiscourseClassifier.new( |
| 98 | + client_url, |
| 99 | + api_key, |
| 100 | + lookup_custom_param("model_name"), |
| 101 | + ) |
| 102 | + end |
| 103 | + |
| 104 | + def hugging_face_client |
| 105 | + DiscourseAi::Inference::HuggingFaceTextEmbeddings.new(endpoint_url, api_key) |
| 106 | + end |
| 107 | + |
| 108 | + def open_ai_client |
| 109 | + DiscourseAi::Inference::OpenAiEmbeddings.new( |
| 110 | + endpoint_url, |
| 111 | + api_key, |
| 112 | + lookup_custom_param("model_name"), |
| 113 | + dimensions, |
| 114 | + ) |
| 115 | + end |
| 116 | + |
| 117 | + def gemini_client |
| 118 | + DiscourseAi::Inference::GeminiEmbeddings.new(endpoint_url, api_key) |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +# == Schema Information |
| 123 | +# |
| 124 | +# Table name: embedding_definitions |
| 125 | +# |
| 126 | +# id :bigint not null, primary key |
| 127 | +# display_name :string not null |
| 128 | +# dimensions :integer not null |
| 129 | +# max_sequence_length :integer not null |
| 130 | +# version :integer default(1), not null |
| 131 | +# pg_function :string not null |
| 132 | +# provider :string not null |
| 133 | +# tokenizer_class :string not null |
| 134 | +# url :string not null |
| 135 | +# api_key :string |
| 136 | +# provider_params :jsonb |
| 137 | +# created_at :datetime not null |
| 138 | +# updated_at :datetime not null |
| 139 | +# |
0 commit comments