Skip to content

Commit

Permalink
Revert clean_json logic temporarily. Eventually, we should do better …
Browse files Browse the repository at this point in the history
…validation here to extract markdown-formatted json.
  • Loading branch information
sabaimran committed Nov 12, 2024
1 parent 1cab6c0 commit d68243a
Showing 1 changed file with 1 addition and 19 deletions.
20 changes: 1 addition & 19 deletions src/khoj/processor/conversation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,25 +527,7 @@ def reciprocal_conversation_to_chatml(message_pair):

def clean_json(response: str):
"""Remove any markdown json codeblock and newline formatting if present. Useful for non schema enforceable models"""
try:
# 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 ""
return response.strip().replace("\n", "").removeprefix("```json").removesuffix("```")


def clean_code_python(code: str):
Expand Down

0 comments on commit d68243a

Please sign in to comment.