-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextraction_cache.py
More file actions
71 lines (49 loc) · 2 KB
/
extraction_cache.py
File metadata and controls
71 lines (49 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Extraction cache example for FMTools.
Demonstrates how to memoize repeated extractions with a sqlite-backed cache.
"""
from __future__ import annotations
import asyncio
import time
from examples._support import AppleFMSetupError, raise_sdk_setup_error, require_apple_fm
try:
import apple_fm_sdk as fm
except Exception as exc: # pragma: no cover - import error path
raise_sdk_setup_error("extraction_cache.py", exc)
try:
from fmtools.cache import ExtractionCache, cached_local_extract
except Exception as exc: # pragma: no cover - import error path
raise_sdk_setup_error("extraction_cache.py", exc)
@fm.generable()
class SupportTicket:
category: str = fm.guide(anyOf=["billing", "technical", "account", "other"])
urgency: str = fm.guide(anyOf=["low", "medium", "high", "critical"])
summary: str = fm.guide(description="One-sentence issue summary")
CACHE = ExtractionCache(ttl=3600)
@cached_local_extract(schema=SupportTicket, cache=CACHE, retries=2)
async def classify_ticket(email_text: str) -> SupportTicket:
"""Classify support email text into category, urgency, and concise summary."""
async def _timed_call(text: str) -> tuple[SupportTicket, float]:
start = time.perf_counter()
result = await classify_ticket(text)
elapsed = time.perf_counter() - start
return result, elapsed
async def main() -> None:
require_apple_fm("extraction_cache.py")
sample = "I was charged twice for my subscription this month and need a refund."
print("First call (cache miss, model inference):")
first, t1 = await _timed_call(sample)
print(f" result: {first}")
print(f" elapsed: {t1:.3f}s")
print("\nSecond call (cache hit):")
second, t2 = await _timed_call(sample)
print(f" result: {second}")
print(f" elapsed: {t2:.3f}s")
print("\nCache stats:")
print(f" {CACHE.stats()}")
if __name__ == "__main__":
try:
asyncio.run(main())
except AppleFMSetupError as exc:
print(exc)
raise SystemExit(2) from exc