Retry truncated Ollama character analysis
This commit is contained in:
@@ -466,6 +466,17 @@ def _extract_ollama_generated_text(resp_json: dict) -> str:
|
||||
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:
|
||||
"""Shrink analysis images so multimodal providers do not burn their full context on pixels."""
|
||||
try:
|
||||
@@ -540,6 +551,10 @@ async def analyze_character_endpoint(request):
|
||||
"images": analysis_images,
|
||||
"stream": False,
|
||||
"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:
|
||||
if response.status != 200:
|
||||
@@ -558,6 +573,28 @@ async def analyze_character_endpoint(request):
|
||||
else:
|
||||
resp_json = await response.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:
|
||||
# OpenAI-compatible vision chat (LM Studio / Custom).
|
||||
content = [{"type": "text", "text": _ANALYZE_PROMPT}]
|
||||
|
||||
Reference in New Issue
Block a user