Skip to content

Commit d821530

Browse files
authored
Merge pull request #15 from jasonacox/v0.15.16
Add /think filter command to chatbot
2 parents 597b887 + c77284a commit d821530

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## 0.15.16 - Think Tags
4+
5+
* Chatbot - Add `/think filter` command and `THINK_FILTER` envrionmental setting to have chatbot filter out (no display) the \<think>\</think> content from models that have built in CoT reasoning like [Deepseek R1](https://huggingface.co/deepseek-ai/DeepSeek-R1).
6+
37
## 0.15.15 - Docker Compose
48

59
* Quick Start using Docker compose for Chatbot.

chatbot/litellm/pull.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#
3+
# Script to help pull a model for the OLLAMA server
4+
# running in a Docker container.
5+
6+
echo "Pull the model for the OLLAMA server."
7+
echo ""
8+
9+
# Ask user to input the model name
10+
if [ -z "$1" ]; then
11+
echo "Model to pull: "
12+
read -r model
13+
MODEL="$model"
14+
else
15+
MODEL="$1"
16+
fi
17+
18+
# Check if the model name is not empty
19+
if [ -n "$MODEL" ]; then
20+
# Ask user if they want to continue
21+
read -p "Are you sure you want to pull the model: $MODEL? (y/n) " -n 1 -r
22+
echo
23+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
24+
echo "Exiting..."
25+
exit 1
26+
fi
27+
echo ""
28+
echo "Pulling the model: $MODEL..."
29+
else
30+
echo "No model name provided. Exiting..."
31+
exit 1
32+
fi
33+
34+
# Check if the Docker container is running
35+
if ! docker ps | grep -q ollama; then
36+
echo "Docker container 'ollama' is not running. Exiting..."
37+
exit 1
38+
fi
39+
40+
docker exec -it ollama bash -c "ollama pull \"$MODEL\""
41+
if [ $? -eq 0 ]; then
42+
echo "Done."
43+
else
44+
echo "Failed to pull the model. Exiting..."
45+
exit 1
46+
fi
47+
48+
echo ""

chatbot/server.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def debug(text):
140140
EXTRA_BODY = os.environ.get("EXTRA_BODY", None) # Extra body parameters for OpenAI API
141141
TOXIC_THRESHOLD = float(os.environ.get("TOXIC_THRESHOLD", 99)) # Toxicity threshold for responses 0-1 or 99 disable
142142
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
143144

144145
# Convert EXTRA_BODY to dictionary if it is proper JSON
145146
if EXTRA_BODY:
@@ -744,6 +745,7 @@ async def home(format: str = None):
744745
"Toxicity Threshold (TOXIC_THRESHOLD)": TOXIC_THRESHOLD,
745746
"Extra Body Parameters (EXTRA_BODY)": EXTRA_BODY,
746747
"Thinking Mode (THINKING)": THINKING,
748+
"Think Tag Filter (THINK_FILTER)": THINK_FILTER,
747749
}
748750
if format == "json":
749751
return data
@@ -949,18 +951,30 @@ async def send_update(session_id):
949951
response= await ask(client[session_id]["prompt"],session_id)
950952
completion_text = ''
951953
tokens = 0
954+
in_thinking = False
952955
# Iterate through the stream of tokens and send to client
953956
stime = time.time()
954957
for event in response:
955958
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() == "":
958961
continue
959962
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
960974
chunk = event_text
961975
completion_text += chunk
962976
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)
964978
# Update footer with stats
965979
await sio.emit('update', {'update':
966980
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):
10251039
client[session_id]["results"] = RESULTS
10261040
client[session_id]["image_data"] = ""
10271041
client[session_id]["model"] = MYMODEL
1042+
client[session_id]["think"] = THINK_FILTER
10281043
# Start continuous task to send updates
10291044
asyncio.create_task(send_update(session_id))
10301045

@@ -1270,6 +1285,7 @@ async def handle_think_command(session_id, p):
12701285
/think on
12711286
/think off
12721287
/think always
1288+
/think filter
12731289
"""
12741290
think = p[6:].strip()
12751291
parts = think.split()
@@ -1287,10 +1303,24 @@ async def handle_think_command(session_id, p):
12871303
client[session_id]["cot_always"] = True
12881304
await sio.emit('update', {'update': '[Chain of Thought Mode Always On]', 'voice': 'user'}, room=session_id)
12891305
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
12901320
else:
12911321
state = "ON" if client[session_id]["cot"] else "OFF"
12921322
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)
12941324

12951325
async def handle_model_command(session_id, p):
12961326
# List or set LLM Models

chatbot/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "v0.15.15"
1+
VERSION = "v0.15.16"

0 commit comments

Comments
 (0)