Class: LLM::Eval::Tracer::Console
- Inherits:
-
Tracer::Null
- Object
- Tracer
- Tracer::Null
- LLM::Eval::Tracer::Console
- Defined in:
- lib/llm/eval/tracer/console.rb
Overview
Console-based tracer.
Constant Summary collapse
- RULE =
58
Instance Method Summary collapse
- #on_exit
- #eval ⇒ LLM::Eval
- #rundir ⇒ String
- #start_trace
- #on_request_start(operation:, model: nil)
- #on_request_finish(operation:, res:)
- #on_request_error(ex:)
-
#on_tool_start(id:, name:, arguments:) ⇒ LLM::Object
The span an ending is handed, with the call's id and name.
- #on_tool_finish(result:)
- #on_tool_error(ex:)
- #on_tool_interrupt(ex:, span:)
- #on_interrupt(scope:)
-
#initialize(llm, options = {}) ⇒ Tracer
constructor
A new instance of Tracer.
Methods inherited from Tracer::Null
Constructor Details
#initialize(llm, options = {}) ⇒ Tracer
Returns a new instance of Tracer.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/llm/eval/tracer/console.rb', line 14 def initialize(llm, = {}) super @tracer = LLM::Object.from( io: [:io] || $stdout, eval: [:eval], provider: llm.name, model: nil, spans: {}, started: nil, opened: clock, header: false ) @counts = LLM::Object.from( turns: 0, requests: 0, input: 0, output: 0, tools: {}, failed: 0, errors: 0, slowest: nil ) end |
Instance Method Details
#on_exit
This method returns an undefined value.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/llm/eval/tracer/console.rb', line 127 def on_exit return unless eval.result buffer = StringIO.new io = @tracer.io @tracer.io = buffer summary @tracer.io = io io.print(buffer.string) FileUtils.mkdir_p(rundir) File.write(File.join(rundir, "sample.txt"), buffer.string.gsub(/\e\[[0-9;]*m/, "")) end |
#eval ⇒ LLM::Eval
29 30 31 |
# File 'lib/llm/eval/tracer/console.rb', line 29 def eval @tracer.eval end |
#rundir ⇒ String
35 36 37 38 39 |
# File 'lib/llm/eval/tracer/console.rb', line 35 def rundir File.join(Dir.getwd, "runs", eval.name, llm.name.to_s, eval.model.to_s, eval.sample.to_s) end |
#start_trace
This method returns an undefined value.
44 45 46 47 |
# File 'lib/llm/eval/tracer/console.rb', line 44 def start_trace(...) @counts.turns += 1 super end |
#on_request_start(operation:, model: nil)
This method returns an undefined value.
52 53 54 55 56 57 58 59 |
# File 'lib/llm/eval/tracer/console.rb', line 52 def on_request_start(operation:, model: nil, **) @tracer.started = clock @tracer.model ||= model @counts.requests += 1 if operation == "chat" return unless operation == "chat" header say "#{paint("->", :cyan)} #{@tracer.provider}#{model ? " #{model}" : ""}" end |
#on_request_finish(operation:, res:)
This method returns an undefined value.
64 65 66 67 68 69 70 |
# File 'lib/llm/eval/tracer/console.rb', line 64 def on_request_finish(operation:, res:, **) return unless operation == "chat" usage = res.usage @counts.input += usage.input_tokens @counts.output += usage.output_tokens say "#{paint("<-", :green)} #{format("%.1fs", clock - @tracer.started)}" end |
#on_request_error(ex:)
This method returns an undefined value.
75 76 77 78 |
# File 'lib/llm/eval/tracer/console.rb', line 75 def on_request_error(ex:, **) @counts.errors += 1 say "#{paint("!!", :red)} #{ex.class}: #{ex.}" end |
#on_tool_start(id:, name:, arguments:) ⇒ LLM::Object
Returns The span an ending is handed, with the call's id and name.
84 85 86 87 88 89 |
# File 'lib/llm/eval/tracer/console.rb', line 84 def on_tool_start(id:, name:, arguments:, **) @tracer.spans[id] = clock @counts.tools[name] = @counts.tools.fetch(name, 0) + 1 say " #{paint("..", :yellow)} #{name} #{trim(arguments)}" LLM::Object.from(id:, name:) end |
#on_tool_finish(result:)
This method returns an undefined value.
94 95 96 97 98 99 100 |
# File 'lib/llm/eval/tracer/console.rb', line 94 def on_tool_finish(result:, **) duration = took(result.id) if duration && (@counts.slowest.nil? || duration > @counts.slowest.last) @counts.slowest = [result.name, duration] end say " #{paint("ok", :green)} #{result.name} #{duration ? format("%.2fs", duration) : "?"}" end |
#on_tool_error(ex:)
This method returns an undefined value.
105 106 107 108 |
# File 'lib/llm/eval/tracer/console.rb', line 105 def on_tool_error(ex:, **) @counts.failed += 1 say " #{paint("no", :red)} #{ex.value[:type]}: #{ex.value[:message]}" end |
#on_tool_interrupt(ex:, span:)
This method returns an undefined value.
113 114 115 116 |
# File 'lib/llm/eval/tracer/console.rb', line 113 def on_tool_interrupt(ex:, span:, **) @counts.failed += 1 say " #{paint("no", :red)} #{span.name} interrupted: #{ex.}" end |
#on_interrupt(scope:)
This method returns an undefined value.
121 122 123 |
# File 'lib/llm/eval/tracer/console.rb', line 121 def on_interrupt(scope:, **) say "#{paint("!!", :red)} #{scope} interrupted" end |