Skip to content

Commit 915d8ac

Browse files
committed
Add Usage to README
1 parent 6bbe478 commit 915d8ac

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Stream text with GPT-4, transcribe and translate audio with Whisper, or create i
2020
- [Installation](#installation)
2121
- [Bundler](#bundler)
2222
- [Gem install](#gem-install)
23-
- [Usage](#usage)
23+
- [How to use](#how-to-use)
2424
- [Quickstart](#quickstart)
2525
- [With Config](#with-config)
2626
- [Custom timeout or base URI](#custom-timeout-or-base-uri)
@@ -103,7 +103,7 @@ and require with:
103103
require "openai"
104104
```
105105

106-
## Usage
106+
## How to use
107107

108108
- Get your API key from [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)
109109
- If you belong to multiple organizations, you can get your Organization ID from [https://platform.openai.com/account/org-settings](https://platform.openai.com/account/org-settings)
@@ -126,6 +126,7 @@ For a more robust setup, you can configure the gem with your API keys, for examp
126126
```ruby
127127
OpenAI.configure do |config|
128128
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
129+
config.admin_token = ENV.fetch("OPENAI_ADMIN_TOKEN") # Optional, used for admin endpoints, created here: https://platform.openai.com/settings/organization/admin-keys
129130
config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
130131
config.log_errors = true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
131132
end
@@ -137,10 +138,10 @@ Then you can create a client like this:
137138
client = OpenAI::Client.new
138139
```
139140

140-
You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token overridden:
141+
You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token and admin_token overridden:
141142

142143
```ruby
143-
client = OpenAI::Client.new(access_token: "access_token_goes_here")
144+
client = OpenAI::Client.new(access_token: "access_token_goes_here", admin_token: "admin_token_goes_here")
144145
```
145146

146147
#### Custom timeout or base URI
@@ -168,8 +169,9 @@ or when configuring the gem:
168169
```ruby
169170
OpenAI.configure do |config|
170171
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
171-
config.log_errors = true # Optional
172+
config.admin_token = ENV.fetch("OPENAI_ADMIN_TOKEN") # Optional, used for admin endpoints, created here: https://platform.openai.com/settings/organization/admin-keys
172173
config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
174+
config.log_errors = true # Optional
173175
config.uri_base = "https://oai.hconeai.com/" # Optional
174176
config.request_timeout = 240 # Optional
175177
config.extra_headers = {
@@ -1462,7 +1464,20 @@ File.binwrite('demo.mp3', response)
14621464
```
14631465

14641466
### Usage
1465-
The Usage API provides information about the consumption of various OpenAI services within your organization. You can retrieve usage data for different endpoints and time periods.
1467+
The Usage API provides information about the cost of various OpenAI services within your organization.
1468+
To use Admin APIs like Usage, you need to set an OPENAI_ADMIN_TOKEN, which can be generated [here](https://platform.openai.com/settings/organization/admin-keys).
1469+
1470+
```ruby
1471+
OpenAI.configure do |config|
1472+
config.admin_token = ENV.fetch("OPENAI_ADMIN_TOKEN")
1473+
end
1474+
1475+
# or
1476+
1477+
client = OpenAI::Client.new(admin_token: "123abc")
1478+
```
1479+
1480+
You can retrieve usage data for different endpoints and time periods:
14661481

14671482
```ruby
14681483
one_day_ago = Time.now.to_i - 86_400
@@ -1527,15 +1542,11 @@ To install this gem onto your local machine, run `bundle exec rake install`.
15271542
To run all tests, execute the command `bundle exec rake`, which will also run the linter (Rubocop). This repository uses [VCR](https://github.com/vcr/vcr) to log API requests.
15281543

15291544
> [!WARNING]
1530-
> If you have an `OPENAI_ACCESS_TOKEN` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
1545+
> If you have an `OPENAI_ACCESS_TOKEN` and `OPENAI_ADMIN_TOKEN` in your `ENV`, running the specs will hit the actual API, which will be slow and cost you money - 2 cents or more! Remove them from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
15311546
15321547
## Release
15331548

1534-
First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set OPENAI_ACCESS_TOKEN in your environment or pass it in like this:
1535-
1536-
```
1537-
OPENAI_ACCESS_TOKEN=123abc bundle exec rspec
1538-
```
1549+
First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set OPENAI_ACCESS_TOKEN and OPENAI_ADMIN_TOKEN in your environment.
15391550

15401551
Then update the version number in `version.rb`, update `CHANGELOG.md`, run `bundle install` to update Gemfile.lock, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
15411552

0 commit comments

Comments
 (0)