Class: NewRelic::Agent::LLM

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/llm.rb

Constant Summary collapse

INPUT =
'input'
CONTENT =
'content'
SEGMENT_PATTERN =
%r{Llm/.+/OpenAI/.+}.freeze

Class Method Summary collapse

Class Method Details

.exempt_event_attribute?(type, key) ⇒ Boolean

LLM content-related attributes are exempt from the 4095 byte limit

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/new_relic/agent/llm.rb', line 23

def self.exempt_event_attribute?(type, key)
  return false unless instrumentation_enabled?

  (type == NewRelic::Agent::Llm::Embedding::EVENT_NAME && key == INPUT) ||
    (type == NewRelic::Agent::Llm::ChatCompletionMessage::EVENT_NAME && key == CONTENT)
end

.instrumentation_enabled?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/new_relic/agent/llm.rb', line 18

def self.instrumentation_enabled?
  NewRelic::Agent.config[:'ai_monitoring.enabled']
end

.openai?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/new_relic/agent/llm.rb', line 30

def self.openai?
  @openai ||= %i[prepend chain].include?(NewRelic::Agent.config[:'instrumentation.ruby_openai']) &&
    NewRelic::Agent.config[:'ai_monitoring.enabled']
end

.openai_parent?(segment) ⇒ Boolean

Used in NetHTTP instrumentation

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/new_relic/agent/llm.rb', line 36

def self.openai_parent?(segment)
  return false unless openai?

  segment&.parent&.name&.match?(SEGMENT_PATTERN)
end

.populate_openai_response_headers(response, parent) ⇒ Object



42
43
44
45
46
# File 'lib/new_relic/agent/llm.rb', line 42

def self.populate_openai_response_headers(response, parent)
  return unless parent.instance_variable_defined?(:@llm_event)

  parent.llm_event.populate_openai_response_headers(response.to_hash)
end