__init__.py
1 from semantic_kernel.kernel import Kernel 2 from semantic_kernel.utils.telemetry.model_diagnostics import decorators 3 4 from mlflow.semantic_kernel.autolog import setup_semantic_kernel_tracing 5 from mlflow.semantic_kernel.tracing_utils import ( 6 patched_kernel_entry_point, 7 semantic_kernel_diagnostics_wrapper, 8 ) 9 from mlflow.telemetry.events import AutologgingEvent 10 from mlflow.telemetry.track import _record_event 11 from mlflow.utils.annotations import experimental as experimental 12 from mlflow.utils.autologging_utils import autologging_integration, safe_patch 13 14 FLAVOR_NAME = "semantic_kernel" 15 16 17 @autologging_integration(FLAVOR_NAME) 18 def autolog( 19 log_traces: bool = True, 20 disable: bool = False, 21 silent: bool = False, 22 ): 23 """ 24 Enables (or disables) and configures autologging from Semantic Kernel to MLflow. 25 Only synchronous calls are supported. Asynchnorous APIs and streaming are not recorded. 26 27 Args: 28 log_traces: If ``True``, traces are logged for Semantic Kernel. 29 If ``False``, no traces are collected during inference. Default to ``True``. 30 disable: If ``True``, disables the Semantic Kernel autologging. Default to ``False``. 31 silent: If ``True``, suppress all event logs and warnings from MLflow during Anthropic 32 autologging. If ``False``, show all events and warnings. 33 """ 34 35 setup_semantic_kernel_tracing() 36 37 # Create root spans for the kernel entry points. 38 for method in ["invoke", "invoke_prompt"]: 39 safe_patch( 40 FLAVOR_NAME, 41 Kernel, 42 method, 43 patched_kernel_entry_point, 44 ) 45 46 # NB: Semantic Kernel uses logging to serialize inputs/outputs. These parsers are used by their 47 # tracing decorators to log the inputs/outputs. These patches give coverage for many additional 48 # methods that are not covered by above entry point patches. 49 for method_name in [ 50 "_set_completion_input", 51 "_set_completion_response", 52 "_set_completion_error", 53 ]: 54 safe_patch( 55 FLAVOR_NAME, 56 decorators, 57 method_name, 58 semantic_kernel_diagnostics_wrapper, 59 ) 60 61 _record_event( 62 AutologgingEvent, {"flavor": FLAVOR_NAME, "log_traces": log_traces, "disable": disable} 63 )