Skip to content

Commit

Permalink
Merge branch 'main' into feature/support-functions
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
n3d1117 committed Aug 4, 2023
2 parents 3b735f3 + 3d2231a commit e1f328b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A [Telegram bot](https://core.telegram.org/bots/api) that integrates with OpenAI
- [x] Transcribe audio and video messages using Whisper (may require [ffmpeg](https://ffmpeg.org))
- [x] Automatic conversation summary to avoid excessive token usage
- [x] Track token usage per user - by [@AlexHTW](https://github.com/AlexHTW)
- [x] Get personal token usage statistics and cost per day/month via the `/stats` command - by [@AlexHTW](https://github.com/AlexHTW)
- [x] Get personal token usage statistics via the `/stats` command - by [@AlexHTW](https://github.com/AlexHTW)
- [x] User budgets and guest budgets - by [@AlexHTW](https://github.com/AlexHTW)
- [x] Stream support
- [x] GPT-4 support
Expand Down
43 changes: 22 additions & 21 deletions bot/openai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,24 +445,25 @@ def __count_tokens(self, messages) -> int:
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens

def get_billing_current_month(self):
"""Gets billed usage for current month from OpenAI API.
:return: dollar amount of usage this month
"""
headers = {
"Authorization": f"Bearer {openai.api_key}"
}
# calculate first and last day of current month
today = date.today()
first_day = date(today.year, today.month, 1)
_, last_day_of_month = monthrange(today.year, today.month)
last_day = date(today.year, today.month, last_day_of_month)
params = {
"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)
usage_month = billing_data["total_usage"] / 100 # convert cent amount to dollars
return usage_month
# No longer works as of July 21st 2023, as OpenAI has removed the billing API
# def get_billing_current_month(self):
# """Gets billed usage for current month from OpenAI API.
#
# :return: dollar amount of usage this month
# """
# headers = {
# "Authorization": f"Bearer {openai.api_key}"
# }
# # calculate first and last day of current month
# today = date.today()
# first_day = date(today.year, today.month, 1)
# _, last_day_of_month = monthrange(today.year, today.month)
# last_day = date(today.year, today.month, last_day_of_month)
# params = {
# "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)
# usage_month = billing_data["total_usage"] / 100 # convert cent amount to dollars
# return usage_month
17 changes: 10 additions & 7 deletions bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ async def stats(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
f"{localized_text(budget_period, bot_language)}: "
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
if is_admin(self.config, user_id):
text_budget += (
f"{localized_text('stats_openai', bot_language)}"
f"{self.openai.get_billing_current_month():.2f}"
)
# if is_admin(self.config, user_id):
# text_budget += (
# f"{localized_text('stats_openai', bot_language)}"
# f"{self.openai.get_billing_current_month():.2f}"
# )

usage_text = text_current_conversation + text_today + text_month + text_budget
await update.message.reply_text(usage_text, parse_mode=constants.ParseMode.MARKDOWN)
Expand Down Expand Up @@ -371,8 +372,10 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):

if is_group_chat(update):
trigger_keyword = self.config['group_trigger_keyword']
if prompt.lower().startswith(trigger_keyword.lower()):
prompt = prompt[len(trigger_keyword):].strip()

if prompt.lower().startswith(trigger_keyword.lower()) or update.message.text.lower().startswith('/chat'):
if prompt.lower().startswith(trigger_keyword.lower()):
prompt = prompt[len(trigger_keyword):].strip()

if update.message.reply_to_message and \
update.message.reply_to_message.text and \
Expand Down

0 comments on commit e1f328b

Please sign in to comment.