-
-
Notifications
You must be signed in to change notification settings - Fork 755
Expand file tree
/
Copy pathreasoning.ts
More file actions
114 lines (100 loc) · 3.13 KB
/
Copy pathreasoning.ts
File metadata and controls
114 lines (100 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const tags = ["think", "reason", "reasoning", "thought"]
export function parseReasoning(text: string): {
type: "reasoning" | "text"
content: string
reasoning_running?: boolean
}[] {
try {
const result: {
type: "reasoning" | "text"
content: string
reasoning_running?: boolean
}[] = []
const tagPattern = new RegExp(`<(${tags.join("|")})>`, "i")
const closeTagPattern = new RegExp(`</(${tags.join("|")})>`, "i")
let currentIndex = 0
let isReasoning = false
while (currentIndex < text.length) {
const openTagMatch = text.slice(currentIndex).match(tagPattern)
const closeTagMatch = text.slice(currentIndex).match(closeTagPattern)
if (!isReasoning && openTagMatch) {
const beforeText = text.slice(
currentIndex,
currentIndex + openTagMatch.index
)
if (beforeText.trim()) {
result.push({ type: "text", content: beforeText.trim() })
}
isReasoning = true
currentIndex += openTagMatch.index! + openTagMatch[0].length
continue
}
if (isReasoning && closeTagMatch) {
const reasoningContent = text.slice(
currentIndex,
currentIndex + closeTagMatch.index
)
if (reasoningContent.trim()) {
result.push({ type: "reasoning", content: reasoningContent.trim() })
}
isReasoning = false
currentIndex += closeTagMatch.index! + closeTagMatch[0].length
continue
}
if (currentIndex < text.length) {
const remainingText = text.slice(currentIndex)
result.push({
type: isReasoning ? "reasoning" : "text",
content: remainingText.trim(),
reasoning_running: isReasoning
})
break
}
}
return result
} catch (e) {
console.error(`Error parsing reasoning: ${e}`)
return [
{
type: "text",
content: text
}
]
}
}
export function isReasoningStarted(text: string): boolean {
const tagPattern = new RegExp(`<(${tags.join("|")})>`, "i")
return tagPattern.test(text)
}
export function isReasoningEnded(text: string): boolean {
const closeTagPattern = new RegExp(`</(${tags.join("|")})>`, "i")
return closeTagPattern.test(text)
}
export function removeReasoning(text: string): string {
const tagPattern = new RegExp(
`<(${tags.join("|")})>.*?</(${tags.join("|")})>`,
"gis"
)
return text.replace(tagPattern, "").trim()
}
export function mergeReasoningContent(
originalText: string,
reasoning: string
): string {
const reasoningTag = "<think>"
originalText = originalText.replace(reasoningTag, "")
return `${reasoningTag}${originalText + reasoning}`
}
export function replaceThinkTagToEM(text: string): string {
const tagPattern = new RegExp(
`<(${tags.join("|")})>.*?</(${tags.join("|")})>`,
"gis"
)
const emStyle = "font-style: italic; font-size: 0.9em; margin-bottom: 1em;"
return text
.replace(tagPattern, (match) => {
return `<em style="${emStyle}">${match.replace(/<(\/)?(${tags.join("|")})>/gi, "")}</em>\n\n`
})
.replaceAll("<think>", "")
.replaceAll("</think>", "")
}