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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +22 -0
- data/CODE_OF_CONDUCT.md +60 -0
- data/LICENSE.txt +21 -0
- data/README.md +317 -0
- data/exe/is_even_ai +7 -0
- data/lib/is_even_ai/cache.rb +46 -0
- data/lib/is_even_ai/cli.rb +146 -0
- data/lib/is_even_ai/configuration.rb +164 -0
- data/lib/is_even_ai/errors.rb +44 -0
- data/lib/is_even_ai/http_client.rb +139 -0
- data/lib/is_even_ai/providers/anthropic.rb +56 -0
- data/lib/is_even_ai/providers/base.rb +150 -0
- data/lib/is_even_ai/providers/gemini.rb +59 -0
- data/lib/is_even_ai/providers/jev.rb +179 -0
- data/lib/is_even_ai/providers/openai.rb +54 -0
- data/lib/is_even_ai/providers/openai_compatible.rb +65 -0
- data/lib/is_even_ai/providers/registry.rb +35 -0
- data/lib/is_even_ai/refinements.rb +31 -0
- data/lib/is_even_ai/result.rb +35 -0
- data/lib/is_even_ai/version.rb +5 -0
- data/lib/is_even_ai.rb +151 -0
- metadata +75 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
module Providers
|
|
5
|
+
# TypeSafe AI "System One" (Jev). Wire schemas follow RESEARCH.md section 2.3.
|
|
6
|
+
#
|
|
7
|
+
# Documented response shapes:
|
|
8
|
+
# noul -> { "type" => "noul", "noul" => 0.99 } (no confidence field)
|
|
9
|
+
# choice -> { "type", "choice", "confidence", "probabilities" }
|
|
10
|
+
# score -> { "type", "score", "confidence", "probabilities", "legend" }
|
|
11
|
+
class Jev < Base
|
|
12
|
+
DEFAULT_BASE_URL = "https://api.typesafe.ai"
|
|
13
|
+
PATH = "/v1/systemone"
|
|
14
|
+
|
|
15
|
+
PRIMARY_KEYS = { noul: "is_even", choice: "parity", score: "evenness" }.freeze
|
|
16
|
+
|
|
17
|
+
SCORE_RUBRIC = [
|
|
18
|
+
"Aggressively odd — unmistakably not divisible by 2",
|
|
19
|
+
"Probably odd — leans strongly away from evenness",
|
|
20
|
+
"Numerically ambiguous — parity cannot be confidently established",
|
|
21
|
+
"Probably even — strongly exhibits evenness",
|
|
22
|
+
"Unquestionably even — textbook divisible by 2"
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
def self.provider_name = :jev
|
|
26
|
+
|
|
27
|
+
def default_model = "jev-latest"
|
|
28
|
+
|
|
29
|
+
def call(input)
|
|
30
|
+
body = http.post_json(
|
|
31
|
+
endpoint(DEFAULT_BASE_URL, PATH),
|
|
32
|
+
request_body(input),
|
|
33
|
+
"Authorization" => "Bearer #{api_key!}"
|
|
34
|
+
)
|
|
35
|
+
build_result(input, body)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def primitive = config.primitive
|
|
41
|
+
|
|
42
|
+
def primary_key = PRIMARY_KEYS.fetch(primitive)
|
|
43
|
+
|
|
44
|
+
def request_body(input)
|
|
45
|
+
{ "model" => model, "state" => state(input), "questions" => questions(input) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def state(input)
|
|
49
|
+
return { "value" => serialize_value(input) } unless non_integer?(input)
|
|
50
|
+
|
|
51
|
+
{
|
|
52
|
+
"value" => serialize_value(input),
|
|
53
|
+
"ruby_type" => input.class.name,
|
|
54
|
+
"non_integer_override" => true
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def questions(input)
|
|
59
|
+
questions = { primary_key => primary_question(input) }
|
|
60
|
+
questions.merge!(fan_out_questions) if config.fan_out
|
|
61
|
+
questions
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def primary_question(input)
|
|
65
|
+
case primitive
|
|
66
|
+
when :noul then noul_question(input)
|
|
67
|
+
when :choice then choice_question(input)
|
|
68
|
+
when :score then score_question(input)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def noul_question(input)
|
|
73
|
+
return { "type" => "noul", "instructions" => "Is `value` an even integer (divisible by 2 with no remainder)?" } unless non_integer?(input)
|
|
74
|
+
|
|
75
|
+
{
|
|
76
|
+
"type" => "noul",
|
|
77
|
+
"instructions" => "Does `value` semantically represent an even integer? It may be a number word, a string, a float or any other object; `ruby_type` says what it was in Ruby.",
|
|
78
|
+
"criteria" => {
|
|
79
|
+
"true" => "Represents an integer divisible by 2 with remainder 0, e.g. \"42\", \"forty-two\", 4.0, \"a dozen\"",
|
|
80
|
+
"false" => "Represents an odd integer or is not an integer at all, e.g. \"a baker's dozen\", 4.5, \"banana\", infinity"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Documented choice: "even" / "odd", plus "not_an_integer" only when the override is on,
|
|
86
|
+
# so the model has somewhere to put non-integers instead of splitting probability across even/odd.
|
|
87
|
+
def choice_question(_input)
|
|
88
|
+
criteria = {
|
|
89
|
+
"even" => "An integer evenly divisible by 2",
|
|
90
|
+
"odd" => "An integer not divisible by 2"
|
|
91
|
+
}
|
|
92
|
+
criteria["not_an_integer"] = "A float, non-numeric string, or invalid number" if config.allow_non_integer
|
|
93
|
+
{ "type" => "choice", "instructions" => "What is the parity of `value`?", "criteria" => criteria }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def score_question(_input)
|
|
97
|
+
{ "type" => "score", "instructions" => "How even is `value`?", "criteria" => SCORE_RUBRIC }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def fan_out_questions
|
|
101
|
+
questions = {
|
|
102
|
+
"is_odd" => { "type" => "noul", "instructions" => "Is `value` an odd integer (not divisible by 2)?" },
|
|
103
|
+
"is_suspiciously_large" => { "type" => "noul", "instructions" => "Is `value` a suspiciously large number?" }
|
|
104
|
+
}
|
|
105
|
+
questions["represents_integer"] = { "type" => "noul", "instructions" => "Does `value` represent an integer of any kind?" } if config.allow_non_integer
|
|
106
|
+
questions
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def build_result(input, body)
|
|
110
|
+
answers = body["answers"]
|
|
111
|
+
raise ParseError, "jev response has no answers: #{body.to_s[0, 200]}" unless answers.is_a?(Hash)
|
|
112
|
+
|
|
113
|
+
answer = answers[primary_key]
|
|
114
|
+
raise ParseError, "jev response is missing the #{primary_key.inspect} answer" unless answer.is_a?(Hash)
|
|
115
|
+
|
|
116
|
+
attributes = send(:"parse_#{primitive}", answer)
|
|
117
|
+
Result.new(
|
|
118
|
+
input: input, provider: name, model: body["model"] || model, primitive: primitive,
|
|
119
|
+
non_integer: non_integer?(input), usage: symbolize_usage(body["usage"]),
|
|
120
|
+
fan_out: config.fan_out ? parse_fan_out(answers) : nil, raw: body, **attributes
|
|
121
|
+
)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def parse_noul(answer)
|
|
125
|
+
probability = number!(answer["noul"], "noul")
|
|
126
|
+
even, uncertain = decide(probability)
|
|
127
|
+
{ even: even, uncertain: uncertain, probability: probability, confidence: ((2 * probability) - 1).abs }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def parse_choice(answer)
|
|
131
|
+
choice = answer["choice"]
|
|
132
|
+
probabilities = answer["probabilities"]
|
|
133
|
+
raise ParseError, "jev choice answer is missing choice/probabilities" unless choice.is_a?(String) && probabilities.is_a?(Hash)
|
|
134
|
+
|
|
135
|
+
probability = number!(probabilities["even"], "probabilities.even")
|
|
136
|
+
confidence = answer["confidence"].is_a?(Numeric) ? answer["confidence"].to_f : nil
|
|
137
|
+
if choice == "not_an_integer"
|
|
138
|
+
return { even: false, uncertain: true, probability: probability, confidence: confidence,
|
|
139
|
+
reasoning: "Jev classified the input as not an integer" }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
even, uncertain = decide(probability)
|
|
143
|
+
{ even: even, uncertain: uncertain, probability: probability, confidence: confidence }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def parse_score(answer)
|
|
147
|
+
score = number!(answer["score"], "score")
|
|
148
|
+
normalized = (score / (SCORE_RUBRIC.size - 1)).clamp(0.0, 1.0)
|
|
149
|
+
even, uncertain = decide(normalized)
|
|
150
|
+
confidence = answer["confidence"].is_a?(Numeric) ? answer["confidence"].to_f : nil
|
|
151
|
+
{
|
|
152
|
+
even: even, uncertain: uncertain, probability: normalized, confidence: confidence,
|
|
153
|
+
reasoning: SCORE_RUBRIC[score.round.clamp(0, SCORE_RUBRIC.size - 1)]
|
|
154
|
+
}
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def parse_fan_out(answers)
|
|
158
|
+
(answers.keys - [primary_key]).to_h do |key|
|
|
159
|
+
value = answers[key]
|
|
160
|
+
[key.to_sym, value.is_a?(Hash) && value["noul"].is_a?(Numeric) ? value["noul"].to_f : nil]
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def number!(value, label)
|
|
165
|
+
return value.to_f if value.is_a?(Numeric)
|
|
166
|
+
|
|
167
|
+
raise ParseError, "jev answer #{label} is not a number: #{value.inspect}"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def symbolize_usage(usage)
|
|
171
|
+
return nil unless usage.is_a?(Hash)
|
|
172
|
+
|
|
173
|
+
token_usage(usage["input_tokens"], usage["output_tokens"])
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
Registry.register(:jev, Jev)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
module Providers
|
|
5
|
+
# OpenAI Responses API with strict structured output (RESEARCH.md section 3.1 / table in 5.2).
|
|
6
|
+
# [UNVERIFIED] The response envelope below (output[].content[].output_text) follows the Responses
|
|
7
|
+
# API reference; the research document's OpenAI section was truncated, so capture one live response.
|
|
8
|
+
class OpenAI < Base
|
|
9
|
+
DEFAULT_BASE_URL = "https://api.openai.com"
|
|
10
|
+
PATH = "/v1/responses"
|
|
11
|
+
|
|
12
|
+
def self.provider_name = :openai
|
|
13
|
+
|
|
14
|
+
def default_model = "gpt-6-luna"
|
|
15
|
+
|
|
16
|
+
def call(input)
|
|
17
|
+
body = http.post_json(
|
|
18
|
+
endpoint(DEFAULT_BASE_URL, PATH), request_body(input), "Authorization" => "Bearer #{api_key!}"
|
|
19
|
+
)
|
|
20
|
+
parse(input, body)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def request_body(input)
|
|
26
|
+
{
|
|
27
|
+
"model" => model,
|
|
28
|
+
"store" => false,
|
|
29
|
+
"reasoning" => { "effort" => "none" },
|
|
30
|
+
"max_output_tokens" => 256,
|
|
31
|
+
"instructions" => system_prompt(input),
|
|
32
|
+
"input" => user_prompt(input),
|
|
33
|
+
"text" => {
|
|
34
|
+
"format" => { "type" => "json_schema", "name" => "parity_check", "strict" => true, "schema" => RESULT_SCHEMA }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def parse(input, body)
|
|
40
|
+
raise ParseError, "openai response incomplete: #{body.dig("incomplete_details", "reason")}" if body["status"] == "incomplete"
|
|
41
|
+
|
|
42
|
+
parts = Array(body["output"]).flat_map { |item| Array(item["content"]) }
|
|
43
|
+
refusal = parts.find { |part| part["type"] == "refusal" }
|
|
44
|
+
raise ParseError, "openai refused the request: #{refusal["refusal"]}" if refusal
|
|
45
|
+
|
|
46
|
+
text = parts.find { |part| part["type"] == "output_text" }&.fetch("text", nil)
|
|
47
|
+
usage = body["usage"].is_a?(Hash) ? token_usage(body["usage"]["input_tokens"], body["usage"]["output_tokens"]) : nil
|
|
48
|
+
generative_result(input, text: text, model: body["model"] || model, usage: usage, raw: body)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Registry.register(:openai, OpenAI)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
module Providers
|
|
5
|
+
# Any server speaking OpenAI Chat Completions at {base_url}/chat/completions: Ollama, Groq,
|
|
6
|
+
# OpenRouter (including typesafe/jev-router), LiteLLM, vLLM. base_url and model are required.
|
|
7
|
+
# No token limit or sampling parameters are sent, since reasoning models reject some of them.
|
|
8
|
+
class OpenAICompatible < Base
|
|
9
|
+
def self.provider_name = :openai_compatible
|
|
10
|
+
|
|
11
|
+
def call(input)
|
|
12
|
+
raise ConfigurationError, "openai_compatible needs a model (config.model)" if config.model.nil?
|
|
13
|
+
|
|
14
|
+
url = "#{base_url!.chomp("/")}/chat/completions"
|
|
15
|
+
headers = {}
|
|
16
|
+
key = api_key_for(url)
|
|
17
|
+
headers["Authorization"] = "Bearer #{key}" if key
|
|
18
|
+
parse(input, http.post_json(url, request_body(input), headers))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def base_url!
|
|
24
|
+
config.resolved_base_url(name) ||
|
|
25
|
+
raise(ConfigurationError, "openai_compatible needs a base_url (config.base_url or IS_EVEN_AI_BASE_URL)")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Local servers (Ollama and friends) may run without a key; remote ones may not.
|
|
29
|
+
def api_key_for(url)
|
|
30
|
+
key = config.resolved_api_key(name)
|
|
31
|
+
return key if key
|
|
32
|
+
return nil if LOCAL_HOSTS.include?(URI.parse(url).host)
|
|
33
|
+
|
|
34
|
+
api_key!
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def request_body(input)
|
|
38
|
+
{
|
|
39
|
+
"model" => model,
|
|
40
|
+
"messages" => [
|
|
41
|
+
{ "role" => "system", "content" => system_prompt(input) },
|
|
42
|
+
{ "role" => "user", "content" => user_prompt(input) }
|
|
43
|
+
],
|
|
44
|
+
"response_format" => {
|
|
45
|
+
"type" => "json_schema",
|
|
46
|
+
"json_schema" => { "name" => "parity_check", "strict" => true, "schema" => RESULT_SCHEMA }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def parse(input, body)
|
|
52
|
+
choice = Array(body["choices"]).first
|
|
53
|
+
raise ParseError, "#{name} returned no choices" unless choice.is_a?(Hash)
|
|
54
|
+
|
|
55
|
+
message = choice["message"] || {}
|
|
56
|
+
raise ParseError, "#{name} refused the request: #{message["refusal"]}" if message["refusal"]
|
|
57
|
+
|
|
58
|
+
usage = body["usage"].is_a?(Hash) ? token_usage(body["usage"]["prompt_tokens"], body["usage"]["completion_tokens"]) : nil
|
|
59
|
+
generative_result(input, text: message["content"], model: body["model"] || model, usage: usage, raw: body)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
Registry.register(:openai_compatible, OpenAICompatible)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
module Providers
|
|
5
|
+
# Provider lookup. Third parties can register their own subclass of Providers::Base:
|
|
6
|
+
# IsEvenAI::Providers::Registry.register(:my_provider, MyProvider)
|
|
7
|
+
module Registry
|
|
8
|
+
@providers = {}
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def register(name, klass)
|
|
12
|
+
@providers[name.to_sym] = klass
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def fetch(name)
|
|
16
|
+
@providers.fetch(name.to_sym) do
|
|
17
|
+
raise ConfigurationError, "Unknown provider #{name.inspect}; expected one of #{names.join(", ")}"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def known?(name)
|
|
22
|
+
@providers.key?(name.to_sym)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def names
|
|
26
|
+
@providers.keys
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def build(config)
|
|
30
|
+
fetch(config.resolved_provider).new(config)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
# Opt-in only. Nothing is monkey-patched globally:
|
|
5
|
+
#
|
|
6
|
+
# using IsEvenAI::Refinements
|
|
7
|
+
# 42.ai_even?
|
|
8
|
+
module Refinements
|
|
9
|
+
# Shared method bodies, imported into each refinement below.
|
|
10
|
+
module Methods
|
|
11
|
+
def ai_even?(**) = IsEvenAI.even?(self, **)
|
|
12
|
+
def ai_odd?(**) = IsEvenAI.odd?(self, **)
|
|
13
|
+
def jev_even?(**) = IsEvenAI.even?(self, **, provider: :jev)
|
|
14
|
+
def jev_odd?(**) = IsEvenAI.odd?(self, **, provider: :jev)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
refine Integer do
|
|
18
|
+
import_methods Methods
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Floats, Rationals, BigDecimals... all require allow_non_integer: true, like everything else.
|
|
22
|
+
refine Numeric do
|
|
23
|
+
import_methods Methods
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Strings require allow_non_integer: true, per call or via IsEvenAI.configure.
|
|
27
|
+
refine String do
|
|
28
|
+
import_methods Methods
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module IsEvenAI
|
|
4
|
+
Result = Data.define(
|
|
5
|
+
:input, :even, :uncertain, :non_integer, :probability, :confidence, :provider, :model,
|
|
6
|
+
:primitive, :escalated, :reasoning, :fan_out, :latency_ms, :usage, :raw
|
|
7
|
+
) do
|
|
8
|
+
def initialize(input:, even:, provider:, uncertain: false, non_integer: false, probability: nil, confidence: nil,
|
|
9
|
+
model: nil, primitive: nil, escalated: false, reasoning: nil, fan_out: nil, latency_ms: 0.0,
|
|
10
|
+
usage: nil, raw: {})
|
|
11
|
+
super
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def even? = even == true
|
|
15
|
+
def odd? = !even?
|
|
16
|
+
def uncertain? = uncertain == true
|
|
17
|
+
def non_integer? = non_integer == true
|
|
18
|
+
def escalated? = escalated == true
|
|
19
|
+
|
|
20
|
+
# Serializable hash (inputs that are not JSON-native are stringified).
|
|
21
|
+
def to_h
|
|
22
|
+
super.merge(input: serializable_input, even: even?, odd: odd?)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def serializable_input
|
|
28
|
+
case input
|
|
29
|
+
when Integer, String, true, false then input
|
|
30
|
+
when Float then input.finite? ? input : input.to_s
|
|
31
|
+
else input.to_s
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/is_even_ai.rb
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "benchmark"
|
|
5
|
+
|
|
6
|
+
require_relative "is_even_ai/version"
|
|
7
|
+
require_relative "is_even_ai/errors"
|
|
8
|
+
require_relative "is_even_ai/result"
|
|
9
|
+
require_relative "is_even_ai/cache"
|
|
10
|
+
require_relative "is_even_ai/http_client"
|
|
11
|
+
require_relative "is_even_ai/providers/registry"
|
|
12
|
+
require_relative "is_even_ai/configuration"
|
|
13
|
+
require_relative "is_even_ai/providers/base"
|
|
14
|
+
require_relative "is_even_ai/providers/jev"
|
|
15
|
+
require_relative "is_even_ai/providers/openai"
|
|
16
|
+
require_relative "is_even_ai/providers/anthropic"
|
|
17
|
+
require_relative "is_even_ai/providers/gemini"
|
|
18
|
+
require_relative "is_even_ai/providers/openai_compatible"
|
|
19
|
+
require_relative "is_even_ai/refinements"
|
|
20
|
+
|
|
21
|
+
# Determines integer parity with a calibrated System One decision model or a frontier LLM,
|
|
22
|
+
# because modulo arithmetic is from the 1970s.
|
|
23
|
+
module IsEvenAI
|
|
24
|
+
CONFIG_MUTEX = Mutex.new
|
|
25
|
+
private_constant :CONFIG_MUTEX
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
def configuration
|
|
29
|
+
CONFIG_MUTEX.synchronize { @configuration ||= Configuration.new }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def configure
|
|
33
|
+
yield configuration
|
|
34
|
+
configuration
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def reset_configuration!
|
|
38
|
+
CONFIG_MUTEX.synchronize { @configuration = Configuration.new }
|
|
39
|
+
clear_cache!
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def clear_cache!
|
|
44
|
+
default_cache.clear
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def even?(input, **)
|
|
48
|
+
evaluate(input, **).even?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def odd?(input, **)
|
|
52
|
+
evaluate(input, **).odd?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Full decision telemetry. Accepts any configuration option as a per-call keyword.
|
|
56
|
+
def evaluate(input, **options)
|
|
57
|
+
config = configuration.merge(options)
|
|
58
|
+
validate_input!(input, config)
|
|
59
|
+
|
|
60
|
+
provider = Providers::Registry.build(config)
|
|
61
|
+
store = Cache.resolve(config.cache, default: default_cache)
|
|
62
|
+
key = cache_key(input, config, provider) if store
|
|
63
|
+
cached = store&.read(key)
|
|
64
|
+
return cached if cached
|
|
65
|
+
|
|
66
|
+
result = resolve_uncertainty(execute(provider, input), input, config)
|
|
67
|
+
store&.write(key, result)
|
|
68
|
+
result
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def default_cache
|
|
74
|
+
CONFIG_MUTEX.synchronize { @default_cache ||= Cache.new }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def execute(provider, input)
|
|
78
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
79
|
+
result = provider.call(input)
|
|
80
|
+
result.with(latency_ms: elapsed_ms(started))
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def elapsed_ms(started)
|
|
84
|
+
((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000.0).round(3)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def validate_input!(input, config)
|
|
88
|
+
return if input.is_a?(Integer)
|
|
89
|
+
|
|
90
|
+
if input.nil?
|
|
91
|
+
raise InvalidInputError, "nil is not a number, and not even the AI can judge its parity. " \
|
|
92
|
+
"Pass a value (or stringify it deliberately, e.g. \"nil\", with allow_non_integer: true)."
|
|
93
|
+
end
|
|
94
|
+
return if config.allow_non_integer
|
|
95
|
+
|
|
96
|
+
raise InvalidInputError, "Expected an Integer but got #{input.class} (#{input.inspect}). " \
|
|
97
|
+
"Pass allow_non_integer: true (or set config.allow_non_integer = true) " \
|
|
98
|
+
"to let the AI judge non-integers anyway."
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# System 1 -> System 2: escalate, ask a human, or refuse to guess.
|
|
102
|
+
def resolve_uncertainty(result, input, config)
|
|
103
|
+
return result unless result.uncertain?
|
|
104
|
+
|
|
105
|
+
if config.fallback_provider
|
|
106
|
+
escalated = escalate(result, input, config)
|
|
107
|
+
return escalated unless escalated.uncertain? && config.strict_confidence
|
|
108
|
+
|
|
109
|
+
raise UncertainParityError.new(
|
|
110
|
+
"Parity of #{input.inspect} is uncertain even after escalating to #{config.fallback_provider}", result: escalated
|
|
111
|
+
)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
if config.on_uncertain
|
|
115
|
+
resolved = config.on_uncertain.call(result)
|
|
116
|
+
return resolved if resolved.is_a?(Result)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
return result unless config.strict_confidence
|
|
120
|
+
|
|
121
|
+
raise UncertainParityError.new(
|
|
122
|
+
"Parity of #{input.inspect} is uncertain (probability #{result.probability.inspect}, " \
|
|
123
|
+
"confidence #{result.confidence.inspect}) and no fallback_provider or on_uncertain hook resolved it",
|
|
124
|
+
result: result
|
|
125
|
+
)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def escalate(original, input, config)
|
|
129
|
+
fallback = config.fallback_config
|
|
130
|
+
fallback.validate!
|
|
131
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
132
|
+
result = Providers::Registry.build(fallback).call(input)
|
|
133
|
+
result.with(
|
|
134
|
+
escalated: true,
|
|
135
|
+
latency_ms: original.latency_ms + elapsed_ms(started),
|
|
136
|
+
raw: result.raw.merge(escalated_from: original.to_h)
|
|
137
|
+
)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def cache_key(input, config, provider)
|
|
141
|
+
JSON.generate(
|
|
142
|
+
[
|
|
143
|
+
provider.name, provider.model, config.resolved_base_url(provider.name), config.primitive,
|
|
144
|
+
input.class.name, input.to_s, config.allow_non_integer, config.fan_out,
|
|
145
|
+
config.yes_threshold, config.no_threshold, config.strict_confidence,
|
|
146
|
+
config.fallback_provider, config.fallback_options.to_a.map(&:to_s)
|
|
147
|
+
]
|
|
148
|
+
)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: is_even_ai
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Chayut Orapinpatipat
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A deadpan, production-grade parody Ruby gem that answers whether an integer
|
|
14
|
+
is even by asking a System One decision model (TypeSafe Jev) or a frontier LLM instead
|
|
15
|
+
of computing n % 2. Features calibrated confidence gating, System 1 to System 2
|
|
16
|
+
escalation, opt-in refinements, and a CLI. Zero runtime dependencies. Part of the
|
|
17
|
+
vibe-sort series.
|
|
18
|
+
email:
|
|
19
|
+
- chayut_o@hotmail.com
|
|
20
|
+
executables:
|
|
21
|
+
- is_even_ai
|
|
22
|
+
extensions: []
|
|
23
|
+
extra_rdoc_files: []
|
|
24
|
+
files:
|
|
25
|
+
- CHANGELOG.md
|
|
26
|
+
- CODE_OF_CONDUCT.md
|
|
27
|
+
- LICENSE.txt
|
|
28
|
+
- README.md
|
|
29
|
+
- exe/is_even_ai
|
|
30
|
+
- lib/is_even_ai.rb
|
|
31
|
+
- lib/is_even_ai/cache.rb
|
|
32
|
+
- lib/is_even_ai/cli.rb
|
|
33
|
+
- lib/is_even_ai/configuration.rb
|
|
34
|
+
- lib/is_even_ai/errors.rb
|
|
35
|
+
- lib/is_even_ai/http_client.rb
|
|
36
|
+
- lib/is_even_ai/providers/anthropic.rb
|
|
37
|
+
- lib/is_even_ai/providers/base.rb
|
|
38
|
+
- lib/is_even_ai/providers/gemini.rb
|
|
39
|
+
- lib/is_even_ai/providers/jev.rb
|
|
40
|
+
- lib/is_even_ai/providers/openai.rb
|
|
41
|
+
- lib/is_even_ai/providers/openai_compatible.rb
|
|
42
|
+
- lib/is_even_ai/providers/registry.rb
|
|
43
|
+
- lib/is_even_ai/refinements.rb
|
|
44
|
+
- lib/is_even_ai/result.rb
|
|
45
|
+
- lib/is_even_ai/version.rb
|
|
46
|
+
homepage: https://github.com/chayuto/is_even_ai
|
|
47
|
+
licenses:
|
|
48
|
+
- MIT
|
|
49
|
+
metadata:
|
|
50
|
+
homepage_uri: https://github.com/chayuto/is_even_ai
|
|
51
|
+
changelog_uri: https://github.com/chayuto/is_even_ai/blob/main/CHANGELOG.md
|
|
52
|
+
documentation_uri: https://rubydoc.info/gems/is_even_ai
|
|
53
|
+
bug_tracker_uri: https://github.com/chayuto/is_even_ai/issues
|
|
54
|
+
rubygems_mfa_required: 'true'
|
|
55
|
+
post_install_message:
|
|
56
|
+
rdoc_options: []
|
|
57
|
+
require_paths:
|
|
58
|
+
- lib
|
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: 3.2.0
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
requirements: []
|
|
70
|
+
rubygems_version: 3.5.22
|
|
71
|
+
signing_key:
|
|
72
|
+
specification_version: 4
|
|
73
|
+
summary: Determine integer parity with TypeSafe Jev, OpenAI, Anthropic Claude, Google
|
|
74
|
+
Gemini, or any OpenAI-compatible endpoint
|
|
75
|
+
test_files: []
|