Merge pull request #4504 from janhq/fix/thread-title-summarization-constraints

fix: thread title summarization doesn't work well on reasoning models
This commit is contained in:
Louis 2025-01-22 15:27:20 +07:00 committed by GitHub
commit a3b242c4b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -132,7 +132,7 @@ export default function ModelHandler() {
return return
} }
const messageContent = message.content[0]?.text?.value let messageContent = message.content[0]?.text?.value
if (!messageContent) { if (!messageContent) {
console.warn( console.warn(
`Failed to update title for thread ${message.thread_id}: Responded content is null!` `Failed to update title for thread ${message.thread_id}: Responded content is null!`
@ -144,27 +144,17 @@ export default function ModelHandler() {
// And no new line character is present // And no new line character is present
// And non-alphanumeric characters should be removed // And non-alphanumeric characters should be removed
if (messageContent.includes('\n')) { if (messageContent.includes('\n')) {
console.warn( messageContent = messageContent.replace(/\n/g, ' ')
`Failed to update title for thread ${message.thread_id}: Title can't contain new line character!` }
) const match = messageContent.match(/<\/think>(.*)$/)
return if (match) {
messageContent = match[1]
} }
// Remove non-alphanumeric characters // Remove non-alphanumeric characters
const cleanedMessageContent = messageContent const cleanedMessageContent = messageContent
.replace(/[^\p{L}\s]+/gu, '') .replace(/[^\p{L}\s]+/gu, '')
.trim() .trim()
// Split the message into words
const words = cleanedMessageContent.split(' ')
if (words.length >= maxWordForThreadTitle) {
console.warn(
`Failed to update title for thread ${message.thread_id}: Title can't be greater than ${maxWordForThreadTitle} words!`
)
return
}
// Do not persist empty message // Do not persist empty message
if (!cleanedMessageContent.trim().length) return if (!cleanedMessageContent.trim().length) return
@ -361,7 +351,7 @@ export default function ModelHandler() {
if (!threadMessages || threadMessages.length === 0) return if (!threadMessages || threadMessages.length === 0) return
const summarizeFirstPrompt = `Summarize in a ${maxWordForThreadTitle}-word Title. Give the title only. "${threadMessages[0]?.content[0]?.text?.value}"` const summarizeFirstPrompt = `Summarize in a ${maxWordForThreadTitle}-word Title. Give the title only. Here is the message: "${threadMessages[0]?.content[0]?.text?.value}"`
// Prompt: Given this query from user {query}, return to me the summary in 10 words as the title // Prompt: Given this query from user {query}, return to me the summary in 10 words as the title
const msgId = ulid() const msgId = ulid()