Repository navigation
Expand file tree
/
Copy pathactivities.py
More file actions
90 lines (66 loc) · 2.72 KB
/
Copy pathactivities.py
File metadata and controls
90 lines (66 loc) · 2.72 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Temporal activities that call SpecStream. Each is one paid call, retried by Temporal when it is safe.
search(SearchInput) POST /v1/web-search, fast tier: $1 per 1,000 searches of up to 10 results
read_pages(ReadInput) POST /v1/extract: $0.80 per 1,000 pages read, up to 20 URLs a call
A refused key, an empty balance or a bad request fails the activity for good (non retryable), since
another try would fail the same way. A busy API (503 or 429) is waited out inside the call, then
left to Temporal's retry policy. read_pages sends one Idempotency-Key per activity, so a retried
read replays the paid reply instead of paying again.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from temporalio import activity
from temporalio.exceptions import ApplicationError
import specstream_client as ss
@dataclass
class SearchInput:
q: str
num: int = 10
@dataclass
class Source:
url: str
title: str = ""
snippet: str = ""
published: str | None = None
@dataclass
class SearchOutput:
q: str
sources: list[Source] = field(default_factory=list)
price_usd: float = 0.0
@dataclass
class ReadInput:
urls: list[str]
question: str = ""
@dataclass
class Page:
url: str
status: str
title: str = ""
published: str | None = None
text: str = ""
@dataclass
class ReadOutput:
pages: list[Page] = field(default_factory=list)
price_usd: float = 0.0
def _fail(error: ss.SpecStreamError) -> ApplicationError:
retry = error.status in (429, 503) or error.status >= 500
return ApplicationError(f"SpecStream {error.status} {error.code}: {error.message}", type=error.code, non_retryable=not retry)
@activity.defn
def search(request: SearchInput) -> SearchOutput:
try:
data = ss.web_search(request.q[:2000], speed="fast", num=request.num)
except ss.SpecStreamError as error:
raise _fail(error) from None
found = [Source(s["url"], s["title"], s["snippet"], s["published"]) for s in ss.sources(data)]
return SearchOutput(request.q, found, float(data.get("price_usd") or 0))
@activity.defn
def read_pages(request: ReadInput) -> ReadOutput:
info = activity.info()
# The same key on every attempt of this activity: a retry replays the paid reply.
key = f"{info.workflow_id}:{info.activity_id}"[:128]
try:
data = ss.extract(request.urls[:20], query=request.question or None, max_chars=4000 if request.question else None,
idempotency_key=key)
except ss.SpecStreamError as error:
raise _fail(error) from None
pages = [Page(p["url"], p["status"], p["title"], p["published"], p["text"]) for p in ss.pages(data)]
return ReadOutput(pages, float(data.get("price_usd") or 0))