Class: LLM::Eval::Tracer::JSON
- Inherits:
-
Tracer::Null
- Object
- Tracer
- Tracer::Null
- LLM::Eval::Tracer::JSON
- Defined in:
- lib/llm/eval/tracer/json.rb
Overview
JSON-based tracer.
Instance Attribute Summary collapse
- #eval ⇒ LLM::Eval readonly
Instance Method Summary collapse
-
#on_exit ⇒ String
Writes the run to disk.
- #on_request_start(operation:, model: nil)
- #on_request_finish(operation:, res: nil)
- #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 26 27 28 29 30 31 32 |
# File 'lib/llm/eval/tracer/json.rb', line 14 def initialize(llm, = {}) super @eval = [:eval] @provider = llm.name @started_at = Time.now.utc @started = nil @spans = {} @requests = [] @tools = [] @errors = [] @root = File.join( Dir.getwd, "runs", @eval.name, llm.name.to_s, eval.model.to_s, eval.sample.to_s ) end |
Instance Attribute Details
#eval ⇒ LLM::Eval (readonly)
9 10 11 |
# File 'lib/llm/eval/tracer/json.rb', line 9 def eval @eval end |
Instance Method Details
#on_exit ⇒ String
Writes the run to disk.
119 120 121 122 123 124 |
# File 'lib/llm/eval/tracer/json.rb', line 119 def on_exit FileUtils.mkdir_p(@root) dest = File.join(@root, "sample.json") File.write(dest, LLM.json.dump(report, pretty: true)) dest end |
#on_request_start(operation:, model: nil)
This method returns an undefined value.
37 38 39 40 41 42 |
# File 'lib/llm/eval/tracer/json.rb', line 37 def on_request_start(operation:, model: nil, **) @started = clock @model ||= model return unless operation == "chat" @requests << {"index" => @requests.size + 1, "model" => model} end |
#on_request_finish(operation:, res: nil)
This method returns an undefined value.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/llm/eval/tracer/json.rb', line 47 def on_request_finish(operation:, res: nil, **) return unless operation == "chat" request = @requests.last return if request.nil? input, output = tokens(res) request["duration_s"] = elapsed request["ok"] = res.respond_to?(:ok?) ? res.ok? : true request["input_tokens"] = input request["output_tokens"] = output request["reply"] = reply(res) request["reasoning"] = reasoning(res) end |
#on_request_error(ex:)
This method returns an undefined value.
63 64 65 66 67 |
# File 'lib/llm/eval/tracer/json.rb', line 63 def on_request_error(ex:, **) @errors << {"kind" => "request", "class" => ex.class.to_s, "message" => ex.} request = @requests.last request["error"] = ex. if request end |
#on_tool_start(id:, name:, arguments:) ⇒ LLM::Object
Returns The span an ending is handed, with the call's id and name.
73 74 75 76 77 |
# File 'lib/llm/eval/tracer/json.rb', line 73 def on_tool_start(id:, name:, arguments:, **) @spans[id] = clock @tools << {"name" => name, "arguments" => plain(arguments)} LLM::Object.from(id:, name:) end |
#on_tool_finish(result:)
This method returns an undefined value.
82 83 84 85 86 87 |
# File 'lib/llm/eval/tracer/json.rb', line 82 def on_tool_finish(result:, **) tool = @tools.last return if tool.nil? tool["duration_s"] = span(result.id) tool["ok"] = true end |
#on_tool_error(ex:)
This method returns an undefined value.
92 93 94 95 96 97 |
# File 'lib/llm/eval/tracer/json.rb', line 92 def on_tool_error(ex:, **) = ex.value[:message].to_s @errors << {"kind" => "tool", "class" => ex.value[:type].to_s, "message" => } tool = @tools.last tool["error"] = if tool end |
#on_tool_interrupt(ex:, span:)
This method returns an undefined value.
102 103 104 105 106 |
# File 'lib/llm/eval/tracer/json.rb', line 102 def on_tool_interrupt(ex:, span:, **) @errors << {"kind" => "tool", "class" => ex.class.to_s, "message" => ex.} tool = @tools.last tool["error"] = ex. if tool end |
#on_interrupt(scope:)
This method returns an undefined value.
111 112 113 |
# File 'lib/llm/eval/tracer/json.rb', line 111 def on_interrupt(scope:, **) @errors << {"kind" => scope.to_s, "class" => "LLM::Interrupt", "message" => "interrupted"} end |