vibe-sort 0.4.0 → 0.5.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 +4 -4
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +20 -0
- data/README.md +99 -7
- data/docs/api_reference.md +13 -3
- data/docs/architecture.md +2 -1
- data/docs/v0.4.0_PLAN.md +203 -0
- data/lib/vibe_sort/client.rb +65 -8
- data/lib/vibe_sort/configuration.rb +37 -9
- data/lib/vibe_sort/error.rb +55 -0
- data/lib/vibe_sort/key_detector.rb +31 -0
- data/lib/vibe_sort/provider_resolver.rb +98 -0
- data/lib/vibe_sort/providers/jev.rb +72 -0
- data/lib/vibe_sort/providers/open_router.rb +29 -0
- data/lib/vibe_sort/result.rb +37 -0
- data/lib/vibe_sort/settings.rb +79 -0
- data/lib/vibe_sort/sorter.rb +15 -3
- data/lib/vibe_sort/system_one/client.rb +189 -0
- data/lib/vibe_sort/system_one/serializer.rb +118 -0
- data/lib/vibe_sort/system_one/strategies/base.rb +127 -0
- data/lib/vibe_sort/system_one/strategies/bogo_sort.rb +83 -0
- data/lib/vibe_sort/system_one/strategies/choice_sort.rb +113 -0
- data/lib/vibe_sort/system_one/strategies/noul_sort.rb +76 -0
- data/lib/vibe_sort/system_one/strategies/score_sort.rb +128 -0
- data/lib/vibe_sort/version.rb +1 -1
- data/lib/vibe_sort.rb +11 -0
- metadata +20 -5
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module VibeSort
|
|
4
|
+
module SystemOne
|
|
5
|
+
module Strategies
|
|
6
|
+
# :noul - Pairwise Comparator Merge Sort.
|
|
7
|
+
#
|
|
8
|
+
# Each comparison is one binary "should A come strictly before B?" question.
|
|
9
|
+
# Model judgments can be non-transitive (A < B < C < A), so this uses a plain
|
|
10
|
+
# top-down merge sort, which always terminates in <= ceil(N log2 N)
|
|
11
|
+
# comparisons, rather than Array#sort. Pair answers are cached symmetrically.
|
|
12
|
+
class NoulSort < Base
|
|
13
|
+
BEFORE_THRESHOLD = 0.55
|
|
14
|
+
AFTER_THRESHOLD = 0.45
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def perform(entries)
|
|
19
|
+
@cache = {}
|
|
20
|
+
@comparisons = Hash.new(0)
|
|
21
|
+
sorted = merge_sort(entries)
|
|
22
|
+
sorted.each { |e| record(e, comparisons: @comparisons[e.index]) }
|
|
23
|
+
sorted
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def merge_sort(entries)
|
|
27
|
+
return entries if entries.size <= 1
|
|
28
|
+
|
|
29
|
+
mid = entries.size / 2
|
|
30
|
+
merge(merge_sort(entries[0...mid]), merge_sort(entries[mid..]))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def merge(left, right)
|
|
34
|
+
merged = []
|
|
35
|
+
merged << (compare(left.first, right.first) <= 0 ? left.shift : right.shift) until left.empty? || right.empty?
|
|
36
|
+
merged + left + right
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Integer] -1 if left belongs before right, 1 if after, 0 for a tie
|
|
40
|
+
def compare(left, right)
|
|
41
|
+
cached = @cache[[left.index, right.index]]
|
|
42
|
+
return cached if cached
|
|
43
|
+
|
|
44
|
+
result = judge(left, right)
|
|
45
|
+
@cache[[left.index, right.index]] = result
|
|
46
|
+
@cache[[right.index, left.index]] = -result
|
|
47
|
+
result
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def judge(left, right)
|
|
51
|
+
@comparisons[left.index] += 1
|
|
52
|
+
@comparisons[right.index] += 1
|
|
53
|
+
state = { "sorting_goal" => goal, "direction" => direction_label,
|
|
54
|
+
"item_a" => left.serialized, "item_b" => right.serialized }
|
|
55
|
+
answer = answer_for(ask(state: state, questions: { "a_before_b" => question }), "a_before_b", "noul")
|
|
56
|
+
probability = answer["noul"].to_f
|
|
57
|
+
return -1 if probability > BEFORE_THRESHOLD
|
|
58
|
+
return 1 if probability < AFTER_THRESHOLD
|
|
59
|
+
|
|
60
|
+
0
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def question
|
|
64
|
+
{
|
|
65
|
+
"type" => "noul",
|
|
66
|
+
"instructions" => "When sorting by `sorting_goal` (#{direction_label}), should `item_a` come strictly before `item_b`?",
|
|
67
|
+
"criteria" => {
|
|
68
|
+
"true" => "`item_a` belongs earlier in the sorted order than `item_b`",
|
|
69
|
+
"false" => "`item_b` belongs earlier than `item_a`, or they are tied"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module VibeSort
|
|
4
|
+
module SystemOne
|
|
5
|
+
module Strategies
|
|
6
|
+
# :score - Normalized Batch Score Sort.
|
|
7
|
+
#
|
|
8
|
+
# One request rates every item on a relative min-max rubric; items are then
|
|
9
|
+
# sorted in Ruby by score (stable on input index). Optionally one more batched
|
|
10
|
+
# request settles adjacent near-ties and low-confidence neighbours.
|
|
11
|
+
#
|
|
12
|
+
# Options: rubric (Array<String>, 2..10), tie_break, tie_epsilon, confidence_floor.
|
|
13
|
+
class ScoreSort < Base
|
|
14
|
+
MIN_LEVELS = 2
|
|
15
|
+
MAX_LEVELS = 10
|
|
16
|
+
TIE_BREAK_MIN_CONFIDENCE = 0.5
|
|
17
|
+
|
|
18
|
+
def initialize(**kwargs)
|
|
19
|
+
super
|
|
20
|
+
@rubric = @options[:rubric] || default_rubric
|
|
21
|
+
validate_rubric!(@rubric)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
# Scores are position-relative to an *ascending* arrangement; :desc is applied
|
|
27
|
+
# by reversing the Ruby sort, so the rubric must not also be flipped.
|
|
28
|
+
def default_goal
|
|
29
|
+
"natural ascending order"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def default_rubric
|
|
33
|
+
[
|
|
34
|
+
"At the absolute bottom / very start of this array for: #{goal}",
|
|
35
|
+
"Clearly below the middle of this array for: #{goal}",
|
|
36
|
+
"Right around the middle / average of this array for: #{goal}",
|
|
37
|
+
"Clearly above the middle of this array for: #{goal}",
|
|
38
|
+
"At the absolute top / very end of this array for: #{goal}"
|
|
39
|
+
]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def validate_rubric!(rubric)
|
|
43
|
+
valid = rubric.is_a?(Array) && rubric.size.between?(MIN_LEVELS, MAX_LEVELS) &&
|
|
44
|
+
rubric.all? { |level| level.is_a?(String) && !level.strip.empty? }
|
|
45
|
+
return if valid
|
|
46
|
+
|
|
47
|
+
raise ArgumentError, "rubric must be an Array of #{MIN_LEVELS}..#{MAX_LEVELS} non-empty descriptive strings"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def perform(entries)
|
|
51
|
+
scores = rate(entries)
|
|
52
|
+
sorted = entries.sort_by { |e| desc? ? [-scores[e.index], e.index] : [scores[e.index], e.index] }
|
|
53
|
+
sorted = break_ties(sorted) if @options.fetch(:tie_break, true)
|
|
54
|
+
sorted
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def rate(entries)
|
|
58
|
+
state = { "sorting_goal" => goal, "items" => entries.to_h { |e| ["i_#{e.index}", e.serialized] } }
|
|
59
|
+
questions = entries.to_h do |e|
|
|
60
|
+
["s_#{e.index}", {
|
|
61
|
+
"type" => "score",
|
|
62
|
+
"instructions" => "Rate item `i_#{e.index}` (#{e.serialized.inspect}) relative to all `items` in `state` for: #{goal}",
|
|
63
|
+
"criteria" => @rubric
|
|
64
|
+
}]
|
|
65
|
+
end
|
|
66
|
+
answers = ask(state: state, questions: questions)
|
|
67
|
+
entries.to_h { |e| [e.index, read_score(e, answer_for(answers, "s_#{e.index}", "score"))] }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def read_score(entry, answer)
|
|
71
|
+
score = answer["score"].to_f
|
|
72
|
+
confidence = answer["confidence"]&.to_f
|
|
73
|
+
record(entry, score: score, confidence: confidence, probabilities: answer["probabilities"])
|
|
74
|
+
@warnings << "Low confidence (#{confidence.round(2)}) scoring item #{entry.index}; its score may be bimodal" if confidence && confidence < floor
|
|
75
|
+
score
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def floor
|
|
79
|
+
@options.fetch(:confidence_floor, 0.60)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def break_ties(sorted)
|
|
83
|
+
pairs = ambiguous_pairs(sorted)
|
|
84
|
+
return sorted if pairs.empty?
|
|
85
|
+
|
|
86
|
+
state = { "sorting_goal" => goal, "direction" => direction_label }
|
|
87
|
+
questions = pairs.to_h { |idx, left, right| ["pair_#{idx}", pair_question(left, right)] }
|
|
88
|
+
answers = ask(state: state, questions: questions)
|
|
89
|
+
apply_swaps(sorted, pairs, answers)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def ambiguous_pairs(sorted)
|
|
93
|
+
epsilon = @options.fetch(:tie_epsilon, 0.15)
|
|
94
|
+
sorted.each_cons(2).with_index.filter_map do |(left, right), idx|
|
|
95
|
+
l = @metadata[left.index]
|
|
96
|
+
r = @metadata[right.index]
|
|
97
|
+
close = (l[:score] - r[:score]).abs < epsilon
|
|
98
|
+
unsure = [l[:confidence], r[:confidence]].any? { |c| c && c < floor }
|
|
99
|
+
[idx, left, right] if close || unsure
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def pair_question(left, right)
|
|
104
|
+
{
|
|
105
|
+
"type" => "choice",
|
|
106
|
+
"instructions" => "Which item should come first in the sorted order for `sorting_goal` (#{direction_label})?",
|
|
107
|
+
"criteria" => { "left" => left.serialized, "right" => right.serialized }
|
|
108
|
+
}
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def apply_swaps(sorted, pairs, answers)
|
|
112
|
+
result = sorted.dup
|
|
113
|
+
used = {}
|
|
114
|
+
pairs.each do |idx, _left, _right|
|
|
115
|
+
answer = answers["pair_#{idx}"]
|
|
116
|
+
next unless answer.is_a?(Hash) && answer["choice"] == "right"
|
|
117
|
+
next unless answer["confidence"].to_f >= TIE_BREAK_MIN_CONFIDENCE
|
|
118
|
+
next if used[idx] || used[idx + 1]
|
|
119
|
+
|
|
120
|
+
result[idx], result[idx + 1] = result[idx + 1], result[idx]
|
|
121
|
+
used[idx] = used[idx + 1] = true
|
|
122
|
+
end
|
|
123
|
+
result
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
data/lib/vibe_sort/version.rb
CHANGED
data/lib/vibe_sort.rb
CHANGED
|
@@ -6,6 +6,9 @@ require "json"
|
|
|
6
6
|
require_relative "vibe_sort/version"
|
|
7
7
|
require_relative "vibe_sort/error"
|
|
8
8
|
require_relative "vibe_sort/key_detector"
|
|
9
|
+
require_relative "vibe_sort/settings"
|
|
10
|
+
require_relative "vibe_sort/result"
|
|
11
|
+
require_relative "vibe_sort/provider_resolver"
|
|
9
12
|
require_relative "vibe_sort/configuration"
|
|
10
13
|
require_relative "vibe_sort/providers/base"
|
|
11
14
|
require_relative "vibe_sort/providers/openai"
|
|
@@ -14,6 +17,14 @@ require_relative "vibe_sort/providers/gemini"
|
|
|
14
17
|
require_relative "vibe_sort/providers/groq"
|
|
15
18
|
require_relative "vibe_sort/providers/space_x_ai"
|
|
16
19
|
require_relative "vibe_sort/providers/open_router"
|
|
20
|
+
require_relative "vibe_sort/system_one/serializer"
|
|
21
|
+
require_relative "vibe_sort/system_one/client"
|
|
22
|
+
require_relative "vibe_sort/system_one/strategies/base"
|
|
23
|
+
require_relative "vibe_sort/system_one/strategies/choice_sort"
|
|
24
|
+
require_relative "vibe_sort/system_one/strategies/score_sort"
|
|
25
|
+
require_relative "vibe_sort/system_one/strategies/noul_sort"
|
|
26
|
+
require_relative "vibe_sort/system_one/strategies/bogo_sort"
|
|
27
|
+
require_relative "vibe_sort/providers/jev"
|
|
17
28
|
require_relative "vibe_sort/sorter"
|
|
18
29
|
require_relative "vibe_sort/client"
|
|
19
30
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: vibe-sort
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chayut Orapinpatipat
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: faraday
|
|
@@ -26,8 +26,9 @@ dependencies:
|
|
|
26
26
|
version: '2.0'
|
|
27
27
|
description: A proof-of-concept Ruby gem that sorts arrays by leveraging LLM APIs
|
|
28
28
|
(OpenAI Chat Completions, Anthropic Messages, Google Gemini, and the OpenAI-compatible
|
|
29
|
-
Groq, SpaceXAI Grok, and OpenRouter APIs)
|
|
30
|
-
|
|
29
|
+
Groq, SpaceXAI Grok, and OpenRouter APIs) or TypeSafe's Jev decision model (System
|
|
30
|
+
One, via TypeSafe or OpenRouter). Infers the provider from the API key format. Demonstrates
|
|
31
|
+
how AI can be used for computational tasks.
|
|
31
32
|
email:
|
|
32
33
|
- chayut_o@hotmail.com
|
|
33
34
|
executables: []
|
|
@@ -47,18 +48,32 @@ files:
|
|
|
47
48
|
- docs/development.md
|
|
48
49
|
- docs/free_model_benchmark_2026-07-12.md
|
|
49
50
|
- docs/v0.2.0_UPDATE.md
|
|
51
|
+
- docs/v0.4.0_PLAN.md
|
|
50
52
|
- lib/vibe/sort.rb
|
|
51
53
|
- lib/vibe_sort.rb
|
|
52
54
|
- lib/vibe_sort/client.rb
|
|
53
55
|
- lib/vibe_sort/configuration.rb
|
|
54
56
|
- lib/vibe_sort/error.rb
|
|
57
|
+
- lib/vibe_sort/key_detector.rb
|
|
58
|
+
- lib/vibe_sort/provider_resolver.rb
|
|
55
59
|
- lib/vibe_sort/providers/anthropic.rb
|
|
56
60
|
- lib/vibe_sort/providers/base.rb
|
|
57
61
|
- lib/vibe_sort/providers/gemini.rb
|
|
58
62
|
- lib/vibe_sort/providers/groq.rb
|
|
63
|
+
- lib/vibe_sort/providers/jev.rb
|
|
64
|
+
- lib/vibe_sort/providers/open_router.rb
|
|
59
65
|
- lib/vibe_sort/providers/openai.rb
|
|
60
66
|
- lib/vibe_sort/providers/space_x_ai.rb
|
|
67
|
+
- lib/vibe_sort/result.rb
|
|
68
|
+
- lib/vibe_sort/settings.rb
|
|
61
69
|
- lib/vibe_sort/sorter.rb
|
|
70
|
+
- lib/vibe_sort/system_one/client.rb
|
|
71
|
+
- lib/vibe_sort/system_one/serializer.rb
|
|
72
|
+
- lib/vibe_sort/system_one/strategies/base.rb
|
|
73
|
+
- lib/vibe_sort/system_one/strategies/bogo_sort.rb
|
|
74
|
+
- lib/vibe_sort/system_one/strategies/choice_sort.rb
|
|
75
|
+
- lib/vibe_sort/system_one/strategies/noul_sort.rb
|
|
76
|
+
- lib/vibe_sort/system_one/strategies/score_sort.rb
|
|
62
77
|
- lib/vibe_sort/version.rb
|
|
63
78
|
homepage: https://github.com/chayuto/vibe-sort
|
|
64
79
|
licenses:
|
|
@@ -89,5 +104,5 @@ rubygems_version: 3.5.22
|
|
|
89
104
|
signing_key:
|
|
90
105
|
specification_version: 4
|
|
91
106
|
summary: AI-powered array sorting using OpenAI, Anthropic Claude, Google Gemini, Groq,
|
|
92
|
-
SpaceXAI Grok, or
|
|
107
|
+
SpaceXAI Grok, OpenRouter, or TypeSafe Jev
|
|
93
108
|
test_files: []
|