mirror of
https://github.com/aljazceru/chatgpt-telegram-bot.git
synced 2025-12-20 22:24:57 +01:00
added current conversation stats to /stats
This commit is contained in:
@@ -28,6 +28,16 @@ class OpenAIHelper:
|
|||||||
self.conversations: dict[int: list] = {} # {chat_id: history}
|
self.conversations: dict[int: list] = {} # {chat_id: history}
|
||||||
self.last_updated: dict[int: datetime] = {} # {chat_id: last_update_timestamp}
|
self.last_updated: dict[int: datetime] = {} # {chat_id: last_update_timestamp}
|
||||||
|
|
||||||
|
def get_conversation_stats(self, chat_id: int) -> tuple[int, int]:
|
||||||
|
"""
|
||||||
|
Gets the number of messages and tokens used in the conversation.
|
||||||
|
:param chat_id: The chat ID
|
||||||
|
:return: A tuple containing the number of messages and tokens used
|
||||||
|
"""
|
||||||
|
if chat_id not in self.conversations:
|
||||||
|
self.reset_chat_history(chat_id)
|
||||||
|
return (len(self.conversations[chat_id]), self.__count_tokens(self.conversations[chat_id]))
|
||||||
|
|
||||||
async def get_chat_response(self, chat_id: int, query: str) -> Union[tuple[str, str], str]:
|
async def get_chat_response(self, chat_id: int, query: str) -> Union[tuple[str, str], str]:
|
||||||
"""
|
"""
|
||||||
Gets a response from the GPT-3 model.
|
Gets a response from the GPT-3 model.
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ class ChatGPT3TelegramBot:
|
|||||||
transcribe_durations = self.usage[user_id].get_current_transcription_duration()
|
transcribe_durations = self.usage[user_id].get_current_transcription_duration()
|
||||||
cost_today, cost_month = self.usage[user_id].get_current_cost()
|
cost_today, cost_month = self.usage[user_id].get_current_cost()
|
||||||
|
|
||||||
|
chat_id = update.effective_chat.id
|
||||||
|
chat_messages, chat_token_length = self.openai.get_conversation_stats(chat_id)
|
||||||
|
|
||||||
usage_text = f"Today:\n"+\
|
usage_text = f"Today:\n"+\
|
||||||
f"{tokens_today} chat tokens used.\n"+\
|
f"{tokens_today} chat tokens used.\n"+\
|
||||||
f"{images_today} images generated.\n"+\
|
f"{images_today} images generated.\n"+\
|
||||||
@@ -80,7 +83,11 @@ class ChatGPT3TelegramBot:
|
|||||||
f"{tokens_month} chat tokens used.\n"+\
|
f"{tokens_month} chat tokens used.\n"+\
|
||||||
f"{images_month} images generated.\n"+\
|
f"{images_month} images generated.\n"+\
|
||||||
f"{transcribe_durations[2]} minutes and {transcribe_durations[3]} seconds transcribed.\n"+\
|
f"{transcribe_durations[2]} minutes and {transcribe_durations[3]} seconds transcribed.\n"+\
|
||||||
f"💰 For a total amount of ${cost_month:.2f}"
|
f"💰 For a total amount of ${cost_month:.2f}"+\
|
||||||
|
f"\n----------------------------\n\n"+\
|
||||||
|
f"Current conversation:\n"+\
|
||||||
|
f"{chat_messages} chat messages in history.\n"+\
|
||||||
|
f"{chat_token_length} chat tokens in history.\n"
|
||||||
await update.message.reply_text(usage_text)
|
await update.message.reply_text(usage_text)
|
||||||
|
|
||||||
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
async def reset(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
@@ -374,7 +381,7 @@ class ChatGPT3TelegramBot:
|
|||||||
"""
|
"""
|
||||||
Handles errors in the telegram-python-bot library.
|
Handles errors in the telegram-python-bot library.
|
||||||
"""
|
"""
|
||||||
logging.debug(f'Exception while handling an update: {context.error}')
|
logging.error(f'Exception while handling an update: {context.error}')
|
||||||
|
|
||||||
def is_group_chat(self, update: Update) -> bool:
|
def is_group_chat(self, update: Update) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user