This commit is contained in:
@@ -56,7 +56,10 @@ async def set_cached_explain(redis, event_id: str, result: dict):
|
|||||||
# arq job functions
|
# arq job functions
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
async def process_ask_question(ctx, question: str, filters: dict, events: list, total: int, excluded_services: list | None):
|
|
||||||
|
async def process_ask_question(
|
||||||
|
ctx, question: str, filters: dict, events: list, total: int, excluded_services: list | None
|
||||||
|
):
|
||||||
"""Background job: call LLM for /api/ask and cache result."""
|
"""Background job: call LLM for /api/ask and cache result."""
|
||||||
from routes.ask import _call_llm
|
from routes.ask import _call_llm
|
||||||
|
|
||||||
@@ -92,6 +95,7 @@ async def process_explain_event(ctx, event_id: str, event: dict, related: list):
|
|||||||
# arq worker configuration
|
# arq worker configuration
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
async def startup(ctx):
|
async def startup(ctx):
|
||||||
from redis.asyncio import Redis
|
from redis.asyncio import Redis
|
||||||
|
|
||||||
|
|||||||
@@ -813,9 +813,17 @@ async def ask_question(body: AskRequest, user: dict = Depends(require_auth)):
|
|||||||
try:
|
try:
|
||||||
answer = await _call_llm(question, events, total=total, excluded_services=excluded_services)
|
answer = await _call_llm(question, events, total=total, excluded_services=excluded_services)
|
||||||
llm_used = True
|
llm_used = True
|
||||||
await set_cached_ask(redis, question, filters_snapshot, events, {
|
await set_cached_ask(
|
||||||
"answer": answer, "llm_used": True, "llm_error": None,
|
redis,
|
||||||
})
|
question,
|
||||||
|
filters_snapshot,
|
||||||
|
events,
|
||||||
|
{
|
||||||
|
"answer": answer,
|
||||||
|
"llm_used": True,
|
||||||
|
"llm_error": None,
|
||||||
|
},
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
llm_error = f"LLM call failed: {exc}"
|
llm_error = f"LLM call failed: {exc}"
|
||||||
logger.warning("LLM call failed, falling back to structured summary", error=str(exc))
|
logger.warning("LLM call failed, falling back to structured summary", error=str(exc))
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ def client(mock_events_collection, mock_watermarks_collection, monkeypatch):
|
|||||||
class FakeRedis:
|
class FakeRedis:
|
||||||
async def get(self, key):
|
async def get(self, key):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def setex(self, key, ttl, value):
|
async def setex(self, key, ttl, value):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ def test_explain_event_with_llm_mock(client, mock_events_collection, monkeypatch
|
|||||||
class FakeRedis:
|
class FakeRedis:
|
||||||
async def get(self, key):
|
async def get(self, key):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def setex(self, key, ttl, value):
|
async def setex(self, key, ttl, value):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -405,7 +405,15 @@ class TestAskCaching:
|
|||||||
"include_tags": None,
|
"include_tags": None,
|
||||||
"exclude_tags": None,
|
"exclude_tags": None,
|
||||||
}
|
}
|
||||||
asyncio.run(set_cached_ask(redis, "What happened to USER-001?", filters_snapshot, [{"id": "evt-cache"}], {"answer": "Cached answer!", "llm_used": True, "llm_error": None}))
|
asyncio.run(
|
||||||
|
set_cached_ask(
|
||||||
|
redis,
|
||||||
|
"What happened to USER-001?",
|
||||||
|
filters_snapshot,
|
||||||
|
[{"id": "evt-cache"}],
|
||||||
|
{"answer": "Cached answer!", "llm_used": True, "llm_error": None},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
async def fake_get_arq_pool():
|
async def fake_get_arq_pool():
|
||||||
return redis
|
return redis
|
||||||
@@ -451,6 +459,7 @@ class TestAskCaching:
|
|||||||
|
|
||||||
async def enqueue_job(self, func, *args, **kwargs):
|
async def enqueue_job(self, func, *args, **kwargs):
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
job = MagicMock()
|
job = MagicMock()
|
||||||
job.job_id = "job-12345"
|
job.job_id = "job-12345"
|
||||||
self.enqueued.append((func, args, kwargs))
|
self.enqueued.append((func, args, kwargs))
|
||||||
|
|||||||
Reference in New Issue
Block a user