33import os
44import uuid
55from pathlib import Path
6- from typing import Any , Optional
6+ from typing import Any , Callable , Optional
77
88from account .constants import Common
99from account .models import User
@@ -291,6 +291,7 @@ def index_document(
291291 document_id : str ,
292292 is_summary : bool = False ,
293293 run_id : str = None ,
294+ text_processor : Optional [type [Any ]] = None ,
294295 ) -> Any :
295296 """Method to index a document.
296297
@@ -340,7 +341,9 @@ def index_document(
340341 # Need to check the user who created profile manager
341342 # has access to adapters configured in profile manager
342343 PromptStudioHelper .validate_profile_manager_owner_access (default_profile )
343-
344+ process_text = None
345+ if text_processor :
346+ process_text = text_processor .process
344347 doc_id = PromptStudioHelper .dynamic_indexer (
345348 profile_manager = default_profile ,
346349 tool_id = tool_id ,
@@ -351,6 +354,7 @@ def index_document(
351354 reindex = True ,
352355 run_id = run_id ,
353356 user_id = user_id ,
357+ process_text = process_text ,
354358 )
355359
356360 logger .info (f"[{ tool_id } ] Indexing successful for doc: { file_name } " )
@@ -372,6 +376,7 @@ def prompt_responder(
372376 id : Optional [str ] = None ,
373377 run_id : str = None ,
374378 profile_manager_id : Optional [str ] = None ,
379+ text_processor : Optional [type [Any ]] = None ,
375380 ) -> Any :
376381 """Execute chain/single run of the prompts. Makes a call to prompt
377382 service and returns the dict of response.
@@ -398,19 +403,26 @@ def prompt_responder(
398403
399404 if id :
400405 return PromptStudioHelper ._execute_single_prompt (
401- id ,
402- doc_path ,
403- doc_name ,
404- tool_id ,
405- org_id ,
406- user_id ,
407- document_id ,
408- run_id ,
409- profile_manager_id ,
406+ id = id ,
407+ doc_path = doc_path ,
408+ doc_name = doc_name ,
409+ tool_id = tool_id ,
410+ org_id = org_id ,
411+ user_id = user_id ,
412+ document_id = document_id ,
413+ run_id = run_id ,
414+ profile_manager_id = profile_manager_id ,
415+ text_processor = text_processor ,
410416 )
411417 else :
412418 return PromptStudioHelper ._execute_prompts_in_single_pass (
413- doc_path , tool_id , org_id , user_id , document_id , run_id
419+ doc_path = doc_path ,
420+ tool_id = tool_id ,
421+ org_id = org_id ,
422+ user_id = user_id ,
423+ document_id = document_id ,
424+ run_id = run_id ,
425+ text_processor = text_processor ,
414426 )
415427
416428 @staticmethod
@@ -424,6 +436,7 @@ def _execute_single_prompt(
424436 document_id ,
425437 run_id ,
426438 profile_manager_id ,
439+ text_processor : Optional [type [Any ]] = None ,
427440 ):
428441 prompt_instance = PromptStudioHelper ._fetch_prompt_from_id (id )
429442 prompt_name = prompt_instance .prompt_key
@@ -458,7 +471,9 @@ def _execute_single_prompt(
458471 LogLevels .RUN ,
459472 "Invoking prompt service" ,
460473 )
461-
474+ process_text = None
475+ if text_processor :
476+ process_text = text_processor .process
462477 try :
463478 response = PromptStudioHelper ._fetch_response (
464479 doc_path = doc_path ,
@@ -470,9 +485,15 @@ def _execute_single_prompt(
470485 run_id = run_id ,
471486 profile_manager_id = profile_manager_id ,
472487 user_id = user_id ,
488+ process_text = process_text ,
473489 )
474490 return PromptStudioHelper ._handle_response (
475- response , run_id , prompts , document_id , False , profile_manager_id
491+ response = response ,
492+ run_id = run_id ,
493+ prompts = prompts ,
494+ document_id = document_id ,
495+ is_single_pass = False ,
496+ profile_manager_id = profile_manager_id ,
476497 )
477498 except Exception as e :
478499 logger .error (
@@ -495,7 +516,13 @@ def _execute_single_prompt(
495516
496517 @staticmethod
497518 def _execute_prompts_in_single_pass (
498- doc_path , tool_id , org_id , user_id , document_id , run_id
519+ doc_path ,
520+ tool_id ,
521+ org_id ,
522+ user_id ,
523+ document_id ,
524+ run_id ,
525+ text_processor : Optional [type [Any ]] = None ,
499526 ):
500527 prompts = PromptStudioHelper .fetch_prompt_from_tool (tool_id )
501528 prompts = [
@@ -513,7 +540,9 @@ def _execute_prompts_in_single_pass(
513540 LogLevels .RUN ,
514541 "Executing prompts in single pass" ,
515542 )
516-
543+ process_text = None
544+ if text_processor :
545+ process_text = text_processor .process
517546 try :
518547 tool = prompts [0 ].tool_id
519548 response = PromptStudioHelper ._fetch_single_pass_response (
@@ -524,9 +553,14 @@ def _execute_prompts_in_single_pass(
524553 document_id = document_id ,
525554 run_id = run_id ,
526555 user_id = user_id ,
556+ process_text = process_text ,
527557 )
528558 return PromptStudioHelper ._handle_response (
529- response , run_id , prompts , document_id , True
559+ response = response ,
560+ run_id = run_id ,
561+ prompts = prompts ,
562+ document_id = document_id ,
563+ is_single_pass = True ,
530564 )
531565 except Exception as e :
532566 logger .error (
@@ -556,7 +590,12 @@ def _get_document_path(org_id, user_id, tool_id, doc_name):
556590
557591 @staticmethod
558592 def _handle_response (
559- response , run_id , prompts , document_id , is_single_pass , profile_manager_id = None
593+ response ,
594+ run_id ,
595+ prompts ,
596+ document_id ,
597+ is_single_pass ,
598+ profile_manager_id = None ,
560599 ):
561600 if response .get ("status" ) == IndexingStatus .PENDING_STATUS .value :
562601 return {
@@ -586,6 +625,7 @@ def _fetch_response(
586625 run_id : str ,
587626 user_id : str ,
588627 profile_manager_id : Optional [str ] = None ,
628+ process_text : Optional [Callable [[str ], str ]] = None ,
589629 ) -> Any :
590630 """Utility function to invoke prompt service. Used internally.
591631
@@ -656,6 +696,7 @@ def _fetch_response(
656696 is_summary = tool .summarize_as_source ,
657697 run_id = run_id ,
658698 user_id = user_id ,
699+ process_text = process_text ,
659700 )
660701 if index_result .get ("status" ) == IndexingStatus .PENDING_STATUS .value :
661702 return {
@@ -713,6 +754,9 @@ def _fetch_response(
713754 tool_settings [TSPKeys .PREAMBLE ] = tool .preamble
714755 tool_settings [TSPKeys .POSTAMBLE ] = tool .postamble
715756 tool_settings [TSPKeys .GRAMMAR ] = grammar_list
757+ tool_settings [TSPKeys .PLATFORM_POSTAMBLE ] = getattr (
758+ settings , TSPKeys .PLATFORM_POSTAMBLE .upper (), ""
759+ )
716760
717761 tool_id = str (tool .tool_id )
718762
@@ -760,6 +804,7 @@ def dynamic_indexer(
760804 is_summary : bool = False ,
761805 reindex : bool = False ,
762806 run_id : str = None ,
807+ process_text : Optional [Callable [[str ], str ]] = None ,
763808 ) -> Any :
764809 """Used to index a file based on the passed arguments.
765810
@@ -842,6 +887,7 @@ def dynamic_indexer(
842887 reindex = reindex ,
843888 output_file_path = extract_file_path ,
844889 usage_kwargs = usage_kwargs .copy (),
890+ process_text = process_text ,
845891 )
846892
847893 PromptStudioIndexHelper .handle_index_manager (
@@ -875,6 +921,7 @@ def _fetch_single_pass_response(
875921 user_id : str ,
876922 document_id : str ,
877923 run_id : str = None ,
924+ process_text : Optional [Callable [[str ], str ]] = None ,
878925 ) -> Any :
879926 tool_id : str = str (tool .tool_id )
880927 outputs : list [dict [str , Any ]] = []
@@ -910,6 +957,7 @@ def _fetch_single_pass_response(
910957 document_id = document_id ,
911958 run_id = run_id ,
912959 user_id = user_id ,
960+ process_text = process_text ,
913961 )
914962 if index_result .get ("status" ) == IndexingStatus .PENDING_STATUS .value :
915963 return {
0 commit comments