109
109
110
110
# model picker auto-switch
111
111
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
-
122
112
if not config_auto .has_section ('ModelAutoSwitch' ):
123
- # Failsafe; no auto-switch config => just keep the existing bot.model
124
113
logging .info ("Auto-switch is not configured. Using default model: %s" , bot .model )
125
114
return True
126
115
127
116
if not config_auto ['ModelAutoSwitch' ].getboolean ('Enabled' , fallback = False ):
128
- # Auto-switch disabled => do nothing
129
117
logging .info ("ModelAutoSwitch.Enabled = False => skipping auto-switch, using %s" , bot .model )
130
118
return True
131
119
132
- # Pull from [ModelAutoSwitch]
133
120
premium_model = config_auto ['ModelAutoSwitch' ].get ('PremiumModel' , 'gpt-4' )
134
121
fallback_model = config_auto ['ModelAutoSwitch' ].get ('FallbackModel' , 'gpt-3.5-turbo' )
135
122
premium_limit = config_auto ['ModelAutoSwitch' ].getint ('PremiumTokenLimit' , 500000 )
136
123
fallback_limit = config_auto ['ModelAutoSwitch' ].getint ('MiniTokenLimit' , 10000000 )
137
124
fallback_action = config_auto ['ModelAutoSwitch' ].get ('FallbackLimitAction' , 'Deny' )
138
125
139
- # Attempt to read today's usage from DB
140
126
if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH :
141
127
logging .warning ("DB not initialized or path missing — can't auto-switch, fallback to default model." )
142
128
return True
143
129
144
130
usage_date = datetime .datetime .utcnow ().strftime ('%Y-%m-%d' )
145
131
daily_usage = _get_daily_usage_sync (DB_PATH , usage_date )
146
132
if not daily_usage :
147
- # Possibly no row yet => usage is (0,0)
148
133
daily_premium_tokens , daily_fallback_tokens = (0 , 0 )
149
134
else :
150
135
daily_premium_tokens , daily_fallback_tokens = daily_usage
151
136
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
+
152
141
# Decide if we can still use the premium model
153
142
if daily_premium_tokens < premium_limit :
154
143
bot .model = premium_model
@@ -171,7 +160,6 @@ def pick_model_auto_switch(bot):
171
160
bot .model = fallback_model
172
161
return True
173
162
else :
174
- # 'Proceed' => silently continue using fallback
175
163
logging .info ("Fallback limit reached => 'Proceed' => continuing anyway with fallback." )
176
164
bot .model = fallback_model
177
165
return True
0 commit comments