Spaces:
Sleeping
Sleeping
fix: history multimodal/list content'i güvenle string'e çevir
Browse files- app/retrieval.py +28 -3
app/retrieval.py
CHANGED
|
@@ -154,13 +154,38 @@ def build_history_block(history: list, max_turns: int = MAX_HISTORY_TURNS) -> st
|
|
| 154 |
return "\n".join(lines)
|
| 155 |
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
def _extract_turn(turn) -> tuple[str, str]:
|
| 158 |
-
"""Hem (user, assistant) tuple hem de {'role','content'} dict pair formatını destekler.
|
|
|
|
| 159 |
if isinstance(turn, (list, tuple)) and len(turn) == 2:
|
| 160 |
-
return (turn[0]
|
| 161 |
if isinstance(turn, dict):
|
| 162 |
role = turn.get("role", "")
|
| 163 |
-
content = turn.get("content", "")
|
| 164 |
if role == "user":
|
| 165 |
return content, ""
|
| 166 |
if role == "assistant":
|
|
|
|
| 154 |
return "\n".join(lines)
|
| 155 |
|
| 156 |
|
| 157 |
+
def _coerce_text(value) -> str:
|
| 158 |
+
"""history içinden gelen mesaj içeriğini string'e çevirir.
|
| 159 |
+
Gradio multimodal/messages formatında content; string, None, liste
|
| 160 |
+
(text bloğu / dosya tuple'ı) veya dict olabilir."""
|
| 161 |
+
if value is None:
|
| 162 |
+
return ""
|
| 163 |
+
if isinstance(value, str):
|
| 164 |
+
return value
|
| 165 |
+
if isinstance(value, (list, tuple)):
|
| 166 |
+
parts = []
|
| 167 |
+
for item in value:
|
| 168 |
+
if isinstance(item, str):
|
| 169 |
+
parts.append(item)
|
| 170 |
+
elif isinstance(item, dict):
|
| 171 |
+
t = item.get("text") or item.get("content") or ""
|
| 172 |
+
if isinstance(t, str):
|
| 173 |
+
parts.append(t)
|
| 174 |
+
return " ".join(p for p in parts if p)
|
| 175 |
+
if isinstance(value, dict):
|
| 176 |
+
t = value.get("text") or value.get("content") or ""
|
| 177 |
+
return t if isinstance(t, str) else ""
|
| 178 |
+
return str(value)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
def _extract_turn(turn) -> tuple[str, str]:
|
| 182 |
+
"""Hem (user, assistant) tuple hem de {'role','content'} dict pair formatını destekler.
|
| 183 |
+
Her iki tarafı _coerce_text'ten geçirerek liste/None/multimodal içeriği güvenle string'e indirger."""
|
| 184 |
if isinstance(turn, (list, tuple)) and len(turn) == 2:
|
| 185 |
+
return _coerce_text(turn[0]), _coerce_text(turn[1])
|
| 186 |
if isinstance(turn, dict):
|
| 187 |
role = turn.get("role", "")
|
| 188 |
+
content = _coerce_text(turn.get("content", ""))
|
| 189 |
if role == "user":
|
| 190 |
return content, ""
|
| 191 |
if role == "assistant":
|