From 8d40ae7b424ad7a627ea20a08d94e40d62e87438 Mon Sep 17 00:00:00 2001 From: stanislavlysenko Date: Tue, 20 Jun 2023 12:07:50 +0300 Subject: [PATCH 1/3] (fix) Passing context --- bot/telegram_bot.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bot/telegram_bot.py b/bot/telegram_bot.py index 3d94bbc..8c8c8a5 100644 --- a/bot/telegram_bot.py +++ b/bot/telegram_bot.py @@ -370,7 +370,14 @@ class ChatGPTTelegramBot: if is_group_chat(update): trigger_keyword = self.config['group_trigger_keyword'] - if prompt.lower().startswith(trigger_keyword.lower()): + + raw_text = update.message.text.lower() + prompt_lower = prompt.lower() + + if raw_text.startswith('/chat') or prompt_lower.startswith(trigger_keyword): + if not prompt_lower.startswith(trigger_keyword): + prompt = f'{trigger_keyword} {prompt}' + prompt = prompt[len(trigger_keyword):].strip() if update.message.reply_to_message and \ From 4b15bafb9e5242ff654cb074b9c977af9064c352 Mon Sep 17 00:00:00 2001 From: Stanislav Lysenko <86171768+stanislavlysenko0912@users.noreply.github.com> Date: Tue, 4 Jul 2023 13:55:42 +0300 Subject: [PATCH 2/3] Simplifying the code for the message reply --- bot/telegram_bot.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/bot/telegram_bot.py b/bot/telegram_bot.py index 8c8c8a5..4cd6f21 100644 --- a/bot/telegram_bot.py +++ b/bot/telegram_bot.py @@ -371,14 +371,9 @@ class ChatGPTTelegramBot: if is_group_chat(update): trigger_keyword = self.config['group_trigger_keyword'] - raw_text = update.message.text.lower() - prompt_lower = prompt.lower() - - if raw_text.startswith('/chat') or prompt_lower.startswith(trigger_keyword): - if not prompt_lower.startswith(trigger_keyword): - prompt = f'{trigger_keyword} {prompt}' - - 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 \ From 3d2231a320379b72d06b639a729631ebdd6baa91 Mon Sep 17 00:00:00 2001 From: ned Date: Fri, 28 Jul 2023 21:42:39 +0200 Subject: [PATCH 3/3] removed billing apis call - fix #385 --- README.md | 2 +- bot/openai_helper.py | 43 ++++++++++++++++++++++--------------------- bot/telegram_bot.py | 11 ++++++----- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ef00be9..44316d2 100644 --- a/README.md +++ b/README.md @@ -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!) Automatic conversation summary to avoid excessive token usage - [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!) Stream support - [x] (NEW!) GPT-4 support diff --git a/bot/openai_helper.py b/bot/openai_helper.py index 97ccc54..60b05b0 100644 --- a/bot/openai_helper.py +++ b/bot/openai_helper.py @@ -335,24 +335,25 @@ class OpenAIHelper: 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 diff --git a/bot/telegram_bot.py b/bot/telegram_bot.py index 4cd6f21..d563eb5 100644 --- a/bot/telegram_bot.py +++ b/bot/telegram_bot.py @@ -128,12 +128,13 @@ class ChatGPTTelegramBot: 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)