-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddings.py
More file actions
516 lines (450 loc) · 18 KB
/
embeddings.py
File metadata and controls
516 lines (450 loc) · 18 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import itertools
import logging
import time
import uuid
from collections.abc import Iterator
from datetime import UTC, date, datetime
from typing import TYPE_CHECKING, TypedDict, Unpack, cast
import attrs
import pandas as pd
import pyarrow as pa
import pyarrow.dataset as ds
from attrs import asdict, define, field
from duckdb import DuckDBPyConnection
from duckdb import IOException as DuckDBIOException
from duckdb_engine import Dialect as DuckDBDialect
from sqlalchemy import and_, select, text
from timdex_dataset_api.record import datetime_iso_parse
from timdex_dataset_api.utils import build_filter_expr_sa
if TYPE_CHECKING:
from timdex_dataset_api import TIMDEXDataset
logger = logging.getLogger(__name__)
TIMDEX_DATASET_EMBEDDINGS_SCHEMA = pa.schema(
(
pa.field("timdex_record_id", pa.string()),
pa.field("run_id", pa.string()),
pa.field("run_record_offset", pa.int32()),
pa.field("embedding_timestamp", pa.timestamp("us", tz="UTC")),
pa.field("embedding_model", pa.string()),
pa.field("embedding_strategy", pa.string()),
pa.field("embedding_vector", pa.list_(pa.float32())),
pa.field("embedding_object", pa.binary()),
pa.field("year", pa.string()),
pa.field("month", pa.string()),
pa.field("day", pa.string()),
)
)
EMBEDDINGS_FILTER_COLUMNS = {
"timdex_record_id",
"run_id",
"run_record_offset",
"embedding_timestamp",
"embedding_model",
"embedding_strategy",
}
# subset of record metadata columns for filtering and selecting
METADATA_SELECT_FILTER_COLUMNS = {
"source",
"run_date",
"run_type",
"action",
"run_timestamp",
}
class EmbeddingsFilters(TypedDict, total=False):
# embeddings columns
timdex_record_id: str
run_id: str
run_record_offset: int
embedding_timestamp: str | datetime
embedding_model: str
embedding_strategy: str
# record metadata columns
source: str | list[str]
run_date: str | date | list[str | date]
run_type: str | list[str]
action: str | list[str]
run_timestamp: str | datetime | list[str | datetime]
@define
class DatasetEmbedding:
"""Container for single record embedding.
Fields:
timdex_record_id: Fields (timdex_record_id, run_id, run_record_offset) combine to
form a composite key that points to a single, distinct record version in the
records data.
run_id: ...
run_record_offset: ...
embedding_model: Embedding model name, e.g. HuggingFace URI
embedding_strategy: Strategy used to create embedding
- this correlates to a transformation strategy in the timdex-embeddings CLI
application, e.g. "full_record"
embedding_timestamp: Timestamp when embedding was created
embedding_vector: Numerical vector representation of embedding
- preferred form for storing embedding as a numerical array
embedding_object: Object representation of the embedding
- example: {token:weight, ...} representation for sparse vector
- flexible enough to hold other representations
"""
timdex_record_id: str = field()
run_id: str = field()
run_record_offset: int = field()
embedding_model: str = field()
embedding_strategy: str = field()
embedding_timestamp: datetime = field( # type: ignore[assignment]
converter=datetime_iso_parse,
default=attrs.Factory(lambda: datetime.now(tz=UTC).isoformat()),
)
embedding_vector: list[float] | None = field(default=None)
embedding_object: bytes | None = field(default=None)
@property
def year(self) -> str:
return self.embedding_timestamp.strftime("%Y")
@property
def month(self) -> str:
return self.embedding_timestamp.strftime("%m")
@property
def day(self) -> str:
return self.embedding_timestamp.strftime("%d")
def to_dict(
self,
) -> dict:
"""Serialize instance as dictionary."""
return {
**asdict(self),
"year": self.year,
"month": self.month,
"day": self.day,
}
class TIMDEXEmbeddings:
def __init__(self, timdex_dataset: "TIMDEXDataset"):
"""Init TIMDEXEmbeddings.
Class to handle the writing and readings of embeddings associated with TIMDEX
records.
Args:
- timdex_dataset: instance of TIMDEXDataset
"""
self.timdex_dataset = timdex_dataset
self.conn = timdex_dataset.conn
self.schema = TIMDEX_DATASET_EMBEDDINGS_SCHEMA
self.partition_columns = ["year", "month", "day"]
# set up embeddings views
self._setup_embeddings_views()
@property
def data_embeddings_root(self) -> str:
return f"{self.timdex_dataset.location.removesuffix('/')}/data/embeddings"
def _setup_embeddings_views(self) -> None:
"""Set up embeddings views in the 'data' schema."""
start_time = time.perf_counter()
try:
self._create_embeddings_view(self.conn)
self._create_current_embeddings_view(self.conn)
self._create_current_run_embeddings_view(self.conn)
except DuckDBIOException:
logger.debug("No embeddings parquet files found")
except Exception as exception: # noqa: BLE001
logger.warning(f"Error creating embeddings views: {exception}")
logger.debug(
"Embeddings views setup for TIMDEXEmbeddings, "
f"{round(time.perf_counter() - start_time, 2)}s"
)
def _create_embeddings_view(self, conn: DuckDBPyConnection) -> None:
"""Create a view that projects over embeddings parquet files."""
logger.debug("creating view data.embeddings")
conn.execute(f"""
create or replace view data.embeddings as
(
select *
from read_parquet(
'{self.data_embeddings_root}/**/*.parquet',
hive_partitioning=true,
filename=true
)
);
""")
def _create_current_embeddings_view(self, conn: DuckDBPyConnection) -> None:
"""Create a view of current embedding records.
This builds on the 'data.embeddings' view. This view includes only
the most current version of each embedding grouped by
[timdex_record_id, embedding_strategy].
"""
logger.debug("creating view data.current_embeddings")
# SQL for the current records logic (CTEs)
conn.execute("""
create or replace view data.current_embeddings as
(
with
-- CTE of embeddings ranked by embedding_timestamp
ce_ranked_embeddings as
(
select
*,
row_number() over (
partition by timdex_record_id, embedding_strategy
order by
embedding_timestamp desc nulls last,
run_record_offset desc nulls last
) as rn
from data.embeddings
)
-- final select for current records (rn = 1)
select
* exclude (rn)
from ce_ranked_embeddings
where rn = 1
);
""")
def _create_current_run_embeddings_view(self, conn: DuckDBPyConnection) -> None:
"""Create a view of current embedding records per run.
This builds on the 'data.embeddings' view. This view includes only
the most current version of each embedding per run grouped by
[timdex_record_id, run_id, embedding_strategy,].
"""
logger.debug("creating view data.current_run_embeddings")
# SQL for the current records logic (CTEs)
conn.execute("""
create or replace view data.current_run_embeddings as
(
with
-- CTE of embeddings ranked by embedding_timestamp
ce_ranked_embeddings as
(
select
*,
row_number() over (
partition by timdex_record_id, run_id, embedding_strategy
order by
embedding_timestamp desc nulls last,
run_id desc nulls last,
run_record_offset desc nulls last
) as rn
from data.embeddings
)
-- final select for current records (rn = 1)
select
* exclude (rn)
from ce_ranked_embeddings
where rn = 1
);
""")
def write(
self,
embeddings_iter: Iterator[DatasetEmbedding],
*,
use_threads: bool = True,
) -> list[ds.WrittenFile]:
"""Write embeddings as parquet files to /data/embeddings.
Approach is similar to TIMDEXDataset.write() for Records:
- use self.data_embeddings_root for location of embeddings parquet files
- use pyarrow Dataset to write rows
"""
start_time = time.perf_counter()
written_files: list[ds.WrittenFile] = []
filesystem, path = self.timdex_dataset.parse_location(self.data_embeddings_root)
embedding_batches_iter = self.create_embedding_batches(embeddings_iter)
ds.write_dataset(
embedding_batches_iter,
base_dir=path,
basename_template="%s-{i}.parquet" % (str(uuid.uuid4())), # noqa: UP031
existing_data_behavior="overwrite_or_ignore",
filesystem=filesystem,
file_visitor=lambda written_file: written_files.append(written_file), # type: ignore[arg-type] # noqa: PLW0108
format="parquet",
max_open_files=500,
max_rows_per_file=self.timdex_dataset.config.max_rows_per_file,
max_rows_per_group=self.timdex_dataset.config.max_rows_per_group,
partitioning=self.partition_columns,
partitioning_flavor="hive",
schema=self.schema,
use_threads=use_threads,
)
self.log_write_statistics(start_time, written_files)
return written_files
def create_embedding_batches(
self, embeddings_iter: Iterator["DatasetEmbedding"]
) -> Iterator[pa.RecordBatch]:
for i, embedding_batch in enumerate(
itertools.batched(
embeddings_iter, self.timdex_dataset.config.write_batch_size
)
):
embedding_dicts = [embedding.to_dict() for embedding in embedding_batch]
batch = pa.RecordBatch.from_pylist(embedding_dicts)
logger.debug(f"Yielding batch {i + 1} for dataset writing.")
yield batch
def log_write_statistics(
self,
start_time: float,
written_files: list[ds.WrittenFile],
) -> None:
"""Parse written files from write and log statistics."""
total_time = round(time.perf_counter() - start_time, 2)
total_files = len(written_files)
total_rows = sum(
[wf.metadata.num_rows for wf in written_files] # type: ignore[attr-defined]
)
total_size = sum([wf.size for wf in written_files]) # type: ignore[attr-defined]
logger.info(
f"Dataset write complete - elapsed: "
f"{total_time}s, "
f"total files: {total_files}, "
f"total rows: {total_rows}, "
f"total size: {total_size}"
)
def read_batches_iter(
self,
table: str = "embeddings",
columns: list[str] | None = None,
limit: int | None = None,
where: str | None = None,
**filters: Unpack[EmbeddingsFilters],
) -> Iterator[pa.RecordBatch]:
"""Yield ETL records as pyarrow.RecordBatches.
This is the base read method. All read methods use this for streaming
batches of records. This method relies on DuckDB to project over all
embeddings parquet files (i.e., no "metadata layer") and filter data.
"""
start_time = time.perf_counter()
if table not in ["embeddings", "current_embeddings", "current_run_embeddings"]:
raise ValueError(f"Invalid table: '{table}'")
# ensure table exists
try:
self.timdex_dataset.get_sa_table("data", table)
except ValueError:
logger.warning(
f"Table '{table}' not found in DuckDB context. Embeddings may not yet "
"exist or TIMDEXDataset.refresh() may be required."
)
return
data_query = self._build_query(
table,
columns,
limit,
where,
**filters,
)
cursor = self.conn.execute(data_query)
yield from cursor.to_arrow_reader(
batch_size=self.timdex_dataset.config.read_batch_size
)
logger.debug(f"read() elapsed: {round(time.perf_counter() - start_time, 2)}s")
def _build_query(
self,
table: str = "embeddings",
columns: list[str] | None = None,
limit: int | None = None,
where: str | None = None,
**filters: Unpack[EmbeddingsFilters],
) -> str:
"""Build SQL query using SQLAlchemy.
The method returns a SQL query string, which SQLAlchemy executes to
fetch results. Always joins to metadata.records to enable filtering
by metadata columns (source, run_date, run_type, action, run_timestamp).
"""
embeddings_table = self.timdex_dataset.get_sa_table("data", table)
metadata_table = self.timdex_dataset.get_sa_table("metadata", "records")
# select specific columns or default to all from embeddings + metadata
if columns:
embeddings_cols = []
metadata_cols = []
for col_name in columns:
if col_name in TIMDEX_DATASET_EMBEDDINGS_SCHEMA.names:
embeddings_cols.append(embeddings_table.c[col_name])
elif col_name in METADATA_SELECT_FILTER_COLUMNS:
metadata_cols.append(metadata_table.c[col_name])
else:
raise ValueError(f"Invalid column: {col_name}")
stmt = select(*embeddings_cols, *metadata_cols)
else:
embeddings_cols = [
embeddings_table.c[col] for col in TIMDEX_DATASET_EMBEDDINGS_SCHEMA.names
]
metadata_cols = [
metadata_table.c[col] for col in METADATA_SELECT_FILTER_COLUMNS
]
stmt = select(*embeddings_cols, *metadata_cols)
# create SQL statement with join to metadata.records
join_condition = and_(
embeddings_table.c.timdex_record_id == metadata_table.c.timdex_record_id,
embeddings_table.c.run_id == metadata_table.c.run_id,
embeddings_table.c.run_record_offset == metadata_table.c.run_record_offset,
)
stmt = stmt.select_from(embeddings_table.join(metadata_table, join_condition))
# split filters between embeddings and metadata tables
embeddings_filters = {
k: v for k, v in filters.items() if k in EMBEDDINGS_FILTER_COLUMNS
}
record_metadata_filters = {
k: v for k, v in filters.items() if k in METADATA_SELECT_FILTER_COLUMNS
}
# apply embeddings filters
embeddings_filter_expr = build_filter_expr_sa(
embeddings_table, **cast("dict", embeddings_filters)
)
if embeddings_filter_expr is not None:
stmt = stmt.where(embeddings_filter_expr)
# apply metadata filters
record_metadata_filter_expr = build_filter_expr_sa(
metadata_table, **cast("dict", record_metadata_filters)
)
if record_metadata_filter_expr is not None:
stmt = stmt.where(record_metadata_filter_expr)
# explicit raw WHERE string
if where is not None and where.strip():
stmt = stmt.where(text(where))
# apply limit if present
if limit:
stmt = stmt.limit(limit)
# using DuckDB dialect, compile to SQL string
compiled = stmt.compile(
dialect=DuckDBDialect(),
compile_kwargs={"literal_binds": True},
)
return str(compiled)
def read_dataframes_iter(
self,
table: str = "embeddings",
columns: list[str] | None = None,
limit: int | None = None,
where: str | None = None,
**filters: Unpack[EmbeddingsFilters],
) -> Iterator[pd.DataFrame]:
for record_batch in self.read_batches_iter(
table=table, columns=columns, limit=limit, where=where, **filters
):
yield record_batch.to_pandas()
def read_dataframe(
self,
table: str = "embeddings",
columns: list[str] | None = None,
limit: int | None = None,
where: str | None = None,
**filters: Unpack[EmbeddingsFilters],
) -> pd.DataFrame | None:
df_batches = [
record_batch.to_pandas()
for record_batch in self.read_batches_iter(
table=table,
columns=columns,
limit=limit,
where=where,
**filters,
)
]
if not df_batches:
return None
return pd.concat(df_batches)
def read_dicts_iter(
self,
table: str = "embeddings",
columns: list[str] | None = None,
limit: int | None = None,
where: str | None = None,
**filters: Unpack[EmbeddingsFilters],
) -> Iterator[dict]:
for record_batch in self.read_batches_iter(
table=table,
columns=columns,
limit=limit,
where=where,
**filters,
):
yield from record_batch.to_pylist()