Class: LLMed::Application
- Inherits:
-
Object
- Object
- LLMed::Application
show all
- Defined in:
- lib/llmed/application.rb
Defined Under Namespace
Classes: CodeComment, Snapshot
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#achieve(name, **opts, &block) ⇒ Object
-
#context(name, **opts, &block) ⇒ Object
Example: application { context "demo" { "content" } } DEPRECATED: Use spec instead of context.
-
#evaluate ⇒ Object
-
#initialize(name:, language:, output_file:, block:, logger:, release:, release_dir:, output_dir:) ⇒ Application
constructor
A new instance of Application.
-
#notify(message) ⇒ Object
-
#output_file(output_dir, mode = 'w', &block) ⇒ Object
-
#patch_or_create(output) ⇒ Object
-
#prepare_release ⇒ Object
-
#prepare_snapshot ⇒ Object
-
#rebuild? ⇒ Boolean
-
#source_code ⇒ Object
-
#spec(name, **opts, &block) ⇒ Object
Example: application { spec "demo" { "content" } }.
-
#system_prompt(configuration) ⇒ Object
-
#write_statistics(response) ⇒ Object
Constructor Details
#initialize(name:, language:, output_file:, block:, logger:, release:, release_dir:, output_dir:) ⇒ Application
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/llmed/application.rb', line 33
def initialize(name:, language:, output_file:, block:, logger:, release:, release_dir:, output_dir:)
snapshot_file = Pathname.new(release_dir) + "#{output_file}.snapshot"
@name = name
@output_file = output_file
@language = language.to_sym
@code_comment = CodeComment.new(language)
@block = block
@contexts = []
@goals = []
@logger = logger
@release = release
@release_dir = release_dir
@output_dir = output_dir
@snapshot = Snapshot.new(snapshot_file)
end
|
Instance Attribute Details
#contexts ⇒ Object
Returns the value of attribute contexts.
8
9
10
|
# File 'lib/llmed/application.rb', line 8
def contexts
@contexts
end
|
#language ⇒ Object
Returns the value of attribute language.
8
9
10
|
# File 'lib/llmed/application.rb', line 8
def language
@language
end
|
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/llmed/application.rb', line 8
def name
@name
end
|
Instance Method Details
#achieve(name, **opts, &block) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/llmed/application.rb', line 68
def achieve(name, **opts, &block)
opts[:release_dir] = @release_dir
goal = Goal.new(name: name, options: opts)
output = goal.instance_eval(&block)
goal.llm(output) unless goal.message?
@goals << goal
end
|
#context(name, **opts, &block) ⇒ Object
Example:
application { context "demo" { "content" } }
DEPRECATED: Use spec instead of context
53
54
55
56
57
58
59
60
|
# File 'lib/llmed/application.rb', line 53
def context(name, **opts, &block)
opts[:release_dir] = @release_dir
ctx = Context.new(name: name, options: opts)
output = ctx.instance_eval(&block)
ctx.llm(output) unless ctx.message?
@contexts << ctx
end
|
#evaluate ⇒ Object
77
78
79
|
# File 'lib/llmed/application.rb', line 77
def evaluate
instance_eval(&@block)
end
|
#notify(message) ⇒ Object
317
318
319
|
# File 'lib/llmed/application.rb', line 317
def notify(message)
Notify.notify("APPLICATION #{@name}", message)
end
|
#output_file(output_dir, mode = 'w', &block) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/llmed/application.rb', line 212
def output_file(output_dir, mode = 'w', &block)
if @output_file.respond_to? :write
yield @output_file
else
path = Pathname.new(output_dir) + @output_file
FileUtils.mkdir_p(File.dirname(path))
@logger.info("APPLICATION #{@name} OUTPUT FILE #{path}")
File.open(path, mode, &block)
end
end
|
#patch_or_create(output) ⇒ Object
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/llmed/application.rb', line 225
def patch_or_create(output)
output_content = output
if @release && File.exist?(release_source_code) && !release_contexts.empty?
output_release = Release.load(File.read(release_source_code), @code_comment)
input_release = Release.load(output, @code_comment)
output_content = output_release.merge!(input_release, user_contexts).content
output_release.changes.each do |change|
action, ctx = change
case action
when :added
@logger.info("APPLICATION #{@name} PATCH ADDING NEW CONTEXT #{ctx.name}")
when :updated
@logger.info("APPLICATION #{@name} PATCH UPDATING CONTEXT #{ctx.name} TO DIGEST #{ctx.digest}")
end
end
end
output_file(@output_dir) do |file|
file.write(output_content)
end
if !File.exist?(release_source_code)
@snapshot.refresh(@contexts)
@logger.info("APPLICATION #{@name} SNAPSHOT REFRESHED")
end
return unless @output_file.is_a?(String)
output_file = Pathname.new(@output_dir) + @output_file
FileUtils.cp(output_file, release_source_code)
FileUtils.cp(output_file, release_main_source_code)
@logger.info("APPLICATION #{@name} RELEASE FILE #{release_source_code}")
end
|
#prepare_release ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/llmed/application.rb', line 188
def prepare_release
@logger.info("APPLICATION #{@name} COMPILING FOR #{@language} RELEASE #{@release}")
return unless @output_file.is_a?(String)
return unless @release
output_file = Pathname.new(@output_dir) + @output_file
if @release && File.exist?(output_file) && !File.exist?(release_source_code)
elsif @release && !File.exist?(output_file) && File.exist?(release_main_source_code)
FileUtils.mkdir_p(File.dirname(output_file))
FileUtils.cp(release_main_source_code, output_file)
return
end
@logger.info("APPLICATION #{@name} INPUT RELEASE FILE #{release_main_source_code}")
end
|
#prepare_snapshot ⇒ Object
182
183
184
185
186
|
# File 'lib/llmed/application.rb', line 182
def prepare_snapshot
@logger.info("APPLICATION #{@name} PREPARING SNAPSHOT #{@snapshot.snapshot_file}")
@snapshot.sync(@contexts)
end
|
#rebuild? ⇒ Boolean
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/llmed/application.rb', line 281
def rebuild?
return true unless @release
return true if release_contexts.empty?
!digests_of_context_to_update.tap do |digests|
digests.each do |digest|
context_by_digest = release_contexts.invert
if context_by_digest[digest].nil?
@logger.info("APPLICATION #{@name} ADDING CONTEXT #{user_contexts.by_digest(digest).name}")
else
@logger.info("APPLICATION #{@name} REBUILDING CONTEXT #{context_by_digest[digest]}")
end
end
end.empty?
end
|
#source_code ⇒ Object
206
207
208
209
210
|
# File 'lib/llmed/application.rb', line 206
def source_code
return unless File.exist?(release_source_code)
File.read(release_source_code)
end
|
#spec(name, **opts, &block) ⇒ Object
Example:
application { spec "demo" { "content" } }
64
65
66
|
# File 'lib/llmed/application.rb', line 64
def spec(name, **opts, &block)
context(name, **opts, &block)
end
|
#system_prompt(configuration) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/llmed/application.rb', line 262
def system_prompt(configuration)
contexts_diffs = @snapshot.diff(contexts)
changes_of_contexts = ''
if contexts_diffs.any?
contexts_diffs.each do |context_name, diffs|
changes_of_contexts += "# Context: #{context_name}\n"
changes_of_contexts += diffs.map { |op, line| "#{op} #{line}" }.join("\n")
end
end
configuration.prompt(language: language,
source_code: source_code,
code_comment_begin: @code_comment.begin,
code_comment_end: @code_comment.end,
update_context_digests: digests_of_context_to_update,
changes_of_contexts: changes_of_contexts,
goals: goals)
end
|
#write_statistics(response) ⇒ Object
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/llmed/application.rb', line 297
def write_statistics(response)
return unless @output_file.is_a?(String)
statistics_file = Pathname.new(@release_dir) + "#{@output_file}.statistics"
File.open(statistics_file, 'a') do |file|
stat = {
inserted_at: Time.now.to_i,
name: @name,
provider: response.provider,
model: response.model,
release: @release,
total_tokens: response.total_tokens,
duration_seconds: response.duration_seconds
}
file.puts stat.to_json
end
@logger.info("APPLICATION #{@name} WROTE STATISTICS FILE #{statistics_file}")
end
|