tests: rework tests and fixes bugs along the way

This commit is contained in:
Mathieu Virbel
2023-08-01 16:05:48 +02:00
parent bc55cfdea3
commit 1f8e4200fd
9 changed files with 126 additions and 54 deletions

View File

@@ -2,6 +2,7 @@ from reflector.logger import logger
from reflector.settings import settings
import asyncio
import json
import re
class LLM:
@@ -48,11 +49,15 @@ class LLM:
def _parse_json(self, result: str) -> dict:
result = result.strip()
# try detecting code block if exist
if result.startswith("```json\n") and result.endswith("```"):
result = result[8:-3]
elif result.startswith("```\n") and result.endswith("```"):
result = result[4:-3]
print(">>>", result)
# starts with ```json\n, ends with ```
# or starts with ```\n, ends with ```
# or starts with \n```javascript\n, ends with ```
regex = r"```(json|javascript|)?(.*)```"
matches = re.findall(regex, result.strip(), re.MULTILINE | re.DOTALL)
if not matches:
return result
# we have a match, try to parse it
result = matches[0][1]
return json.loads(result.strip())