-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickstart_feast.py
More file actions
323 lines (278 loc) · 8.63 KB
/
Copy pathquickstart_feast.py
File metadata and controls
323 lines (278 loc) · 8.63 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""Sheaf + Feast feature store quickstart.
This example shows how to use a Feast online feature store as the input source
for time series predictions instead of passing raw history in the request.
The flow:
1. Build a local Feast repo (SQLite online store — no external services).
2. Define an "asset_prices" feature view that stores 30-day close history
as a list[float] for each ticker.
3. Materialise a few rows into the online store.
4. Start a Sheaf ASGI server with feast_repo_path wired to the repo.
5. Send requests with feature_ref (no raw history) and print predictions.
Install:
pip install 'sheaf-serve[time-series,feast]'
Run:
python examples/quickstart_feast.py
"""
from __future__ import annotations
import datetime
import os
import sys
import tempfile
# ---------------------------------------------------------------------------
# Check deps before the heavy imports
# ---------------------------------------------------------------------------
try:
import feast # noqa: F401
except ImportError:
print("feast is required. Install with: pip install 'sheaf-serve[feast]'")
sys.exit(1)
try:
import chronos # noqa: F401
except ImportError:
print(
"chronos-forecasting is required for the time-series backend.\n"
"Install with: pip install 'sheaf-serve[time-series]'"
)
sys.exit(1)
import pandas as pd
from feast import Entity, FeatureStore, FeatureView, Field, FileSource
from feast.types import Array, Float32
from starlette.testclient import TestClient
from sheaf.api.base import ModelType
from sheaf.modal_server import _build_asgi_app
from sheaf.spec import ModelSpec, ResourceConfig
# ---------------------------------------------------------------------------
# Sample data — 30-day closing prices for three tickers (made-up values)
# ---------------------------------------------------------------------------
_HISTORY: dict[str, list[float]] = {
"AAPL": [
170.1,
171.5,
172.0,
170.8,
173.2,
174.5,
175.0,
173.8,
176.0,
177.2,
178.5,
177.0,
179.3,
180.1,
179.8,
181.0,
182.5,
181.3,
183.0,
184.2,
183.5,
185.0,
186.3,
185.8,
187.0,
188.5,
187.2,
189.0,
190.3,
191.0,
],
"MSFT": [
330.0,
332.5,
331.0,
334.0,
336.3,
335.1,
338.0,
337.5,
340.0,
341.2,
343.5,
342.0,
344.3,
345.1,
344.8,
346.0,
347.5,
346.3,
348.0,
349.2,
348.5,
350.0,
351.3,
350.8,
352.0,
353.5,
352.2,
354.0,
355.3,
356.0,
],
"GOOG": [
140.0,
141.0,
143.0,
142.5,
144.0,
143.5,
145.0,
144.2,
146.0,
147.3,
148.5,
147.0,
149.3,
150.1,
149.8,
151.0,
152.5,
151.3,
153.0,
154.2,
153.5,
155.0,
156.3,
155.8,
157.0,
158.5,
157.2,
159.0,
160.3,
161.0,
],
}
# ---------------------------------------------------------------------------
# Step 1 — Build the Feast repo in a temp directory
# ---------------------------------------------------------------------------
def build_feast_repo(repo_dir: str) -> None:
print(f"Building Feast repo at {repo_dir} ...")
# feature_store.yaml — local provider, SQLite online store
yaml = """\
project: sheaf_quickstart
registry: registry.db
provider: local
online_store:
type: sqlite
path: online_store.db
entity_key_serialization_version: 2
"""
with open(os.path.join(repo_dir, "feature_store.yaml"), "w") as fh:
fh.write(yaml)
# DataFrame with one row per ticker
now = datetime.datetime.utcnow()
df = pd.DataFrame(
{
"ticker": list(_HISTORY.keys()),
"close_history_30d": list(_HISTORY.values()),
"event_timestamp": [now] * len(_HISTORY),
"created": [now] * len(_HISTORY),
}
)
parquet_path = os.path.join(repo_dir, "prices.parquet")
df.to_parquet(parquet_path, index=False)
# Entity + FeatureView definitions
ticker_entity = Entity(name="ticker", join_keys=["ticker"])
source = FileSource(
path=parquet_path,
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
prices_view = FeatureView(
name="asset_prices",
entities=[ticker_entity],
schema=[Field(name="close_history_30d", dtype=Array(Float32))],
source=source,
ttl=datetime.timedelta(days=1),
)
store = FeatureStore(repo_path=repo_dir)
store.apply([ticker_entity, prices_view])
# Materialise into the online store
print("Materialising features into SQLite online store ...")
store.materialize_incremental(
end_date=datetime.datetime.utcnow() + datetime.timedelta(minutes=1)
)
print("Done.\n")
# ---------------------------------------------------------------------------
# Step 2 — Verify the feature store resolves correctly before serving
# ---------------------------------------------------------------------------
def verify_feast_resolution(repo_dir: str) -> None:
from sheaf.integrations.feast import FeastResolver
resolver = FeastResolver(repo_dir)
resolver.load()
print("Verifying Feast resolution:")
for ticker in _HISTORY:
history = resolver.resolve(
__import__("sheaf.api.time_series", fromlist=["FeatureRef"]).FeatureRef(
feature_view="asset_prices",
feature_name="close_history_30d",
entity_key="ticker",
entity_value=ticker,
)
)
n = len(history)
assert n == 30, f"Expected 30 values for {ticker}, got {n}"
print(
f" {ticker}: [{history[0]:.1f}, {history[1]:.1f}, ..., {history[-1]:.1f}]"
f" ({n} values)"
)
print()
# ---------------------------------------------------------------------------
# Step 3 — Start Sheaf and send feature_ref requests
# ---------------------------------------------------------------------------
def run_predictions(repo_dir: str) -> None:
# For the quickstart we use Chronos-Bolt-Small (downloads ~300 MB on first run)
spec = ModelSpec(
name="chronos-small",
model_type=ModelType.TIME_SERIES,
backend="chronos2",
backend_kwargs={"model_id": "amazon/chronos-bolt-small", "device_map": "cpu"},
resources=ResourceConfig(num_cpus=1),
feast_repo_path=repo_dir,
)
print("Loading Chronos backend (this downloads weights on first run) ...")
app = _build_asgi_app([spec])
client = TestClient(app)
print("Server ready.\n")
print("Sending feature_ref requests (no raw history in the payload):")
print("-" * 60)
for ticker in _HISTORY:
payload = {
"model_type": "time_series",
"model_name": "chronos-small",
"feature_ref": {
"feature_view": "asset_prices",
"feature_name": "close_history_30d",
"entity_key": "ticker",
"entity_value": ticker,
},
"horizon": 7,
"frequency": "1d",
"output_mode": "quantiles",
"quantile_levels": [0.1, 0.5, 0.9],
}
r = client.post("/chronos-small/predict", json=payload)
if r.status_code != 200:
print(f"{ticker}: ERROR {r.status_code} — {r.text}")
continue
body = r.json()
# quantiles: dict[str, list[float]] — keys "0.1", "0.5", "0.9"
q = body["quantiles"]
p10, p50, p90 = q["0.1"], q["0.5"], q["0.9"]
last_price = _HISTORY[ticker][-1]
print(f"\n{ticker} (last close: ${last_price:.2f})")
print(" 7-day forecast (p10 / median / p90):")
for day, (lo_v, mid_v, hi_v) in enumerate(zip(p10, p50, p90), 1):
print(f" day {day}: ${lo_v:7.2f} / ${mid_v:7.2f} / ${hi_v:7.2f}")
print()
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
with tempfile.TemporaryDirectory() as repo_dir:
build_feast_repo(repo_dir)
verify_feast_resolution(repo_dir)
run_predictions(repo_dir)
print("Quickstart complete.")
if __name__ == "__main__":
main()