Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/services/llm.py +19 -3
app/services/llm.py
CHANGED
|
@@ -4,6 +4,22 @@ from app.config import get_settings
|
|
| 4 |
from app.models.question import Answer
|
| 5 |
from app.models.checklist import ChecklistItem
|
| 6 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class LLMService:
|
|
@@ -42,7 +58,7 @@ class LLMService:
|
|
| 42 |
]
|
| 43 |
)
|
| 44 |
|
| 45 |
-
result =
|
| 46 |
return result["questions"]
|
| 47 |
|
| 48 |
def analyze_round_and_generate_questions(
|
|
@@ -99,7 +115,7 @@ class LLMService:
|
|
| 99 |
]
|
| 100 |
)
|
| 101 |
|
| 102 |
-
return
|
| 103 |
|
| 104 |
def generate_checklist(
|
| 105 |
self,
|
|
@@ -162,7 +178,7 @@ class LLMService:
|
|
| 162 |
]
|
| 163 |
)
|
| 164 |
|
| 165 |
-
return
|
| 166 |
|
| 167 |
|
| 168 |
def get_llm_service() -> LLMService:
|
|
|
|
| 4 |
from app.models.question import Answer
|
| 5 |
from app.models.checklist import ChecklistItem
|
| 6 |
import json
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def extract_json(text: str) -> dict:
|
| 11 |
+
"""Извлекает JSON из текста, даже если он обернут в markdown code blocks"""
|
| 12 |
+
# Пробуем найти JSON в code blocks
|
| 13 |
+
json_match = re.search(r'```(?:json)?\s*([\s\S]*?)```', text)
|
| 14 |
+
if json_match:
|
| 15 |
+
text = json_match.group(1).strip()
|
| 16 |
+
|
| 17 |
+
# Пробуем найти JSON объект напрямую
|
| 18 |
+
json_match = re.search(r'\{[\s\S]*\}', text)
|
| 19 |
+
if json_match:
|
| 20 |
+
text = json_match.group(0)
|
| 21 |
+
|
| 22 |
+
return json.loads(text)
|
| 23 |
|
| 24 |
|
| 25 |
class LLMService:
|
|
|
|
| 58 |
]
|
| 59 |
)
|
| 60 |
|
| 61 |
+
result = extract_json(response.content[0].text)
|
| 62 |
return result["questions"]
|
| 63 |
|
| 64 |
def analyze_round_and_generate_questions(
|
|
|
|
| 115 |
]
|
| 116 |
)
|
| 117 |
|
| 118 |
+
return extract_json(response.content[0].text)
|
| 119 |
|
| 120 |
def generate_checklist(
|
| 121 |
self,
|
|
|
|
| 178 |
]
|
| 179 |
)
|
| 180 |
|
| 181 |
+
return extract_json(response.content[0].text)
|
| 182 |
|
| 183 |
|
| 184 |
def get_llm_service() -> LLMService:
|