Retry truncated Ollama character analysis

This commit is contained in:
OpenClaw Agent
2026-07-10 11:23:28 +00:00
parent 2bd748c8a6
commit 6ec28d33a4

View File

@@ -466,6 +466,17 @@ def _extract_ollama_generated_text(resp_json: dict) -> str:
return "" return ""
def _analysis_text_is_usable(text: str) -> bool:
text = (text or "").strip()
if not text:
return False
if len(text) < 24:
return False
if len(text.split()) < 6:
return False
return True
def _compress_analysis_image_b64(b64_payload: str, max_dim: int = 768, quality: int = 82) -> str: def _compress_analysis_image_b64(b64_payload: str, max_dim: int = 768, quality: int = 82) -> str:
"""Shrink analysis images so multimodal providers do not burn their full context on pixels.""" """Shrink analysis images so multimodal providers do not burn their full context on pixels."""
try: try:
@@ -540,6 +551,10 @@ async def analyze_character_endpoint(request):
"images": analysis_images, "images": analysis_images,
"stream": False, "stream": False,
"keep_alive": 0, "keep_alive": 0,
"options": {
"temperature": 0.2,
"num_predict": 120,
},
} }
async with session.post(f"{base_url}/api/generate", json=payload, timeout=300) as response: async with session.post(f"{base_url}/api/generate", json=payload, timeout=300) as response:
if response.status != 200: if response.status != 200:
@@ -558,6 +573,28 @@ async def analyze_character_endpoint(request):
else: else:
resp_json = await response.json() resp_json = await response.json()
generated_text = _extract_ollama_generated_text(resp_json) generated_text = _extract_ollama_generated_text(resp_json)
if not _analysis_text_is_usable(generated_text):
chat_payload = {
"model": model_name,
"messages": [{
"role": "user",
"content": _ANALYZE_PROMPT,
"images": analysis_images,
}],
"stream": False,
"keep_alive": 0,
"options": {
"temperature": 0.2,
"num_predict": 120,
},
}
async with session.post(f"{base_url}/api/chat", json=chat_payload, timeout=300) as response:
if response.status == 200:
resp_json = await response.json()
chat_text = _extract_ollama_generated_text(resp_json)
if _analysis_text_is_usable(chat_text):
generated_text = chat_text
else: else:
# OpenAI-compatible vision chat (LM Studio / Custom). # OpenAI-compatible vision chat (LM Studio / Custom).
content = [{"type": "text", "text": _ANALYZE_PROMPT}] content = [{"type": "text", "text": _ANALYZE_PROMPT}]