Commit 525a505
🚰 feat: Stream Document Embeddings to Database in Batches (#214)
* Process documents using async pipeline with concurrent embedding generation and DB insertion.
This provides better memory efficiency by immediately inserting embeddings as they're generated.
* - mad maxsize of embedding-queue configurable with EMBEDIING_MAX_QUEUE_SIZE env var
* implemented fixes for copilot code review
* implemented fixes for copilot code review
- white space changes and remove unneeded import
* implemented fixes for copilot code review
- added documentation in function header for executor argument
* feat: improve streaming document embeddings with bug fixes and tests
This PR implements several critical fixes and improvements for the batch
embedding feature that streams document embeddings to reduce memory consumption.
## Critical Fixes
- Fix blocking async in _process_documents_batched_sync by wrapping sync
calls in run_in_executor()
- Unify rollback behavior: add `if all_ids:` guard to async path to only
rollback when documents were actually inserted
- Fix producer error handling: move `put(None)` to finally block to always
signal completion
- Add task_done() call for None signal in consumer
- Fix consumer error handling: put exception object in results_queue instead
of empty list, enabling proper error propagation
## Code Quality Improvements
- Remove dead `# temp_vector_store` comment
- Remove ineffective `del` statements (don't help with memory in Python)
- Fix all f-string logging to use %-style formatting
- Update docstrings to accurately describe function behavior
- Add type hints (AsyncPgVector, PgVector, ThreadPoolExecutor)
- Rename functions for clarity: embedding_producer → batch_producer,
database_consumer → embedding_consumer
- Add early return for empty document lists
## Tests (31 new tests)
- tests/test_batch_processing.py: 23 unit tests covering async pipeline,
sync batched processing, edge cases, and producer-consumer pattern
- tests/test_batch_processing_integration.py: 8 integration tests including
memory optimization verification with tracemalloc
## Documentation
- Add EMBEDDING_BATCH_SIZE and EMBEDDING_MAX_QUEUE_SIZE to README env vars
- Add "Embedding Batch Processing" section with configuration guide
- Add "Running Tests" section with commands and test categories
- Improve inline comments in config.py explaining batch processing trade-offs
- Add pytest.ini for test configuration
Closes #213
* feat: add utility function for batch calculation and update tests
This commit introduces a new utility function `calculate_num_batches` to streamline batch size calculations across the document processing routes. The function handles various edge cases, including zero items and batch sizes of zero. Additionally, existing tests have been updated to utilize this new function, ensuring accurate batch count calculations and improving test coverage for edge cases.
* refactor: update vector store mocking in tests to use AsyncPgVector
This commit modifies the test setup in `test_main.py` to replace the mocking of vector store methods with the `AsyncPgVector` class. The changes include updating the signatures of dummy functions to accept `self` and ensuring that all relevant methods are patched correctly. This enhances the accuracy of the tests by aligning them with the asynchronous implementation of the vector store.
* test: clear cache for embedding function in vector store tests
This commit updates the test setup in `test_main.py` to clear the cache of the `get_cached_query_embedding` function before running tests. This ensures that the mock embedding function is used consistently, improving the reliability of the tests. Additionally, the comment for overriding the embedding function has been clarified to indicate that it uses a dummy implementation that does not call OpenAI.
* test: enhance vector store tests with dummy embedding function
This commit updates the test setup in `test_main.py` to clear the cache of the `get_cached_query_embedding` function and replaces it with a dummy implementation that returns predefined embeddings. This change improves the reliability of the tests by ensuring consistent behavior during test execution.
---------
Co-authored-by: Marc Amick <[email protected]>
Co-authored-by: Danny Avila <[email protected]>1 parent 65c64ed commit 525a505
File tree
7 files changed
+1213
-26
lines changed- app
- routes
- tests
7 files changed
+1213
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| |||
95 | 97 | | |
96 | 98 | | |
97 | 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 | + | |
98 | 135 | | |
99 | 136 | | |
100 | 137 | | |
| |||
169 | 206 | | |
170 | 207 | | |
171 | 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 | + | |
172 | 284 | | |
173 | 285 | | |
174 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
73 | 89 | | |
74 | 90 | | |
75 | 91 | | |
| |||
0 commit comments