def get_content_between_a_b(start_tag, end_tag, text): extracted_text = "" start_index = text.find(start_tag) while start_index != -1: end_index = text.find(end_tag, start_index + len(start_tag)) if end_index != -1: extracted_text += text[start_index + len(start_tag) : end_index] + " " start_index = text.find(start_tag, end_index + len(end_tag)) else: break return extracted_text.strip() def extract(text, type,hard = True): if text: target_str = get_content_between_a_b(f"<{type}>", f"", text) if target_str: return target_str elif hard: return text else: return "" else: return "" def extract_json(text): if "```json" in text: target_str = get_content_between_a_b("```json", "```", text) return target_str else: return text