removed billing apis call - fix #385

This commit is contained in:
ned
2023-07-28 21:42:39 +02:00
parent 672ab3e79c
commit 3d2231a320
3 changed files with 29 additions and 27 deletions

View File

@@ -22,7 +22,7 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
- [x] (NEW!) Transcribe audio and video messages using Whisper (may require [ffmpeg](https://ffmpeg.org)) - [x] (NEW!) Transcribe audio and video messages using Whisper (may require [ffmpeg](https://ffmpeg.org))
- [x] (NEW!) Automatic conversation summary to avoid excessive token usage - [x] (NEW!) Automatic conversation summary to avoid excessive token usage
- [x] (NEW!) Track token usage per user - by [@AlexHTW](https://github.com/AlexHTW) - [x] (NEW!) Track token usage per user - by [@AlexHTW](https://github.com/AlexHTW)
- [x] (NEW!) Get personal token usage statistics and cost per day/month via the `/stats` command - by [@AlexHTW](https://github.com/AlexHTW) - [x] (NEW!) Get personal token usage statistics via the `/stats` command - by [@AlexHTW](https://github.com/AlexHTW)
- [x] (NEW!) User budgets and guest budgets - by [@AlexHTW](https://github.com/AlexHTW) - [x] (NEW!) User budgets and guest budgets - by [@AlexHTW](https://github.com/AlexHTW)
- [x] (NEW!) Stream support - [x] (NEW!) Stream support
- [x] (NEW!) GPT-4 support - [x] (NEW!) GPT-4 support

View File

@@ -335,24 +335,25 @@ class OpenAIHelper:
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens return num_tokens
def get_billing_current_month(self): # No longer works as of July 21st 2023, as OpenAI has removed the billing API
"""Gets billed usage for current month from OpenAI API. # def get_billing_current_month(self):
# """Gets billed usage for current month from OpenAI API.
:return: dollar amount of usage this month #
""" # :return: dollar amount of usage this month
headers = { # """
"Authorization": f"Bearer {openai.api_key}" # headers = {
} # "Authorization": f"Bearer {openai.api_key}"
# calculate first and last day of current month # }
today = date.today() # # calculate first and last day of current month
first_day = date(today.year, today.month, 1) # today = date.today()
_, last_day_of_month = monthrange(today.year, today.month) # first_day = date(today.year, today.month, 1)
last_day = date(today.year, today.month, last_day_of_month) # _, last_day_of_month = monthrange(today.year, today.month)
params = { # last_day = date(today.year, today.month, last_day_of_month)
"start_date": first_day, # params = {
"end_date": last_day # "start_date": first_day,
} # "end_date": last_day
response = requests.get("https://api.openai.com/dashboard/billing/usage", headers=headers, params=params) # }
billing_data = json.loads(response.text) # response = requests.get("https://api.openai.com/dashboard/billing/usage", headers=headers, params=params)
usage_month = billing_data["total_usage"] / 100 # convert cent amount to dollars # billing_data = json.loads(response.text)
return usage_month # usage_month = billing_data["total_usage"] / 100 # convert cent amount to dollars
# return usage_month

View File

@@ -128,12 +128,13 @@ class ChatGPTTelegramBot:
f"{localized_text(budget_period, bot_language)}: " f"{localized_text(budget_period, bot_language)}: "
f"${remaining_budget:.2f}.\n" f"${remaining_budget:.2f}.\n"
) )
# No longer works as of July 21st 2023, as OpenAI has removed the billing API
# add OpenAI account information for admin request # add OpenAI account information for admin request
if is_admin(self.config, user_id): # if is_admin(self.config, user_id):
text_budget += ( # text_budget += (
f"{localized_text('stats_openai', bot_language)}" # f"{localized_text('stats_openai', bot_language)}"
f"{self.openai.get_billing_current_month():.2f}" # f"{self.openai.get_billing_current_month():.2f}"
) # )
usage_text = text_current_conversation + text_today + text_month + text_budget usage_text = text_current_conversation + text_today + text_month + text_budget
await update.message.reply_text(usage_text, parse_mode=constants.ParseMode.MARKDOWN) await update.message.reply_text(usage_text, parse_mode=constants.ParseMode.MARKDOWN)