is_even_ai 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsEvenAI
4
+ class Configuration
5
+ PRIMITIVES = %i[noul choice score].freeze
6
+
7
+ DEFAULTS = {
8
+ provider: nil,
9
+ api_key: nil,
10
+ model: nil,
11
+ base_url: nil,
12
+ jev_primitive: :noul,
13
+ yes_threshold: 0.7,
14
+ no_threshold: 0.3,
15
+ strict_confidence: false,
16
+ allow_non_integer: false,
17
+ fallback_provider: nil,
18
+ fallback_options: {},
19
+ on_uncertain: nil,
20
+ cache: false,
21
+ open_timeout: 5,
22
+ read_timeout: 30,
23
+ max_retries: 2,
24
+ fan_out: false
25
+ }.freeze
26
+
27
+ # Per-call keyword aliases.
28
+ ALIASES = { primitive: :jev_primitive }.freeze
29
+
30
+ API_KEY_ENV = {
31
+ jev: %w[TYPESAFE_API_KEY],
32
+ openai: %w[OPENAI_API_KEY],
33
+ anthropic: %w[ANTHROPIC_API_KEY],
34
+ gemini: %w[GEMINI_API_KEY GOOGLE_API_KEY],
35
+ openai_compatible: %w[OPENAI_API_KEY IS_EVEN_AI_API_KEY]
36
+ }.freeze
37
+
38
+ # Order matters: first present key wins when IS_EVEN_AI_PROVIDER is not set.
39
+ DETECTION_ORDER = [
40
+ [:jev, %w[TYPESAFE_API_KEY]],
41
+ [:openai, %w[OPENAI_API_KEY]],
42
+ [:anthropic, %w[ANTHROPIC_API_KEY]],
43
+ [:gemini, %w[GEMINI_API_KEY GOOGLE_API_KEY]],
44
+ [:openai_compatible, %w[IS_EVEN_AI_API_KEY]]
45
+ ].freeze
46
+
47
+ attr_accessor(*DEFAULTS.keys)
48
+
49
+ def initialize
50
+ DEFAULTS.each { |key, value| public_send(:"#{key}=", value.dup) }
51
+ end
52
+
53
+ def initialize_copy(source)
54
+ super
55
+ @fallback_options = source.fallback_options.dup
56
+ end
57
+
58
+ # A copy with per-call overrides applied and validated.
59
+ def merge(overrides = {})
60
+ copy = dup
61
+ overrides.each do |key, value|
62
+ key = ALIASES.fetch(key.to_sym, key.to_sym)
63
+ raise ConfigurationError, "Unknown option #{key.inspect}" unless DEFAULTS.key?(key)
64
+
65
+ copy.public_send(:"#{key}=", value)
66
+ end
67
+ copy.validate!
68
+ copy
69
+ end
70
+
71
+ def validate!
72
+ validate_provider!(provider, "provider")
73
+ validate_provider!(fallback_provider, "fallback_provider")
74
+ raise ConfigurationError, "Unknown jev_primitive #{jev_primitive.inspect}; expected one of #{PRIMITIVES.join(", ")}" unless PRIMITIVES.include?(jev_primitive&.to_sym)
75
+
76
+ validate_thresholds!
77
+ raise ConfigurationError, "fallback_options must be a Hash" unless fallback_options.is_a?(Hash)
78
+ raise ConfigurationError, "max_retries must be a non-negative Integer" unless max_retries.is_a?(Integer) && max_retries >= 0
79
+
80
+ %i[open_timeout read_timeout].each do |key|
81
+ value = public_send(key)
82
+ raise ConfigurationError, "#{key} must be a positive number" unless value.is_a?(Numeric) && value.positive?
83
+ end
84
+ self
85
+ end
86
+
87
+ def primitive
88
+ jev_primitive.to_sym
89
+ end
90
+
91
+ def resolved_provider
92
+ explicit = provider || env("IS_EVEN_AI_PROVIDER")
93
+ return explicit.to_sym if explicit
94
+
95
+ DETECTION_ORDER.each do |name, vars|
96
+ return name if vars.any? { |var| env(var) }
97
+ end
98
+ raise ConfigurationError,
99
+ "No provider configured. Set config.provider, IS_EVEN_AI_PROVIDER, or one of: " \
100
+ "TYPESAFE_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GOOGLE_API_KEY, IS_EVEN_AI_API_KEY."
101
+ end
102
+
103
+ def resolved_api_key(name = resolved_provider)
104
+ api_key || API_KEY_ENV.fetch(name.to_sym, []).filter_map { |var| env(var) }.first
105
+ end
106
+
107
+ def resolved_base_url(name = resolved_provider)
108
+ return base_url if base_url
109
+
110
+ case name.to_sym
111
+ when :jev then env("TYPESAFE_BASE_URL") || env("IS_EVEN_AI_BASE_URL")
112
+ when :openai_compatible then env("IS_EVEN_AI_BASE_URL")
113
+ end
114
+ end
115
+
116
+ # Configuration used for System 2 escalation. Credentials, model and URL of the
117
+ # primary provider never leak into the fallback.
118
+ def fallback_config
119
+ copy = dup
120
+ copy.provider = fallback_provider
121
+ copy.api_key = nil
122
+ copy.model = nil
123
+ copy.base_url = nil
124
+ copy.fallback_provider = nil
125
+ copy.fallback_options = {}
126
+ copy.on_uncertain = nil
127
+ copy.strict_confidence = false
128
+ copy.fan_out = false
129
+ copy.merge(fallback_options)
130
+ end
131
+
132
+ def inspect
133
+ pairs = DEFAULTS.keys.map do |key|
134
+ value = key == :api_key && api_key ? "[REDACTED]" : public_send(key).inspect
135
+ "#{key}=#{value}"
136
+ end
137
+ "#<#{self.class} #{pairs.join(" ")}>"
138
+ end
139
+
140
+ private
141
+
142
+ def env(name)
143
+ value = ENV.fetch(name, nil)
144
+ value unless value.nil? || value.strip.empty?
145
+ end
146
+
147
+ def validate_provider!(name, label)
148
+ return if name.nil?
149
+ return if Providers::Registry.known?(name)
150
+
151
+ raise ConfigurationError,
152
+ "Unknown #{label} #{name.inspect}; expected one of #{Providers::Registry.names.join(", ")}"
153
+ end
154
+
155
+ def validate_thresholds!
156
+ [yes_threshold, no_threshold].each do |value|
157
+ raise ConfigurationError, "Thresholds must be numbers between 0.0 and 1.0" unless value.is_a?(Numeric) && value.between?(0, 1)
158
+ end
159
+ return if no_threshold <= yes_threshold
160
+
161
+ raise ConfigurationError, "no_threshold (#{no_threshold}) must not exceed yes_threshold (#{yes_threshold})"
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsEvenAI
4
+ class Error < StandardError; end
5
+
6
+ # Missing API key, unknown provider, invalid primitive or thresholds.
7
+ class ConfigurationError < Error; end
8
+
9
+ # A non-Integer was passed while allow_non_integer is false, or nil was passed at all.
10
+ class InvalidInputError < Error; end
11
+
12
+ class APIError < Error
13
+ attr_reader :status_code, :response_body, :provider
14
+
15
+ def initialize(message = nil, status_code: nil, response_body: nil, provider: nil)
16
+ super(message)
17
+ @status_code = status_code
18
+ @response_body = response_body
19
+ @provider = provider
20
+ end
21
+ end
22
+
23
+ class RateLimitError < APIError
24
+ attr_reader :retry_after
25
+
26
+ def initialize(message = nil, retry_after: nil, **details)
27
+ super(message, **details)
28
+ @retry_after = retry_after
29
+ end
30
+ end
31
+
32
+ # Malformed or unexpected provider payload.
33
+ class ParseError < Error; end
34
+
35
+ # Parity could not be established with enough certainty.
36
+ class UncertainParityError < Error
37
+ attr_reader :result
38
+
39
+ def initialize(message = "Parity is uncertain", result: nil)
40
+ super(message)
41
+ @result = result
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "json"
6
+ require "uri"
7
+
8
+ module IsEvenAI
9
+ # Minimal stdlib JSON-over-HTTPS client with retries (exponential backoff, full jitter).
10
+ class HttpClient
11
+ RETRYABLE_STATUSES = [429, 500, 502, 503, 504, 529].freeze
12
+ NETWORK_ERRORS = [
13
+ Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, Errno::ECONNRESET, EOFError
14
+ ].freeze
15
+ BILLING_MARKERS = %w[credit_balance_exhausted insufficient_quota billing_hard_limit_reached].freeze
16
+ WRITE_TIMEOUT = 10
17
+ BASE_DELAY = 0.5
18
+ MAX_DELAY = 8.0
19
+ MAX_RETRY_AFTER = 30.0
20
+
21
+ class << self
22
+ # Swappable so specs never actually sleep.
23
+ attr_writer :sleeper
24
+
25
+ def sleeper
26
+ @sleeper ||= ->(seconds) { sleep(seconds) }
27
+ end
28
+ end
29
+
30
+ def initialize(provider:, open_timeout: 5, read_timeout: 30, max_retries: 2)
31
+ @provider = provider
32
+ @open_timeout = open_timeout
33
+ @read_timeout = read_timeout
34
+ @max_retries = max_retries
35
+ end
36
+
37
+ # POSTs a JSON body and returns the parsed JSON response.
38
+ def post_json(url, body, headers = {})
39
+ uri = URI.parse(url)
40
+ payload = JSON.generate(body)
41
+ attempts = 0
42
+
43
+ loop do
44
+ attempts += 1
45
+ response = perform(uri, payload, headers)
46
+ status = response.code.to_i
47
+ return parse_body(response) if status.between?(200, 299)
48
+
49
+ if retryable?(status, response) && attempts <= @max_retries
50
+ pause(retry_delay(response, attempts))
51
+ next
52
+ end
53
+ raise_http_error(status, response)
54
+ rescue *NETWORK_ERRORS => e
55
+ raise api_error("#{@provider} request failed: #{e.class}: #{e.message}") if attempts > @max_retries
56
+
57
+ pause(backoff(attempts))
58
+ rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError => e
59
+ raise api_error("#{@provider} connection failed: #{e.class}: #{e.message}")
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def perform(uri, payload, headers)
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = uri.scheme == "https"
68
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
69
+ http.open_timeout = @open_timeout
70
+ http.read_timeout = @read_timeout
71
+ http.write_timeout = WRITE_TIMEOUT
72
+ http.max_retries = 0 # our retry policy is the only retry policy
73
+
74
+ request = Net::HTTP::Post.new(uri.request_uri)
75
+ request["Content-Type"] = "application/json"
76
+ request["Accept"] = "application/json"
77
+ request["User-Agent"] = "is_even_ai/#{VERSION}"
78
+ headers.each { |name, value| request[name] = value }
79
+ request.body = payload
80
+ http.start { |connection| connection.request(request) }
81
+ end
82
+
83
+ def parse_body(response)
84
+ parsed = JSON.parse(response.body.to_s)
85
+ return parsed if parsed.is_a?(Hash)
86
+
87
+ raise ParseError, "#{@provider} returned JSON that is not an object: #{response.body.to_s[0, 200]}"
88
+ rescue JSON::ParserError => e
89
+ raise ParseError, "#{@provider} returned malformed JSON (#{e.message[0, 80]}): #{response.body.to_s[0, 200]}"
90
+ end
91
+
92
+ def retryable?(status, response)
93
+ return false unless RETRYABLE_STATUSES.include?(status)
94
+
95
+ body = response.body.to_s
96
+ BILLING_MARKERS.none? { |marker| body.include?(marker) }
97
+ end
98
+
99
+ def retry_delay(response, attempts)
100
+ after = parse_retry_after(response)
101
+ after ? [after, MAX_RETRY_AFTER].min : backoff(attempts)
102
+ end
103
+
104
+ def backoff(attempts)
105
+ rand * [MAX_DELAY, BASE_DELAY * (2**(attempts - 1))].min
106
+ end
107
+
108
+ def pause(seconds)
109
+ self.class.sleeper.call(seconds)
110
+ end
111
+
112
+ def parse_retry_after(response)
113
+ value = response["retry-after"]
114
+ Float(value, exception: false)&.then { |seconds| seconds.negative? ? nil : seconds }
115
+ end
116
+
117
+ def raise_http_error(status, response)
118
+ body = response.body.to_s
119
+ message = "#{@provider} API error (HTTP #{status}): #{error_message(body)}"
120
+ details = { status_code: status, response_body: body, provider: @provider }
121
+ raise RateLimitError.new(message, retry_after: parse_retry_after(response), **details) if status == 429
122
+
123
+ raise APIError.new(message, **details)
124
+ end
125
+
126
+ def api_error(message)
127
+ APIError.new(message, provider: @provider)
128
+ end
129
+
130
+ def error_message(body)
131
+ data = JSON.parse(body)
132
+ error = data["error"] if data.is_a?(Hash)
133
+ text = error.is_a?(Hash) ? error["message"] : error
134
+ (text || data.to_s).to_s[0, 300]
135
+ rescue JSON::ParserError
136
+ body[0, 300]
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsEvenAI
4
+ module Providers
5
+ # Anthropic Messages API, version 2026-10-21 (RESEARCH.md section 3.2): Bearer auth, no sampling
6
+ # parameters (they return HTTP 400), GA structured output via output_config.format.
7
+ class Anthropic < Base
8
+ DEFAULT_BASE_URL = "https://api.anthropic.com"
9
+ PATH = "/v1/messages"
10
+ API_VERSION = "2026-10-21"
11
+
12
+ def self.provider_name = :anthropic
13
+
14
+ def default_model = "claude-haiku-5-5"
15
+
16
+ def call(input)
17
+ headers = { "Authorization" => "Bearer #{api_key!}", "anthropic-version" => API_VERSION }
18
+ body = http.post_json(endpoint(DEFAULT_BASE_URL, PATH), request_body(input), headers)
19
+ parse(input, body)
20
+ end
21
+
22
+ private
23
+
24
+ # Only haiku accepts thinking: disabled. Other models think adaptively and thinking tokens
25
+ # count toward max_tokens, so they get a larger ceiling.
26
+ def thinking_disabled?
27
+ model.to_s.start_with?("claude-haiku")
28
+ end
29
+
30
+ def request_body(input)
31
+ body = {
32
+ "model" => model,
33
+ "max_tokens" => thinking_disabled? ? 256 : 1024,
34
+ "system" => system_prompt(input),
35
+ "messages" => [{ "role" => "user", "content" => user_prompt(input) }],
36
+ "output_config" => { "format" => { "type" => "json_schema", "schema" => RESULT_SCHEMA } }
37
+ }
38
+ body["thinking"] = { "type" => "disabled" } if thinking_disabled?
39
+ body
40
+ end
41
+
42
+ def parse(input, body)
43
+ case body["stop_reason"]
44
+ when "refusal" then raise ParseError, "anthropic refused the request"
45
+ when "max_tokens" then raise ParseError, "anthropic response was truncated (max_tokens)"
46
+ end
47
+
48
+ text = Array(body["content"]).find { |block| block["type"] == "text" }&.fetch("text", nil)
49
+ usage = body["usage"].is_a?(Hash) ? token_usage(body["usage"]["input_tokens"], body["usage"]["output_tokens"]) : nil
50
+ generative_result(input, text: text, model: body["model"] || model, usage: usage, raw: body)
51
+ end
52
+ end
53
+
54
+ Registry.register(:anthropic, Anthropic)
55
+ end
56
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsEvenAI
4
+ module Providers
5
+ # Shared contract for providers. Subclasses implement #call(input) -> Result and
6
+ # #default_model; generative providers reuse the prompt and parsing helpers below.
7
+ class Base
8
+ LOCAL_HOSTS = %w[localhost 127.0.0.1 ::1 [::1] 0.0.0.0 host.docker.internal].freeze
9
+
10
+ RESULT_SCHEMA = {
11
+ "type" => "object",
12
+ "properties" => {
13
+ "is_even" => { "type" => "boolean" },
14
+ "confidence" => { "type" => "number" },
15
+ "reasoning" => { "type" => "string" }
16
+ },
17
+ "required" => %w[is_even confidence reasoning],
18
+ "additionalProperties" => false
19
+ }.freeze
20
+
21
+ SYSTEM_PROMPT = <<~PROMPT
22
+ You are a rigorous parity-checking service. Determine whether the supplied value is an even integer (an integer divisible by 2 with no remainder).
23
+ Respond only with JSON matching the schema: is_even (boolean), confidence (number from 0 to 1 describing how certain you are), reasoning (one short sentence).
24
+ PROMPT
25
+
26
+ NON_INTEGER_PROMPT = <<~PROMPT
27
+ The value may not be an integer. Evaluate whether it semantically represents an even integer: for example "42", "forty-two", 4.0 and "a dozen" do; "a baker's dozen" (13), 4.5, "banana" and infinity do not.
28
+ When the value does not represent an even integer, set is_even to false, lower your confidence for values that are not integers at all, and explain why.
29
+ PROMPT
30
+
31
+ attr_reader :config
32
+
33
+ def self.provider_name
34
+ raise NotImplementedError, "#{name} must define .provider_name"
35
+ end
36
+
37
+ def initialize(config)
38
+ @config = config
39
+ end
40
+
41
+ def call(_input)
42
+ raise NotImplementedError, "#{self.class} must implement #call"
43
+ end
44
+
45
+ def default_model
46
+ nil
47
+ end
48
+
49
+ def model
50
+ config.model || default_model
51
+ end
52
+
53
+ def name
54
+ self.class.provider_name
55
+ end
56
+
57
+ private
58
+
59
+ def http
60
+ @http ||= HttpClient.new(
61
+ provider: name, open_timeout: config.open_timeout,
62
+ read_timeout: config.read_timeout, max_retries: config.max_retries
63
+ )
64
+ end
65
+
66
+ def api_key!
67
+ key = config.resolved_api_key(name)
68
+ return key if key
69
+
70
+ env_vars = Configuration::API_KEY_ENV.fetch(name, []).join(" or ")
71
+ hint = env_vars.empty? ? "config.api_key" : "#{env_vars} or config.api_key"
72
+ raise ConfigurationError, "Missing API key for provider :#{name}. Set #{hint}."
73
+ end
74
+
75
+ # Joins the configured (or default) base URL and an API path, tolerating a trailing "/v1".
76
+ def endpoint(default_base, path)
77
+ base = (config.resolved_base_url(name) || default_base).to_s.chomp("/")
78
+ return base if base.end_with?(path)
79
+
80
+ base = base.delete_suffix("/v1") if path.start_with?("/v1/")
81
+ "#{base}#{path}"
82
+ end
83
+
84
+ def non_integer?(input)
85
+ !input.is_a?(Integer)
86
+ end
87
+
88
+ # JSON-safe representation of the input value.
89
+ def serialize_value(input)
90
+ case input
91
+ when Integer, String then input
92
+ when Float then input.finite? ? input : input.to_s
93
+ when Numeric, Symbol then input.to_s
94
+ else input.inspect
95
+ end
96
+ end
97
+
98
+ def system_prompt(input)
99
+ non_integer?(input) ? "#{SYSTEM_PROMPT}#{NON_INTEGER_PROMPT}" : SYSTEM_PROMPT
100
+ end
101
+
102
+ def user_prompt(input)
103
+ "Value: #{serialize_value(input).inspect}\nRuby type: #{input.class.name}"
104
+ end
105
+
106
+ # Maps a calibrated probability onto [even, uncertain] using the configured bands.
107
+ def decide(probability)
108
+ if probability >= config.yes_threshold
109
+ [true, false]
110
+ elsif probability <= config.no_threshold
111
+ [false, false]
112
+ else
113
+ [probability >= 0.5, true]
114
+ end
115
+ end
116
+
117
+ def parse_json_text(text)
118
+ raise ParseError, "#{name} returned no text content" if text.nil? || text.strip.empty?
119
+
120
+ JSON.parse(text)
121
+ rescue JSON::ParserError
122
+ embedded = text[/\{.*\}/m]
123
+ begin
124
+ embedded ? JSON.parse(embedded) : raise(ParseError, "#{name} returned non-JSON text: #{text[0, 200]}")
125
+ rescue JSON::ParserError
126
+ raise ParseError, "#{name} returned non-JSON text: #{text[0, 200]}"
127
+ end
128
+ end
129
+
130
+ # Builds a Result from the structured {is_even, confidence, reasoning} answer of a generative model.
131
+ def generative_result(input, text:, model:, usage:, raw:)
132
+ data = parse_json_text(text)
133
+ raise ParseError, "#{name} answer is missing a boolean is_even: #{text[0, 200]}" unless data.is_a?(Hash) && [true, false].include?(data["is_even"])
134
+
135
+ confidence = data["confidence"].is_a?(Numeric) ? data["confidence"].to_f.clamp(0.0, 1.0) : nil
136
+ Result.new(
137
+ input: input, even: data["is_even"], provider: name, model: model, non_integer: non_integer?(input),
138
+ uncertain: !confidence.nil? && confidence <= config.no_threshold,
139
+ confidence: confidence, reasoning: data["reasoning"], usage: usage, raw: raw
140
+ )
141
+ end
142
+
143
+ def token_usage(input_tokens, output_tokens)
144
+ return nil if input_tokens.nil? && output_tokens.nil?
145
+
146
+ { input_tokens: input_tokens, output_tokens: output_tokens }
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IsEvenAI
4
+ module Providers
5
+ # Google Gemini via the legacy generateContent endpoint (RESEARCH.md section 3.3). The newer
6
+ # Interactions API is not used: its raw response shape is [UNVERIFIED] in the research.
7
+ class Gemini < Base
8
+ DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com"
9
+ BLOCKING_FINISH_REASONS = %w[SAFETY PROHIBITED_CONTENT BLOCKLIST SPII RECITATION MAX_TOKENS].freeze
10
+
11
+ def self.provider_name = :gemini
12
+
13
+ def default_model = "gemini-3.5-flash-lite"
14
+
15
+ def call(input)
16
+ url = endpoint(DEFAULT_BASE_URL, "/v1beta/models/#{model}:generateContent")
17
+ body = http.post_json(url, request_body(input), "x-goog-api-key" => api_key!)
18
+ parse(input, body)
19
+ end
20
+
21
+ private
22
+
23
+ def request_body(input)
24
+ {
25
+ "systemInstruction" => { "parts" => [{ "text" => system_prompt(input) }] },
26
+ "contents" => [{ "role" => "user", "parts" => [{ "text" => user_prompt(input) }] }],
27
+ "generationConfig" => {
28
+ "responseMimeType" => "application/json",
29
+ "responseJsonSchema" => RESULT_SCHEMA,
30
+ "maxOutputTokens" => 1024
31
+ }
32
+ }
33
+ end
34
+
35
+ def parse(input, body)
36
+ block_reason = body.dig("promptFeedback", "blockReason")
37
+ raise ParseError, "gemini blocked the prompt: #{block_reason}" if block_reason
38
+
39
+ candidate = Array(body["candidates"]).first
40
+ raise ParseError, "gemini returned no candidates" unless candidate.is_a?(Hash)
41
+
42
+ finish = candidate["finishReason"]
43
+ raise ParseError, "gemini stopped with finishReason #{finish}" if BLOCKING_FINISH_REASONS.include?(finish)
44
+
45
+ text = Array(candidate.dig("content", "parts")).find { |part| part["text"] }&.fetch("text", nil)
46
+ generative_result(input, text: text, model: body["modelVersion"] || model, usage: usage(body["usageMetadata"]), raw: body)
47
+ end
48
+
49
+ def usage(metadata)
50
+ return nil unless metadata.is_a?(Hash)
51
+
52
+ output = metadata["candidatesTokenCount"].to_i + metadata["thoughtsTokenCount"].to_i
53
+ token_usage(metadata["promptTokenCount"], output)
54
+ end
55
+ end
56
+
57
+ Registry.register(:gemini, Gemini)
58
+ end
59
+ end