Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

Hide Gemma models reasoning tags (#7948)

Kirsa Denisdef32f8ab45e
diff --git a/web-app/src/lib/__tests__/messages.test.ts b/web-app/src/lib/__tests__/messages.test.ts
index 80665d577..3c9d23629 100644
--- a/web-app/src/lib/__tests__/messages.test.ts
+++ b/web-app/src/lib/__tests__/messages.test.ts
@@ -24,12 +24,32 @@ describe('parseReasoning', () => {
expect(result).toEqual({ reasoningSegment: '<think>thinking...', textSegment: '' })
})
+ it('detects in-progress <thought> tag (no closing)', () => {
+ const result = parseReasoning('<thought>thinking...')
+ expect(result).toEqual({ reasoningSegment: '<thought>thinking...', textSegment: '' })
+ })
+
it('detects completed <think> tag', () => {
const result = parseReasoning('<think>reason</think>answer')
expect(result.reasoningSegment).toBe('<think>reason</think>')
expect(result.textSegment).toBe('answer')
})
+ it('detects completed <thought> tag', () => {
+ const result = parseReasoning('<thought>reason</thought>answer')
+ expect(result.reasoningSegment).toBe('<thought>reason</thought>')
+ expect(result.textSegment).toBe('answer')
+ })
+
+ it('handles text before closed reasoning tags', () => {
+ const text = 'Some prefix <think>Internal thought</think>Some suffix'
+ const result = parseReasoning(text)
+ expect(result).toEqual({
+ reasoningSegment: 'Some prefix <think>Internal thought</think>',
+ textSegment: 'Some suffix',
+ })
+ })
+
it('detects in-progress analysis channel', () => {
const text = '<|channel|>analysis<|message|>analyzing...'
const result = parseReasoning(text)
@@ -43,6 +63,15 @@ describe('parseReasoning', () => {
expect(result.reasoningSegment).toContain('<|channel|>analysis')
expect(result.textSegment).toBe('reply')
})
+
+ it('handles multiple tags by splitting at the first closed tag', () => {
+ const text = '<think>First</think><think>Second</think>Answer'
+ const result = parseReasoning(text)
+ expect(result).toEqual({
+ reasoningSegment: '<think>First</think>',
+ textSegment: '<think>Second</think>Answer',
+ })
+ })
})
// ---------------------------------------------------------------------------
@@ -511,6 +540,7 @@ describe('extractContentPartsFromUIMessage', () => {
type: 'tool-search',
toolCallId: 'tc-1',
input: { q: 'test' },
+ state: 'output-available',
output: 'result',
},
],
@@ -553,4 +583,4 @@ describe('extractContentPartsFromUIMessage', () => {
expect(content).toHaveLength(1)
expect(content[0].text!.value).toBe('actual')
})
-})
+})
\ No newline at end of file
diff --git a/web-app/src/lib/messages.ts b/web-app/src/lib/messages.ts
index 6662e2619..1376734ca 100644
--- a/web-app/src/lib/messages.ts
+++ b/web-app/src/lib/messages.ts
@@ -135,7 +135,9 @@ export type ToolResult = {
*/
export const parseReasoning = (text: string) => {
// Check for thinking formats
- const hasThinkTag = text.includes('<think>') && !text.includes('</think>')
+ const hasThinkTag =
+ (text.includes('<think>') && !text.includes('</think>')) ||
+ (text.includes('<thought>') && !text.includes('</thought>'))
const hasAnalysisChannel =
text.includes('<|channel|>analysis<|message|>') &&
!text.includes('<|start|>assistant<|channel|>final<|message|>')
@@ -144,7 +146,7 @@ export const parseReasoning = (text: string) => {
return { reasoningSegment: text, textSegment: '' }
// Check for completed think tag format
- const thinkMatch = text.match(/<think>([\s\S]*?)<\/think>/)
+ const thinkMatch = text.match(/<(think|thought)>([\s\S]*?)<\/\1>/)
if (thinkMatch?.index !== undefined) {
const splitIndex = thinkMatch.index + thinkMatch[0].length
return {
@@ -194,22 +196,22 @@ export function convertThreadMessageToUIMessage(
// BACKWARD COMPATIBILITY: Handle old format with <think> tags
if (reasoningSegment) {
- // Extract reasoning text from <think> tags
+ // Extract reasoning text from <think> or <thought> tags
const completedMatch = reasoningSegment.match(
- /<think>([\s\S]*)<\/think>/
+ /<(think|thought)>([\s\S]*)<\/\1>/
)
if (completedMatch) {
parts.push({
type: 'reasoning',
- text: completedMatch[1],
+ text: completedMatch[2],
})
} else {
- // In-progress reasoning - extract content after <think> tag
- const inProgressMatch = reasoningSegment.match(/<think>([\s\S]*)/)
+ // In-progress reasoning - extract content after <think> or <thought> tag
+ const inProgressMatch = reasoningSegment.match(/<(think|thought)>([\s\S]*)/)
if (inProgressMatch) {
parts.push({
type: 'reasoning',
- text: inProgressMatch[1],
+ text: inProgressMatch[2],
})
}
}
diff --git a/web-app/src/lib/model-factory.ts b/web-app/src/lib/model-factory.ts
index 33eed4c4d..79ed512f9 100644
--- a/web-app/src/lib/model-factory.ts
+++ b/web-app/src/lib/model-factory.ts
@@ -301,6 +301,33 @@ function createFoundationModelsFetch(
}
}
+/**
+ * Map of model keywords to their respective reasoning tags.
+ * Used for models that use tags other than the default 'think'.
+ */
+const REASONING_TAG_MAP: Record<string, string> = {
+ gemma: 'thought',
+}
+
+/**
+ * The default tag used for reasoning extraction if no specific override is found.
+ */
+const DEFAULT_REASONING_TAG = 'think'
+
+/**
+ * Determines the reasoning tag name based on the model ID.
+ * Defaults to 'think' if no specific override is found in the map.
+ */
+function getReasoningTagName(modelId: string): string {
+ const lowerId = modelId.toLowerCase()
+ for (const [keyword, tag] of Object.entries(REASONING_TAG_MAP)) {
+ if (lowerId.includes(keyword)) {
+ return tag
+ }
+ }
+ return DEFAULT_REASONING_TAG
+}
+
/**
* Factory for creating language models based on provider type.
* Supports native AI SDK providers (Anthropic, Google) and OpenAI-compatible providers.
@@ -409,7 +436,7 @@ export class ModelFactory {
return wrapLanguageModel({
model,
middleware: extractReasoningMiddleware({
- tagName: 'think',
+ tagName: getReasoningTagName(modelId),
separator: '\n',
}),
})
@@ -499,7 +526,7 @@ export class ModelFactory {
return wrapLanguageModel({
model: model,
middleware: extractReasoningMiddleware({
- tagName: 'think',
+ tagName: getReasoningTagName(modelId),
separator: '\n',
}),
})
@@ -573,7 +600,7 @@ export class ModelFactory {
return wrapLanguageModel({
model,
middleware: extractReasoningMiddleware({
- tagName: 'think',
+ tagName: getReasoningTagName(modelId),
separator: '\n',
}),
})
@@ -771,6 +798,14 @@ export class ModelFactory {
fetch: fetchImpl,
})
- return openAICompatible.languageModel(modelId)
+ const model = openAICompatible.languageModel(modelId)
+
+ return wrapLanguageModel({
+ model,
+ middleware: extractReasoningMiddleware({
+ tagName: getReasoningTagName(modelId),
+ separator: '\n',
+ }),
+ })
}
}