Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-guard/src/lexigram/ai/guard/__init__.py: 53%

17 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""lexigram-ai-safety — Content safety and guardrails for Lexigram Framework. 

2 

3Canonical import paths 

4----------------------- 

5GuardPipeline: from lexigram.ai.guard import GuardPipeline 

6GuardConfig: from lexigram.ai.guard import GuardConfig 

7GuardModule: from lexigram.ai.guard import GuardModule 

8GuardProvider: from lexigram.ai.guard import GuardProvider 

9GuardCheckResult: from lexigram.ai.guard import GuardCheckResult 

10AggregateGuardResult: from lexigram.ai.guard import AggregateGuardResult 

11GuardAction: from lexigram.ai.guard import GuardAction 

12PromptInjectionDetector: from lexigram.ai.guard import PromptInjectionDetector 

13PIIDetector: from lexigram.ai.guard import PIIDetector 

14InputLengthGuard: from lexigram.ai.guard import InputLengthGuard 

15TopicRestrictor: from lexigram.ai.guard import TopicRestrictor 

16PIIRedactor: from lexigram.ai.guard import PIIRedactor 

17OutputLengthGuard: from lexigram.ai.guard import OutputLengthGuard 

18 

19Quick Start 

20----------- 

21 

22 from lexigram.ai.guard import GuardModule, GuardConfig 

23 from lexigram import LexigramApplication 

24 

25 app = LexigramApplication( 

26 modules=[ 

27 ..., 

28 GuardModule( 

29 config=GuardConfig( 

30 injection_detection=True, 

31 pii_action="redact", 

32 max_input_chars=8000, 

33 ) 

34 ), 

35 ], 

36 ) 

37 

38 # In a service, inject the pipeline and use it: 

39 from lexigram.ai.guard import GuardPipeline 

40 from lexigram.result import err 

41 from lexigram.di import inject 

42 

43 class MyLLMService: 

44 def __init__(self, guard: GuardPipeline) -> None: 

45 self._guard = guard 

46 

47 async def chat(self, user_message: str) -> str: 

48 result = await self._guard.check_input(user_message) 

49 if result.blocked: 

50 raise ValueError(f"Input blocked: {result.blocking_result}") 

51 safe_input = result.final_content or user_message 

52 llm_response = await self._llm.complete(safe_input) 

53 output = await self._guard.check_output(llm_response) 

54 return output.final_content or llm_response 

55""" 

56 

57from __future__ import annotations 

58 

59import importlib.metadata 

60import pkgutil 

61from typing import TYPE_CHECKING 

62 

63__path__ = pkgutil.extend_path(__path__, __name__) 

64 

65from lexigram.ai.guard.constants import __version__ as __version__ 

66 

67if TYPE_CHECKING: 

68 from lexigram.ai.guard.config import GuardConfig 

69 from lexigram.ai.guard.decorators import guarded 

70 from lexigram.ai.guard.di.provider import GuardProvider 

71 from lexigram.ai.guard.exceptions import ( 

72 GuardConfigurationError, 

73 GuardError, 

74 GuardPipelineError, 

75 ) 

76 from lexigram.ai.guard.hooks import ( 

77 GuardInputCheckedHook, 

78 GuardOutputCheckedHook, 

79 GuardPipelineCompletedHook, 

80 ) 

81 from lexigram.ai.guard.input.base import AbstractInputGuard 

82 from lexigram.ai.guard.input.injection import PromptInjectionDetector 

83 from lexigram.ai.guard.input.length import InputLengthGuard 

84 from lexigram.ai.guard.input.pii import PIIDetector 

85 from lexigram.ai.guard.input.topic import TopicRestrictor 

86 from lexigram.ai.guard.module import GuardModule 

87 from lexigram.ai.guard.output.base import AbstractOutputGuard 

88 from lexigram.ai.guard.output.length import OutputLengthGuard 

89 from lexigram.ai.guard.output.pii_redactor import PIIRedactor 

90 from lexigram.ai.guard.pipeline.guard_pipeline import GuardPipeline 

91 from lexigram.ai.guard.pipeline.result import ( 

92 AggregateGuardResult, 

93 GuardAction, 

94 GuardCheckResult, 

95 ) 

96 

97_LAZY_IMPORTS: dict[str, tuple[str, str]] = { 

98 "GuardInputCheckedHook": ("lexigram.ai.guard.hooks", "GuardInputCheckedHook"), 

99 "GuardOutputCheckedHook": ("lexigram.ai.guard.hooks", "GuardOutputCheckedHook"), 

100 "GuardPipelineCompletedHook": ( 

101 "lexigram.ai.guard.hooks", 

102 "GuardPipelineCompletedHook", 

103 ), 

104 "GuardConfig": ("lexigram.ai.guard.config", "GuardConfig"), 

105 "guarded": ("lexigram.ai.guard.decorators", "guarded"), 

106 "GuardProvider": ("lexigram.ai.guard.di.provider", "GuardProvider"), 

107 "GuardError": ("lexigram.ai.guard.exceptions", "GuardError"), 

108 "GuardConfigurationError": ( 

109 "lexigram.ai.guard.exceptions", 

110 "GuardConfigurationError", 

111 ), 

112 "GuardPipelineError": ("lexigram.ai.guard.exceptions", "GuardPipelineError"), 

113 "AbstractInputGuard": ("lexigram.ai.guard.input.base", "AbstractInputGuard"), 

114 "PromptInjectionDetector": ( 

115 "lexigram.ai.guard.input.injection", 

116 "PromptInjectionDetector", 

117 ), 

118 "InputLengthGuard": ("lexigram.ai.guard.input.length", "InputLengthGuard"), 

119 "PIIDetector": ("lexigram.ai.guard.input.pii", "PIIDetector"), 

120 "TopicRestrictor": ("lexigram.ai.guard.input.topic", "TopicRestrictor"), 

121 "GuardModule": ("lexigram.ai.guard.module", "GuardModule"), 

122 "AbstractOutputGuard": ("lexigram.ai.guard.output.base", "AbstractOutputGuard"), 

123 "OutputLengthGuard": ("lexigram.ai.guard.output.length", "OutputLengthGuard"), 

124 "PIIRedactor": ("lexigram.ai.guard.output.pii_redactor", "PIIRedactor"), 

125 "GuardPipeline": ("lexigram.ai.guard.pipeline.guard_pipeline", "GuardPipeline"), 

126 "AggregateGuardResult": ( 

127 "lexigram.ai.guard.pipeline.result", 

128 "AggregateGuardResult", 

129 ), 

130 "GuardAction": ("lexigram.ai.guard.pipeline.result", "GuardAction"), 

131 "GuardCheckResult": ("lexigram.ai.guard.pipeline.result", "GuardCheckResult"), 

132 # --- Protocols --- 

133 "GuardPipelineProtocol": ("lexigram.ai.guard.protocols", "GuardPipelineProtocol"), 

134 "GuardResultProtocol": ("lexigram.ai.guard.protocols", "GuardResultProtocol"), 

135 "InputGuardProtocol": ("lexigram.ai.guard.protocols", "InputGuardProtocol"), 

136 "OutputGuardProtocol": ("lexigram.ai.guard.protocols", "OutputGuardProtocol"), 

137 # --- Events --- 

138 "InputGuardTriggeredEvent": ( 

139 "lexigram.ai.guard.events", 

140 "InputGuardTriggeredEvent", 

141 ), 

142 "OutputGuardTriggeredEvent": ( 

143 "lexigram.ai.guard.events", 

144 "OutputGuardTriggeredEvent", 

145 ), 

146} 

147 

148 

149def __getattr__(name: str) -> object: 

150 """Lazily import public symbols on first access.""" 

151 if name in _LAZY_IMPORTS: 

152 import importlib 

153 

154 mod_path, attr = _LAZY_IMPORTS[name] 

155 module = importlib.import_module(mod_path) 

156 value = getattr(module, attr) 

157 globals()[name] = value 

158 return value 

159 raise AttributeError(f"module {__name__!r} has no attribute {name!r}") 

160 

161 

162__all__ = list(_LAZY_IMPORTS.keys())