@@ -140,6 +140,7 @@ def debug(text):
140
140
EXTRA_BODY = os .environ .get ("EXTRA_BODY" , None ) # Extra body parameters for OpenAI API
141
141
TOXIC_THRESHOLD = float (os .environ .get ("TOXIC_THRESHOLD" , 99 )) # Toxicity threshold for responses 0-1 or 99 disable
142
142
THINKING = os .environ .get ("THINKING" , "false" ).lower () == "true" # Set to True to enable thinking mode by default
143
+ THINK_FILTER = os .environ .get ("THINK_FILTER" , "false" ).lower () == "true" # Set to True to enable thinking filter
143
144
144
145
# Convert EXTRA_BODY to dictionary if it is proper JSON
145
146
if EXTRA_BODY :
@@ -744,6 +745,7 @@ async def home(format: str = None):
744
745
"Toxicity Threshold (TOXIC_THRESHOLD)" : TOXIC_THRESHOLD ,
745
746
"Extra Body Parameters (EXTRA_BODY)" : EXTRA_BODY ,
746
747
"Thinking Mode (THINKING)" : THINKING ,
748
+ "Think Tag Filter (THINK_FILTER)" : THINK_FILTER ,
747
749
}
748
750
if format == "json" :
749
751
return data
@@ -949,18 +951,30 @@ async def send_update(session_id):
949
951
response = await ask (client [session_id ]["prompt" ],session_id )
950
952
completion_text = ''
951
953
tokens = 0
954
+ in_thinking = False
952
955
# Iterate through the stream of tokens and send to client
953
956
stime = time .time ()
954
957
for event in response :
955
958
event_text = event .choices [0 ].delta .content
956
- # Skip prefixed newlines
957
- if tokens == 0 and event_text == "\n " :
959
+ # check for no tokens or a string just full of nany number of newlines only
960
+ if tokens == 0 and event_text . strip () == "" :
958
961
continue
959
962
if event_text :
963
+ if client [session_id ]["think" ]:
964
+ if "<think>" in event_text :
965
+ in_thinking = True
966
+ await sio .emit ('update' , {'update' : '' , 'voice' : 'ai' },room = session_id )
967
+ continue
968
+ elif "</think>" in event_text :
969
+ in_thinking = False
970
+ token = 0
971
+ continue
972
+ if in_thinking :
973
+ continue
960
974
chunk = event_text
961
975
completion_text += chunk
962
976
tokens += 1
963
- await sio .emit ('update' , {'update' : chunk , 'voice' : 'ai' },room = session_id )
977
+ await sio .emit ('update' , {'update' : chunk , 'voice' : 'ai' }, room = session_id )
964
978
# Update footer with stats
965
979
await sio .emit ('update' , {'update' :
966
980
f"TinyLLM Chatbot { VERSION } - { client [session_id ]['model' ]} - Tokens: { tokens } - TPS: { tokens / (time .time ()- stime ):.1f} " ,
@@ -1025,6 +1039,7 @@ async def send_update(session_id):
1025
1039
client [session_id ]["results" ] = RESULTS
1026
1040
client [session_id ]["image_data" ] = ""
1027
1041
client [session_id ]["model" ] = MYMODEL
1042
+ client [session_id ]["think" ] = THINK_FILTER
1028
1043
# Start continuous task to send updates
1029
1044
asyncio .create_task (send_update (session_id ))
1030
1045
@@ -1270,6 +1285,7 @@ async def handle_think_command(session_id, p):
1270
1285
/think on
1271
1286
/think off
1272
1287
/think always
1288
+ /think filter
1273
1289
"""
1274
1290
think = p [6 :].strip ()
1275
1291
parts = think .split ()
@@ -1287,10 +1303,24 @@ async def handle_think_command(session_id, p):
1287
1303
client [session_id ]["cot_always" ] = True
1288
1304
await sio .emit ('update' , {'update' : '[Chain of Thought Mode Always On]' , 'voice' : 'user' }, room = session_id )
1289
1305
return
1306
+ elif parts and parts [0 ] == "filter" :
1307
+ state = ""
1308
+ if len (parts ) >= 2 :
1309
+ if parts [1 ] == "on" :
1310
+ client [session_id ]["think" ] = True
1311
+ state = "ON"
1312
+ else :
1313
+ client [session_id ]["think" ] = False
1314
+ state = "OFF"
1315
+ else :
1316
+ client [session_id ]["think" ] = not client [session_id ]["think" ]
1317
+ state = "ON" if client [session_id ]["think" ] else "OFF"
1318
+ await sio .emit ('update' , {'update' : f'[Think Filter is { state } ]' , 'voice' : 'user' }, room = session_id )
1319
+ return
1290
1320
else :
1291
1321
state = "ON" if client [session_id ]["cot" ] else "OFF"
1292
1322
state = "ALWAYS" if client [session_id ]["cot_always" ] else state
1293
- await sio .emit ('update' , {'update' : f'[ Chain of Thought is { state } - Commands : /think {{on|off|always}} ] ' , 'voice' : 'user' }, room = session_id )
1323
+ await sio .emit ('update' , {'update' : f'Chain of Thought is { state } \n - Chain of Thought : /think {{on|off|always}}\n - Filter Think Tags: /think filter {{on|off}} ' , 'voice' : 'user' }, room = session_id )
1294
1324
1295
1325
async def handle_model_command (session_id , p ):
1296
1326
# List or set LLM Models
0 commit comments