Skip to content

Commit 0cc29de

Browse files
committed
v0.76 - premium mode auto-switching
1 parent 2052d70 commit 0cc29de

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/text_message_handler.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,35 @@
109109

110110
# model picker auto-switch
111111
def pick_model_auto_switch(bot):
112-
"""
113-
Reads today's usage from SQLite, checks config.ini [ModelAutoSwitch]
114-
and sets `bot.model` to either the premium or fallback model as needed.
115-
Returns True if we successfully picked a model,
116-
Returns False if usage is so high that we must deny further requests.
117-
"""
118-
119-
logging.info(f"Daily premium tokens = {daily_premium_tokens}, daily fallback tokens = {daily_fallback_tokens}")
120-
logging.info(f"Premium limit = {premium_limit}, Fallback limit = {fallback_limit}, fallback_action = {fallback_action}")
121-
122112
if not config_auto.has_section('ModelAutoSwitch'):
123-
# Failsafe; no auto-switch config => just keep the existing bot.model
124113
logging.info("Auto-switch is not configured. Using default model: %s", bot.model)
125114
return True
126115

127116
if not config_auto['ModelAutoSwitch'].getboolean('Enabled', fallback=False):
128-
# Auto-switch disabled => do nothing
129117
logging.info("ModelAutoSwitch.Enabled = False => skipping auto-switch, using %s", bot.model)
130118
return True
131119

132-
# Pull from [ModelAutoSwitch]
133120
premium_model = config_auto['ModelAutoSwitch'].get('PremiumModel', 'gpt-4')
134121
fallback_model = config_auto['ModelAutoSwitch'].get('FallbackModel', 'gpt-3.5-turbo')
135122
premium_limit = config_auto['ModelAutoSwitch'].getint('PremiumTokenLimit', 500000)
136123
fallback_limit = config_auto['ModelAutoSwitch'].getint('MiniTokenLimit', 10000000)
137124
fallback_action = config_auto['ModelAutoSwitch'].get('FallbackLimitAction', 'Deny')
138125

139-
# Attempt to read today's usage from DB
140126
if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH:
141127
logging.warning("DB not initialized or path missing — can't auto-switch, fallback to default model.")
142128
return True
143129

144130
usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d')
145131
daily_usage = _get_daily_usage_sync(DB_PATH, usage_date)
146132
if not daily_usage:
147-
# Possibly no row yet => usage is (0,0)
148133
daily_premium_tokens, daily_fallback_tokens = (0, 0)
149134
else:
150135
daily_premium_tokens, daily_fallback_tokens = daily_usage
151136

137+
# --> NOW WE CAN SAFELY LOG THE USAGE & LIMITS <--
138+
logging.info(f"Daily premium tokens = {daily_premium_tokens}, daily fallback tokens = {daily_fallback_tokens}")
139+
logging.info(f"Premium limit = {premium_limit}, Fallback limit = {fallback_limit}, fallback_action = {fallback_action}")
140+
152141
# Decide if we can still use the premium model
153142
if daily_premium_tokens < premium_limit:
154143
bot.model = premium_model
@@ -171,7 +160,6 @@ def pick_model_auto_switch(bot):
171160
bot.model = fallback_model
172161
return True
173162
else:
174-
# 'Proceed' => silently continue using fallback
175163
logging.info("Fallback limit reached => 'Proceed' => continuing anyway with fallback.")
176164
bot.model = fallback_model
177165
return True

0 commit comments

Comments
 (0)