Example df generated by this function: | event_timestamp | document_id | chunk_id | chunk_text | embedding | created | |------------------+-------------+----------+------------------+-----------+------------------| | 2021-03-17 19:31 | doc_1 | chunk-1 | Hello
(
documents: Dict[str, str],
start_date: datetime,
end_date: datetime,
embedding_size: int = 60,
)
| 6 | |
| 7 | |
| 8 | def create_document_chunks_df( |
| 9 | documents: Dict[str, str], |
| 10 | start_date: datetime, |
| 11 | end_date: datetime, |
| 12 | embedding_size: int = 60, |
| 13 | ) -> pd.DataFrame: |
| 14 | """ |
| 15 | Example df generated by this function: |
| 16 | |
| 17 | | event_timestamp | document_id | chunk_id | chunk_text | embedding | created | |
| 18 | |------------------+-------------+----------+------------------+-----------+------------------| |
| 19 | | 2021-03-17 19:31 | doc_1 | chunk-1 | Hello world | [0.1, ...]| 2021-03-24 19:34 | |
| 20 | | 2021-03-17 19:31 | doc_1 | chunk-2 | How are you? | [0.2, ...]| 2021-03-24 19:34 | |
| 21 | | 2021-03-17 19:31 | doc_2 | chunk-1 | This is a test | [0.3, ...]| 2021-03-24 19:34 | |
| 22 | | 2021-03-17 19:31 | doc_2 | chunk-2 | Document chunk | [0.4, ...]| 2021-03-24 19:34 | |
| 23 | """ |
| 24 | df_hourly = pd.DataFrame( |
| 25 | { |
| 26 | "event_timestamp": [ |
| 27 | pd.Timestamp(dt, unit="ms").round("ms") |
| 28 | for dt in pd.date_range( |
| 29 | start=start_date, |
| 30 | end=end_date, |
| 31 | freq="1h", |
| 32 | inclusive="left", |
| 33 | tz="UTC", |
| 34 | ) |
| 35 | ] |
| 36 | + [ |
| 37 | pd.Timestamp( |
| 38 | year=2021, month=4, day=12, hour=7, minute=0, second=0, tz="UTC" |
| 39 | ) |
| 40 | ] |
| 41 | } |
| 42 | ) |
| 43 | df_all_chunks = pd.DataFrame() |
| 44 | |
| 45 | for doc_id, doc_text in documents.items(): |
| 46 | chunks = doc_text.split(". ") # Simple chunking by sentence |
| 47 | for chunk_id, chunk_text in enumerate(chunks, start=1): |
| 48 | df_hourly_copy = df_hourly.copy() |
| 49 | df_hourly_copy["document_id"] = doc_id |
| 50 | df_hourly_copy["chunk_id"] = f"chunk-{chunk_id}" |
| 51 | df_hourly_copy["chunk_text"] = chunk_text |
| 52 | df_all_chunks = pd.concat([df_hourly_copy, df_all_chunks]) |
| 53 | |
| 54 | df_all_chunks.reset_index(drop=True, inplace=True) |
| 55 | rows = df_all_chunks["event_timestamp"].count() |
| 56 | |
| 57 | # Generate random embeddings for each chunk |
| 58 | df_all_chunks["embedding"] = [ |
| 59 | np.random.rand(embedding_size).tolist() for _ in range(rows) |
| 60 | ] |
| 61 | df_all_chunks["created"] = pd.to_datetime(pd.Timestamp.now(tz=None).round("ms")) |
| 62 | |
| 63 | # Create duplicate rows that should be filtered by created timestamp |
| 64 | late_row = df_all_chunks[rows // 2 : rows // 2 + 1] |
| 65 | df_all_chunks = pd.concat([df_all_chunks, late_row, late_row], ignore_index=True) |
no test coverage detected