|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyLLM |
| 4 | + # Manages model aliases, allowing users to reference models by simpler names |
| 5 | + # that map to specific model versions across different providers. |
| 6 | + # |
| 7 | + # Aliases are defined in aliases.json and follow the format: |
| 8 | + # { |
| 9 | + # "simple-name": { |
| 10 | + # "provider1": "specific-version-for-provider1", |
| 11 | + # "provider2": "specific-version-for-provider2" |
| 12 | + # } |
| 13 | + # } |
| 14 | + class Aliases |
| 15 | + class << self |
| 16 | + # Resolves a model ID to its provider-specific version |
| 17 | + # |
| 18 | + # @param model_id [String] the model identifier or alias |
| 19 | + # @param provider_slug [String, Symbol, nil] optional provider to resolve for |
| 20 | + # @return [String] the resolved model ID or the original if no alias exists |
| 21 | + def resolve(model_id, provider_slug = nil) |
| 22 | + provider_aliases = aliases[model_id] |
| 23 | + return model_id unless provider_aliases |
| 24 | + |
| 25 | + if provider_slug |
| 26 | + provider_aliases[provider_slug.to_s] || model_id |
| 27 | + else |
| 28 | + provider_aliases.values.first || model_id |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # Returns the loaded aliases mapping |
| 33 | + # @return [Hash] the aliases mapping |
| 34 | + def aliases |
| 35 | + @aliases ||= load_aliases |
| 36 | + end |
| 37 | + |
| 38 | + # Loads aliases from the JSON file |
| 39 | + # @return [Hash] the loaded aliases |
| 40 | + def load_aliases |
| 41 | + file_path = File.expand_path('aliases.json', __dir__) |
| 42 | + if File.exist?(file_path) |
| 43 | + JSON.parse(File.read(file_path)) |
| 44 | + else |
| 45 | + {} |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + # Reloads aliases from disk |
| 50 | + # @return [Hash] the reloaded aliases |
| 51 | + def reload! |
| 52 | + @aliases = load_aliases |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | +end |
0 commit comments