-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy path__init__.py
More file actions
230 lines (218 loc) · 8.67 KB
/
__init__.py
File metadata and controls
230 lines (218 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
LLM Module for PraisonAI Agents.
This module provides language model integrations with lazy loading
to minimize import time when LLM functionality is not immediately needed.
"""
import os
# Ensure litellm telemetry is disabled before any imports
os.environ["LITELLM_TELEMETRY"] = "False"
import threading
# Module-level cache for lazy-loaded classes
_lazy_cache = {}
_cache_lock = threading.Lock()
def __getattr__(name):
"""Lazy load LLM classes to avoid importing litellm at module load time."""
if name in _lazy_cache:
return _lazy_cache[name]
with _cache_lock:
# Double-check after acquiring lock
if name in _lazy_cache:
return _lazy_cache[name]
if name == "LLM":
from .llm import LLM
_lazy_cache[name] = LLM
return LLM
elif name == "LLMContextLengthExceededException":
from .llm import LLMContextLengthExceededException
_lazy_cache[name] = LLMContextLengthExceededException
return LLMContextLengthExceededException
elif name == "OpenAIClient":
from .openai_client import OpenAIClient
_lazy_cache[name] = OpenAIClient
return OpenAIClient
elif name == "get_openai_client":
from .openai_client import get_openai_client
_lazy_cache[name] = get_openai_client
return get_openai_client
elif name == "ChatCompletionMessage":
from .openai_client import ChatCompletionMessage
_lazy_cache[name] = ChatCompletionMessage
return ChatCompletionMessage
elif name == "Choice":
from .openai_client import Choice
_lazy_cache[name] = Choice
return Choice
elif name == "CompletionTokensDetails":
from .openai_client import CompletionTokensDetails
_lazy_cache[name] = CompletionTokensDetails
return CompletionTokensDetails
elif name == "PromptTokensDetails":
from .openai_client import PromptTokensDetails
_lazy_cache[name] = PromptTokensDetails
return PromptTokensDetails
elif name == "CompletionUsage":
from .openai_client import CompletionUsage
_lazy_cache[name] = CompletionUsage
return CompletionUsage
elif name == "ChatCompletion":
from .openai_client import ChatCompletion
_lazy_cache[name] = ChatCompletion
return ChatCompletion
elif name == "ToolCall":
from .openai_client import ToolCall
_lazy_cache[name] = ToolCall
return ToolCall
elif name == "process_stream_chunks":
from .openai_client import process_stream_chunks
_lazy_cache[name] = process_stream_chunks
return process_stream_chunks
elif name == "supports_structured_outputs":
from .model_capabilities import supports_structured_outputs
_lazy_cache[name] = supports_structured_outputs
return supports_structured_outputs
elif name == "supports_streaming_with_tools":
from .model_capabilities import supports_streaming_with_tools
_lazy_cache[name] = supports_streaming_with_tools
return supports_streaming_with_tools
elif name == "ModelRouter":
from .model_router import ModelRouter
_lazy_cache[name] = ModelRouter
return ModelRouter
elif name == "ModelProfile":
from .model_router import ModelProfile
_lazy_cache[name] = ModelProfile
return ModelProfile
elif name == "TaskComplexity":
from .model_router import TaskComplexity
_lazy_cache[name] = TaskComplexity
return TaskComplexity
elif name == "create_routing_agent":
from .model_router import create_routing_agent
_lazy_cache[name] = create_routing_agent
return create_routing_agent
elif name == "RateLimiter":
from .rate_limiter import RateLimiter
_lazy_cache[name] = RateLimiter
return RateLimiter
elif name == "TokenUsage":
from .llm import TokenUsage
_lazy_cache[name] = TokenUsage
return TokenUsage
elif name == "LLMProviderProtocol":
from .protocols import LLMProviderProtocol
_lazy_cache[name] = LLMProviderProtocol
return LLMProviderProtocol
elif name == "ModelCapabilitiesProtocol":
from .protocols import ModelCapabilitiesProtocol
_lazy_cache[name] = ModelCapabilitiesProtocol
return ModelCapabilitiesProtocol
elif name == "LLMRateLimiterProtocol":
from .protocols import LLMRateLimiterProtocol
_lazy_cache[name] = LLMRateLimiterProtocol
return LLMRateLimiterProtocol
elif name == "LLMFailoverProtocol":
from .protocols import LLMFailoverProtocol
_lazy_cache[name] = LLMFailoverProtocol
return LLMFailoverProtocol
elif name == "LLMProviderError":
from .protocols import LLMProviderError
_lazy_cache[name] = LLMProviderError
return LLMProviderError
elif name == "RateLimitError":
from .protocols import RateLimitError
_lazy_cache[name] = RateLimitError
return RateLimitError
elif name == "ModelNotAvailableError":
from .protocols import ModelNotAvailableError
_lazy_cache[name] = ModelNotAvailableError
return ModelNotAvailableError
elif name == "ContextLengthExceededError":
from .protocols import ContextLengthExceededError
_lazy_cache[name] = ContextLengthExceededError
return ContextLengthExceededError
elif name == "UnifiedLLMProtocol":
from .protocols import UnifiedLLMProtocol
_lazy_cache[name] = UnifiedLLMProtocol
return UnifiedLLMProtocol
elif name == "LiteLLMAdapter":
from .unified_adapters import LiteLLMAdapter
_lazy_cache[name] = LiteLLMAdapter
return LiteLLMAdapter
elif name == "OpenAIAdapter":
from .unified_adapters import OpenAIAdapter
_lazy_cache[name] = OpenAIAdapter
return OpenAIAdapter
elif name == "UnifiedLLMDispatcher":
from .unified_adapters import UnifiedLLMDispatcher
_lazy_cache[name] = UnifiedLLMDispatcher
return UnifiedLLMDispatcher
elif name == "create_llm_dispatcher":
from .unified_adapters import create_llm_dispatcher
_lazy_cache[name] = create_llm_dispatcher
return create_llm_dispatcher
elif name == "sanitize_messages":
from .sanitize import sanitize_messages
_lazy_cache[name] = sanitize_messages
return sanitize_messages
elif name == "strip_surrogates":
from .sanitize import strip_surrogates
_lazy_cache[name] = strip_surrogates
return strip_surrogates
elif name == "sanitize_text":
from .sanitize import sanitize_text
_lazy_cache[name] = sanitize_text
return sanitize_text
elif name == "ErrorCategory":
from .error_classifier import ErrorCategory
_lazy_cache[name] = ErrorCategory
return ErrorCategory
elif name == "classify_error":
from .error_classifier import classify_error
_lazy_cache[name] = classify_error
return classify_error
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = [
"LLM",
"LLMContextLengthExceededException",
"OpenAIClient",
"get_openai_client",
"ChatCompletionMessage",
"Choice",
"CompletionTokensDetails",
"PromptTokensDetails",
"CompletionUsage",
"ChatCompletion",
"ToolCall",
"process_stream_chunks",
"supports_structured_outputs",
"supports_streaming_with_tools",
"ModelRouter",
"ModelProfile",
"TaskComplexity",
"create_routing_agent",
"RateLimiter",
"TokenUsage",
# Protocols
"LLMProviderProtocol",
"ModelCapabilitiesProtocol",
"LLMRateLimiterProtocol",
"LLMFailoverProtocol",
"UnifiedLLMProtocol",
# Adapters
"LiteLLMAdapter",
"OpenAIAdapter",
"UnifiedLLMDispatcher",
"create_llm_dispatcher",
# Exceptions
"LLMProviderError",
"RateLimitError",
"ModelNotAvailableError",
"ContextLengthExceededError",
# Sanitization (G6)
"sanitize_messages",
"strip_surrogates",
"sanitize_text",
# Error Classification (G5)
"ErrorCategory",
"classify_error"
]