Sha256: 6d50ce15067d53888e2f886fe416ef3451b4e511efe99edf1391fff812dd506b

Contents?: true

Size: 1.59 KB

Versions: 13

Compression:

Stored size: 1.59 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative 'llm/llm_event'
require_relative 'llm/chat_completion_message'
require_relative 'llm/chat_completion_summary'
require_relative 'llm/embedding'
require_relative 'llm/response_headers'

module NewRelic
  module Agent
    class LLM
      INPUT = 'input'
      CONTENT = 'content'
      SEGMENT_PATTERN = %r{Llm/.+/OpenAI/.+}.freeze

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

      # LLM content-related attributes are exempt from the 4095 byte limit
      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

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

      # Used in NetHTTP instrumentation
      def self.openai_parent?(segment)
        return false unless openai?

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

      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
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
newrelic_rpm-9.17.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.16.1 lib/new_relic/agent/llm.rb
newrelic_rpm-9.16.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.15.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.14.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.13.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.12.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.11.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.10.2 lib/new_relic/agent/llm.rb
newrelic_rpm-9.10.1 lib/new_relic/agent/llm.rb
newrelic_rpm-9.10.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.9.0 lib/new_relic/agent/llm.rb
newrelic_rpm-9.8.0 lib/new_relic/agent/llm.rb