Spaces:
Running
Running
Fix broad starter grounding and monitoring
Browse files- tai_helper/llm.py +10 -0
- tai_helper/monitoring.py +2 -1
- tests/test_grounding.py +32 -0
- tests/test_monitoring.py +55 -0
tai_helper/llm.py
CHANGED
|
@@ -265,10 +265,13 @@ _QUERY_RELEVANCE_STOP = frozenset(
|
|
| 265 |
"at",
|
| 266 |
"be",
|
| 267 |
"can",
|
|
|
|
| 268 |
"come",
|
| 269 |
"comes",
|
| 270 |
"course",
|
| 271 |
"courses",
|
|
|
|
|
|
|
| 272 |
"do",
|
| 273 |
"does",
|
| 274 |
"every",
|
|
@@ -282,6 +285,7 @@ _QUERY_RELEVANCE_STOP = frozenset(
|
|
| 282 |
"has",
|
| 283 |
"have",
|
| 284 |
"having",
|
|
|
|
| 285 |
"how",
|
| 286 |
"i",
|
| 287 |
"if",
|
|
@@ -304,12 +308,17 @@ _QUERY_RELEVANCE_STOP = frozenset(
|
|
| 304 |
"please",
|
| 305 |
"program",
|
| 306 |
"provide",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
"really",
|
| 308 |
"that",
|
| 309 |
"the",
|
| 310 |
"their",
|
| 311 |
"there",
|
| 312 |
"this",
|
|
|
|
| 313 |
"to",
|
| 314 |
"towards",
|
| 315 |
"true",
|
|
@@ -317,6 +326,7 @@ _QUERY_RELEVANCE_STOP = frozenset(
|
|
| 317 |
"we",
|
| 318 |
"what",
|
| 319 |
"which",
|
|
|
|
| 320 |
"with",
|
| 321 |
"long",
|
| 322 |
"you",
|
|
|
|
| 265 |
"at",
|
| 266 |
"be",
|
| 267 |
"can",
|
| 268 |
+
"choose",
|
| 269 |
"come",
|
| 270 |
"comes",
|
| 271 |
"course",
|
| 272 |
"courses",
|
| 273 |
+
"decide",
|
| 274 |
+
"deciding",
|
| 275 |
"do",
|
| 276 |
"does",
|
| 277 |
"every",
|
|
|
|
| 285 |
"has",
|
| 286 |
"have",
|
| 287 |
"having",
|
| 288 |
+
"help",
|
| 289 |
"how",
|
| 290 |
"i",
|
| 291 |
"if",
|
|
|
|
| 308 |
"please",
|
| 309 |
"program",
|
| 310 |
"provide",
|
| 311 |
+
"recommend",
|
| 312 |
+
"recommendation",
|
| 313 |
+
"recommendations",
|
| 314 |
+
"recommended",
|
| 315 |
"really",
|
| 316 |
"that",
|
| 317 |
"the",
|
| 318 |
"their",
|
| 319 |
"there",
|
| 320 |
"this",
|
| 321 |
+
"take",
|
| 322 |
"to",
|
| 323 |
"towards",
|
| 324 |
"true",
|
|
|
|
| 326 |
"we",
|
| 327 |
"what",
|
| 328 |
"which",
|
| 329 |
+
"want",
|
| 330 |
"with",
|
| 331 |
"long",
|
| 332 |
"you",
|
tai_helper/monitoring.py
CHANGED
|
@@ -135,6 +135,7 @@ class HelperMonitor:
|
|
| 135 |
}
|
| 136 |
if self.error_message:
|
| 137 |
span.error_info = {
|
| 138 |
-
"
|
| 139 |
"message": _truncate(self.error_message, 1000),
|
|
|
|
| 140 |
}
|
|
|
|
| 135 |
}
|
| 136 |
if self.error_message:
|
| 137 |
span.error_info = {
|
| 138 |
+
"exception_type": "RuntimeError",
|
| 139 |
"message": _truncate(self.error_message, 1000),
|
| 140 |
+
"traceback": "",
|
| 141 |
}
|
tests/test_grounding.py
CHANGED
|
@@ -575,6 +575,38 @@ def _query_bound_result(page: dict, query: str, target_offer_id: str):
|
|
| 575 |
)
|
| 576 |
|
| 577 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
def test_query_binding_rejects_an_exact_span_from_the_wrong_offer() -> None:
|
| 579 |
page = _with_spans(
|
| 580 |
{
|
|
|
|
| 575 |
)
|
| 576 |
|
| 577 |
|
| 578 |
+
def test_broad_course_decision_starter_accepts_retrieved_offer_evidence() -> None:
|
| 579 |
+
span = "Build and evaluate production-ready agentic AI systems."
|
| 580 |
+
page = _with_spans(
|
| 581 |
+
{
|
| 582 |
+
"chunk_id": "agent-engineering-overview",
|
| 583 |
+
"title": "Agent Engineering",
|
| 584 |
+
"kind": "course",
|
| 585 |
+
"offer_id": "agent-engineering",
|
| 586 |
+
"entity_id": "offer:agent-engineering",
|
| 587 |
+
"url": "https://towardsai.com/academy/agent-engineering/",
|
| 588 |
+
"headings": ["Agent Engineering"],
|
| 589 |
+
"text": span,
|
| 590 |
+
},
|
| 591 |
+
span,
|
| 592 |
+
)
|
| 593 |
+
raw = _model_json(
|
| 594 |
+
text=span,
|
| 595 |
+
quote=span,
|
| 596 |
+
chunk_id=page["chunk_id"],
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
+
result = llm.validate_grounded_result(
|
| 600 |
+
raw,
|
| 601 |
+
[page],
|
| 602 |
+
query="I want help deciding which course to take.",
|
| 603 |
+
target_offer_ids=frozenset(),
|
| 604 |
+
)
|
| 605 |
+
|
| 606 |
+
assert result.is_answered
|
| 607 |
+
assert result.answer == span
|
| 608 |
+
|
| 609 |
+
|
| 610 |
def test_query_binding_rejects_an_exact_span_from_the_wrong_offer() -> None:
|
| 611 |
page = _with_spans(
|
| 612 |
{
|
tests/test_monitoring.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
from tai_helper.monitoring import HelperMonitor
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class _FakeSpan:
|
| 9 |
+
usage: dict[str, Any] | None = None
|
| 10 |
+
error_info: dict[str, str] | None = None
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class _FakeSpanContext:
|
| 14 |
+
def __init__(self, span: _FakeSpan) -> None:
|
| 15 |
+
self.span = span
|
| 16 |
+
|
| 17 |
+
def __enter__(self) -> _FakeSpan:
|
| 18 |
+
return self.span
|
| 19 |
+
|
| 20 |
+
def __exit__(self, *_args: object) -> None:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class _FakeOpik:
|
| 25 |
+
def __init__(self) -> None:
|
| 26 |
+
self.span = _FakeSpan()
|
| 27 |
+
self.kwargs: dict[str, Any] = {}
|
| 28 |
+
|
| 29 |
+
def start_as_current_span(self, **kwargs: Any) -> _FakeSpanContext:
|
| 30 |
+
self.kwargs = kwargs
|
| 31 |
+
return _FakeSpanContext(self.span)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_error_span_uses_current_opik_error_info_schema() -> None:
|
| 35 |
+
opik = _FakeOpik()
|
| 36 |
+
monitor = HelperMonitor(
|
| 37 |
+
query="Unsupported claim",
|
| 38 |
+
answer="Contact the team.",
|
| 39 |
+
current_url="https://towardsai.com/academy/mentorship/",
|
| 40 |
+
selected_prompt="I want to find mentors",
|
| 41 |
+
visitor_key="visitor",
|
| 42 |
+
thread_id="thread",
|
| 43 |
+
sources=[],
|
| 44 |
+
usage={},
|
| 45 |
+
latency_ms=1,
|
| 46 |
+
error_message="grounding validation failed",
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
monitor._write_span(opik)
|
| 50 |
+
|
| 51 |
+
assert opik.span.error_info == {
|
| 52 |
+
"exception_type": "RuntimeError",
|
| 53 |
+
"message": "grounding validation failed",
|
| 54 |
+
"traceback": "",
|
| 55 |
+
}
|