Class: TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/kaba/test_runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, schema:, type_name:, prompt:, validate:) ⇒ TestRunner



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/kaba/test_runner.rb', line 4

def initialize(path, schema:, type_name:, prompt:, validate:)

  @test_files = Dir.glob(File.join(File.expand_path(path), '*.target.json'))
  @lines = []
  @schema = schema
  @type_name = type_name
  @prompt = prompt || Prompt.new(@schema, @type_name)
  @validate = validate || Validate.new(schema: @schema, type_name: @type_name)

  @type_right_total = 0
  @score_total = 0

end

Class Method Details

.report_template_pathObject



100
101
102
# File 'lib/kaba/test_runner.rb', line 100

def report_template_path
  @report_template_path || File.join(__dir__, 'report.html.erb')
end

.report_template_path=(path) ⇒ Object



104
105
106
# File 'lib/kaba/test_runner.rb', line 104

def report_template_path=(path)
  @report_template_path = path
end

Instance Method Details

#_each(limit: nil) ⇒ Object



18
19
20
21
22
# File 'lib/kaba/test_runner.rb', line 18

def _each(limit: nil)
  @test_files.first(limit || @test_files.size).each do |file|
    yield(Row.new(file), self)
  end
end

#save(file_path) ⇒ Object



92
93
94
95
96
# File 'lib/kaba/test_runner.rb', line 92

def save(file_path)
  File.open(File.expand_path(file_path), 'w') do |file|
    file.puts ERB.new(File.read(self.class.report_template_path)).result(binding)
  end
end

#scan(limit: nil, model: 'spark-general-4.0', judge_model: 'spark-general-4.0', judge_temperature: 0.1, temperature: 0.1) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kaba/test_runner.rb', line 24

def scan(
  limit: nil, 
  model: 'spark-general-4.0', 
  judge_model: 'spark-general-4.0',  
  judge_temperature: 0.1, 
  temperature: 0.1
  )

  progressbar = TTY::ProgressBar.new(
    "Test: [:bar] :percent :current/:total",
     total: @test_files.first(limit || @test_files.size).size
  )

  progressbar.start

  Async do
    _each(limit: limit) do |row|
      Async do |task|
        input = @prompt.render(File.read row.input_file)

        target = "        ```json\n        \#{JSON.pretty_generate(JSON.parse(File.read(row.target_path)))}\n        ```\n        Markdown\n        output = Application.llm_client.chat(\n          parameters: {\n            model: model,\n            messages: [ { role: 'user', content: input } ],\n            temperature: temperature,\n          }\n        ).dig(\"choices\", 0, \"message\", \"content\")\n        \n\n        output_json = JSON.parse_llm_response output\n\n        type_check_response = JSON.parse @validate.run(output_json).body\n        @type_right_total += 1 if type_check_response[\"success\"]\n        \n        judge_input = Judge.new(input: input, output: output, target: target).render\n        judge_response = Application.llm_client.chat(\n          parameters: {\n            model: judge_model,\n            messages: [ { role: 'user', content: judge_input } ],\n            temperature: judge_temperature,\n          }\n        ).dig(\"choices\", 0, \"message\", \"content\")\n\n        judge_json = JSON.parse_llm_response judge_response\n        @score_total += judge_json[\"score\"].to_i\n\n        @lines << {\n          row: row,\n          input: input,\n          output: output,\n          output_json: output_json,\n          type_check_response: type_check_response,\n          target: target,\n          judge_response: judge_response,\n          judge_json: judge_json,\n        }\n\n        progressbar.advance\n      end\n    end\n  end.wait\nend\n"