Class: DSPy::Storage::ProgramStorage
- Inherits:
-
Object
- Object
- DSPy::Storage::ProgramStorage
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/storage/program_storage.rb
Overview
Storage system for saving and loading optimized DSPy programs Handles serialization of optimization results, program state, and history tracking
Defined Under Namespace
Classes: SavedProgram
Instance Attribute Summary collapse
-
#create_directories ⇒ Object
readonly
Returns the value of attribute create_directories.
-
#storage_path ⇒ Object
readonly
Returns the value of attribute storage_path.
Instance Method Summary collapse
- #delete_program(program_id) ⇒ Object
- #export_programs(program_ids, export_path) ⇒ Object
- #get_history ⇒ Object
- #import_programs(import_path) ⇒ Object
-
#initialize(storage_path: "./dspy_storage", create_directories: true) ⇒ ProgramStorage
constructor
A new instance of ProgramStorage.
- #list_programs ⇒ Object
- #load_program(program_id) ⇒ Object
- #save_program(program, optimization_result, program_id: nil, metadata: {}) ⇒ Object
Constructor Details
#initialize(storage_path: "./dspy_storage", create_directories: true) ⇒ ProgramStorage
Returns a new instance of ProgramStorage.
140 141 142 143 144 145 |
# File 'lib/dspy/storage/program_storage.rb', line 140 def initialize(storage_path: "./dspy_storage", create_directories: true) @storage_path = File.(storage_path) @create_directories = create_directories setup_storage_directory if @create_directories end |
Instance Attribute Details
#create_directories ⇒ Object (readonly)
Returns the value of attribute create_directories.
132 133 134 |
# File 'lib/dspy/storage/program_storage.rb', line 132 def create_directories @create_directories end |
#storage_path ⇒ Object (readonly)
Returns the value of attribute storage_path.
129 130 131 |
# File 'lib/dspy/storage/program_storage.rb', line 129 def storage_path @storage_path end |
Instance Method Details
#delete_program(program_id) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/dspy/storage/program_storage.rb', line 232 def delete_program(program_id) file_path = program_file_path(program_id) if File.exist?(file_path) File.delete(file_path) remove_from_history(program_id) emit_delete_event(program_id) true else false end end |
#export_programs(program_ids, export_path) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/dspy/storage/program_storage.rb', line 247 def export_programs(program_ids, export_path) programs = program_ids.map { |id| load_program(id) }.compact dspy_version = begin DSPy::VERSION rescue "unknown" end export_data = { exported_at: Time.now.iso8601, dspy_version: dspy_version, programs: programs.map(&:to_h) } File.write(export_path, JSON.pretty_generate(export_data)) emit_export_event(export_path, programs.size) end |
#get_history ⇒ Object
223 224 225 226 227 228 |
# File 'lib/dspy/storage/program_storage.rb', line 223 def get_history history_path = File.join(@storage_path, "history.json") return { programs: [], summary: {} } unless File.exist?(history_path) JSON.parse(File.read(history_path), symbolize_names: true) end |
#import_programs(import_path) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/dspy/storage/program_storage.rb', line 268 def import_programs(import_path) data = JSON.parse(File.read(import_path), symbolize_names: true) imported = [] data[:programs].each do |program_data| saved_program = SavedProgram.from_h(program_data) # Save with new timestamp but preserve original ID file_path = program_file_path(saved_program.program_id) File.write(file_path, JSON.pretty_generate(saved_program.to_h)) update_history(saved_program) imported << saved_program end emit_import_event(import_path, imported.size) imported end |
#list_programs ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/dspy/storage/program_storage.rb', line 213 def list_programs history_path = File.join(@storage_path, "history.json") return [] unless File.exist?(history_path) history_data = JSON.parse(File.read(history_path), symbolize_names: true) history_data[:programs] || [] end |
#load_program(program_id) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/dspy/storage/program_storage.rb', line 188 def load_program(program_id) emit_load_start_event(program_id) begin file_path = program_file_path(program_id) unless File.exist?(file_path) emit_load_error_event(program_id, "Program not found: #{program_id}") return nil end data = JSON.parse(File.read(file_path), symbolize_names: true) saved_program = SavedProgram.from_h(data) emit_load_complete_event(saved_program) saved_program rescue => error emit_load_error_event(program_id, error) nil end end |
#save_program(program, optimization_result, program_id: nil, metadata: {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dspy/storage/program_storage.rb', line 156 def save_program(program, optimization_result, program_id: nil, metadata: {}) emit_save_start_event(program_id) begin # Convert optimization result to hash if it's an object result_hash = optimization_result.respond_to?(:to_h) ? optimization_result.to_h : optimization_result saved_program = SavedProgram.new( program: program, optimization_result: result_hash, metadata: , program_id: program_id ) # Write to file file_path = program_file_path(saved_program.program_id) File.write(file_path, JSON.pretty_generate(saved_program.to_h)) # Update history update_history(saved_program) emit_save_complete_event(saved_program) saved_program rescue => error emit_save_error_event(program_id, error) raise end end |