From f97489f49b485c811b13ad865cb8b2d14130320f Mon Sep 17 00:00:00 2001 From: ned Date: Thu, 2 Mar 2023 20:38:56 +0100 Subject: [PATCH] added conf option to show token usage --- .env.example | 5 ++++- README.md | 4 +++- gpt_helper.py | 15 +++++++++++---- main.py | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 17f745f1..47df54da 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,7 @@ OPENAI_API_KEY="XXX" TELEGRAM_BOT_TOKEN="XXX" # Comma separated list of telegram user IDs, or * to allow all -ALLOWED_TELEGRAM_USER_IDS="USER_ID_1, str: ) if len(response.choices) > 0: + answer = '' + if len(response.choices) > 1 and self.config['n_choices'] > 1: - answer = '' for index, choice in enumerate(response.choices): if index == 0: self.history.append({"role": "assistant", "content": choice['message']['content']}) answer += f'{index+1}\u20e3\n' answer += choice['message']['content'] answer += '\n\n' - return answer else: answer = response.choices[0]['message']['content'] self.history.append({"role": "assistant", "content": answer}) - return answer + + if self.config['show_usage']: + answer += "\n\n---\n" \ + f"💰 Tokens used: {str(response.usage['total_tokens'])}" \ + f" ({str(response.usage['prompt_tokens'])} prompt," \ + f" {str(response.usage['completion_tokens'])} completion)" + + return answer else: logging.error('No response from GPT-3') - return "No response from GPT-3" + return "⚠️ _An error has occurred_ ⚠️\nPlease try again in a while." except openai.error.RateLimitError as e: logging.exception(e) diff --git a/main.py b/main.py index 2d65e189..f11af833 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,7 @@ def main(): # Setup configurations gpt_config = { 'api_key': os.environ['OPENAI_API_KEY'], + 'show_usage': os.environ.get('SHOW_USAGE', 'true').lower() == 'true', # 'gpt-3.5-turbo' or 'gpt-3.5-turbo-0301' 'model': 'gpt-3.5-turbo',