Repository navigation
feat: improve Unsloth Studio chat title generation quality - #6697
danielhanchen merged 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the chat title generation logic by incorporating the assistant's first response as context, updating the model prompt to focus on summarizing the conversation topic, and adding a fail-safe check to catch echoed labels. It also introduces a Python regression test suite to verify these changes. The review feedback highlights opportunities to expand the fail-safe regex to eliminate dead code, optimize message retrieval by avoiding array slicing, and improve the robustness of the brace-counting helper in the test suite.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if (/^\s*(user|assistant)\s*:/i.test(title)) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
The new check /^\s*(user|assistant)\s*:/i successfully catches echoed labels before punctuation stripping. However, the subsequent check /\b(user|base|lora|assistant)\s*:/i.test(title) (on line 457) is now dead code because the preceding title.replace(/[.!?:;,]+/g, " ") replaces all colons with spaces, meaning a colon will never be matched there.
To handle all potential echoed labels (including base and lora) and clean up the dead code, we should expand this regex to include them, and then the redundant post-punctuation check can be safely removed.
| if (/^\s*(user|assistant)\s*:/i.test(title)) { | |
| return null; | |
| } | |
| if (/^\s*(user|assistant|base|lora)\s*:/i.test(title)) { | |
| return null; | |
| } |
| const firstAssistant = | ||
| firstUserIndex === -1 | ||
| ? undefined | ||
| : messages | ||
| .slice(firstUserIndex + 1) | ||
| .find((m) => m.role === "assistant"); |
There was a problem hiding this comment.
Instead of slicing the messages array to find the first assistant message after the user message, you can use the index argument of the find callback. This avoids allocating a new shallow copy of the array on every title generation run.
const firstAssistant =
firstUserIndex === -1
? undefined
: messages.find((m, i) => m.role === "assistant" && i > firstUserIndex);
| def _balanced_block(src: str, anchor: str) -> str: | ||
| start = src.find(anchor) | ||
| assert start != -1, f"anchor {anchor!r} not found" | ||
| body_start = src.find("{", start) | ||
| assert body_start != -1, f"body opener after {anchor!r} not found" | ||
|
|
||
| depth = 0 | ||
| for index in range(body_start, len(src)): | ||
| char = src[index] | ||
| if char == "{": | ||
| depth += 1 | ||
| elif char == "}": | ||
| depth -= 1 | ||
| if depth == 0: | ||
| return src[start : index + 1] | ||
| raise AssertionError(f"unbalanced block after {anchor!r}") |
There was a problem hiding this comment.
The _balanced_block helper relies on a simple brace-counting parser. This is highly fragile and will fail if the TSX file contains braces inside string literals, template literals (e.g., ${variable}), comments, or regular expressions (e.g., /[a-z]{1,3}/).
While a full AST parser in Python is complex, you should at least document this limitation or use a more robust regex-based block extractor to prevent future formatting or syntax changes from unexpectedly breaking the test suite.
Consolidate the echo guard into a single leading-label check (now also covering base and lora) and drop the post-punctuation duplicate that could never match a colon once punctuation is stripped. Swap the slice-based first-assistant lookup for an indexed find to avoid copying the messages array, and note the brace counter's assumptions in the test helper.
|
Pushed a small follow-up addressing the review notes:
Typecheck ( |
|
Thanks! |
Summary
Auto-generated chat titles in Unsloth Studio were derived from the first user message alone, with a prompt that asked the model to title "the user's message." For conversational threads that produced titles echoing the user's exact phrasing rather than naming what the conversation was actually about. This improves the quality of those titles in two ways: the title model now receives the first assistant reply as additional context, and the system prompt asks it to summarize the conversation topic rather than the user's wording.
Why this matters
A chat title is the only thing distinguishing threads in the sidebar, so a title that parrots the opening message ("how do i reset") is much less useful than one that names the topic ("Password reset flow"). Issue #6322 asks for better title generation; feeding the assistant's response gives the model the context it needs to summarize the exchange instead of restating the prompt.
Changes
The title-generation path now locates the first user message and the first assistant reply that follows it, clips both, and passes them to the model under explicit
User:/Assistant:labels. Because labeling the payload makes it possible for the model to echo aUser:/Assistant:prefix back into the title, the existing echo fail-safe innormalizeTitlenow runs against the raw title before punctuation is stripped (the punctuation pass would otherwise erase the:that the guard keys on), so an echoed label falls back to the deterministic user-text title instead of being persisted.Testing
Added
tests/studio/test_chat_title_generation.py, a source-level regression suite (the Studio frontend has no JS test harness) covering: the prompt targeting the conversation topic, the payload carrying the optional assistant reply, the adapter selecting the first assistant reply after the first user message, the auto-title-disabled fallback, the model-failure fallback, and the normalizer's output constraints including the role-label guard. All 6 pass under pytest;ruff checkon the new file is clean.Fixes #6322