(self, textToProcess: str, documentId: str, generateQuestions: bool, generateSummaries: bool)
| 140 | # Celery task for processing text |
| 141 | @celery_app.task(bind=True, rate_limit="1/m", name="celery_worker.process_text_task") |
| 142 | def process_text_task(self, textToProcess: str, documentId: str, generateQuestions: bool, generateSummaries: bool): |
| 143 | logging.info(f"Starting process for document {documentId}") |
| 144 | self.update_state(state=AppConfig.PROCESSING_DOCUMENT, meta={"documentId": documentId}) |
| 145 | |
| 146 | global active_tasks_count |
| 147 | |
| 148 | # Task Concurrency Management |
| 149 | with active_tasks_lock: |
| 150 | # Check if the maximum number of concurrent tasks has been reached |
| 151 | if active_tasks_count >= MAX_CONCURRENT_TASKS: |
| 152 | # Requeue or delay the task |
| 153 | raise self.retry(countdown=60) # Retry after 60 seconds |
| 154 | |
| 155 | # Increment the count of active tasks |
| 156 | active_tasks_count += 1 |
| 157 | |
| 158 | try: |
| 159 | doc = telegram.text_to_docs(textToProcess) |
| 160 | |
| 161 | # Setup splitters |
| 162 | parent_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=24) |
| 163 | child_splitter = TokenTextSplitter(chunk_size=100, chunk_overlap=24) |
| 164 | parent_documents = parent_splitter.split_documents(doc) |
| 165 | |
| 166 | # Setup embeddings |
| 167 | embeddings = OpenAIEmbeddings(openai_api_key=AppConfig.OPENAI_API_KEY, embedding_dimension=AppConfig.EMBEDDING_DIMENSION) |
| 168 | |
| 169 | # Iterate through parent and child chunks for document and generate structure |
| 170 | for i, parent in enumerate(parent_documents): |
| 171 | |
| 172 | self.update_state(state=AppConfig.PROCESSING_PAGES, meta={"page": i+1, "total_pages": len(parent_documents), "documentId": documentId}) |
| 173 | logging.info(f"processing chunk {i+1} of {len(parent_documents)} for document {documentId}") |
| 174 | |
| 175 | child_documents = child_splitter.split_documents([parent]) |
| 176 | params = { |
| 177 | "document_uuid": documentId, |
| 178 | "parent_uuid": str(uuid.uuid4()), |
| 179 | "name": f"Page {i+1}", |
| 180 | "parent_text": parent.page_content, |
| 181 | "parent_id": i, |
| 182 | "parent_embedding": embeddings.embed_query(parent.page_content), |
| 183 | "children": [ |
| 184 | { |
| 185 | "text": c.page_content, |
| 186 | "id": str(uuid.uuid4()), |
| 187 | "name": f"{i}-{ic+1}", |
| 188 | "embedding": embeddings.embed_query(c.page_content), |
| 189 | } |
| 190 | for ic, c in enumerate(child_documents) |
| 191 | ], |
| 192 | } |
| 193 | |
| 194 | try: |
| 195 | # Ingest data |
| 196 | with driver.session() as session : |
| 197 | session.run( |
| 198 | """ |
| 199 | MERGE (p:Page {uuid: $parent_uuid}) |
nothing calls this directly
no test coverage detected