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.

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)
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)
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)
Copy link

codecov bot commented Jan 29, 2024

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (06ff541) 98.44% compared to head (ffd8a2d) 98.03%.

Files Patch % Lines
billing.go 71.42% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #591      +/-   ##
==========================================
- Coverage   98.44%   98.03%   -0.41%     
==========================================
  Files          24       25       +1     
  Lines        1347     1373      +26     
==========================================
+ Hits         1326     1346      +20     
- Misses         15       19       +4     
- Partials        6        8       +2     

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

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.

None yet

2 participants