Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/services/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,11 @@ def format_response(response):
response = response.replace("*", "")
response = response.replace("#", "")

# Remove markdown syntax
response = re.sub(r"\[.*\]", "", response)
response = re.sub(r"\(.*\)", "", response)
# Remove markdown syntax. Use non-greedy .*? so each bracket/paren
# group is removed independently; the greedy form would eat all text
# between the first opener and the last closer on the same line.
response = re.sub(r"\[.*?\]", "", response)
response = re.sub(r"\(.*?\)", "", response)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove nested parenthetical groups completely

When the LLM emits a parenthetical group that itself contains parentheses, such as a Markdown URL like [source](https://example.com/Foo_(bar)), this non-greedy pattern stops at the inner ) and leaves a dangling ) in the returned script. The previous greedy pattern removed that whole group, so this change regresses those nested-parenthesis cases while still feeding the sanitized script to downstream narration/subtitle generation.

Useful? React with 👍 / 👎.


# Split the script into paragraphs
paragraphs = response.split("\n\n")
Expand Down
35 changes: 35 additions & 0 deletions test/services/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ def fake_generate_response(prompt, app_config=None):
self.assertIs(captured["app_config"], app_config)
self.assertEqual(captured["app_config"]["openai_api_key"], "snapshot-key")

def test_generate_script_strips_each_bracket_group_independently(self):
"""
format_response must remove each [bracket] and (paren) group in
isolation. The greedy form [.*] matches from the first opener to
the *last* closer on the line, silently deleting all text in between.

Example – greedy bug:
"[Intro] Great content [end]" → "." (all inner text lost)
Expected with non-greedy fix:
"[Intro] Great content [end]" → " Great content "
"""

def fake_generate_response(prompt):
# Two bracket groups and two paren groups on the same line.
return (
"[Scene: Beach] A beautiful day at the [location: ocean].\n\n"
"Save (at least) 10% of your income (monthly)."
)

with patch.object(
llm, "_generate_response", side_effect=fake_generate_response
):
result = llm.generate_script(
video_subject="savings tips", language="en-US"
)

# Each bracket / paren group should be gone, but the surrounding words
# must survive.
self.assertNotIn("[", result)
self.assertNotIn("]", result)
self.assertNotIn("(", result)
self.assertNotIn(")", result)
self.assertIn("A beautiful day at the", result)
self.assertIn("10% of your income", result)

def test_generate_terms_can_request_script_ordered_keywords(self):
"""
按文案顺序匹配素材依赖 LLM 返回有序关键词。这里不调用真实模型,
Expand Down
Loading