This commit is contained in:
@@ -126,6 +126,44 @@ class TestRagEnabledHits:
|
||||
assert result.has_high_confidence is True
|
||||
assert '本週業績漲' in result.synthesize()
|
||||
|
||||
def test_mark_saved_call_only_when_requested_and_confident(self, rag_enabled, monkeypatch):
|
||||
"""RAG-first caller 明確標記時,高信心命中才計入 saved_call。"""
|
||||
from services import rag_service as rs
|
||||
|
||||
monkeypatch.setattr(
|
||||
'services.ollama_service.ollama_service.generate_embedding',
|
||||
lambda text, model="bge-m3:latest": _fake_embedding(),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
rs.rag_service,
|
||||
'_select_hits',
|
||||
lambda **_kw: [{'id': 101, 'content': '命中內容', 'score': 0.95}],
|
||||
)
|
||||
captured = {}
|
||||
|
||||
def _capture_async_log(**kwargs):
|
||||
captured.update(kwargs)
|
||||
|
||||
monkeypatch.setattr(rs.rag_service, '_async_log', _capture_async_log)
|
||||
|
||||
result = rs.rag_service.query(
|
||||
"本週業績趨勢",
|
||||
caller='openclaw_qa',
|
||||
mark_saved_call=True,
|
||||
)
|
||||
|
||||
assert result.saved_call is True
|
||||
assert captured['saved_call'] is True
|
||||
|
||||
result = rs.rag_service.query(
|
||||
"本週業績趨勢",
|
||||
caller='admin_quality_trend',
|
||||
mark_saved_call=False,
|
||||
)
|
||||
|
||||
assert result.saved_call is False
|
||||
assert captured['saved_call'] is False
|
||||
|
||||
def test_low_confidence_no_hit(self, rag_enabled, monkeypatch):
|
||||
"""所有結果 distance>0.15 → similarity<0.85 → SQL 已過濾,回空 → has_high_confidence False。"""
|
||||
from services import rag_service as rs
|
||||
@@ -292,10 +330,12 @@ class TestFireAndForgetLog:
|
||||
threshold=0.85,
|
||||
hits=[{'id': 101}],
|
||||
request_id='req-1',
|
||||
saved_call=True,
|
||||
)
|
||||
|
||||
assert "embedding_signature" in captured["sql"]
|
||||
assert captured["params"]["embedding_signature"] == rs.get_embedding_signature()
|
||||
assert captured["params"]["saved_call"] is True
|
||||
fake_session.commit.assert_called_once()
|
||||
|
||||
def test_embedding_failure_falls_back_to_empty(self, rag_enabled, monkeypatch):
|
||||
|
||||
Reference in New Issue
Block a user