Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
48df71b
1
Parent(s):
27d17a3
(fix) Add stripThinkBlocks helper for reasoning tag removal
Browse files
src/lib/utils/stripThinkBlocks.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { THINK_BLOCK_REGEX } from "$lib/constants/thinkBlockRegex";
|
| 2 |
+
|
| 3 |
+
const THINK_OPEN_TAG = "<think>";
|
| 4 |
+
const THINK_CLOSE_TAG = "</think>";
|
| 5 |
+
|
| 6 |
+
export function stripThinkBlocks(text: string): string {
|
| 7 |
+
if (!text) return "";
|
| 8 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 9 |
+
const result = text.replace(THINK_BLOCK_REGEX, "");
|
| 10 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 11 |
+
return result;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export function hasUnclosedThinkBlock(text: string): boolean {
|
| 15 |
+
if (!text) return false;
|
| 16 |
+
const lastOpen = text.lastIndexOf(THINK_OPEN_TAG);
|
| 17 |
+
if (lastOpen === -1) return false;
|
| 18 |
+
const nextClose = text.indexOf(THINK_CLOSE_TAG, lastOpen);
|
| 19 |
+
return nextClose === -1;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
export function splitThinkSegments(text: string): string[] {
|
| 23 |
+
const source = text ?? "";
|
| 24 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 25 |
+
const segments = source.split(THINK_BLOCK_REGEX);
|
| 26 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 27 |
+
return segments;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
export function hasThinkSegments(text: string): boolean {
|
| 31 |
+
if (!text) return false;
|
| 32 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 33 |
+
const match = THINK_BLOCK_REGEX.test(text);
|
| 34 |
+
THINK_BLOCK_REGEX.lastIndex = 0;
|
| 35 |
+
return match;
|
| 36 |
+
}
|