Class: LLM::Tracer::Logger

Inherits:
Tracer
  • Object
show all
Defined in:
lib/llm/tracer/logger.rb

Overview

The LLM::Tracer::Logger class provides a tracer that provides logging facilities through Ruby's standard library.

Examples:

llm = LLM.openai(key: ENV["KEY"])
# Log to a file
llm.tracer = LLM::Tracer.logger(llm, path: "/tmp/log.txt")
# Log to $stdout (default)
llm.tracer = LLM::Tracer.logger(llm, io: $stdout)

Instance Method Summary collapse

Constructor Details

#initialize(llm, options = {}) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • llm (LLM::Provider) —

    A provider

  • options (Hash) (defaults to: {}) —

    A hash of options



18
19
20
21
# File 'lib/llm/tracer/logger.rb', line 18

def initialize(llm, options = {})
  super
  setup!(**options)
end

Instance Method Details

#on_exit ⇒ nil

No-op.

Returns:

  • (nil)


140
141
142
# File 'lib/llm/tracer/logger.rb', line 140

def on_exit
  nil
end

#on_request_start(operation:, model: nil)

This method returns an undefined value.

Parameters:

  • operation (String)
  • model (String) (defaults to: nil)
  • inputs (Hash, nil) —

    Optional span attributes (e.g. gen_ai.input.messages) from llm.rb or caller.

  • request_id (String) —

    The id of the request. It is a UUIDv7, and it is shared by #on_request_start, #on_request_finish, and #on_request_error for the same request.



26
27
28
29
30
31
32
# File 'lib/llm/tracer/logger.rb', line 26

def on_request_start(operation:, model: nil, **)
  case operation
  when "chat" then start_chat(operation:, model:)
  when "retrieval" then start_retrieval(operation:)
  else nil
  end
end

#on_request_finish(operation:, res:, model: nil)

This method returns an undefined value.

Parameters:

  • operation (String)
  • res (LLM::Response)
  • span (Object, nil)
  • model (String) (defaults to: nil)
  • outputs (Hash, nil) —

    Optional span attributes (e.g. gen_ai.output.messages) from llm.rb or caller.

  • metadata (Hash, nil) —

    Optional metadata from llm.rb or caller.

  • request_id (String) —

    The id of the request that can be matched back to #on_request_start



37
38
39
40
41
42
43
# File 'lib/llm/tracer/logger.rb', line 37

def on_request_finish(operation:, res:, model: nil, **)
  case operation
  when "chat" then finish_chat(operation:, res:, model:)
  when "retrieval" then finish_retrieval(operation:, res:)
  else nil
  end
end

#on_request_error(ex:)

This method returns an undefined value.

Parameters:



48
49
50
51
52
53
54
55
56
# File 'lib/llm/tracer/logger.rb', line 48

def on_request_error(ex:, **)
  @logger.error(
    tracer: "llm.rb (logger)",
    event: "request.error",
    provider: llm.name,
    error_class: ex.class.to_s,
    error_message: ex.message
  )
end

#on_tool_start(id:, name:, arguments:, model:) ⇒ LLM::Object

Note:

An "ending" describes the method that matches the end of a tool call, and it can be one of: on_tool_finish, on_tool_error or on_tool_interrupt.

Returns The span an ending is handed, with the call's id and name.

Parameters:

  • id (String) —

    The tool call ID assigned by the model/provider

  • name (String) —

    The tool (function) name.

  • arguments (Hash) —

    The parsed tool arguments.

  • model (String) —

    The model name

Returns:

  • (LLM::Object) —

    The span an ending is handed, with the call's id and name



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/llm/tracer/logger.rb', line 65

def on_tool_start(id:, name:, arguments:, model:, **)
  @logger.info(
    tracer: "llm.rb (logger)",
    event: "tool.start",
    provider: llm.name,
    operation: "execute_tool",
    tool_id: id,
    tool_name: name,
    tool_arguments: arguments,
    model:
  )
  LLM::Object.from(id:, name:)
end

#on_tool_finish(result:)

This method returns an undefined value.

Parameters:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/llm/tracer/logger.rb', line 82

def on_tool_finish(result:, **)
  @logger.info(
    tracer: "llm.rb (logger)",
    event: "tool.finish",
    provider: llm.name,
    operation: "execute_tool",
    tool_id: result.id,
    tool_name: result.name,
    tool_result: result.value
  )
end

#on_tool_error(ex:)

This method returns an undefined value.

Parameters:



97
98
99
100
101
102
103
104
105
106
# File 'lib/llm/tracer/logger.rb', line 97

def on_tool_error(ex:, **)
  @logger.error(
    tracer: "llm.rb (logger)",
    event: "tool.error",
    provider: llm.name,
    operation: "execute_tool",
    error_class: ex.value[:type],
    error_message: ex.value[:message]
  )
end

#on_tool_interrupt(ex:, span:)

This method returns an undefined value.

Parameters:



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/llm/tracer/logger.rb', line 111

def on_tool_interrupt(ex:, span:, **)
  @logger.info(
    tracer: "llm.rb (logger)",
    event: "tool.interrupt",
    provider: llm.name,
    operation: "execute_tool",
    tool_id: span.id,
    tool_name: span.name,
    error_class: ex.class.to_s,
    error_message: ex.message
  )
end

#on_interrupt(scope:, request_id: nil)

This method returns an undefined value.

Parameters:



127
128
129
130
131
132
133
134
135
# File 'lib/llm/tracer/logger.rb', line 127

def on_interrupt(scope:, request_id: nil, **)
  @logger.info(
    tracer: "llm.rb (logger)",
    event: "#{scope}.interrupt",
    provider: llm.name,
    scope:,
    request_id:
  )
end