AshokGakr commited on
Commit
73e461f
·
verified ·
1 Parent(s): 43962c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -22
app.py CHANGED
@@ -630,17 +630,15 @@ COMMUNICATION STYLE:
630
  TOOLS AVAILABLE:
631
  - web_search(query: str) -> str: Use for fresh facts, current info, or specific lookups.
632
 
633
- TOOLS JSON (machine-readable):
634
- <|tool_list_start|>
635
- {tools_json_str}
636
- <|tool_list_end|>
637
 
638
- TOOL CALLING RULES:
639
- - If you need a tool, respond ONLY with a tool call wrapped by tokens:
640
  <|tool_call_start|>[web_search(query="...")]<|tool_call_end|>
 
641
  - JSON tool calls are also accepted:
642
  {{"tool_call": {{"name": "web_search", "args": {{"query": "..."}}}}}}
643
- - Do not include any other text when calling a tool.
644
  - If no tool is needed, respond normally.
645
 
646
  EMOTIONAL CONTEXT:
@@ -759,6 +757,23 @@ Please provide a helpful response that:
759
  start_time = time.time()
760
  streaming_started = False
761
  tool_call = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
762
 
763
  def parse_tool_call(text: str) -> Optional[Dict[str, Any]]:
764
  # 1) Tool-call token format: <|tool_call_start|>...<|tool_call_end|>
@@ -881,11 +896,13 @@ Please provide a helpful response that:
881
  # Flush buffered text
882
  if buffer:
883
  token_count += 1
884
- response_text += buffer
885
- yield json.dumps({
886
- "type": "token",
887
- "data": buffer
888
- }, ensure_ascii=False) + "\n\n"
 
 
889
  buffer = ""
890
 
891
  await asyncio.sleep(0.005)
@@ -893,11 +910,13 @@ Please provide a helpful response that:
893
 
894
  # Streaming normal tokens
895
  token_count += 1
896
- response_text += new_text
897
- yield json.dumps({
898
- "type": "token",
899
- "data": new_text
900
- }, ensure_ascii=False) + "\n\n"
 
 
901
 
902
  # Small delay for smooth streaming
903
  await asyncio.sleep(0.005)
@@ -989,11 +1008,13 @@ Please provide a helpful response that:
989
 
990
  if buffer:
991
  token_count += 1
992
- response_text += buffer
993
- yield json.dumps({
994
- "type": "token",
995
- "data": buffer
996
- }, ensure_ascii=False) + "\n\n"
 
 
997
  buffer = ""
998
 
999
  # Calculate stats
 
630
  TOOLS AVAILABLE:
631
  - web_search(query: str) -> str: Use for fresh facts, current info, or specific lookups.
632
 
633
+ List of tools: <|tool_list_start|>{tools_json_str}<|tool_list_end|>
 
 
 
634
 
635
+ TOOL USE:
636
+ - When you need a tool, respond with a Pythonic tool call wrapped by tokens:
637
  <|tool_call_start|>[web_search(query="...")]<|tool_call_end|>
638
+ - You may include a brief note after the tool call (optional).
639
  - JSON tool calls are also accepted:
640
  {{"tool_call": {{"name": "web_search", "args": {{"query": "..."}}}}}}
641
+ - Do not answer the user’s question until after tool results are available.
642
  - If no tool is needed, respond normally.
643
 
644
  EMOTIONAL CONTEXT:
 
757
  start_time = time.time()
758
  streaming_started = False
759
  tool_call = None
760
+ special_tokens = [
761
+ "<|im_end|>",
762
+ "<|im_start|>",
763
+ "<|endoftext|>",
764
+ "<|startoftext|>",
765
+ "<|tool_call_start|>",
766
+ "<|tool_call_end|>",
767
+ "<|tool_list_start|>",
768
+ "<|tool_list_end|>",
769
+ ]
770
+
771
+ def strip_special_tokens(text: str) -> str:
772
+ cleaned = text
773
+ for tok in special_tokens:
774
+ if tok in cleaned:
775
+ cleaned = cleaned.replace(tok, "")
776
+ return cleaned
777
 
778
  def parse_tool_call(text: str) -> Optional[Dict[str, Any]]:
779
  # 1) Tool-call token format: <|tool_call_start|>...<|tool_call_end|>
 
896
  # Flush buffered text
897
  if buffer:
898
  token_count += 1
899
+ cleaned = strip_special_tokens(buffer)
900
+ if cleaned:
901
+ response_text += cleaned
902
+ yield json.dumps({
903
+ "type": "token",
904
+ "data": cleaned
905
+ }, ensure_ascii=False) + "\n\n"
906
  buffer = ""
907
 
908
  await asyncio.sleep(0.005)
 
910
 
911
  # Streaming normal tokens
912
  token_count += 1
913
+ cleaned = strip_special_tokens(new_text)
914
+ if cleaned:
915
+ response_text += cleaned
916
+ yield json.dumps({
917
+ "type": "token",
918
+ "data": cleaned
919
+ }, ensure_ascii=False) + "\n\n"
920
 
921
  # Small delay for smooth streaming
922
  await asyncio.sleep(0.005)
 
1008
 
1009
  if buffer:
1010
  token_count += 1
1011
+ cleaned = strip_special_tokens(buffer)
1012
+ if cleaned:
1013
+ response_text += cleaned
1014
+ yield json.dumps({
1015
+ "type": "token",
1016
+ "data": cleaned
1017
+ }, ensure_ascii=False) + "\n\n"
1018
  buffer = ""
1019
 
1020
  # Calculate stats