Keep only the last segment after bracketed metadata in LLM responses
The model dumps paraphrased context and style labels in [brackets] before its actual roast. Instead of just removing bracket lines (which leaves the preamble text), split on them and keep only the last non-empty segment — the real answer is always last. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
18
cogs/chat.py
18
cogs/chat.py
@@ -205,12 +205,13 @@ class ChatCog(commands.Cog):
|
||||
await typing_ctx.__aexit__(None, None, None)
|
||||
|
||||
# Strip leaked metadata the LLM may echo back.
|
||||
# The LLM paraphrases/reformats injected context in unpredictable ways,
|
||||
# so nuke any [bracketed block] that sits on its own line — real roasts
|
||||
# don't use standalone bracketed text.
|
||||
# The LLM often dumps paraphrased context and style labels in [brackets]
|
||||
# before/between its actual answer. Split on those bracket lines and
|
||||
# keep only the last non-empty segment — the real roast is always last.
|
||||
if response:
|
||||
response = re.sub(r"^\s*\[[^\]]*\]\s*$", "", response, flags=re.MULTILINE)
|
||||
response = re.sub(r"\n{2,}", "\n", response).strip()
|
||||
segments = re.split(r"^\s*\[[^\]]*\]\s*$", response, flags=re.MULTILINE)
|
||||
segments = [s.strip() for s in segments if s.strip()]
|
||||
response = segments[-1] if segments else ""
|
||||
|
||||
if not response:
|
||||
log_channel = discord.utils.get(message.guild.text_channels, name="bcs-log")
|
||||
@@ -317,10 +318,11 @@ class ChatCog(commands.Cog):
|
||||
recent_bot_replies=recent_bot_replies,
|
||||
)
|
||||
|
||||
# Strip leaked metadata (same catch-all as main chat path)
|
||||
# Strip leaked metadata (same approach as main chat path)
|
||||
if response:
|
||||
response = re.sub(r"^\s*\[[^\]]*\]\s*$", "", response, flags=re.MULTILINE)
|
||||
response = re.sub(r"\n{2,}", "\n", response).strip()
|
||||
segments = re.split(r"^\s*\[[^\]]*\]\s*$", response, flags=re.MULTILINE)
|
||||
segments = [s.strip() for s in segments if s.strip()]
|
||||
response = segments[-1] if segments else ""
|
||||
|
||||
if not response:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user