OneLLM 0.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onellm/.version +1 -0
- onellm/__init__.py +123 -0
- onellm/audio.py +218 -0
- onellm/chat_completion.py +398 -0
- onellm/client.py +676 -0
- onellm/completion.py +199 -0
- onellm/config.py +191 -0
- onellm/embedding.py +170 -0
- onellm/errors.py +250 -0
- onellm/files.py +318 -0
- onellm/image.py +207 -0
- onellm/models.py +359 -0
- onellm/providers/__init__.py +48 -0
- onellm/providers/base.py +279 -0
- onellm/providers/fallback.py +688 -0
- onellm/providers/openai.py +1231 -0
- onellm/speech.py +142 -0
- onellm/types/__init__.py +61 -0
- onellm/types/common.py +371 -0
- onellm/utils/__init__.py +46 -0
- onellm/utils/fallback.py +102 -0
- onellm/utils/retry.py +175 -0
- onellm/utils/streaming.py +357 -0
- onellm/utils/token_counter.py +238 -0
- onellm/validators.py +1314 -0
- onellm-0.0.6.dist-info/METADATA +832 -0
- onellm-0.0.6.dist-info/RECORD +30 -0
- onellm-0.0.6.dist-info/WHEEL +5 -0
- onellm-0.0.6.dist-info/licenses/LICENSE +201 -0
- onellm-0.0.6.dist-info/top_level.txt +1 -0
onellm/providers/base.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# Unified interface for LLM providers using OpenAI format
|
|
5
|
+
# https://github.com/muxi-ai/onellm
|
|
6
|
+
#
|
|
7
|
+
# Copyright (C) 2025 Ran Aroussi
|
|
8
|
+
#
|
|
9
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
# you may not use this file except in compliance with the License.
|
|
11
|
+
# You may obtain a copy of the License at
|
|
12
|
+
#
|
|
13
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
#
|
|
15
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
# See the License for the specific language governing permissions and
|
|
19
|
+
# limitations under the License.
|
|
20
|
+
|
|
21
|
+
"""
|
|
22
|
+
Base provider interface for OneLLM.
|
|
23
|
+
|
|
24
|
+
This module defines the abstract base class that all provider
|
|
25
|
+
implementations must follow, as well as utility functions for
|
|
26
|
+
working with providers.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from abc import ABC, abstractmethod
|
|
30
|
+
from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple, Type, Union
|
|
31
|
+
|
|
32
|
+
from ..models import (
|
|
33
|
+
ChatCompletionResponse,
|
|
34
|
+
ChatCompletionChunk,
|
|
35
|
+
CompletionResponse,
|
|
36
|
+
EmbeddingResponse,
|
|
37
|
+
FileObject,
|
|
38
|
+
)
|
|
39
|
+
from ..types import Message
|
|
40
|
+
from ..utils.fallback import FallbackConfig
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def parse_model_name(model: str) -> Tuple[str, str]:
|
|
44
|
+
"""
|
|
45
|
+
Parse a model name with a provider prefix.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
model: Model name with provider prefix (e.g., 'openai/gpt-4')
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
Tuple of (provider, model_name)
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
ValueError: If no provider prefix is found
|
|
55
|
+
"""
|
|
56
|
+
# Check if the model name contains a provider prefix (separated by '/')
|
|
57
|
+
if "/" in model:
|
|
58
|
+
# Split the model name into provider and model components
|
|
59
|
+
provider, model_name = model.split("/", 1)
|
|
60
|
+
return provider, model_name
|
|
61
|
+
else:
|
|
62
|
+
# Raise an error if the model name doesn't follow the expected format
|
|
63
|
+
raise ValueError(
|
|
64
|
+
f"Model name '{model}' does not contain a provider prefix. "
|
|
65
|
+
f"Use format 'provider/model-name' (e.g., 'openai/gpt-4')."
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class Provider(ABC):
|
|
70
|
+
"""
|
|
71
|
+
Base class for all LLM providers.
|
|
72
|
+
|
|
73
|
+
This abstract class defines the interface that all provider implementations
|
|
74
|
+
must implement. It includes capability flags that indicate what features
|
|
75
|
+
each provider supports, and abstract methods for core LLM operations.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
# Provider capability flags
|
|
79
|
+
json_mode_support = False # Whether the provider supports JSON mode output
|
|
80
|
+
|
|
81
|
+
# Multi-modal capabilities
|
|
82
|
+
vision_support = False # Image input support
|
|
83
|
+
audio_input_support = False # Audio input support
|
|
84
|
+
video_input_support = False # Video input support
|
|
85
|
+
|
|
86
|
+
# Streaming capabilities
|
|
87
|
+
streaming_support = False # Basic streaming support
|
|
88
|
+
token_by_token_support = False # Granular streaming
|
|
89
|
+
|
|
90
|
+
# Realtime capabilities
|
|
91
|
+
realtime_support = False # Realtime API support
|
|
92
|
+
|
|
93
|
+
@classmethod
|
|
94
|
+
def get_provider_name(cls) -> str:
|
|
95
|
+
"""
|
|
96
|
+
Get the name of the provider.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The provider name in lowercase, derived from the class name.
|
|
100
|
+
"""
|
|
101
|
+
# Extract provider name from class name by removing "Provider" suffix
|
|
102
|
+
return cls.__name__.replace("Provider", "").lower()
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
async def create_chat_completion(
|
|
106
|
+
self, messages: List[Message], model: str, stream: bool = False, **kwargs
|
|
107
|
+
) -> Union[ChatCompletionResponse, AsyncGenerator[ChatCompletionChunk, None]]:
|
|
108
|
+
"""
|
|
109
|
+
Create a chat completion.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
messages: List of messages in the conversation
|
|
113
|
+
model: Model name without provider prefix
|
|
114
|
+
stream: Whether to stream the response
|
|
115
|
+
**kwargs: Additional model parameters
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
ChatCompletionResponse or a generator yielding ChatCompletionChunk objects
|
|
119
|
+
"""
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
@abstractmethod
|
|
123
|
+
async def create_completion(
|
|
124
|
+
self, prompt: str, model: str, stream: bool = False, **kwargs
|
|
125
|
+
) -> Union[CompletionResponse, AsyncGenerator[Any, None]]:
|
|
126
|
+
"""
|
|
127
|
+
Create a text completion.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
prompt: Text prompt to complete
|
|
131
|
+
model: Model name without provider prefix
|
|
132
|
+
stream: Whether to stream the response
|
|
133
|
+
**kwargs: Additional model parameters
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
CompletionResponse or a generator yielding completion chunks
|
|
137
|
+
"""
|
|
138
|
+
pass
|
|
139
|
+
|
|
140
|
+
@abstractmethod
|
|
141
|
+
async def create_embedding(
|
|
142
|
+
self, input: Union[str, List[str]], model: str, **kwargs
|
|
143
|
+
) -> EmbeddingResponse:
|
|
144
|
+
"""
|
|
145
|
+
Create embeddings for the provided input.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
input: Text or list of texts to embed
|
|
149
|
+
model: Model name without provider prefix
|
|
150
|
+
**kwargs: Additional model parameters
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
EmbeddingResponse containing the generated embeddings
|
|
154
|
+
"""
|
|
155
|
+
pass
|
|
156
|
+
|
|
157
|
+
@abstractmethod
|
|
158
|
+
async def upload_file(self, file: Any, purpose: str, **kwargs) -> FileObject:
|
|
159
|
+
"""
|
|
160
|
+
Upload a file to the provider.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
file: File to upload (path, bytes, or file-like object)
|
|
164
|
+
purpose: Purpose of the file
|
|
165
|
+
**kwargs: Additional parameters
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
FileObject representing the uploaded file
|
|
169
|
+
"""
|
|
170
|
+
pass
|
|
171
|
+
|
|
172
|
+
@abstractmethod
|
|
173
|
+
async def download_file(self, file_id: str, **kwargs) -> bytes:
|
|
174
|
+
"""
|
|
175
|
+
Download a file from the provider.
|
|
176
|
+
|
|
177
|
+
Args:
|
|
178
|
+
file_id: ID of the file to download
|
|
179
|
+
**kwargs: Additional parameters
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
Bytes content of the file
|
|
183
|
+
"""
|
|
184
|
+
pass
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
# Registry of provider classes - stores mapping of provider names to their implementation classes
|
|
188
|
+
_PROVIDER_REGISTRY: Dict[str, Type[Provider]] = {}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def register_provider(provider_name: str, provider_class: Type[Provider]) -> None:
|
|
192
|
+
"""
|
|
193
|
+
Register a provider class.
|
|
194
|
+
|
|
195
|
+
This function adds a provider implementation to the registry, making it
|
|
196
|
+
available for use throughout the library.
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
provider_name: Name of the provider (lowercase)
|
|
200
|
+
provider_class: Provider class to register
|
|
201
|
+
"""
|
|
202
|
+
# Add the provider class to the registry with the given name as the key
|
|
203
|
+
_PROVIDER_REGISTRY[provider_name] = provider_class
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def get_provider(provider_name: str, **kwargs) -> Provider:
|
|
207
|
+
"""
|
|
208
|
+
Get a provider instance by name.
|
|
209
|
+
|
|
210
|
+
This function instantiates a provider class from the registry based on the
|
|
211
|
+
provided name.
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
provider_name: Name of the provider (lowercase)
|
|
215
|
+
**kwargs: Additional parameters to pass to the provider constructor
|
|
216
|
+
|
|
217
|
+
Returns:
|
|
218
|
+
Provider instance
|
|
219
|
+
|
|
220
|
+
Raises:
|
|
221
|
+
ValueError: If the provider is not supported
|
|
222
|
+
"""
|
|
223
|
+
# Look up the provider class in the registry
|
|
224
|
+
provider_class = _PROVIDER_REGISTRY.get(provider_name)
|
|
225
|
+
if provider_class is None:
|
|
226
|
+
# If provider not found, generate a helpful error message with available options
|
|
227
|
+
supported = ", ".join(_PROVIDER_REGISTRY.keys())
|
|
228
|
+
raise ValueError(
|
|
229
|
+
f"Provider '{provider_name}' is not supported. "
|
|
230
|
+
f"Supported providers: {supported}"
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# Instantiate and return the provider class with the provided kwargs
|
|
234
|
+
return provider_class(**kwargs)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def list_providers() -> List[str]:
|
|
238
|
+
"""
|
|
239
|
+
Get a list of registered provider names.
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
List of provider names available in the registry
|
|
243
|
+
"""
|
|
244
|
+
# Return the keys from the provider registry
|
|
245
|
+
return list(_PROVIDER_REGISTRY.keys())
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def get_provider_with_fallbacks(
|
|
249
|
+
primary_model: str,
|
|
250
|
+
fallback_models: Optional[List[str]] = None,
|
|
251
|
+
fallback_config: Optional[FallbackConfig] = None,
|
|
252
|
+
) -> Tuple[Provider, str]:
|
|
253
|
+
"""
|
|
254
|
+
Get a provider with fallback support.
|
|
255
|
+
|
|
256
|
+
This function creates either a standard provider or a fallback provider
|
|
257
|
+
that can try multiple models in sequence if the primary model fails.
|
|
258
|
+
|
|
259
|
+
Args:
|
|
260
|
+
primary_model: Primary model to use (in 'provider/model' format)
|
|
261
|
+
fallback_models: Optional list of fallback models (in 'provider/model' format)
|
|
262
|
+
fallback_config: Optional configuration for fallback behavior
|
|
263
|
+
|
|
264
|
+
Returns:
|
|
265
|
+
Tuple of (provider, model_name)
|
|
266
|
+
"""
|
|
267
|
+
# Parse primary model name to extract provider and model
|
|
268
|
+
provider_name, model_name = parse_model_name(primary_model)
|
|
269
|
+
|
|
270
|
+
# If no fallbacks specified, just return the normal provider
|
|
271
|
+
if not fallback_models:
|
|
272
|
+
return get_provider(provider_name), model_name
|
|
273
|
+
|
|
274
|
+
# Import here to avoid circular imports
|
|
275
|
+
from .fallback import FallbackProviderProxy
|
|
276
|
+
|
|
277
|
+
# Create a fallback provider with all models (primary + fallbacks)
|
|
278
|
+
all_models = [primary_model] + fallback_models
|
|
279
|
+
return FallbackProviderProxy(all_models, fallback_config), model_name
|