2023-12-28 08:28:01 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
|
|
|
module VectorRepresentations
|
|
|
|
class Gemini < Base
|
2024-02-01 14:54:09 -05:00
|
|
|
class << self
|
|
|
|
def name
|
|
|
|
"gemini"
|
|
|
|
end
|
|
|
|
|
|
|
|
def correctly_configured?
|
|
|
|
SiteSetting.ai_gemini_api_key.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def dependant_setting_names
|
|
|
|
%w[ai_gemini_api_key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-28 08:28:01 -05:00
|
|
|
def id
|
|
|
|
5
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
def dimensions
|
|
|
|
768
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_sequence_length
|
2024-02-27 16:52:53 -05:00
|
|
|
1536 # Gemini has a max sequence length of 2048, but the API has a limit of 10000 bytes, hence the lower value
|
2023-12-28 08:28:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def pg_function
|
|
|
|
"<=>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pg_index_type
|
2024-08-08 10:55:20 -04:00
|
|
|
"halfvec_cosine_ops"
|
2023-12-28 08:28:01 -05:00
|
|
|
end
|
|
|
|
|
2024-03-08 11:02:50 -05:00
|
|
|
def vector_from(text, asymetric: false)
|
2024-11-25 11:12:43 -05:00
|
|
|
inference_client.perform!(text).dig(:embedding, :values)
|
2023-12-28 08:28:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# There is no public tokenizer for Gemini, and from the ones we already ship in the plugin
|
|
|
|
# OpenAI gets the closest results. Gemini Tokenizer results in ~10% less tokens, so it's safe
|
|
|
|
# to use OpenAI tokenizer since it will overestimate the number of tokens.
|
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::OpenAiTokenizer
|
|
|
|
end
|
2024-11-25 11:12:43 -05:00
|
|
|
|
|
|
|
def inference_client
|
|
|
|
DiscourseAi::Inference::GeminiEmbeddings.instance
|
|
|
|
end
|
2023-12-28 08:28:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|