Skip to content

Commit 215a61b

Browse files
committed
Add OPENAI_ADMIN_TOKEN and Usage endpoints
1 parent 04a3b5d commit 215a61b

15 files changed

+791
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [7.4.0] - 2024-02-07
9+
10+
### Added
11+
12+
- Add support for OPENAI_ADMIN_TOKEN to allow for administrative endpoints to be called.
13+
- Add support for Usage endpoints.
14+
815
## [7.3.1] - 2024-10-15
916

1017
### Fixed

lib/openai.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
require_relative "openai/audio"
1919
require_relative "openai/version"
2020
require_relative "openai/batches"
21+
require_relative "openai/usage"
2122

2223
module OpenAI
2324
class Error < StandardError; end
2425
class ConfigurationError < Error; end
26+
class AuthenticationError < Error; end
2527

2628
class MiddlewareErrors < Faraday::Middleware
2729
def call(env)
@@ -41,6 +43,7 @@ def call(env)
4143

4244
class Configuration
4345
attr_accessor :access_token,
46+
:admin_token,
4447
:api_type,
4548
:api_version,
4649
:log_errors,
@@ -56,6 +59,7 @@ class Configuration
5659

5760
def initialize
5861
@access_token = nil
62+
@admin_token = nil
5963
@api_type = nil
6064
@api_version = DEFAULT_API_VERSION
6165
@log_errors = DEFAULT_LOG_ERRORS

lib/openai/client.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ module OpenAI
22
class Client
33
include OpenAI::HTTP
44

5-
SENSITIVE_ATTRIBUTES = %i[@access_token @organization_id @extra_headers].freeze
5+
SENSITIVE_ATTRIBUTES = %i[@access_token @admin_token @organization_id @extra_headers].freeze
66
CONFIG_KEYS = %i[
7+
access_token
8+
admin_token
79
api_type
810
api_version
9-
access_token
11+
extra_headers
1012
log_errors
1113
organization_id
12-
uri_base
1314
request_timeout
14-
extra_headers
15+
uri_base
1516
].freeze
1617
attr_reader *CONFIG_KEYS, :faraday_middleware
18+
attr_writer :access_token
1719

1820
def initialize(config = {}, &faraday_middleware)
1921
CONFIG_KEYS.each do |key|
@@ -99,10 +101,25 @@ def moderations(parameters: {})
99101
json_post(path: "/moderations", parameters: parameters)
100102
end
101103

104+
def usage
105+
@usage ||= OpenAI::Usage.new(client: self)
106+
end
107+
102108
def azure?
103109
@api_type&.to_sym == :azure
104110
end
105111

112+
def admin
113+
unless admin_token
114+
e = "You must set an OPENAI_ADMIN_TOKEN= to use administrative endpoints:\n\n https://platform.openai.com/settings/organization/admin-keys"
115+
raise AuthenticationError, e
116+
end
117+
118+
dup.tap do |client|
119+
client.access_token = client.admin_token
120+
end
121+
end
122+
106123
def beta(apis)
107124
dup.tap do |client|
108125
client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))

lib/openai/compatibility.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module OpenAI
33
VERSION = ::OpenAI::VERSION
44

55
Error = ::OpenAI::Error
6+
AuthenticationError = ::OpenAI::AuthenticationError
67
ConfigurationError = ::OpenAI::ConfigurationError
78
Configuration = ::OpenAI::Configuration
89
MiddlewareErrors = ::OpenAI::MiddlewareErrors

lib/openai/usage.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module OpenAI
2+
class Usage
3+
def initialize(client:)
4+
@client = client
5+
end
6+
7+
def completions(parameters: {})
8+
@client.admin.get(
9+
path: "/organization/usage/completions",
10+
parameters: parameters
11+
)
12+
end
13+
14+
def embeddings(parameters: {})
15+
@client.admin.get(
16+
path: "/organization/usage/embeddings",
17+
parameters: parameters
18+
)
19+
end
20+
21+
def moderations(parameters: {})
22+
@client.admin.get(
23+
path: "/organization/usage/moderations",
24+
parameters: parameters
25+
)
26+
end
27+
28+
def images(parameters: {})
29+
@client.admin.get(
30+
path: "/organization/usage/images",
31+
parameters: parameters
32+
)
33+
end
34+
35+
def audio_speeches(parameters: {})
36+
@client.admin.get(
37+
path: "/organization/usage/audio_speeches",
38+
parameters: parameters
39+
)
40+
end
41+
42+
def audio_transcriptions(parameters: {})
43+
@client.admin.get(
44+
path: "/organization/usage/audio_transcriptions",
45+
parameters: parameters
46+
)
47+
end
48+
49+
def vector_stores(parameters: {})
50+
@client.admin.get(
51+
path: "/organization/usage/vector_stores",
52+
parameters: parameters
53+
)
54+
end
55+
56+
def code_interpreter_sessions(parameters: {})
57+
@client.admin.get(
58+
path: "/organization/usage/code_interpreter_sessions",
59+
parameters: parameters
60+
)
61+
end
62+
63+
def costs(parameters: {})
64+
@client.admin.get(
65+
path: "/organization/costs",
66+
parameters: parameters
67+
)
68+
end
69+
end
70+
end

spec/fixtures/cassettes/usage_audio_speeches.yml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/usage_audio_transcriptions.yml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)