修正 Ollama fallback 與 PPT vision payload
All checks were successful
CD Pipeline / deploy (push) Successful in 1m30s

This commit is contained in:
OoO
2026-05-18 21:32:15 +08:00
parent aa8c2c7148
commit 1cf1fd01b1
6 changed files with 119 additions and 12 deletions

View File

@@ -138,6 +138,31 @@ def test_generate_same_host_breaks_loop():
assert mock_post.call_count == 1
def test_generate_forces_final_fallback_when_unhealthy_ttl_expires_mid_request():
"""長 timeout 期間 unhealthy TTL 過期時,第三輪仍應打 111 fallback。"""
import requests
from services import ollama_service as oss
from services.ollama_service import OllamaService
svc = OllamaService()
hosts = [
oss.OLLAMA_HOST_PRIMARY,
oss.OLLAMA_HOST_SECONDARY,
oss.OLLAMA_HOST_PRIMARY, # 模擬 primary unhealthy mark 過期後 resolver 又選回 primary
]
with patch('services.ollama_service.resolve_ollama_host', side_effect=hosts), \
patch('services.ollama_service.requests.post',
side_effect=requests.Timeout('all timeout')) as mock_post:
resp = svc.generate('test')
posted_hosts = [call.args[0].split('/api/generate')[0] for call in mock_post.call_args_list]
assert resp.success is False
assert mock_post.call_count == 3
assert posted_hosts == [oss.OLLAMA_HOST_PRIMARY, oss.OLLAMA_HOST_SECONDARY, oss.OLLAMA_HOST_FALLBACK]
assert 'all 3 hosts failed' in (resp.error or '')
def test_generate_token_parsing_phase13():
"""Phase 13 補強OllamaResponse 解 prompt_eval_count + eval_count"""
from services.ollama_service import OllamaService

View File

@@ -77,6 +77,34 @@ def test_no_issues_response(fake_image, monkeypatch):
assert payload['images'] and isinstance(payload['images'][0], str)
def test_check_image_compresses_valid_png_before_ollama(monkeypatch, tmp_path):
"""有效截圖送 Ollama 前應轉成較輕的 JPEG payload。"""
monkeypatch.setenv('PPT_VISION_ENABLED', 'true')
import base64
Image = pytest.importorskip("PIL.Image")
from services.ppt_vision_service import PPTVisionService
image_path = tmp_path / 'slide.png'
Image.new('RGB', (1800, 1000), color=(245, 238, 226)).save(image_path)
fake_resp = MagicMock(status_code=200)
fake_resp.json.return_value = {'response': '✅ 無視覺異常'}
with patch('services.ollama_service.resolve_ollama_host',
return_value='http://test:11434'), \
patch('services.ollama_service.requests.post', return_value=fake_resp) as mock_post:
svc = PPTVisionService()
result = svc.check_image(str(image_path))
payload = mock_post.call_args.kwargs['json']
encoded = base64.b64decode(payload['images'][0])
assert result.success is True
assert encoded.startswith(b'\xff\xd8')
assert len(encoded) < image_path.stat().st_size
assert payload['options']['num_predict'] == 256
assert payload['keep_alive'] == '5m'
def test_issues_detected(fake_image, monkeypatch):
"""minicpm-v 回多個 ⚠️ marker → issues_found 應含解析的問題"""
monkeypatch.setenv('PPT_VISION_ENABLED', 'true')