|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyLLM |
| 4 | + module ModelCapabilities |
| 5 | + # Determines capabilities and pricing for Google Gemini models |
| 6 | + module Gemini # rubocop:disable Metrics/ModuleLength |
| 7 | + module_function |
| 8 | + |
| 9 | + def context_window_for(model_id) |
| 10 | + case model_id |
| 11 | + when /gemini-2\.0-flash/ then 1_048_576 |
| 12 | + when /gemini-1\.5-pro/ then 2_097_152 |
| 13 | + when /gemini-1\.5/ then 1_048_576 |
| 14 | + when /text-embedding/, /embedding-001/ then 2_048 |
| 15 | + else 32_768 # Sensible default for unknown models |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + def max_tokens_for(model_id) |
| 20 | + case model_id |
| 21 | + when /gemini-2\.0-flash/, /gemini-1\.5/ then 8_192 |
| 22 | + when /text-embedding/, /embedding-001/ then 768 # Output dimension size for embeddings |
| 23 | + when /aqa/ then 1_024 |
| 24 | + else 4_096 # Sensible default |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def input_price_for(model_id) |
| 29 | + PRICES.dig(pricing_family(model_id), :input) || default_input_price |
| 30 | + end |
| 31 | + |
| 32 | + def output_price_for(model_id) |
| 33 | + PRICES.dig(pricing_family(model_id), :output) || default_output_price |
| 34 | + end |
| 35 | + |
| 36 | + def supports_vision?(model_id) |
| 37 | + return false if model_id.match?(/text-embedding|embedding-001|aqa/) |
| 38 | + return false if model_id.match?(/flash-lite/) |
| 39 | + return false if model_id.match?(/imagen/) |
| 40 | + |
| 41 | + # Only pro and regular flash models support vision |
| 42 | + model_id.match?(/gemini-[12]\.(?:5|0)-(?:pro|flash)(?!-lite)/) |
| 43 | + end |
| 44 | + |
| 45 | + def supports_functions?(model_id) |
| 46 | + return false if model_id.match?(/text-embedding|embedding-001|aqa/) |
| 47 | + return false if model_id.match?(/imagen/) |
| 48 | + return false if model_id.match?(/flash-lite/) |
| 49 | + return false if model_id.match?(/bison|gecko|evergreen/) |
| 50 | + |
| 51 | + # Currently only full models support function calling |
| 52 | + model_id.match?(/gemini-[12]\.(?:5|0)-(?:pro|flash)(?!-lite)/) |
| 53 | + end |
| 54 | + |
| 55 | + def supports_json_mode?(model_id) |
| 56 | + return false if model_id.match?(/text-embedding|embedding-001|aqa/) |
| 57 | + return false if model_id.match?(/imagen/) |
| 58 | + return false if model_id.match?(/flash-lite/) |
| 59 | + return false if model_id.match?(/bison|gecko|evergreen/) |
| 60 | + |
| 61 | + # Gemini 1.5+ models support JSON mode |
| 62 | + model_id.match?(/gemini-[12]\.(?:5|0)-(?:pro|flash)(?!-lite)/) |
| 63 | + end |
| 64 | + |
| 65 | + def format_display_name(model_id) |
| 66 | + return model_id unless model_id.start_with?('models/') |
| 67 | + |
| 68 | + model_id |
| 69 | + .delete_prefix('models/') |
| 70 | + .split('-') |
| 71 | + .map(&:capitalize) |
| 72 | + .join(' ') |
| 73 | + .gsub(/(\d+\.\d+)/, ' \1') # Add space before version numbers |
| 74 | + .gsub(/\s+/, ' ') # Clean up multiple spaces |
| 75 | + .strip |
| 76 | + end |
| 77 | + |
| 78 | + def model_type(model_id) |
| 79 | + case model_id |
| 80 | + when /text-embedding|embedding/ then 'embedding' |
| 81 | + when /imagen/ then 'image' |
| 82 | + when /bison|text-bison/ then 'legacy' |
| 83 | + else 'chat' |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + def model_family(model_id) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength |
| 88 | + case model_id |
| 89 | + when /gemini-2\.0-flash-lite/ then 'gemini20_flash_lite' |
| 90 | + when /gemini-2\.0-flash/ then 'gemini20_flash' |
| 91 | + when /gemini-1\.5-flash-8b/ then 'gemini15_flash_8b' |
| 92 | + when /gemini-1\.5-flash/ then 'gemini15_flash' |
| 93 | + when /gemini-1\.5-pro/ then 'gemini15_pro' |
| 94 | + when /text-embedding-004/ then 'embedding4' |
| 95 | + when /embedding-001/ then 'embedding1' |
| 96 | + when /bison|text-bison/ then 'bison' |
| 97 | + when /imagen/ then 'imagen3' |
| 98 | + else 'other' |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def pricing_family(model_id) |
| 103 | + case model_id |
| 104 | + when /gemini-2\.0-flash-lite/ then :flash_lite_2 # rubocop:disable Naming/VariableNumber |
| 105 | + when /gemini-2\.0-flash/ then :flash_2 # rubocop:disable Naming/VariableNumber |
| 106 | + when /gemini-1\.5-flash-8b/ then :flash_8b |
| 107 | + when /gemini-1\.5-flash/ then :flash |
| 108 | + when /gemini-1\.5-pro/ then :pro |
| 109 | + when /text-embedding|embedding/ then :embedding |
| 110 | + else :base |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + PRICES = { |
| 115 | + flash_2: { input: 0.10, output: 0.40 }, # Gemini 2.0 Flash # rubocop:disable Naming/VariableNumber |
| 116 | + flash_lite_2: { input: 0.075, output: 0.30 }, # Gemini 2.0 Flash Lite # rubocop:disable Naming/VariableNumber |
| 117 | + flash: { input: 0.075, output: 0.30 }, # Gemini 1.5 Flash basic pricing |
| 118 | + flash_8b: { input: 0.0375, output: 0.15 }, # Gemini 1.5 Flash 8B |
| 119 | + pro: { input: 1.25, output: 5.0 }, # Gemini 1.5 Pro |
| 120 | + embedding: { input: 0.00, output: 0.00 } # Text Embedding models are free |
| 121 | + }.freeze |
| 122 | + |
| 123 | + def default_input_price |
| 124 | + 0.075 # Default to Flash pricing |
| 125 | + end |
| 126 | + |
| 127 | + def default_output_price |
| 128 | + 0.30 # Default to Flash pricing |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | +end |
0 commit comments