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/client.py
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
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
|
+
OpenAI-compatible client interface for OneLLM.
|
|
23
|
+
|
|
24
|
+
This module implements an interface that matches OpenAI's Python client structure,
|
|
25
|
+
making it a drop-in replacement for OpenAI's client with the same API structure.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from typing import Any, Dict, List, Optional, Union
|
|
29
|
+
|
|
30
|
+
from .chat_completion import ChatCompletion
|
|
31
|
+
from .completion import Completion
|
|
32
|
+
from .embedding import Embedding
|
|
33
|
+
from .files import File
|
|
34
|
+
from .image import Image
|
|
35
|
+
from .audio import AudioTranscription, AudioTranslation
|
|
36
|
+
from .speech import Speech
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ChatCompletionsResource:
|
|
40
|
+
"""Chat completions API resource"""
|
|
41
|
+
|
|
42
|
+
def create(
|
|
43
|
+
self,
|
|
44
|
+
model: str,
|
|
45
|
+
messages: List[Dict[str, Any]],
|
|
46
|
+
stream: bool = False,
|
|
47
|
+
fallback_models: Optional[List[str]] = None,
|
|
48
|
+
**kwargs
|
|
49
|
+
):
|
|
50
|
+
"""
|
|
51
|
+
Create a chat completion using ChatCompletion.create()
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
model: The model identifier to use for the completion
|
|
55
|
+
messages: A list of message dictionaries representing the conversation history
|
|
56
|
+
stream: Whether to stream the response or return it all at once
|
|
57
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
58
|
+
**kwargs: Additional parameters to pass to the model
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
A chat completion response object or a stream of completion chunks
|
|
62
|
+
"""
|
|
63
|
+
# Automatically add provider prefix if not present
|
|
64
|
+
# This allows users to specify just "gpt-4" instead of "openai/gpt-4"
|
|
65
|
+
if "/" not in model:
|
|
66
|
+
model = f"openai/{model}"
|
|
67
|
+
|
|
68
|
+
# Add provider prefix to fallback models if needed
|
|
69
|
+
# This ensures consistent model naming across primary and fallback models
|
|
70
|
+
if fallback_models:
|
|
71
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
72
|
+
if "/" not in fallback_model:
|
|
73
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
74
|
+
|
|
75
|
+
return ChatCompletion.create(
|
|
76
|
+
model=model,
|
|
77
|
+
messages=messages,
|
|
78
|
+
stream=stream,
|
|
79
|
+
fallback_models=fallback_models,
|
|
80
|
+
**kwargs
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
async def acreate(
|
|
84
|
+
self,
|
|
85
|
+
model: str,
|
|
86
|
+
messages: List[Dict[str, Any]],
|
|
87
|
+
stream: bool = False,
|
|
88
|
+
fallback_models: Optional[List[str]] = None,
|
|
89
|
+
**kwargs
|
|
90
|
+
):
|
|
91
|
+
"""
|
|
92
|
+
Create a chat completion asynchronously using ChatCompletion.acreate()
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
model: The model identifier to use for the completion
|
|
96
|
+
messages: A list of message dictionaries representing the conversation history
|
|
97
|
+
stream: Whether to stream the response or return it all at once
|
|
98
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
99
|
+
**kwargs: Additional parameters to pass to the model
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
A chat completion response object or an async generator of completion chunks
|
|
103
|
+
"""
|
|
104
|
+
# Automatically add provider prefix if not present
|
|
105
|
+
if "/" not in model:
|
|
106
|
+
model = f"openai/{model}"
|
|
107
|
+
|
|
108
|
+
# Add provider prefix to fallback models if needed
|
|
109
|
+
if fallback_models:
|
|
110
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
111
|
+
if "/" not in fallback_model:
|
|
112
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
113
|
+
|
|
114
|
+
return await ChatCompletion.acreate(
|
|
115
|
+
model=model,
|
|
116
|
+
messages=messages,
|
|
117
|
+
stream=stream,
|
|
118
|
+
fallback_models=fallback_models,
|
|
119
|
+
**kwargs
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class ChatResource:
|
|
124
|
+
"""Chat API resource"""
|
|
125
|
+
|
|
126
|
+
def __init__(self):
|
|
127
|
+
"""
|
|
128
|
+
Initialize the Chat resource with completions subresource
|
|
129
|
+
"""
|
|
130
|
+
self.completions = ChatCompletionsResource()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class CompletionsResource:
|
|
134
|
+
"""Completions API resource"""
|
|
135
|
+
|
|
136
|
+
def create(
|
|
137
|
+
self,
|
|
138
|
+
model: str,
|
|
139
|
+
prompt: str,
|
|
140
|
+
stream: bool = False,
|
|
141
|
+
fallback_models: Optional[List[str]] = None,
|
|
142
|
+
**kwargs
|
|
143
|
+
):
|
|
144
|
+
"""
|
|
145
|
+
Create a completion using Completion.create()
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
model: The model identifier to use for the completion
|
|
149
|
+
prompt: The text prompt to generate completions for
|
|
150
|
+
stream: Whether to stream the response or return it all at once
|
|
151
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
152
|
+
**kwargs: Additional parameters to pass to the model
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
A completion response object or a stream of completion chunks
|
|
156
|
+
"""
|
|
157
|
+
# Automatically add provider prefix if not present
|
|
158
|
+
if "/" not in model:
|
|
159
|
+
model = f"openai/{model}"
|
|
160
|
+
|
|
161
|
+
# Add provider prefix to fallback models if needed
|
|
162
|
+
if fallback_models:
|
|
163
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
164
|
+
if "/" not in fallback_model:
|
|
165
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
166
|
+
|
|
167
|
+
return Completion.create(
|
|
168
|
+
model=model,
|
|
169
|
+
prompt=prompt,
|
|
170
|
+
stream=stream,
|
|
171
|
+
fallback_models=fallback_models,
|
|
172
|
+
**kwargs
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
async def acreate(
|
|
176
|
+
self,
|
|
177
|
+
model: str,
|
|
178
|
+
prompt: str,
|
|
179
|
+
stream: bool = False,
|
|
180
|
+
fallback_models: Optional[List[str]] = None,
|
|
181
|
+
**kwargs
|
|
182
|
+
):
|
|
183
|
+
"""
|
|
184
|
+
Create a completion asynchronously using Completion.acreate()
|
|
185
|
+
|
|
186
|
+
Args:
|
|
187
|
+
model: The model identifier to use for the completion
|
|
188
|
+
prompt: The text prompt to generate completions for
|
|
189
|
+
stream: Whether to stream the response or return it all at once
|
|
190
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
191
|
+
**kwargs: Additional parameters to pass to the model
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
A completion response object or an async generator of completion chunks
|
|
195
|
+
"""
|
|
196
|
+
# Automatically add provider prefix if not present
|
|
197
|
+
if "/" not in model:
|
|
198
|
+
model = f"openai/{model}"
|
|
199
|
+
|
|
200
|
+
# Add provider prefix to fallback models if needed
|
|
201
|
+
if fallback_models:
|
|
202
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
203
|
+
if "/" not in fallback_model:
|
|
204
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
205
|
+
|
|
206
|
+
return await Completion.acreate(
|
|
207
|
+
model=model,
|
|
208
|
+
prompt=prompt,
|
|
209
|
+
stream=stream,
|
|
210
|
+
fallback_models=fallback_models,
|
|
211
|
+
**kwargs
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class EmbeddingsResource:
|
|
216
|
+
"""Embeddings API resource"""
|
|
217
|
+
|
|
218
|
+
def create(
|
|
219
|
+
self,
|
|
220
|
+
model: str,
|
|
221
|
+
input: Union[str, List[str]],
|
|
222
|
+
fallback_models: Optional[List[str]] = None,
|
|
223
|
+
**kwargs
|
|
224
|
+
):
|
|
225
|
+
"""
|
|
226
|
+
Create embeddings using Embedding.create()
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
model: The model identifier to use for generating embeddings
|
|
230
|
+
input: The text or list of texts to generate embeddings for
|
|
231
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
232
|
+
**kwargs: Additional parameters to pass to the model
|
|
233
|
+
|
|
234
|
+
Returns:
|
|
235
|
+
An embedding response object containing vector representations
|
|
236
|
+
"""
|
|
237
|
+
# Automatically add provider prefix if not present
|
|
238
|
+
if "/" not in model:
|
|
239
|
+
model = f"openai/{model}"
|
|
240
|
+
|
|
241
|
+
# Add provider prefix to fallback models if needed
|
|
242
|
+
if fallback_models:
|
|
243
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
244
|
+
if "/" not in fallback_model:
|
|
245
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
246
|
+
|
|
247
|
+
return Embedding.create(
|
|
248
|
+
model=model,
|
|
249
|
+
input=input,
|
|
250
|
+
fallback_models=fallback_models,
|
|
251
|
+
**kwargs
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
async def acreate(
|
|
255
|
+
self,
|
|
256
|
+
model: str,
|
|
257
|
+
input: Union[str, List[str]],
|
|
258
|
+
fallback_models: Optional[List[str]] = None,
|
|
259
|
+
**kwargs
|
|
260
|
+
):
|
|
261
|
+
"""
|
|
262
|
+
Create embeddings asynchronously using Embedding.acreate()
|
|
263
|
+
|
|
264
|
+
Args:
|
|
265
|
+
model: The model identifier to use for generating embeddings
|
|
266
|
+
input: The text or list of texts to generate embeddings for
|
|
267
|
+
fallback_models: Optional list of models to try if the primary model fails
|
|
268
|
+
**kwargs: Additional parameters to pass to the model
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
An embedding response object containing vector representations
|
|
272
|
+
"""
|
|
273
|
+
# Automatically add provider prefix if not present
|
|
274
|
+
if "/" not in model:
|
|
275
|
+
model = f"openai/{model}"
|
|
276
|
+
|
|
277
|
+
# Add provider prefix to fallback models if needed
|
|
278
|
+
if fallback_models:
|
|
279
|
+
for i, fallback_model in enumerate(fallback_models):
|
|
280
|
+
if "/" not in fallback_model:
|
|
281
|
+
fallback_models[i] = f"openai/{fallback_model}"
|
|
282
|
+
|
|
283
|
+
return await Embedding.acreate(
|
|
284
|
+
model=model,
|
|
285
|
+
input=input,
|
|
286
|
+
fallback_models=fallback_models,
|
|
287
|
+
**kwargs
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
class ImagesResource:
|
|
292
|
+
"""Images API resource"""
|
|
293
|
+
|
|
294
|
+
def create(
|
|
295
|
+
self,
|
|
296
|
+
model: str,
|
|
297
|
+
prompt: str,
|
|
298
|
+
**kwargs
|
|
299
|
+
):
|
|
300
|
+
"""
|
|
301
|
+
Create images using Image.create()
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
model: The model identifier to use for image generation
|
|
305
|
+
prompt: The text prompt describing the image to generate
|
|
306
|
+
**kwargs: Additional parameters like size, quality, style, etc.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
An image generation response object with URLs or base64 data
|
|
310
|
+
"""
|
|
311
|
+
# Automatically add provider prefix if not present
|
|
312
|
+
if "/" not in model:
|
|
313
|
+
model = f"openai/{model}"
|
|
314
|
+
return Image.create(model=model, prompt=prompt, **kwargs)
|
|
315
|
+
|
|
316
|
+
async def acreate(
|
|
317
|
+
self,
|
|
318
|
+
model: str,
|
|
319
|
+
prompt: str,
|
|
320
|
+
**kwargs
|
|
321
|
+
):
|
|
322
|
+
"""
|
|
323
|
+
Create images asynchronously using Image.create() with await
|
|
324
|
+
|
|
325
|
+
Args:
|
|
326
|
+
model: The model identifier to use for image generation
|
|
327
|
+
prompt: The text prompt describing the image to generate
|
|
328
|
+
**kwargs: Additional parameters like size, quality, style, etc.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
An image generation response object with URLs or base64 data
|
|
332
|
+
"""
|
|
333
|
+
# Automatically add provider prefix if not present
|
|
334
|
+
if "/" not in model:
|
|
335
|
+
model = f"openai/{model}"
|
|
336
|
+
# Use the same create method - it will return a coroutine when called from here
|
|
337
|
+
return await Image.create(model=model, prompt=prompt, **kwargs)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
class AudioResource:
|
|
341
|
+
"""Audio API resource"""
|
|
342
|
+
|
|
343
|
+
def __init__(self):
|
|
344
|
+
"""
|
|
345
|
+
Initialize the Audio resource with transcriptions and translations subresources
|
|
346
|
+
"""
|
|
347
|
+
self.transcriptions = AudioTranscriptionsResource()
|
|
348
|
+
self.translations = AudioTranslationsResource()
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class AudioTranscriptionsResource:
|
|
352
|
+
"""Audio transcriptions API resource"""
|
|
353
|
+
|
|
354
|
+
def create(
|
|
355
|
+
self,
|
|
356
|
+
model: str,
|
|
357
|
+
file: str,
|
|
358
|
+
**kwargs
|
|
359
|
+
):
|
|
360
|
+
"""
|
|
361
|
+
Create audio transcriptions using AudioTranscription.create()
|
|
362
|
+
|
|
363
|
+
Args:
|
|
364
|
+
model: The model identifier to use for transcription
|
|
365
|
+
file: Path to the audio file or file-like object to transcribe
|
|
366
|
+
**kwargs: Additional parameters like language, prompt, etc.
|
|
367
|
+
|
|
368
|
+
Returns:
|
|
369
|
+
A transcription response object containing the transcribed text
|
|
370
|
+
"""
|
|
371
|
+
# Automatically add provider prefix if not present
|
|
372
|
+
if "/" not in model:
|
|
373
|
+
model = f"openai/{model}"
|
|
374
|
+
return AudioTranscription.create(model=model, file=file, **kwargs)
|
|
375
|
+
|
|
376
|
+
async def acreate(
|
|
377
|
+
self,
|
|
378
|
+
model: str,
|
|
379
|
+
file: str,
|
|
380
|
+
**kwargs
|
|
381
|
+
):
|
|
382
|
+
"""
|
|
383
|
+
Create audio transcriptions asynchronously
|
|
384
|
+
|
|
385
|
+
Args:
|
|
386
|
+
model: The model identifier to use for transcription
|
|
387
|
+
file: Path to the audio file or file-like object to transcribe
|
|
388
|
+
**kwargs: Additional parameters like language, prompt, etc.
|
|
389
|
+
|
|
390
|
+
Returns:
|
|
391
|
+
A transcription response object containing the transcribed text
|
|
392
|
+
"""
|
|
393
|
+
# Automatically add provider prefix if not present
|
|
394
|
+
if "/" not in model:
|
|
395
|
+
model = f"openai/{model}"
|
|
396
|
+
# Use the same create method - it will return a coroutine when called from here
|
|
397
|
+
return await AudioTranscription.create(model=model, file=file, **kwargs)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
class AudioTranslationsResource:
|
|
401
|
+
"""Audio translations API resource"""
|
|
402
|
+
|
|
403
|
+
def create(
|
|
404
|
+
self,
|
|
405
|
+
model: str,
|
|
406
|
+
file: str,
|
|
407
|
+
**kwargs
|
|
408
|
+
):
|
|
409
|
+
"""
|
|
410
|
+
Create audio translations using AudioTranslation.create()
|
|
411
|
+
|
|
412
|
+
Args:
|
|
413
|
+
model: The model identifier to use for translation
|
|
414
|
+
file: Path to the audio file or file-like object to translate
|
|
415
|
+
**kwargs: Additional parameters like prompt, response_format, etc.
|
|
416
|
+
|
|
417
|
+
Returns:
|
|
418
|
+
A translation response object containing the translated text
|
|
419
|
+
"""
|
|
420
|
+
# Automatically add provider prefix if not present
|
|
421
|
+
if "/" not in model:
|
|
422
|
+
model = f"openai/{model}"
|
|
423
|
+
return AudioTranslation.create(model=model, file=file, **kwargs)
|
|
424
|
+
|
|
425
|
+
async def acreate(
|
|
426
|
+
self,
|
|
427
|
+
model: str,
|
|
428
|
+
file: str,
|
|
429
|
+
**kwargs
|
|
430
|
+
):
|
|
431
|
+
"""
|
|
432
|
+
Create audio translations asynchronously using AudioTranslation.create() with await
|
|
433
|
+
|
|
434
|
+
Args:
|
|
435
|
+
model: The model identifier to use for translation
|
|
436
|
+
file: Path to the audio file or file-like object to translate
|
|
437
|
+
**kwargs: Additional parameters like prompt, response_format, etc.
|
|
438
|
+
|
|
439
|
+
Returns:
|
|
440
|
+
A translation response object containing the translated text
|
|
441
|
+
"""
|
|
442
|
+
# Automatically add provider prefix if not present
|
|
443
|
+
if "/" not in model:
|
|
444
|
+
model = f"openai/{model}"
|
|
445
|
+
# Use the same create method - it will return a coroutine when called from here
|
|
446
|
+
return await AudioTranslation.create(model=model, file=file, **kwargs)
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
class SpeechResource:
|
|
450
|
+
"""Speech API resource"""
|
|
451
|
+
|
|
452
|
+
def create(
|
|
453
|
+
self,
|
|
454
|
+
model: str,
|
|
455
|
+
input: str,
|
|
456
|
+
voice: str,
|
|
457
|
+
**kwargs
|
|
458
|
+
):
|
|
459
|
+
"""
|
|
460
|
+
Create speech synthesis using Speech.create()
|
|
461
|
+
|
|
462
|
+
Args:
|
|
463
|
+
model: The model identifier to use for speech synthesis
|
|
464
|
+
input: The text to convert to speech
|
|
465
|
+
voice: The voice to use for the speech (e.g., "alloy", "echo", "fable")
|
|
466
|
+
**kwargs: Additional parameters like response_format, speed, etc.
|
|
467
|
+
|
|
468
|
+
Returns:
|
|
469
|
+
A speech response object containing the audio data
|
|
470
|
+
"""
|
|
471
|
+
# Automatically add provider prefix if not present
|
|
472
|
+
if "/" not in model:
|
|
473
|
+
model = f"openai/{model}"
|
|
474
|
+
return Speech.create(model=model, input=input, voice=voice, **kwargs)
|
|
475
|
+
|
|
476
|
+
async def acreate(
|
|
477
|
+
self,
|
|
478
|
+
model: str,
|
|
479
|
+
input: str,
|
|
480
|
+
voice: str,
|
|
481
|
+
**kwargs
|
|
482
|
+
):
|
|
483
|
+
"""
|
|
484
|
+
Create speech synthesis asynchronously using Speech.create() with await
|
|
485
|
+
|
|
486
|
+
Args:
|
|
487
|
+
model: The model identifier to use for speech synthesis
|
|
488
|
+
input: The text to convert to speech
|
|
489
|
+
voice: The voice to use for the speech (e.g., "alloy", "echo", "fable")
|
|
490
|
+
**kwargs: Additional parameters like response_format, speed, etc.
|
|
491
|
+
|
|
492
|
+
Returns:
|
|
493
|
+
A speech response object containing the audio data
|
|
494
|
+
"""
|
|
495
|
+
# Automatically add provider prefix if not present
|
|
496
|
+
if "/" not in model:
|
|
497
|
+
model = f"openai/{model}"
|
|
498
|
+
# Use the same create method - it will return a coroutine when called from here
|
|
499
|
+
return await Speech.create(model=model, input=input, voice=voice, **kwargs)
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
class FilesResource:
|
|
503
|
+
"""Files API resource"""
|
|
504
|
+
|
|
505
|
+
def create(
|
|
506
|
+
self,
|
|
507
|
+
file: str,
|
|
508
|
+
purpose: str,
|
|
509
|
+
**kwargs
|
|
510
|
+
):
|
|
511
|
+
"""
|
|
512
|
+
Create file using File.upload()
|
|
513
|
+
|
|
514
|
+
Args:
|
|
515
|
+
file: Path to the file to upload
|
|
516
|
+
purpose: The intended purpose of the file (e.g., "fine-tune", "assistants")
|
|
517
|
+
**kwargs: Additional parameters for the file upload
|
|
518
|
+
|
|
519
|
+
Returns:
|
|
520
|
+
A file object containing metadata about the uploaded file
|
|
521
|
+
"""
|
|
522
|
+
return File.upload(file=file, purpose=purpose, provider="openai", **kwargs)
|
|
523
|
+
|
|
524
|
+
async def acreate(
|
|
525
|
+
self,
|
|
526
|
+
file: str,
|
|
527
|
+
purpose: str,
|
|
528
|
+
**kwargs
|
|
529
|
+
):
|
|
530
|
+
"""
|
|
531
|
+
Create file asynchronously using File.aupload()
|
|
532
|
+
|
|
533
|
+
Args:
|
|
534
|
+
file: Path to the file to upload
|
|
535
|
+
purpose: The intended purpose of the file (e.g., "fine-tune", "assistants")
|
|
536
|
+
**kwargs: Additional parameters for the file upload
|
|
537
|
+
|
|
538
|
+
Returns:
|
|
539
|
+
A file object containing metadata about the uploaded file
|
|
540
|
+
"""
|
|
541
|
+
return await File.aupload(file=file, purpose=purpose, provider="openai", **kwargs)
|
|
542
|
+
|
|
543
|
+
def retrieve(self, file_id: str, **kwargs):
|
|
544
|
+
"""
|
|
545
|
+
Retrieve file using File.download()
|
|
546
|
+
|
|
547
|
+
Args:
|
|
548
|
+
file_id: The ID of the file to retrieve
|
|
549
|
+
**kwargs: Additional parameters for the file retrieval
|
|
550
|
+
|
|
551
|
+
Returns:
|
|
552
|
+
The content of the requested file
|
|
553
|
+
"""
|
|
554
|
+
return File.download(file_id=file_id, provider="openai", **kwargs)
|
|
555
|
+
|
|
556
|
+
async def aretrieve(self, file_id: str, **kwargs):
|
|
557
|
+
"""
|
|
558
|
+
Retrieve file asynchronously using File.adownload()
|
|
559
|
+
|
|
560
|
+
Args:
|
|
561
|
+
file_id: The ID of the file to retrieve
|
|
562
|
+
**kwargs: Additional parameters for the file retrieval
|
|
563
|
+
|
|
564
|
+
Returns:
|
|
565
|
+
The content of the requested file
|
|
566
|
+
"""
|
|
567
|
+
return await File.adownload(file_id=file_id, provider="openai", **kwargs)
|
|
568
|
+
|
|
569
|
+
def list(self, **kwargs):
|
|
570
|
+
"""
|
|
571
|
+
List files
|
|
572
|
+
|
|
573
|
+
Args:
|
|
574
|
+
**kwargs: Additional parameters for filtering the file list
|
|
575
|
+
|
|
576
|
+
Returns:
|
|
577
|
+
A list of file objects containing metadata about available files
|
|
578
|
+
"""
|
|
579
|
+
return File.list(provider="openai", **kwargs)
|
|
580
|
+
|
|
581
|
+
async def alist(self, **kwargs):
|
|
582
|
+
"""
|
|
583
|
+
List files asynchronously
|
|
584
|
+
|
|
585
|
+
Args:
|
|
586
|
+
**kwargs: Additional parameters for filtering the file list
|
|
587
|
+
|
|
588
|
+
Returns:
|
|
589
|
+
A list of file objects containing metadata about available files
|
|
590
|
+
"""
|
|
591
|
+
return await File.alist(provider="openai", **kwargs)
|
|
592
|
+
|
|
593
|
+
def delete(self, file_id: str, **kwargs):
|
|
594
|
+
"""
|
|
595
|
+
Delete file
|
|
596
|
+
|
|
597
|
+
Args:
|
|
598
|
+
file_id: The ID of the file to delete
|
|
599
|
+
**kwargs: Additional parameters for the file deletion
|
|
600
|
+
|
|
601
|
+
Returns:
|
|
602
|
+
A deletion status object
|
|
603
|
+
"""
|
|
604
|
+
return File.delete(file_id=file_id, provider="openai", **kwargs)
|
|
605
|
+
|
|
606
|
+
async def adelete(self, file_id: str, **kwargs):
|
|
607
|
+
"""
|
|
608
|
+
Delete file asynchronously
|
|
609
|
+
|
|
610
|
+
Args:
|
|
611
|
+
file_id: The ID of the file to delete
|
|
612
|
+
**kwargs: Additional parameters for the file deletion
|
|
613
|
+
|
|
614
|
+
Returns:
|
|
615
|
+
A deletion status object
|
|
616
|
+
"""
|
|
617
|
+
return await File.adelete(file_id=file_id, provider="openai", **kwargs)
|
|
618
|
+
|
|
619
|
+
def content(self, file_id: str, **kwargs):
|
|
620
|
+
"""
|
|
621
|
+
Get file content
|
|
622
|
+
|
|
623
|
+
Args:
|
|
624
|
+
file_id: The ID of the file to retrieve content for
|
|
625
|
+
**kwargs: Additional parameters for the content retrieval
|
|
626
|
+
|
|
627
|
+
Returns:
|
|
628
|
+
The content of the requested file
|
|
629
|
+
"""
|
|
630
|
+
return File.download(file_id=file_id, provider="openai", **kwargs)
|
|
631
|
+
|
|
632
|
+
async def acontent(self, file_id: str, **kwargs):
|
|
633
|
+
"""
|
|
634
|
+
Get file content asynchronously
|
|
635
|
+
|
|
636
|
+
Args:
|
|
637
|
+
file_id: The ID of the file to retrieve content for
|
|
638
|
+
**kwargs: Additional parameters for the content retrieval
|
|
639
|
+
|
|
640
|
+
Returns:
|
|
641
|
+
The content of the requested file
|
|
642
|
+
"""
|
|
643
|
+
return await File.adownload(file_id=file_id, provider="openai", **kwargs)
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
class Client:
|
|
647
|
+
"""
|
|
648
|
+
Base client class for OneLLM that mimics OpenAI's client interface.
|
|
649
|
+
This provides a drop-in replacement for OpenAI's client.
|
|
650
|
+
"""
|
|
651
|
+
|
|
652
|
+
def __init__(self, api_key: Optional[str] = None, **kwargs):
|
|
653
|
+
"""
|
|
654
|
+
Initialize client with API key and other options.
|
|
655
|
+
|
|
656
|
+
Args:
|
|
657
|
+
api_key: Optional API key to use for provider requests
|
|
658
|
+
**kwargs: Additional configuration options
|
|
659
|
+
"""
|
|
660
|
+
# Initialize all API resources that match OpenAI's client structure
|
|
661
|
+
self.chat = ChatResource()
|
|
662
|
+
self.completions = CompletionsResource()
|
|
663
|
+
self.embeddings = EmbeddingsResource()
|
|
664
|
+
self.images = ImagesResource()
|
|
665
|
+
self.audio = AudioResource()
|
|
666
|
+
self.speech = SpeechResource()
|
|
667
|
+
self.files = FilesResource()
|
|
668
|
+
|
|
669
|
+
# Store API key and other configuration (to be used by providers)
|
|
670
|
+
self.api_key = api_key
|
|
671
|
+
self.config = kwargs
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
# Alias for OpenAI = Client for backward compatibility
|
|
675
|
+
# This allows users to use either onellm.OpenAI or onellm.Client
|
|
676
|
+
OpenAI = Client
|