mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-06 05:39:12 +00:00
Revert clean_json logic temporarily. Eventually, we should do better validation here to extract markdown-formatted json.
This commit is contained in:
@@ -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):
|
||||||
|
|||||||
Reference in New Issue
Block a user