Revert clean_json logic temporarily. Eventually, we should do better validation here to extract markdown-formatted json.

This commit is contained in:
sabaimran 2024-11-11 21:05:17 -08:00
parent 1cab6c081f
commit d68243a3fb

View file

@ -527,25 +527,7 @@ def reciprocal_conversation_to_chatml(message_pair):
def clean_json(response: str): def clean_json(response: str):
"""Remove any markdown json codeblock and newline formatting if present. Useful for non schema enforceable models""" """Remove any markdown json codeblock and newline formatting if present. Useful for non schema enforceable models"""
try: return response.strip().replace("\n", "").removeprefix("```json").removesuffix("```")
# Remove markdown code blocks
cleaned = response.strip().replace("```json", "").replace("```", "")
# Find JSON array/object pattern
json_match = re.search(r"\[.*\]|\{.*\}", cleaned, re.DOTALL)
if not json_match:
return ""
# Extract matched JSON
json_str = json_match.group()
# Validate by parsing
json.loads(json_str)
return json_str.strip()
except (json.JSONDecodeError, AttributeError):
return ""
def clean_code_python(code: str): def clean_code_python(code: str):