Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI: Update response headers #2507

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions lib/new_relic/agent/llm/response_headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ module NewRelic
module Agent
module Llm
module ResponseHeaders
ATTRIBUTES = %i[response_organization llm_version rate_limit_requests
rate_limit_tokens rate_limit_remaining_requests
rate_limit_remaining_tokens rate_limit_reset_requests
rate_limit_reset_tokens]
ATTRIBUTES = %i[response_organization llm_version ratelimit_limit_requests
ratelimit_limit_tokens ratelimit_remaining_requests
ratelimit_remaining_tokens ratelimit_reset_requests
ratelimit_reset_tokens ratelimit_limit_tokens_usage_based
ratelimit_reset_tokens_usage_based
ratelimit_remaining_tokens_usage_based]

ATTRIBUTE_NAME_EXCEPTIONS = {
response_organization: 'response.organization',
llm_version: 'response.headers.llm_version',
rate_limit_requests: 'response.headers.ratelimitLimitRequests',
rate_limit_tokens: 'response.headers.ratelimitLimitTokens',
rate_limit_remaining_requests: 'response.headers.ratelimitRemainingRequests',
rate_limit_remaining_tokens: 'response.headers.ratelimitRemainingTokens',
rate_limit_reset_requests: 'response.headers.ratelimitResetRequests',
rate_limit_reset_tokens: 'response.headers.ratelimitResetTokens'
ratelimit_limit_requests: 'response.headers.ratelimitLimitRequests',
ratelimit_limit_tokens: 'response.headers.ratelimitLimitTokens',
ratelimit_remaining_requests: 'response.headers.ratelimitRemainingRequests',
ratelimit_remaining_tokens: 'response.headers.ratelimitRemainingTokens',
ratelimit_reset_requests: 'response.headers.ratelimitResetRequests',
ratelimit_reset_tokens: 'response.headers.ratelimitResetTokens',
ratelimit_limit_tokens_usage_based: 'response.headers.ratelimitLimitTokensUsageBased',
ratelimit_reset_tokens_usage_based: 'response.headers.ratelimitResetTokensUsageBased',
ratelimit_remaining_tokens_usage_based: 'response.headers.ratelimitRemainingTokensUsageBased'
}

OPENAI_ORGANIZATION = 'openai-organization'
Expand All @@ -29,6 +35,9 @@ module ResponseHeaders
X_RATELIMIT_REMAINING_TOKENS = 'x-ratelimit-remaining-tokens'
X_RATELIMIT_RESET_REQUESTS = 'x-ratelimit-reset-requests'
X_RATELIMIT_RESET_TOKENS = 'x-ratelimit-reset-tokens'
X_RATELIMIT_LIMIT_TOKENS_USAGE_BASED = 'x-ratelimit-limit-tokens-usage-based'
X_RATELIMIT_RESET_TOKENS_USAGE_BASED = 'x-ratelimit-reset-tokens-usage-based'
X_RATELIMIT_REMAINING_TOKENS_USAGE_BASED = 'x-ratelimit-remaining-tokens-usage-based'
X_REQUEST_ID = 'x-request-id'

attr_accessor(*ATTRIBUTES)
Expand All @@ -41,12 +50,29 @@ def populate_openai_response_headers(headers)
self.request_id = headers[X_REQUEST_ID]&.first
self.response_organization = headers[OPENAI_ORGANIZATION]&.first
self.llm_version = headers[OPENAI_VERSION]&.first
self.rate_limit_requests = headers[X_RATELIMIT_LIMIT_REQUESTS]&.first
self.rate_limit_tokens = headers[X_RATELIMIT_LIMIT_TOKENS]&.first
self.rate_limit_remaining_requests = headers[X_RATELIMIT_REMAINING_REQUESTS]&.first
self.rate_limit_remaining_tokens = headers[X_RATELIMIT_REMAINING_TOKENS]&.first
self.rate_limit_reset_requests = headers[X_RATELIMIT_RESET_REQUESTS]&.first
self.rate_limit_reset_tokens = headers[X_RATELIMIT_RESET_TOKENS]&.first
self.ratelimit_limit_requests = headers[X_RATELIMIT_LIMIT_REQUESTS]&.first.to_i
self.ratelimit_limit_tokens = headers[X_RATELIMIT_LIMIT_TOKENS]&.first.to_i
remaining_headers(headers)
reset_headers(headers)
tokens_usage_based_headers(headers)
Comment on lines +55 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch condition for the method was getting to be too high, so I grouped headers with similar names into their own methods

end

private

def remaining_headers(headers)
self.ratelimit_remaining_requests = headers[X_RATELIMIT_REMAINING_REQUESTS]&.first.to_i
self.ratelimit_remaining_tokens = headers[X_RATELIMIT_REMAINING_TOKENS]&.first.to_i
end

def reset_headers(headers)
self.ratelimit_reset_requests = headers[X_RATELIMIT_RESET_REQUESTS]&.first
self.ratelimit_reset_tokens = headers[X_RATELIMIT_RESET_TOKENS]&.first
end

def tokens_usage_based_headers(headers)
self.ratelimit_limit_tokens_usage_based = headers[X_RATELIMIT_LIMIT_TOKENS_USAGE_BASED]&.first.to_i
self.ratelimit_reset_tokens_usage_based = headers[X_RATELIMIT_RESET_TOKENS_USAGE_BASED]&.first
self.ratelimit_remaining_tokens_usage_based = headers[X_RATELIMIT_REMAINING_TOKENS_USAGE_BASED]&.first.to_i
end
end
end
Expand Down
30 changes: 18 additions & 12 deletions test/new_relic/agent/llm/chat_completion_summary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ def test_record_creates_an_event
summary.duration = '500'
summary.error = 'true'
summary.llm_version = '2022-01-01'
summary.rate_limit_requests = '100'
summary.rate_limit_tokens = '101'
summary.rate_limit_reset_tokens = '102'
summary.rate_limit_reset_requests = '103'
summary.rate_limit_remaining_tokens = '104'
summary.rate_limit_remaining_requests = '105'
summary.ratelimit_limit_requests = 200
summary.ratelimit_limit_tokens = 40000
summary.ratelimit_reset_tokens = '180ms'
summary.ratelimit_reset_requests = '11m32.334s'
summary.ratelimit_remaining_tokens = 39880
summary.ratelimit_remaining_requests = 198
summary.ratelimit_limit_tokens_usage_based = 40000
summary.ratelimit_reset_tokens_usage_based = '180ms'
summary.ratelimit_remaining_tokens_usage_based = 39880

summary.record
_, events = NewRelic::Agent.agent.custom_event_aggregator.harvest!
Expand All @@ -91,12 +94,15 @@ def test_record_creates_an_event
assert_equal '500', attributes['duration']
assert_equal 'true', attributes['error']
assert_equal '2022-01-01', attributes['response.headers.llm_version']
assert_equal '100', attributes['response.headers.ratelimitLimitRequests']
assert_equal '101', attributes['response.headers.ratelimitLimitTokens']
assert_equal '102', attributes['response.headers.ratelimitResetTokens']
assert_equal '103', attributes['response.headers.ratelimitResetRequests']
assert_equal '104', attributes['response.headers.ratelimitRemainingTokens']
assert_equal '105', attributes['response.headers.ratelimitRemainingRequests']
assert_equal 200, attributes['response.headers.ratelimitLimitRequests']
assert_equal 40000, attributes['response.headers.ratelimitLimitTokens']
assert_equal '180ms', attributes['response.headers.ratelimitResetTokens']
assert_equal '11m32.334s', attributes['response.headers.ratelimitResetRequests']
assert_equal 39880, attributes['response.headers.ratelimitRemainingTokens']
assert_equal 198, attributes['response.headers.ratelimitRemainingRequests']
assert_equal 40000, attributes['response.headers.ratelimitLimitTokensUsageBased']
assert_equal '180ms', attributes['response.headers.ratelimitResetTokensUsageBased']
assert_equal 39880, attributes['response.headers.ratelimitRemainingTokensUsageBased']
end
end

Expand Down
30 changes: 18 additions & 12 deletions test/new_relic/agent/llm/embedding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ def test_record_creates_an_event
embedding.duration = '500'
embedding.error = 'true'
embedding.llm_version = '2022-01-01'
embedding.rate_limit_requests = '100'
embedding.rate_limit_tokens = '101'
embedding.rate_limit_reset_tokens = '102'
embedding.rate_limit_reset_requests = '103'
embedding.rate_limit_remaining_tokens = '104'
embedding.rate_limit_remaining_requests = '105'
embedding.ratelimit_limit_requests = 200
embedding.ratelimit_limit_tokens = 40000
embedding.ratelimit_reset_tokens = '180ms'
embedding.ratelimit_reset_requests = '11m32.334s'
embedding.ratelimit_remaining_tokens = 39880
embedding.ratelimit_remaining_requests = 198
embedding.ratelimit_limit_tokens_usage_based = 40000
embedding.ratelimit_reset_tokens_usage_based = '180ms'
embedding.ratelimit_remaining_tokens_usage_based = 39880

embedding.record
_, events = NewRelic::Agent.agent.custom_event_aggregator.harvest!
Expand All @@ -80,12 +83,15 @@ def test_record_creates_an_event
assert_equal '500', attributes['duration']
assert_equal 'true', attributes['error']
assert_equal '2022-01-01', attributes['response.headers.llm_version']
assert_equal '100', attributes['response.headers.ratelimitLimitRequests']
assert_equal '101', attributes['response.headers.ratelimitLimitTokens']
assert_equal '102', attributes['response.headers.ratelimitResetTokens']
assert_equal '103', attributes['response.headers.ratelimitResetRequests']
assert_equal '104', attributes['response.headers.ratelimitRemainingTokens']
assert_equal '105', attributes['response.headers.ratelimitRemainingRequests']
assert_equal 200, attributes['response.headers.ratelimitLimitRequests']
assert_equal 40000, attributes['response.headers.ratelimitLimitTokens']
assert_equal '180ms', attributes['response.headers.ratelimitResetTokens']
assert_equal '11m32.334s', attributes['response.headers.ratelimitResetRequests']
assert_equal 39880, attributes['response.headers.ratelimitRemainingTokens']
assert_equal 198, attributes['response.headers.ratelimitRemainingRequests']
assert_equal 40000, attributes['response.headers.ratelimitLimitTokensUsageBased']
assert_equal '180ms', attributes['response.headers.ratelimitResetTokensUsageBased']
assert_equal 39880, attributes['response.headers.ratelimitRemainingTokensUsageBased']
end
end

Expand Down
19 changes: 13 additions & 6 deletions test/new_relic/agent/llm/response_headers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def openai_response_headers_hash
"x-ratelimit-remaining-tokens" => ["149990"],
"x-ratelimit-reset-requests" => ["7m12s"],
"x-ratelimit-reset-tokens" => ["4ms"],
# The following *-tokens-usage-based entries are best guesses to fulfill the spec. We haven't been able to create a request that returns these headers.
"x-ratelimit-limit-tokens-usage-based" => ["40000"],
"x-ratelimit-reset-tokens-usage-based" => ["180ms"],
"x-ratelimit-remaining-tokens-usage-based" => ["39880"],
"x-request-id" => ["123abc456"],
"cf-cache-status" => ["DYNAMIC"],
"set-cookie" =>
Expand All @@ -47,12 +51,15 @@ def test_populate_openai_response_headers
event.populate_openai_response_headers(openai_response_headers_hash)

assert_equal '2020-10-01', event.llm_version
assert_equal '200', event.rate_limit_requests
assert_equal '150000', event.rate_limit_tokens
assert_equal '199', event.rate_limit_remaining_requests
assert_equal '149990', event.rate_limit_remaining_tokens
assert_equal '7m12s', event.rate_limit_reset_requests
assert_equal '4ms', event.rate_limit_reset_tokens
assert_equal 200, event.ratelimit_limit_requests
assert_equal 150000, event.ratelimit_limit_tokens
assert_equal 199, event.ratelimit_remaining_requests
assert_equal 149990, event.ratelimit_remaining_tokens
assert_equal '7m12s', event.ratelimit_reset_requests
assert_equal '4ms', event.ratelimit_reset_tokens
assert_equal 40000, event.ratelimit_limit_tokens_usage_based
assert_equal '180ms', event.ratelimit_reset_tokens_usage_based
assert_equal 39880, event.ratelimit_remaining_tokens_usage_based
end
end
end
Loading