Repository navigation
Expand file tree
/
Copy pathcursor_pattern.py
More file actions
227 lines (179 loc) · 6.49 KB
/
Copy pathcursor_pattern.py
File metadata and controls
227 lines (179 loc) · 6.49 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
"""Runnable Cursor example for manual checkpoint control.
Use Cursor when a task needs to store custom checkpoint state, track a different
source table than its destination table, or coordinate multiple source tables.
"""
from __future__ import annotations
import json
import os
from dataclasses import dataclass
from pathlib import Path
import dataframely as dy
import polars as pl
import avalanche as ava
def example_root() -> Path:
configured = os.environ.get("AVALANCHE_EXAMPLE_ROOT")
root = (
Path(configured)
if configured
else Path(".avalanche/catalogs/cursor_workflow") / str(os.getpid())
)
root.mkdir(parents=True, exist_ok=True)
return root
EXAMPLE_ROOT = example_root()
class DocumentSchema(dy.Schema):
doc_id = dy.String(nullable=False)
title = dy.String(nullable=False)
body = dy.String(nullable=False)
class ChunkSchema(dy.Schema):
chunk_id = dy.String(nullable=False)
doc_id = dy.String(nullable=False)
chunk_number = dy.Int32(nullable=False)
text = dy.String(nullable=False)
class EmbeddingSchema(dy.Schema):
chunk_id = dy.String(nullable=False)
model = dy.String(nullable=False)
vector = dy.Binary(nullable=False)
@dataclass(frozen=True)
class PendingEmbeddings:
data: pl.DataFrame | None
chunk_count: int
source_snapshot_id: int
class ExampleNamespace(ava.IcebergNs):
ns_config = ava.IcebergNsConfig(
name="cursor_example",
base_location=str(EXAMPLE_ROOT / "warehouse"),
)
document = ava.IcebergTable(schema=DocumentSchema)
chunk = ava.IcebergTable(schema=ChunkSchema)
embedding = ava.IcebergTable(schema=EmbeddingSchema)
ns = ExampleNamespace(
catalog="cursor-example",
load_catalog_props={"type": "sql", "uri": f"sqlite:///{EXAMPLE_ROOT / 'catalog.db'}"},
)
@ava.source
def load_documents(*, docs=ns.document):
"""Append sample documents to the local catalog."""
ns.push()
rows = pl.DataFrame(
{
"doc_id": ["doc-10", "doc-20"],
"title": ["Manual cursor", "Coordination"],
"body": [
"Cursor stores checkpoints in table metadata.",
"Cursor can coordinate more than one table snapshot.",
],
}
)
return docs.append(rows)
@ava.step
def chunk_documents(
_loaded: object = None,
*,
cursor=ava.Cursor(ns.chunk, key="document_snapshot"),
source=ns.document,
dest=ns.chunk,
) -> str:
"""Chunk newly appended documents and advance the cursor."""
with cursor.tx() as tx:
last_snapshot = cursor.get()
doc_df = source.append_scan(start_snapshot_id=last_snapshot).to_polars()
current_snapshot = source.current_snapshot().snapshot_id
if doc_df.is_empty():
cursor.set(current_snapshot)
return "no documents to chunk"
chunks = process_documents_to_chunks(doc_df)
tx.append(chunks.to_arrow())
cursor.set(current_snapshot)
return f"chunked {len(doc_df)} documents into {len(chunks)} chunks"
@ava.step
def embed_chunks_per_model(
_chunked: object = None,
*,
model: str = "local_demo",
source=ns.chunk,
checkpoint=ava.Cursor(ns.embedding, key="embedding_models"),
) -> PendingEmbeddings:
"""Compute one model's embeddings without committing shared table state."""
last_snapshot = checkpoint.get()
chunk_df = source.append_scan(start_snapshot_id=last_snapshot).to_polars()
current_snapshot = source.current_snapshot().snapshot_id
embeddings = None if chunk_df.is_empty() else generate_embeddings(chunk_df, model=model)
return PendingEmbeddings(
data=embeddings,
chunk_count=len(chunk_df),
source_snapshot_id=current_snapshot,
)
@ava.step
def persist_embeddings(
local_batch: PendingEmbeddings,
backup_batch: PendingEmbeddings,
*,
cursor=ava.Cursor(ns.embedding, key="embedding_models"),
) -> str:
"""Commit parallel embedding results and their shared checkpoint once."""
if local_batch.source_snapshot_id != backup_batch.source_snapshot_id:
raise ValueError("Embedding branches read different chunk snapshots")
frames = [batch.data for batch in (local_batch, backup_batch) if batch.data is not None]
with cursor.tx() as tx:
if frames:
tx.append(pl.concat(frames).to_arrow())
cursor.set(local_batch.source_snapshot_id)
total_embeddings = sum(batch.chunk_count for batch in (local_batch, backup_batch))
return f"persisted {total_embeddings} embeddings"
@ava.dest
def sync_to_vector_db(
_persisted_embeddings: object = None,
*,
cursor=ava.Cursor(ns.embedding, key="vector_db_sync"),
chunks=ns.chunk,
embeddings=ns.embedding,
) -> str:
"""Sync chunk and embedding snapshots to the vector database."""
with cursor.transaction():
previous_state = cursor.get()
current_state = [
chunks.current_snapshot().snapshot_id,
embeddings.current_snapshot().snapshot_id,
]
if previous_state and json.loads(previous_state) == current_state:
return "vector db already synced"
chunk_count = len(chunks.read())
embedding_count = len(embeddings.read())
cursor.set(json.dumps(current_state))
return f"synced {chunk_count} chunks and {embedding_count} embeddings"
@ava.workflow
def cursor_workflow():
return (
load_documents()
>> chunk_documents()
>> (embed_chunks_per_model() & embed_chunks_per_model(model="backup_demo"))
>> persist_embeddings()
>> sync_to_vector_db()
)
def process_documents_to_chunks(doc_df: pl.DataFrame) -> pl.DataFrame:
rows: list[dict[str, object]] = []
for doc in doc_df.iter_rows(named=True):
rows.append(
{
"chunk_id": f"{doc['doc_id']}-chunk-1",
"doc_id": doc["doc_id"],
"chunk_number": 1,
"text": doc["body"],
}
)
return pl.DataFrame(rows)
def generate_embeddings(chunk_df: pl.DataFrame, *, model: str) -> pl.DataFrame:
return pl.DataFrame(
{
"chunk_id": chunk_df["chunk_id"],
"model": [model] * len(chunk_df),
"vector": [f"{model}:{chunk_id}".encode() for chunk_id in chunk_df["chunk_id"]],
}
)
def _main() -> None:
result = cursor_workflow().run(executor=ava.LocalExecutor()).result()
print("Cursor example complete")
print(result)
print(f"Artifacts: {EXAMPLE_ROOT}")
if __name__ == "__main__":
_main()