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

Add support for querying billing details #591

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mikeb26
Copy link
Contributor

@mikeb26 mikeb26 commented Nov 16, 2023

Describe the change
This PR adds support for querying daily costs usage information (the same as shown on https://platform.openai.com/usage).

Describe your solution
The implementation uses the public endpoint available at https://api.openai.com/dashboard/billing/usage However, note that this API is not documented in https://platform.openai.com/docs/api-reference and thus this PR may be controversial to merge. The API requires a browser session key and will explicitly reject requests made using an API key.

Tests
I've added positive and negative unit tests in billing_test.go and mock the dashboard OpenAPI endpoint following the test pattern that is already established in chat_test.go.

Additional context
I'm using this feature in a CLI based gpt tool that I wrote which enables the user to query billing information from within the tool itself rather than switch to a web browser (https://github.com/mikeb26/gptcli/blob/main/cmd/gptcli/main.go#L272).

@sashabaranov
Copy link
Owner

@mikeb26 Hey, thank you for the PR! Could you please provide an example program that uses this code? (Or integration test maybe?) It would greatly help reviewing this change

@mikeb26
Copy link
Contributor Author

mikeb26 commented Nov 21, 2023

@mikeb26 Hey, thank you for the PR! Could you please provide an example program that uses this code? (Or integration test maybe?) It would greatly help reviewing this change

@sashabaranov you bet. i'm using this in a CLI based chat tool i wrote. reference here:

https://github.com/mikeb26/gptcli/blob/main/cmd/gptcli/main.go#L270

example output:

$ gptcli
gptcli> billing
Usage from 2023-10-23 - 2023-11-22:
2023-10-26:
        GPT-3.5: <$0.01
2023-11-07:
        GPT-4 Turbo: $0.24
        GPT-3.5 Turbo: <$0.01
        Base models: <$0.01
2023-11-08:
        GPT-4 Turbo: $0.25
2023-11-09:
        GPT-4 Turbo: $1.32
2023-11-10:
        GPT-4 Turbo: $0.34
2023-11-14:
        GPT-4 Turbo: $0.26
2023-11-15:
        GPT-4 Turbo: $0.50
2023-11-16:
        GPT-4 Turbo: $0.65
        GPT-3.5 Turbo: $0.01
2023-11-17:
        GPT-4 Turbo: $0.08
        GPT-3.5 Turbo: <$0.01
2023-11-18:
        GPT-4 Turbo: $0.04
2023-11-20:
        GPT-4 Turbo: $0.24
        GPT-3.5 Turbo: <$0.01

Total: $3.94

The PR also adds a unit test.

Copy link

codecov bot commented Jan 29, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 98.74%. Comparing base (774fc9d) to head (121bab2).
Report is 42 commits behind head on master.

Files Patch % Lines
billing.go 76.47% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #591      +/-   ##
==========================================
+ Coverage   98.46%   98.74%   +0.28%     
==========================================
  Files          24       27       +3     
  Lines        1364     1438      +74     
==========================================
+ Hits         1343     1420      +77     
+ Misses         15       10       -5     
- Partials        6        8       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This commit adds support for querying daily costs usage information
(as shown on https://platform.openai.com/usage). The implementation
uses the public endpoing available at
https://api.openai.com/dashboard/billing/usage  However, note that
this API is not documented in
https://platform.openai.com/docs/api-reference and may thus be
controversial to include. The API requires a browser session key and
will explicitly reject requests made using an API key.

(cherry picked from commit 2f53565)
(cherry picked from commit 33269e5)
(cherry picked from commit 26947c0)
(cherry picked from commit 7cf5c6b)
(cherry picked from commit 7669bf1)
(cherry picked from commit 7943ad7)
It looks like the github action build environment for this repo uses
a go 1.19.x environment which does not have time.DateOnly. Replace
with local constant until this is upgraded.

(cherry picked from commit 22886dd)
(cherry picked from commit 5225542)
(cherry picked from commit fbaa1f1)
(cherry picked from commit 46091ed)
(cherry picked from commit f477bcf)
(cherry picked from commit dc6b161)
It looks like the github action build environment for this repo uses
a go 1.19.x environment which does not have time.Compare(). Replace
with time.Before() and time.After() instead.

(cherry picked from commit a579ea8)
(cherry picked from commit ffd8a2d)
(cherry picked from commit 24eaaf5)
(cherry picked from commit 6020e24)
(cherry picked from commit 3abc47f)
(cherry picked from commit a1f95a8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants