Class: Desiru::Persistence::Repositories::TrainingExampleRepository
- Inherits:
-
BaseRepository
- Object
- BaseRepository
- Desiru::Persistence::Repositories::TrainingExampleRepository
show all
- Defined in:
- lib/desiru/persistence/repositories/training_example_repository.rb
Overview
Repository for training example records
Instance Attribute Summary
#model_class
Instance Method Summary
collapse
#all, #count, #create, #delete?, #exists?, #find, #find_by, #paginate, #update, #where
Constructor Details
Returns a new instance of TrainingExampleRepository.
10
11
12
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 10
def initialize
super(Models::TrainingExample)
end
|
Instance Method Details
#bulk_create(module_name, examples, dataset_type: 'training') ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 46
def bulk_create(module_name, examples, dataset_type: 'training')
transaction do
examples.map do |example|
create(
module_name: module_name,
dataset_type: dataset_type,
inputs: example[:inputs],
expected_outputs: example[:outputs],
metadata: example[:metadata]
)
end
end
end
|
#export_for_training(module_name, format: :dspy) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 76
def export_for_training(module_name, format: :dspy)
examples = find_by_module(module_name, dataset_type: 'training')
case format
when :dspy
examples.map do |ex|
{
inputs: ex.inputs,
outputs: ex.expected_outputs
}
end
when :jsonl
examples.map do |ex|
JSON.generate({
inputs: ex.inputs,
outputs: ex.expected_outputs,
metadata: ex.metadata
})
end.join("\n")
else
raise ArgumentError, "Unknown format: #{format}"
end
end
|
#find_by_module(module_name, dataset_type: nil) ⇒ Object
14
15
16
17
18
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 14
def find_by_module(module_name, dataset_type: nil)
scope = dataset.where(module_name: module_name)
scope = scope.where(dataset_type: dataset_type) if dataset_type
scope.all
end
|
#find_least_used(module_name, limit = 10) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 27
def find_least_used(module_name, limit = 10)
dataset
.where(module_name: module_name)
.order(:used_count, :last_used_at)
.limit(limit)
.all
end
|
#find_unused(module_name, limit = 10) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 20
def find_unused(module_name, limit = 10)
dataset
.where(module_name: module_name, used_count: 0)
.limit(limit)
.all
end
|
#mark_as_used?(id) ⇒ Boolean
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 35
def mark_as_used?(id)
record = find(id)
return false unless record
record.update(
used_count: record.used_count + 1,
last_used_at: Time.now
)
true
end
|
#split_dataset(module_name, train_ratio: 0.8, val_ratio: 0.1) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/desiru/persistence/repositories/training_example_repository.rb', line 60
def split_dataset(module_name, train_ratio: 0.8, val_ratio: 0.1)
all_examples = find_by_module(module_name)
total = all_examples.length
train_size = (total * train_ratio).floor
val_size = (total * val_ratio).floor
shuffled = all_examples.shuffle
{
training: shuffled[0...train_size],
validation: shuffled[train_size...(train_size + val_size)],
test: shuffled[(train_size + val_size)..]
}
end
|