Sha256: 131980c11efdccd5fbe876dd8e763b3ba4afe05f5ec831c64b38d6e3085cf8e6

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 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

module NewRelic
  module Agent
    module Llm
      class Embedding < LlmEvent
        include ResponseHeaders

        ATTRIBUTES = %i[input request_model token_count duration error].freeze
        ATTRIBUTE_NAME_EXCEPTIONS = {
          request_model: 'request.model'
        }.freeze
        ERROR_EMBEDDING_ID = 'embedding_id'
        EVENT_NAME = 'LlmEmbedding'

        attr_accessor(*ATTRIBUTES)

        def attributes
          LlmEvent::ATTRIBUTES + ResponseHeaders::ATTRIBUTES + ATTRIBUTES
        end

        def attribute_name_exceptions
          # TODO: OLD RUBIES < 2.6
          # Hash#merge accepts multiple arguments in 2.6
          # Remove condition once support for Ruby <2.6 is dropped
          if RUBY_VERSION >= '2.6.0'
            LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS, ATTRIBUTE_NAME_EXCEPTIONS)
          else
            LlmEvent::ATTRIBUTE_NAME_EXCEPTIONS.merge(ResponseHeaders::ATTRIBUTE_NAME_EXCEPTIONS).merge(ATTRIBUTE_NAME_EXCEPTIONS)
          end
        end

        def event_name
          EVENT_NAME
        end

        def error_attributes(exception)
          attrs = {}
          attrs[ERROR_EMBEDDING_ID] = id

          error_attributes_from_response(exception, attrs)
        end

        private

        def error_attributes_from_response(exception, attrs)
          return attrs unless exception.respond_to?(:response)

          attrs[ERROR_ATTRIBUTE_STATUS_CODE] = exception.response.dig(:status)
          attrs[ERROR_ATTRIBUTE_CODE] = exception.response.dig(:body, ERROR_STRING, CODE_STRING)
          attrs[ERROR_ATTRIBUTE_PARAM] = exception.response.dig(:body, ERROR_STRING, PARAM_STRING)

          attrs
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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