Module: EmbeddingUtil

Defined in:
lib/embedding_util.rb,
lib/embedding_util/cli.rb,
lib/embedding_util/result.rb,
lib/embedding_util/profile.rb,
lib/embedding_util/version.rb,
lib/embedding_util/profiles.rb,
lib/embedding_util/provider.rb,
lib/embedding_util/server_model.rb,
lib/embedding_util/configuration.rb,
lib/embedding_util/server_manager.rb,
lib/embedding_util/runtime_command.rb,
lib/embedding_util/provider_registry.rb,
lib/embedding_util/providers/endpoint.rb,
lib/embedding_util/providers/self_hosted.rb

Defined Under Namespace

Modules: Profiles, Providers Classes: CLI, Configuration, EmbeddingResult, EndpointError, EndpointNotFoundError, Error, Profile, Provider, ProviderRegistry, RankedDocument, RerankResult, RuntimeCommand, ServerManager, ServerModel, UnsupportedProviderError

Constant Summary collapse

VERSION =
"0.1.1"
SERVER_MODEL_PREFIXES =
{
  "embedding" => :embedding,
  "reranker" => :reranker,
  "rerank" => :reranker
}.freeze

Class Method Summary collapse

Class Method Details

.configurationObject



41
42
43
# File 'lib/embedding_util.rb', line 41

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



45
46
47
# File 'lib/embedding_util.rb', line 45

def configure
  yield configuration
end

.embed(text, **options) ⇒ Object



71
72
73
# File 'lib/embedding_util.rb', line 71

def embed(text, **options)
  embed_result(text, **options).embedding
end

.embed_many(texts, profile: configuration.resolved_profile, provider: nil, **_options) ⇒ Object



75
76
77
# File 'lib/embedding_util.rb', line 75

def embed_many(texts, profile: configuration.resolved_profile, provider: nil, **_options)
  selected_provider(provider).embed(normalize_texts(texts), profile: resolve_profile(profile)).embedding
end

.embed_result(input, profile: configuration.resolved_profile, provider: nil) ⇒ Object

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/embedding_util.rb', line 79

def embed_result(input, profile: configuration.resolved_profile, provider: nil)
  scalar = !input.is_a?(Array)
  texts = normalize_texts(input)
  result = selected_provider(provider).embed(texts, profile: resolve_profile(profile))
  return result unless scalar

  raise EndpointError, "server returned no embeddings for the given input" if result.embedding.empty?

  EmbeddingResult.new(
    embedding: result.embedding.fetch(0),
    model: result.model,
    profile: result.profile,
    provider: result.provider,
    metadata: result.
  )
end

.normalize_texts(input) ⇒ Object



112
113
114
# File 'lib/embedding_util.rb', line 112

def normalize_texts(input)
  input.is_a?(Array) ? input.map(&:to_s) : [input.to_s]
end

.profile(name = configuration.profile) ⇒ Object



108
109
110
# File 'lib/embedding_util.rb', line 108

def profile(name = configuration.profile)
  resolve_profile(name)
end

.profilesObject



104
105
106
# File 'lib/embedding_util.rb', line 104

def profiles
  Profiles.all
end

.register_provider(provider_class) ⇒ Object



63
64
65
# File 'lib/embedding_util.rb', line 63

def register_provider(provider_class)
  registry.register(provider_class)
end

.registryObject



54
55
56
57
58
59
60
61
# File 'lib/embedding_util.rb', line 54

def registry
  @registry ||= begin
    registry = ProviderRegistry.new
    registry.register(Providers::Endpoint)
    registry.register(Providers::SelfHosted)
    registry
  end
end

.rerank(query, documents, **options) ⇒ Object



96
97
98
# File 'lib/embedding_util.rb', line 96

def rerank(query, documents, **options)
  rerank_result(query, documents, **options).results
end

.rerank_result(query, documents, profile: configuration.resolved_profile, provider: nil) ⇒ Object



100
101
102
# File 'lib/embedding_util.rb', line 100

def rerank_result(query, documents, profile: configuration.resolved_profile, provider: nil)
  selected_provider(provider).rerank(query.to_s, Array(documents).map(&:to_s), profile: resolve_profile(profile))
end

.reset_configuration!Object



49
50
51
52
# File 'lib/embedding_util.rb', line 49

def reset_configuration!
  @configuration = Configuration.new
  @registry = nil
end

.resolve_profile(value) ⇒ Object



116
117
118
# File 'lib/embedding_util.rb', line 116

def resolve_profile(value)
  value.is_a?(Profile) ? value : Profiles.fetch(value)
end

.selected_provider(provider) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/embedding_util.rb', line 120

def selected_provider(provider)
  return registry.resolve(config: configuration) unless provider

  local_config = configuration.dup
  local_config.provider = provider
  registry.resolve(config: local_config)
end

.supportObject



67
68
69
# File 'lib/embedding_util.rb', line 67

def support
  registry.support(config: configuration)
end