Skip to content

Commit

Permalink
Merge pull request #216 from AlexHTW/fix-token-count-if-stream-false
Browse files Browse the repository at this point in the history
Fix token counting if stream=false
  • Loading branch information
n3d1117 committed Apr 13, 2023
2 parents c6932c8 + f045579 commit 980aa15
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bot/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ async def prompt(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
total_tokens = int(tokens)

else:
total_tokens = 0
async def _reply():
nonlocal total_tokens
response, total_tokens = await self.openai.get_chat_response(chat_id=chat_id, query=prompt)

# Split into chunks of 4096 characters (Telegram's message limit)
Expand Down Expand Up @@ -488,7 +490,8 @@ async def _reply():
allowed_user_ids = self.config['allowed_user_ids'].split(',')
if str(user_id) not in allowed_user_ids and 'guests' in self.usage:
self.usage["guests"].add_chat_tokens(total_tokens, self.config['token_price'])
except:
except Exception as e:
logging.warning(f'Failed to add tokens to usage_logs: {str(e)}')
pass

except Exception as e:
Expand Down

0 comments on commit 980aa15

Please sign in to comment.