Class: LLM::Tracer::EnhancedLogger
- Inherits:
-
Tracer
- Object
- Tracer
- LLM::Tracer::EnhancedLogger
- Defined in:
- lib/llm/tracer/enhanced_logger.rb
Overview
The LLM::Tracer::EnhancedLogger class writes a readable log of every chat request, tool call and interrupt, then a summary when the tracer exits. Color is used when the IO the tracer writes to is a terminal.
Constant Summary collapse
- RULE =
Returns The width of a rule.
58
Instance Method Summary collapse
-
#on_exit ⇒ nil
Writes the summary, then closes the file the tracer opened.
- #on_request_start(operation:, model: nil, request_id:)
- #on_request_finish(operation:, res:, request_id:)
- #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 = {}) ⇒ EnhancedLogger
constructor
A new instance of EnhancedLogger.
Constructor Details
#initialize(llm, options = {}) ⇒ EnhancedLogger
Returns a new instance of EnhancedLogger.
24 25 26 27 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 24 def initialize(llm, = {}) super setup!(**) end |
Instance Method Details
#on_exit ⇒ nil
Writes the summary, then closes the file the tracer opened.
111 112 113 114 115 116 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 111 def on_exit summary @file&.close @file = nil nil end |
#on_request_start(operation:, model: nil, request_id:)
This method returns an undefined value.
32 33 34 35 36 37 38 39 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 32 def on_request_start(operation:, model: nil, request_id:, **) @model = model || @model @started[request_id] = clock return unless operation == "chat" @counts.requests += 1 header say "#{paint("->", :cyan)} #{llm.name}#{model ? " #{model}" : ""}" end |
#on_request_finish(operation:, res:, request_id:)
This method returns an undefined value.
44 45 46 47 48 49 50 51 52 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 44 def on_request_finish(operation:, res:, request_id:, **) elapsed = elapsed!(request_id) return unless operation == "chat" tokens = format_tokens(res) parts = [paint("<-", :green)] parts << format("%.2fs", elapsed) if elapsed parts << tokens if tokens say parts.join(" ") end |
#on_request_error(ex:)
This method returns an undefined value.
57 58 59 60 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 57 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.
66 67 68 69 70 71 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 66 def on_tool_start(id:, name:, arguments:, **) @spans[id] = clock @counts.tools[name] = @counts.tools.fetch(name, 0) + 1 say " #{paint("..", :yellow)} #{name}(#{format_arguments(arguments)})" LLM::Object.from(id:, name:) end |
#on_tool_finish(result:)
This method returns an undefined value.
76 77 78 79 80 81 82 83 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 76 def on_tool_finish(result:, **) duration = span!(result.id) if duration && (@counts.slowest.nil? || duration > @counts.slowest.last) @counts.slowest = [result.name, duration] end timing = duration ? " #{format("%.2fs", duration)}" : "" say " #{paint("ok", :green)} #{result.name}#{timing}" end |
#on_tool_error(ex:)
This method returns an undefined value.
88 89 90 91 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 88 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.
96 97 98 99 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 96 def on_tool_interrupt(ex:, span:, **) @counts.failed += 1 say " #{paint("no", :red)} tool #{span.name} (#{format_id(span.id)}) received an interrupt" end |
#on_interrupt(scope:)
This method returns an undefined value.
104 105 106 |
# File 'lib/llm/tracer/enhanced_logger.rb', line 104 def on_interrupt(scope:, **) say "#{paint("!!", :red)} #{llm.name} interrupt received (scope=#{scope})" end |