|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from feast import ( |
| 4 | + Entity, |
| 5 | + FeatureService, |
| 6 | + FeatureView, |
| 7 | + Field, |
| 8 | + FileSource, |
| 9 | + PushSource, |
| 10 | +) |
| 11 | +from feast.data_format import ParquetFormat |
| 12 | +from feast.types import Array, Float32, String |
| 13 | +from feast.value_type import ValueType |
| 14 | + |
| 15 | +# Entity: Identifies each city document/chunk in the knowledge base |
| 16 | +city = Entity( |
| 17 | + name="city_id", |
| 18 | + value_type=ValueType.INT64, |
| 19 | + description="Unique identifier for each city Wikipedia summary (document chunk ID).", |
| 20 | + join_keys=["city_id"], |
| 21 | +) |
| 22 | + |
| 23 | +# Data Source: Parquet file containing city summaries with pre-computed embeddings |
| 24 | +city_summaries_source = FileSource( |
| 25 | + name="city_summaries_source", |
| 26 | + file_format=ParquetFormat(), |
| 27 | + path="./data/city_wikipedia_summaries_with_embeddings.parquet", |
| 28 | + timestamp_field="event_timestamp", |
| 29 | + description="Wikipedia summaries of US cities (batch).", |
| 30 | +) |
| 31 | + |
| 32 | +# Push Source: same schema as batch; allows near real-time ingestion of new/updated docs |
| 33 | +city_summaries_push_source = PushSource( |
| 34 | + name="city_summaries_push_source", |
| 35 | + batch_source=city_summaries_source, |
| 36 | + description="Push source for real-time updates to city summaries/embeddings.", |
| 37 | +) |
| 38 | + |
| 39 | +# Feature View 1: City embeddings for semantic/vector search (RAG retrieval) |
| 40 | +city_summary_embeddings = FeatureView( |
| 41 | + name="city_summary_embeddings", |
| 42 | + description="City Wikipedia summaries with embeddings for semantic search. ", |
| 43 | + entities=[city], |
| 44 | + schema=[ |
| 45 | + Field( |
| 46 | + name="vector", |
| 47 | + dtype=Array(Float32), |
| 48 | + description="384-dimensional sentence embedding for semantic similarity search (MiniLM).", |
| 49 | + vector_index=True, |
| 50 | + vector_search_metric="COSINE", |
| 51 | + ), |
| 52 | + Field( |
| 53 | + name="sentence_chunks", |
| 54 | + dtype=String, |
| 55 | + description="Chunked sentences from the Wikipedia summary.", |
| 56 | + ), |
| 57 | + ], |
| 58 | + source=city_summaries_source, |
| 59 | + ttl=timedelta(days=1), |
| 60 | + online=True, |
| 61 | + tags={"team": "ml-platform", "use_case": "city_qa", "type": "vector"}, |
| 62 | +) |
| 63 | + |
| 64 | +# Feature View 2: City metadata for scalar lookups (no vector search) |
| 65 | +city_metadata = FeatureView( |
| 66 | + name="city_metadata", |
| 67 | + description="City metadata including state and full Wikipedia summary. ", |
| 68 | + entities=[city], |
| 69 | + schema=[ |
| 70 | + Field( |
| 71 | + name="state", |
| 72 | + dtype=String, |
| 73 | + description="US state where the city is located (e.g., 'New York, New York').", |
| 74 | + ), |
| 75 | + Field( |
| 76 | + name="wiki_summary", |
| 77 | + dtype=String, |
| 78 | + description="Full Wikipedia summary of the city.", |
| 79 | + ), |
| 80 | + ], |
| 81 | + source=city_summaries_source, |
| 82 | + ttl=timedelta(hours=2), |
| 83 | + online=True, |
| 84 | + tags={"team": "ml-platform", "use_case": "city_qa", "type": "metadata"}, |
| 85 | +) |
| 86 | + |
| 87 | +# Feature View 3: Fresh embeddings (PushSource) for near real-time doc updates |
| 88 | +city_summary_embeddings_realtime = FeatureView( |
| 89 | + name="city_summary_embeddings_realtime", |
| 90 | + description="Same as city_summary_embeddings but with real-time ingestion (PushSource).", |
| 91 | + entities=[city], |
| 92 | + schema=[ |
| 93 | + Field( |
| 94 | + name="vector", |
| 95 | + dtype=Array(Float32), |
| 96 | + description="384-dimensional sentence embedding for semantic similarity search.", |
| 97 | + vector_index=True, |
| 98 | + vector_search_metric="COSINE", |
| 99 | + ), |
| 100 | + Field( |
| 101 | + name="sentence_chunks", |
| 102 | + dtype=String, |
| 103 | + description="Chunked sentences from the Wikipedia summary.", |
| 104 | + ), |
| 105 | + ], |
| 106 | + source=city_summaries_push_source, |
| 107 | + ttl=timedelta(hours=2), |
| 108 | + online=True, |
| 109 | + tags={ |
| 110 | + "team": "ml-platform", |
| 111 | + "use_case": "city_qa", |
| 112 | + "type": "vector", |
| 113 | + "ingestion": "push", |
| 114 | + }, |
| 115 | +) |
| 116 | + |
| 117 | +# Feature Service: Bundles features for the City Q&A retrieval endpoint |
| 118 | +city_qa_v1 = FeatureService( |
| 119 | + name="city_qa_v1", |
| 120 | + features=[ |
| 121 | + city_summary_embeddings, |
| 122 | + city_metadata, |
| 123 | + ], |
| 124 | + description="Feature service for City Information Q&A. ", |
| 125 | + tags={"team": "ml-platform", "version": "v1"}, |
| 126 | +) |
| 127 | + |
| 128 | +# Feature service that includes push-backed and request-time features |
| 129 | +city_qa_v2 = FeatureService( |
| 130 | + name="city_qa_v2", |
| 131 | + features=[ |
| 132 | + city_summary_embeddings_realtime, |
| 133 | + city_metadata, |
| 134 | + ], |
| 135 | + description="City Q&A with push ingestion and request-time context (query_text, user_id).", |
| 136 | + tags={"team": "ml-platform", "version": "v2"}, |
| 137 | +) |
0 commit comments