File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed
Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 99def detector_dispatcher (types = None ):
1010 """Decorator to dispatch to processing function based on type of the detector.
1111
12+ This decorator allows us to reuse same function name for different types of detectors.
13+ For example, the same function name can be used for text chat and context analysis
14+ detectors. These decorated functions for these detectors will have different arguments
15+ and implementation but they share the same function name.
16+
17+ NOTE: At the time of invoking these decorated function, the user needs to specify the type
18+ of the detector using fn_type argument.
19+
20+ CAUTION: Since this decorator allow re-use of the name, one must take care of using different types
21+ for testing different functions.
22+
1223 Args:
1324 types (list): Type of the detector this function applies to.
1425 args: Positional arguments passed to the processing function.
@@ -17,11 +28,12 @@ def detector_dispatcher(types=None):
1728 Examples
1829 --------
1930
20- @detector_dispatcher(types["foo"])
31+ @detector_dispatcher(types= ["foo"])
2132 def f(x):
2233 pass
2334
24- @detector_dispatcher(types["bar"])
35+ # Decorator can take multiple types as well
36+ @detector_dispatcher(types=["bar", "baz"])
2537 def f(x):
2638 pass
2739
Original file line number Diff line number Diff line change @@ -82,6 +82,8 @@ def apply_output_template(
8282
8383 ##### Chat request processing functions ####################################
8484
85+ # Usage of detector_dispatcher allows same function name to be called for different types of
86+ # detectors with different arguments and implementation.
8587 @detector_dispatcher (types = [DetectorType .TEXT_CHAT ])
8688 def apply_task_template (
8789 self , request : ChatDetectionRequest
Original file line number Diff line number Diff line change @@ -63,6 +63,8 @@ def __preprocess(
6363
6464 return request
6565
66+ # Decorating this function to make it cleaner for future iterations of this function
67+ # to support other types of detectors
6668 @detector_dispatcher (types = [DetectorType .TEXT_CONTEXT_DOC ])
6769 def _request_to_chat_completion_request (
6870 self , request : ContextAnalysisRequest , model_name : str
@@ -142,6 +144,8 @@ def _request_to_chat_completion_request(
142144
143145 ##### General request / response processing functions ##################
144146
147+ # Used detector_dispatcher decorator to allow for the same function to be called
148+ # for different types of detectors with different request types etc.
145149 @detector_dispatcher (types = [DetectorType .TEXT_CHAT ])
146150 def preprocess_request (
147151 self , request : ChatDetectionRequest
You can’t perform that action at this time.
0 commit comments