|
| 1 | +--- |
| 2 | +description: Rules for controller module and business logic |
| 3 | +globs: ["controller.py"] |
| 4 | +alwaysApply: true |
| 5 | +--- |
| 6 | + |
| 7 | +# Controllers Guidelines |
| 8 | + |
| 9 | +The controller module (`controller.py`) contains business logic for embedding operations and orchestrates interactions between routes, submodules, embedders, and external services. |
| 10 | + |
| 11 | +## Import Patterns |
| 12 | + |
| 13 | +```python |
| 14 | +# Submodules |
| 15 | +from submodules.model.business_objects import ( |
| 16 | + attribute, |
| 17 | + embedding, |
| 18 | + general, |
| 19 | + project, |
| 20 | + record, |
| 21 | + tokenization, |
| 22 | + notification, |
| 23 | + organization, |
| 24 | +) |
| 25 | +from submodules.model import enums, daemon |
| 26 | +from submodules.s3 import controller as s3 |
| 27 | + |
| 28 | +# Embedders |
| 29 | +from src.embedders import Transformer, util |
| 30 | +from src.embedders.classification.contextual import ( |
| 31 | + OpenAISentenceEmbedder, |
| 32 | + HuggingFaceSentenceEmbedder, |
| 33 | +) |
| 34 | +from src.util import request_util |
| 35 | +from src.util.decorator import param_throttle |
| 36 | +from src.util.embedders import get_embedder |
| 37 | +from src.util.notification import send_project_update |
| 38 | +``` |
| 39 | + |
| 40 | +## Function Patterns |
| 41 | + |
| 42 | +**Async embedding operations:** |
| 43 | +```python |
| 44 | +from submodules.model import daemon |
| 45 | +from fastapi import status |
| 46 | + |
| 47 | +def manage_encoding_thread(project_id: str, embedding_id: str) -> int: |
| 48 | + daemon.run_without_db_token(prepare_run, project_id, embedding_id) |
| 49 | + return status.HTTP_200_OK |
| 50 | +``` |
| 51 | + |
| 52 | +**Embedding lifecycle:** |
| 53 | +```python |
| 54 | +def delete_embedding(project_id: str, embedding_id: str) -> int: |
| 55 | + object_name = f"embedding_tensors_{embedding_id}.csv.bz2" |
| 56 | + org_id = organization.get_id_by_project_id(project_id) |
| 57 | + s3.delete_object(org_id, f"{project_id}/{object_name}") |
| 58 | + request_util.delete_embedding_from_neural_search(embedding_id) |
| 59 | + json_path = util.INFERENCE_DIR / project_id / f"embedder-{embedding_id}.json" |
| 60 | + json_path.unlink(missing_ok=True) |
| 61 | + return status.HTTP_200_OK |
| 62 | +``` |
| 63 | + |
| 64 | +**Embedding state management:** |
| 65 | +```python |
| 66 | +def run_encoding(project_id: str, user_id: str, embedding_id: str, ...) -> int: |
| 67 | + session_token = general.get_ctx_token() |
| 68 | + try: |
| 69 | + # Update embedding state |
| 70 | + embedding.update_embedding_state_encoding(project_id, embedding_id, with_commit=True) |
| 71 | + send_project_update(project_id, f"embedding:{embedding_id}:state:{enums.EmbeddingState.ENCODING.value}") |
| 72 | + |
| 73 | + # Process batches |
| 74 | + for pair in generate_batches(...): |
| 75 | + embedding.create_tensors(project_id, embedding_id, record_ids_batched, tensors, with_commit=True) |
| 76 | + send_progress_update_throttle(project_id, embedding_id, state, initial_count) |
| 77 | + |
| 78 | + # Finalize |
| 79 | + embedding.update_embedding_state_finished(project_id, embedding_id, with_commit=True) |
| 80 | + finally: |
| 81 | + general.remove_and_refresh_session(session_token) |
| 82 | + return status.HTTP_200_OK |
| 83 | +``` |
| 84 | + |
| 85 | +## Business Logic Patterns |
| 86 | + |
| 87 | +**Batch processing:** |
| 88 | +```python |
| 89 | +def generate_batches( |
| 90 | + project_id: str, |
| 91 | + record_ids: List[str], |
| 92 | + embedding_type: str, |
| 93 | + attribute_values_raw: List[str], |
| 94 | + embedder: Transformer, |
| 95 | + attribute_name: str, |
| 96 | + for_delta: bool = False, |
| 97 | +) -> Iterator[Dict[List[str], List[Any]]]: |
| 98 | + # Process records in batches using embedder.batch_size |
| 99 | + # Yield batches of record_ids and embeddings |
| 100 | + pass |
| 101 | +``` |
| 102 | + |
| 103 | +**Session management:** |
| 104 | +```python |
| 105 | +def prepare_run(project_id: str, embedding_id: str) -> None: |
| 106 | + session_token = general.get_ctx_token() |
| 107 | + try: |
| 108 | + t = __prepare_encoding(project_id, embedding_id) |
| 109 | + finally: |
| 110 | + general.remove_and_refresh_session(session_token) |
| 111 | + if t: |
| 112 | + run_encoding(*t) |
| 113 | +``` |
| 114 | + |
| 115 | +**Error handling with notifications:** |
| 116 | +```python |
| 117 | +try: |
| 118 | + # Embedding operation |
| 119 | + pass |
| 120 | +except Exception as e: |
| 121 | + embedding.update_embedding_state_failed(project_id, embedding_id, with_commit=True) |
| 122 | + send_project_update(project_id, f"embedding:{embedding_id}:state:{enums.EmbeddingState.FAILED.value}") |
| 123 | + notification.create( |
| 124 | + project_id, |
| 125 | + user_id, |
| 126 | + str(e), |
| 127 | + enums.Notification.ERROR.value, |
| 128 | + enums.NotificationType.EMBEDDING_CREATION_FAILED.value, |
| 129 | + True, |
| 130 | + ) |
| 131 | + return status.HTTP_500_INTERNAL_SERVER_ERROR |
| 132 | +``` |
| 133 | + |
| 134 | +**Throttled progress updates:** |
| 135 | +```python |
| 136 | +@param_throttle(seconds=5) |
| 137 | +def send_progress_update_throttle( |
| 138 | + project_id: str, embedding_id: str, state: str, initial_count: int |
| 139 | +) -> None: |
| 140 | + progress = resolve_progress(embedding_id, state, initial_count) |
| 141 | + send_project_update(project_id, f"embedding:{embedding_id}:progress:{progress}") |
| 142 | +``` |
| 143 | + |
| 144 | +## Best Practices |
| 145 | + |
| 146 | +1. Single responsibility per function |
| 147 | +2. Always validate inputs and check embedding existence |
| 148 | +3. Use type hints for all parameters |
| 149 | +4. Use `with_commit=True` when modifying database state |
| 150 | +5. Use submodule business objects, never SQLAlchemy directly |
| 151 | +6. Manage database sessions with `general.get_ctx_token()` and `general.remove_and_refresh_session()` |
| 152 | +7. Use `daemon.run_without_db_token()` for background operations |
| 153 | +8. Update embedding state and send project updates for progress tracking |
| 154 | +9. Clean up resources (delete embedders, call gc.collect()) after operations |
| 155 | +10. Handle errors gracefully with appropriate notifications and state updates |
0 commit comments