From 9efab84767c71ec0d387d7b7b1e95fdfeaaa62cd Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Thu, 13 Feb 2025 16:47:49 +0100 Subject: [PATCH] Update personality.py --- lollms/personality.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lollms/personality.py b/lollms/personality.py index 536a1af..5e9f48f 100644 --- a/lollms/personality.py +++ b/lollms/personality.py @@ -1327,26 +1327,36 @@ Don't forget encapsulate the code inside a html code tag. This is mandatory. debug=None, callback=callback ) - + def extract_thinking_blocks(self, text: str) -> List[str]: """ - Extracts content between or tags from a given text. - + Extracts content between or tags from a given text. + If a closing tag is present without an opening tag, the content from the start of the text up to the closing tag is extracted. + If both opening and closing tags are present, extracts all content between them. + Parameters: - text (str): The text containing thinking blocks - + text (str): The text containing thinking blocks + Returns: - List[str]: List of extracted thinking contents + List[str]: List of extracted thinking contents """ - import re - - # Pattern to match both and blocks with matching tags + # Pattern to match both thinking and think blocks with matching tags pattern = r'<(thinking|think)>(.*?)' matches = re.finditer(pattern, text, re.DOTALL) - - # Extract content from the second group (index 2) and clean + + # Extract content from the second group and clean thinking_blocks = [match.group(2).strip() for match in matches] - + + # Check for closing tags without opening tags + if not thinking_blocks: + # Look for any closing tag without a matching opening tag + closing_pattern = r'' + closing_match = re.search(closing_pattern, text) + if closing_match: + # Extract content from start up to the closing tag + content = text.split(' str: