Neural Mastery
← Practice
Streaming Stop Sequence Cuttereasy

Streaming Stop Sequence Cutter

easy Agents in Production 15–20 min

NumPy allowed · plain Python earns 10 bonus points

LLM step 1/4

Logits become a next-token distribution

Softmax turns a model score vector into probabilities.
Open lesson →
01prompt
02tokens
03logits
04probabilities
attention→next token
mechanism65%
layer20%
pattern10%
model5%

Softmax(logits) → probability distribution

Enforce generation termination boundaries by cutting completed or streamed text at the earliest occurrence of stop tokens.

Task

Implement apply_stop_sequences(text, stop_sequences). Find the earliest index in text where any non-empty sequence from stop_sequences appears. If found, truncate text immediately before the sequence, set stopped: True, and record matched_sequence. If none found or stop_sequences is empty, return original text with stopped: False and matched_sequence: None.

Function Signature

apply_stop_sequences(text: str, stop_sequences: list[str]) -> dict[str, str | bool]

Examples

Example 1: Single stop sequence matched
Input: text = 'Step 1: Done.\nUser: What next?' stop_sequences = ['\nUser:']
Output: {'truncated_text': 'Step 1: Done.', 'stopped': True, 'matched_sequence': '\nUser:'}
Example 2: Multiple sequences earliest wins
Input: text = 'Alpha Bravo Charlie Delta' stop_sequences = ['Charlie', 'Bravo']
Output: {'truncated_text': 'Alpha ', 'stopped': True, 'matched_sequence': 'Bravo'}
Example 3: No stop sequence matched
Input: text = 'Hello world without stops.' stop_sequences = ['STOP', 'END']
Output: {'truncated_text': 'Hello world without stops.', 'stopped': False, 'matched_sequence': None}
Example 4: Empty stop sequences list
Input: text = 'Raw text unchanged.' stop_sequences = []
Output: {'truncated_text': 'Raw text unchanged.', 'stopped': False, 'matched_sequence': None}

Constraints

  • Truncate text immediately preceding the stop sequence.
  • Pick the earliest occurring sequence if multiple match.
  • A plain-Python solution earns 10 bonus points.
Python3Saved ✓
def apply_stop_sequences(text: str, stop_sequences: list[str]) -> dict[str, str | bool]:
"""Truncate generated text at the earliest occurrence of any stop sequence.
Args:
text: Generated output text.
stop_sequences: List of delimiter strings that trigger stopping.
Returns:
Dict with 'truncated_text', 'stopped' (bool), and 'matched_sequence'.
"""
# Your implementation here
pass

Case 1: Single stop sequence matched
Input: text = 'Step 1: Done.\nUser: What next?' stop_sequences = ['\nUser:']
Expected: {'truncated_text': 'Step 1: Done.', 'stopped': True, 'matched_sequence': '\nUser:'}
Case 2: Multiple sequences earliest wins
Input: text = 'Alpha Bravo Charlie Delta' stop_sequences = ['Charlie', 'Bravo']
Expected: {'truncated_text': 'Alpha ', 'stopped': True, 'matched_sequence': 'Bravo'}
Case 3: No stop sequence matched
Input: text = 'Hello world without stops.' stop_sequences = ['STOP', 'END']
Expected: {'truncated_text': 'Hello world without stops.', 'stopped': False, 'matched_sequence': None}
Case 4: Empty stop sequences list
Input: text = 'Raw text unchanged.' stop_sequences = []
Expected: {'truncated_text': 'Raw text unchanged.', 'stopped': False, 'matched_sequence': None}