Skip to content

Commit 898f1e2

Browse files
LostRuinsNexesenex
authored andcommitted
fix trim_stop failing on some edge cases
1 parent 737ea6e commit 898f1e2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

koboldcpp.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,23 @@ def bring_terminal_to_foreground():
591591
ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 9)
592592
ctypes.windll.user32.SetForegroundWindow(ctypes.windll.kernel32.GetConsoleWindow())
593593

594-
def string_contains_sequence_substring(inputstr,sequences):
594+
def string_has_overlap(str_a, str_b, maxcheck):
595+
max_overlap = min(maxcheck, len(str_a), len(str_b))
596+
for i in range(1, max_overlap + 1):
597+
if str_a[-i:] == str_b[:i]:
598+
return True
599+
return False
600+
601+
def string_contains_or_overlaps_sequence_substring(inputstr, sequences):
595602
if inputstr.strip()=="":
596603
return False
597604
for s in sequences:
598605
if s.strip()=="":
599606
continue
600607
if s.strip() in inputstr.strip() or inputstr.strip() in s.strip():
601608
return True
609+
if string_has_overlap(inputstr, s, 10):
610+
return True
602611
return False
603612

604613
import struct
@@ -1810,7 +1819,7 @@ async def handle_sse_stream(self, genparams, api_format):
18101819
if tokenStr!="" or streamDone:
18111820
sseq = genparams.get('stop_sequence', [])
18121821
trimstop = genparams.get('trim_stop', False)
1813-
if trimstop and not streamDone and string_contains_sequence_substring(tokenStr,sseq):
1822+
if trimstop and not streamDone and string_contains_or_overlaps_sequence_substring(tokenStr,sseq):
18141823
tokenReserve += tokenStr
18151824
await asyncio.sleep(async_sleep_short) #if a stop sequence could trigger soon, do not send output
18161825
else:

0 commit comments

Comments
 (0)