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
|
@@ -0,0 +1,832 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: onellm
|
|
3
|
+
Version: 0.0.6
|
|
4
|
+
Summary: A unified interface for interacting with large language models from various providers - a complete drop-in replacement for OpenAI's client
|
|
5
|
+
Home-page: https://github.com/muxi-ai/onellm
|
|
6
|
+
Author: Ran Aroussi
|
|
7
|
+
Author-email: Ran Aroussi <ran@aroussi.com>
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Project-URL: Homepage, https://github.com/muxi-ai/onellm
|
|
10
|
+
Project-URL: Documentation, https://docs.muxi.io
|
|
11
|
+
Project-URL: Issues, https://github.com/muxi-ai/onellm/issues
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Dynamic: author
|
|
23
|
+
Dynamic: home-page
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
Dynamic: requires-python
|
|
26
|
+
|
|
27
|
+
# OneLLM
|
|
28
|
+
|
|
29
|
+
[](https://pypi.python.org/pypi/onellm)
|
|
30
|
+
[](https://pypi.python.org/pypi/onellm)
|
|
31
|
+
[](https://pypi.python.org/pypi/onellm)
|
|
32
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
33
|
+
[](https://github.com/muxi-ai/onellm)
|
|
34
|
+
|
|
35
|
+
[](./CONTRIBUTING.md)
|
|
36
|
+
|
|
37
|
+
### A "drop-in replacement" for OpenAI's client that offers a unified interface for interacting with large language models from various providers, with support for hundreds of models, built-in fallback mechanisms, and enhanced reliability features.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
> [!TIP]
|
|
42
|
+
> #### Support this project by starring this repo on GitHub!
|
|
43
|
+
>
|
|
44
|
+
> More stars β more visibility β more contributors β better features β more robust tool for everyone π
|
|
45
|
+
>
|
|
46
|
+
> <a href="https://github.com/muxi-ai/onellm" target="_blank"><img src="https://img.shields.io/github/stars/ranaroussi/onellm.svg?style=social&label=Star&maxAge=60" alt="Star this repo"></a>
|
|
47
|
+
>
|
|
48
|
+
> Thank you for your support! π
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## π Table of Contents
|
|
53
|
+
|
|
54
|
+
- [Overview](https://github.com/muxi-ai/onellm/blob/main/README.md#-overview)
|
|
55
|
+
- [Getting Started](https://github.com/muxi-ai/onellm/blob/main/README.md#-getting-started)
|
|
56
|
+
- [Key Features](https://github.com/muxi-ai/onellm/blob/main/README.md#-key-features)
|
|
57
|
+
- [Supported Providers](https://github.com/muxi-ai/onellm/blob/main/README.md#-supported-providers)
|
|
58
|
+
- [Architecture](https://github.com/muxi-ai/onellm/blob/main/README.md#-architecture)
|
|
59
|
+
- [API Design](https://github.com/muxi-ai/onellm/blob/main/README.md#-api-design)
|
|
60
|
+
- [Advanced Features](https://github.com/muxi-ai/onellm/blob/main/README.md#-advanced-features)
|
|
61
|
+
- [Migration from OpenAI](https://github.com/muxi-ai/onellm/blob/main/README.md#-migration-from-openai)
|
|
62
|
+
- [Model Naming Convention](https://github.com/muxi-ai/onellm/blob/main/README.md#-model-naming-convention)
|
|
63
|
+
- [Configuration](https://github.com/muxi-ai/onellm/blob/main/README.md#-configuration)
|
|
64
|
+
- [Test Coverage](https://github.com/muxi-ai/onellm/blob/main/README.md#-test-coverage)
|
|
65
|
+
- [Documentation](https://github.com/muxi-ai/onellm/blob/main/README.md#-documentation)
|
|
66
|
+
- [Call for Contributions](https://github.com/muxi-ai/onellm/blob/main/README.md#-call-for-contributions)
|
|
67
|
+
- [License](https://github.com/muxi-ai/onellm/blob/main/README.md#-license)
|
|
68
|
+
- [Acknowledgements](https://github.com/muxi-ai/onellm/blob/main/README.md#-acknowledgements)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
## π Overview
|
|
72
|
+
|
|
73
|
+
**OneLLM** is a lightweight, provider-agnostic Python library that offers a unified interface for interacting with large language models (LLMs) from various providers. It simplifies the integration of LLMs into applications by providing a consistent API while abstracting away provider-specific implementation details.
|
|
74
|
+
|
|
75
|
+
The library follows the OpenAI client API design pattern, making it familiar to developers already using OpenAI and enabling easy migration for existing applications. **Simply change your import statements and instantly gain access to hundreds of models** across dozens of providers while maintaining your existing code structure.
|
|
76
|
+
|
|
77
|
+
With support for 25+ providers, OneLLM gives you access to approximately 300+ unique language models through a single, consistent interface - from the latest proprietary models to open-source alternatives, all accessible through familiar OpenAI-compatible patterns.
|
|
78
|
+
|
|
79
|
+
> [!NOTE]
|
|
80
|
+
> **Work in Progress**: OneLLM is currently under active development. The full functionality has been achieved using OpenAI, and I'm now working on adding support for more providers. [Contributions are welcome](./CONTRIBUTING.md) to help expand provider support!
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## π Getting Started
|
|
85
|
+
|
|
86
|
+
### Installation
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Basic installation (includes OpenAI compatibility)
|
|
90
|
+
pip install OneLLM
|
|
91
|
+
|
|
92
|
+
# For all providers (includes dependencies for future provider support)
|
|
93
|
+
pip install "OneLLM[all]"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Quick Win: Your First LLM Call
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# Basic usage with OpenAI-compatible syntax
|
|
100
|
+
from onellm import ChatCompletion
|
|
101
|
+
|
|
102
|
+
response = ChatCompletion.create(
|
|
103
|
+
model="openai/gpt-4o-mini",
|
|
104
|
+
messages=[
|
|
105
|
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
106
|
+
{"role": "user", "content": "Hello, how are you?"}
|
|
107
|
+
]
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
print(response.choices[0].message["content"])
|
|
111
|
+
# Output: I'm doing well, thank you for asking! I'm here and ready to help you...
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
For more detailed examples, check out the [examples directory](./examples).
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## β¨ Key Features
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
| Feature | Description |
|
|
122
|
+
|---------|-------------|
|
|
123
|
+
| **π¦ Drop-in replacement** | Use your existing OpenAI code with minimal changes |
|
|
124
|
+
| **π Provider-agnostic** | Support for 300+ models across 25+ LLM providers |
|
|
125
|
+
| **π Automatic fallback** | Seamlessly switch to alternative models when needed |
|
|
126
|
+
| **π Auto-retry mechanism** | Retry the same model multiple times before failing |
|
|
127
|
+
| **π§© OpenAI-compatible** | Familiar interface for developers used to OpenAI |
|
|
128
|
+
| **πΊ Streaming support** | Real-time streaming responses from supported providers |
|
|
129
|
+
| **πΌοΈ Multi-modal capabilities** | Support for text, images, audio across compatible models |
|
|
130
|
+
| **πΌοΈ Local LLM support** | Support for local LLMs via Ollama and llama.cpp |
|
|
131
|
+
| **π·οΈ Consistent naming** | Clear `provider/model-name` format for attribution |
|
|
132
|
+
| **π§ͺ Comprehensive tests** | 96% test coverage ensuring reliability |
|
|
133
|
+
| **π Apache-2.0 license** | Open-source license that protects contributions |
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## π Supported Providers
|
|
138
|
+
|
|
139
|
+
OneLLM supports models through three main approaches:
|
|
140
|
+
|
|
141
|
+
1. **Direct integration with provider APIs**
|
|
142
|
+
2. **OpenRouter connectivity for aggregated access**
|
|
143
|
+
3. **Local model support via Ollama and llama.cpp**
|
|
144
|
+
|
|
145
|
+
### Notable Models Available
|
|
146
|
+
|
|
147
|
+
Through these providers, you gain access to hundreds of models, including:
|
|
148
|
+
|
|
149
|
+
<div align="center">
|
|
150
|
+
|
|
151
|
+
<!-- Model categories -->
|
|
152
|
+
<table>
|
|
153
|
+
<tr>
|
|
154
|
+
<th>Model Family</th>
|
|
155
|
+
<th>Notable Models</th>
|
|
156
|
+
</tr>
|
|
157
|
+
<tr>
|
|
158
|
+
<td><strong>OpenAI Family</strong></td>
|
|
159
|
+
<td>GPT-4o, GPT-4 Turbo, o3</td>
|
|
160
|
+
</tr>
|
|
161
|
+
<tr>
|
|
162
|
+
<td><strong>Claude Family</strong></td>
|
|
163
|
+
<td>Claude 3 Opus, Claude 3 Sonnet, Claude 3 Haiku</td>
|
|
164
|
+
</tr>
|
|
165
|
+
<tr>
|
|
166
|
+
<td><strong>Llama Family</strong></td>
|
|
167
|
+
<td>Llama 3 70B, Llama 3 8B, Code Llama</td>
|
|
168
|
+
</tr>
|
|
169
|
+
<tr>
|
|
170
|
+
<td><strong>Mistral Family</strong></td>
|
|
171
|
+
<td>Mistral Large, Mistral 7B, Mixtral</td>
|
|
172
|
+
</tr>
|
|
173
|
+
<tr>
|
|
174
|
+
<td><strong>Gemini Family</strong></td>
|
|
175
|
+
<td>Gemini Pro, Gemini Ultra, Gemini Flash</td>
|
|
176
|
+
</tr>
|
|
177
|
+
<tr>
|
|
178
|
+
<td><strong>Embeddings</strong></td>
|
|
179
|
+
<td>Ada-002, text-embedding-3-small/large, Cohere embeddings</td>
|
|
180
|
+
</tr>
|
|
181
|
+
<tr>
|
|
182
|
+
<td><strong>Multimodal</strong></td>
|
|
183
|
+
<td>GPT-4 Vision, Claude 3 Vision, Gemini Pro Vision</td>
|
|
184
|
+
</tr>
|
|
185
|
+
</table>
|
|
186
|
+
|
|
187
|
+
</div>
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## ποΈ Architecture
|
|
192
|
+
|
|
193
|
+
OneLLM follows a modular architecture with clear separation of concerns:
|
|
194
|
+
|
|
195
|
+
```mermaid
|
|
196
|
+
---
|
|
197
|
+
config:
|
|
198
|
+
look: handDrawn
|
|
199
|
+
theme: mc
|
|
200
|
+
themeVariables:
|
|
201
|
+
background: 'transparent'
|
|
202
|
+
primaryColor: '#fff0'
|
|
203
|
+
secondaryColor: 'transparent'
|
|
204
|
+
tertiaryColor: 'transparent'
|
|
205
|
+
mainBkg: 'transparent'
|
|
206
|
+
|
|
207
|
+
flowchart:
|
|
208
|
+
layout: fixed
|
|
209
|
+
---
|
|
210
|
+
flowchart TD
|
|
211
|
+
%% User API Layer
|
|
212
|
+
User(User Application) --> ChatCompletion
|
|
213
|
+
User --> Completion
|
|
214
|
+
User --> Embedding
|
|
215
|
+
User --> OpenAIClient["OpenAI Client Interface"]
|
|
216
|
+
|
|
217
|
+
subgraph API["Public API Layer"]
|
|
218
|
+
ChatCompletion["ChatCompletion\n.create() / .acreate()"]
|
|
219
|
+
Completion["Completion\n.create() / .acreate()"]
|
|
220
|
+
Embedding["Embedding\n.create() / .acreate()"]
|
|
221
|
+
OpenAIClient
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
%% Core logic
|
|
225
|
+
subgraph Core["Core Logic"]
|
|
226
|
+
Router["Provider Router"]
|
|
227
|
+
Config["Configuration\nEnvironment Variables\nAPI Keys"]
|
|
228
|
+
FallbackManager["Fallback Manager"]
|
|
229
|
+
RetryManager["Retry Manager"]
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
%% Provider Layer
|
|
233
|
+
BaseProvider["Provider Interface<br>(Base Class)"]
|
|
234
|
+
|
|
235
|
+
subgraph Implementations["Provider Implementations"]
|
|
236
|
+
OpenAI["OpenAI"]
|
|
237
|
+
Anthropic["Anthropic"]
|
|
238
|
+
GoogleProvider["Google"]
|
|
239
|
+
Groq["Groq"]
|
|
240
|
+
Ollama["Local LLMs"]
|
|
241
|
+
OtherProviders["20+ Others"]
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
%% Utilities
|
|
245
|
+
subgraph Utilities["Utilities"]
|
|
246
|
+
Streaming["Streaming<br>Handlers"]
|
|
247
|
+
TokenCounting["Token<br>Counter"]
|
|
248
|
+
ErrorHandling["Error<br>Handling"]
|
|
249
|
+
Types["Type<br>Definitions"]
|
|
250
|
+
Models["Response<br>Models"]
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
%% External services
|
|
254
|
+
OpenAIAPI["OpenAI API"]
|
|
255
|
+
AnthropicAPI["Anthropic API"]
|
|
256
|
+
GoogleAPI["Google API"]
|
|
257
|
+
GroqAPI["Groq API"]
|
|
258
|
+
LocalModels["Ollama/llama.cpp"]
|
|
259
|
+
OtherAPIs["..."]
|
|
260
|
+
|
|
261
|
+
%% Connections
|
|
262
|
+
ChatCompletion --> Router
|
|
263
|
+
Completion --> Router
|
|
264
|
+
Embedding --> Router
|
|
265
|
+
OpenAIClient --> Router
|
|
266
|
+
|
|
267
|
+
Router --> Config
|
|
268
|
+
Router --> FallbackManager
|
|
269
|
+
FallbackManager --> RetryManager
|
|
270
|
+
|
|
271
|
+
RetryManager --> BaseProvider
|
|
272
|
+
|
|
273
|
+
BaseProvider --> OpenAI
|
|
274
|
+
BaseProvider --> Anthropic
|
|
275
|
+
BaseProvider --> GoogleProvider
|
|
276
|
+
BaseProvider --> Groq
|
|
277
|
+
BaseProvider --> Ollama
|
|
278
|
+
BaseProvider --> OtherProviders
|
|
279
|
+
|
|
280
|
+
BaseProvider --> Streaming
|
|
281
|
+
BaseProvider --> TokenCounting
|
|
282
|
+
BaseProvider --> ErrorHandling
|
|
283
|
+
|
|
284
|
+
OpenAI --> OpenAIAPI
|
|
285
|
+
Anthropic --> AnthropicAPI
|
|
286
|
+
GoogleProvider --> GoogleAPI
|
|
287
|
+
Groq --> GroqAPI
|
|
288
|
+
Ollama --> LocalModels
|
|
289
|
+
OtherProviders --> OtherAPIs
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
### Core Components
|
|
293
|
+
|
|
294
|
+
1. **Public Interface Classes**
|
|
295
|
+
- `ChatCompletion` - For chat-based interactions
|
|
296
|
+
- `Completion` - For text completion
|
|
297
|
+
- `Embedding` - For generating embeddings
|
|
298
|
+
|
|
299
|
+
2. **Provider Interface**
|
|
300
|
+
- Abstract base class defining required methods
|
|
301
|
+
- Provider-specific implementations
|
|
302
|
+
|
|
303
|
+
3. **Configuration System**
|
|
304
|
+
- Environment variable support
|
|
305
|
+
- Runtime configuration options
|
|
306
|
+
|
|
307
|
+
4. **Error Handling**
|
|
308
|
+
- Standardized error types
|
|
309
|
+
- Provider-specific error mapping
|
|
310
|
+
|
|
311
|
+
5. **Fallback System**
|
|
312
|
+
- Automatic retries with alternative models
|
|
313
|
+
- Configurable fallback chains
|
|
314
|
+
- Graceful degradation options
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## π API Design
|
|
319
|
+
|
|
320
|
+
OneLLM mirrors the OpenAI Python client library API for familiarity:
|
|
321
|
+
|
|
322
|
+
### Chat Completions
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
from onellm import ChatCompletion
|
|
326
|
+
|
|
327
|
+
# Basic usage (identical to OpenAI's client)
|
|
328
|
+
response = ChatCompletion.create(
|
|
329
|
+
model="openai/gpt-4",
|
|
330
|
+
messages=[
|
|
331
|
+
{"role": "system", "content": "You are a helpful assistant."},
|
|
332
|
+
{"role": "user", "content": "Hello, how are you?"}
|
|
333
|
+
]
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
# Streaming
|
|
337
|
+
for chunk in ChatCompletion.create(
|
|
338
|
+
model="anthropic/claude-3-opus",
|
|
339
|
+
messages=[{"role": "user", "content": "Write a poem about AI"}],
|
|
340
|
+
stream=True
|
|
341
|
+
):
|
|
342
|
+
print(chunk.choices[0].delta.content, end="", flush=True)
|
|
343
|
+
|
|
344
|
+
# With fallback options
|
|
345
|
+
response = ChatCompletion.create(
|
|
346
|
+
model=["openai/gpt-4", "anthropic/claude-3-opus", "mistral/mistral-large"],
|
|
347
|
+
messages=[{"role": "user", "content": "Explain quantum computing"}]
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
# With retries and fallbacks for enhanced reliability
|
|
351
|
+
response = ChatCompletion.create(
|
|
352
|
+
model="openai/gpt-4",
|
|
353
|
+
messages=[{"role": "user", "content": "Explain quantum computing"}],
|
|
354
|
+
retries=3, # Try gpt-4 up to 3 additional times before failing or using fallbacks
|
|
355
|
+
fallback_models=["anthropic/claude-3-opus", "mistral/mistral-large"]
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
# JSON mode for structured outputs
|
|
359
|
+
response = ChatCompletion.create(
|
|
360
|
+
model="openai/gpt-4o",
|
|
361
|
+
messages=[
|
|
362
|
+
{"role": "user", "content": "List the top 3 planets by size"}
|
|
363
|
+
],
|
|
364
|
+
response_format={"type": "json_object"} # Request structured JSON output
|
|
365
|
+
)
|
|
366
|
+
print(response.choices[0].message["content"]) # Outputs valid, parseable JSON
|
|
367
|
+
|
|
368
|
+
# Multi-modal with images
|
|
369
|
+
response = ChatCompletion.create(
|
|
370
|
+
model="openai/gpt-4-vision",
|
|
371
|
+
messages=[
|
|
372
|
+
{"role": "user", "content": [
|
|
373
|
+
{"type": "text", "text": "What's in this image?"},
|
|
374
|
+
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
|
|
375
|
+
]}
|
|
376
|
+
]
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
# Multi-modal with audio
|
|
380
|
+
response = ChatCompletion.create(
|
|
381
|
+
model="anthropic/claude-3-sonnet",
|
|
382
|
+
messages=[
|
|
383
|
+
{"role": "user", "content": [
|
|
384
|
+
{"type": "text", "text": "Transcribe and analyze this audio clip"},
|
|
385
|
+
{"type": "audio_url", "audio_url": {"url": "https://example.com/audio.mp3"}}
|
|
386
|
+
]}
|
|
387
|
+
]
|
|
388
|
+
)
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### Completions
|
|
392
|
+
|
|
393
|
+
```python
|
|
394
|
+
from onellm import Completion
|
|
395
|
+
|
|
396
|
+
response = Completion.create(
|
|
397
|
+
model="groq/llama3-70b",
|
|
398
|
+
prompt="Once upon a time",
|
|
399
|
+
max_tokens=100
|
|
400
|
+
)
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### Embeddings
|
|
404
|
+
|
|
405
|
+
```python
|
|
406
|
+
from onellm import Embedding
|
|
407
|
+
|
|
408
|
+
response = Embedding.create(
|
|
409
|
+
model="openai/text-embedding-3-small",
|
|
410
|
+
input="The quick brown fox jumps over the lazy dog"
|
|
411
|
+
)
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## π οΈ Advanced Features
|
|
417
|
+
|
|
418
|
+
### Fallback Chains for Enhanced Reliability
|
|
419
|
+
|
|
420
|
+
OneLLM includes built-in fallback support to handle API errors gracefully:
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
response = ChatCompletion.create(
|
|
424
|
+
model="openai/gpt-4",
|
|
425
|
+
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
426
|
+
fallback_models=[
|
|
427
|
+
"anthropic/claude-3-haiku", # Try Claude if GPT-4 fails
|
|
428
|
+
"openai/gpt-3.5-turbo" # Try GPT-3.5 if Claude fails
|
|
429
|
+
]
|
|
430
|
+
)
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
If the primary model fails due to service unavailability, rate limiting, or other retriable errors, OneLLM automatically tries the fallback models in sequence.
|
|
434
|
+
|
|
435
|
+
### Automatic Retries
|
|
436
|
+
|
|
437
|
+
For transient errors, you can configure OneLLM to retry the same model multiple times before falling back to alternatives:
|
|
438
|
+
|
|
439
|
+
```python
|
|
440
|
+
response = ChatCompletion.create(
|
|
441
|
+
model="openai/gpt-4",
|
|
442
|
+
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
443
|
+
retries=3, # Will try the same model up to 3 additional times if it fails
|
|
444
|
+
fallback_models=["anthropic/claude-3-haiku", "openai/gpt-3.5-turbo"]
|
|
445
|
+
)
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
This is implemented using the fallback mechanism under the hood, making it both powerful and efficient.
|
|
449
|
+
|
|
450
|
+
### Fallback Chains + Automatic Retries Architecture
|
|
451
|
+
|
|
452
|
+
```mermaid
|
|
453
|
+
---
|
|
454
|
+
config:
|
|
455
|
+
look: handDrawn
|
|
456
|
+
theme: mc
|
|
457
|
+
themeVariables:
|
|
458
|
+
background: 'transparent'
|
|
459
|
+
primaryColor: '#fff0'
|
|
460
|
+
secondaryColor: 'transparent'
|
|
461
|
+
tertiaryColor: 'transparent'
|
|
462
|
+
mainBkg: 'transparent'
|
|
463
|
+
|
|
464
|
+
flowchart:
|
|
465
|
+
layout: fixed
|
|
466
|
+
---
|
|
467
|
+
flowchart TD
|
|
468
|
+
START(["Client Request"]) --> REQUEST["Chat/Completion Request"]
|
|
469
|
+
REQUEST --> PRIMARY["Primary Model<br>e.g., openai/gpt-4"]
|
|
470
|
+
|
|
471
|
+
PRIMARY --> API_CHECK{"API<br>Available?"}
|
|
472
|
+
API_CHECK -->|Yes| MODEL_CHECK{"Model<br>Available?"}
|
|
473
|
+
MODEL_CHECK -->|Yes| QUOTA_CHECK{"Quota/Rate<br>Limits OK?"}
|
|
474
|
+
QUOTA_CHECK -->|Yes| SUCCESS["Successful Response"]
|
|
475
|
+
SUCCESS --> RESPONSE(["Return to Client"])
|
|
476
|
+
|
|
477
|
+
API_CHECK -->|No| RETRY_DECISION{"Retry<br>Count < Max?"}
|
|
478
|
+
MODEL_CHECK -->|No| RETRY_DECISION
|
|
479
|
+
QUOTA_CHECK -->|No| RETRY_DECISION
|
|
480
|
+
|
|
481
|
+
RETRY_DECISION -->|Yes| RETRY["Retry with Delay<br>(Same Model)"]
|
|
482
|
+
RETRY --> PRIMARY
|
|
483
|
+
|
|
484
|
+
RETRY_DECISION -->|No| FALLBACK_CHECK{"Fallbacks<br>Available?"}
|
|
485
|
+
|
|
486
|
+
FALLBACK_CHECK -->|Yes| FALLBACK_MODEL["Next Fallback Model<br>e.g., anthropic/claude-3-haiku"]
|
|
487
|
+
FALLBACK_MODEL --> FALLBACK_TRY["Try Fallback"]
|
|
488
|
+
FALLBACK_TRY --> FALLBACK_API_CHECK{"API<br>Available?"}
|
|
489
|
+
|
|
490
|
+
FALLBACK_API_CHECK -->|Yes| FALLBACK_SUCCESS["Successful Response"]
|
|
491
|
+
FALLBACK_SUCCESS --> RESPONSE
|
|
492
|
+
|
|
493
|
+
FALLBACK_API_CHECK -->|No| NEXT_FALLBACK{"More<br>Fallbacks?"}
|
|
494
|
+
NEXT_FALLBACK -->|Yes| FALLBACK_MODEL
|
|
495
|
+
NEXT_FALLBACK -->|No| ERROR["Error Response"]
|
|
496
|
+
|
|
497
|
+
FALLBACK_CHECK -->|No| ERROR
|
|
498
|
+
ERROR --> RESPONSE
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
### JSON Mode for Structured Outputs
|
|
504
|
+
|
|
505
|
+
For applications that require structured data, OneLLM supports JSON mode with compatible providers:
|
|
506
|
+
|
|
507
|
+
```python
|
|
508
|
+
response = ChatCompletion.create(
|
|
509
|
+
model="openai/gpt-4o",
|
|
510
|
+
messages=[
|
|
511
|
+
{"role": "user", "content": "List the top 3 programming languages with their key features"}
|
|
512
|
+
],
|
|
513
|
+
response_format={"type": "json_object"} # Request JSON output
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
# The response contains valid, parseable JSON
|
|
517
|
+
json_response = response.choices[0].message["content"]
|
|
518
|
+
print(json_response) # Structured JSON data
|
|
519
|
+
|
|
520
|
+
# Parse it with standard libraries
|
|
521
|
+
import json
|
|
522
|
+
structured_data = json.loads(json_response)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
When using providers that don't natively support JSON mode, OneLLM automatically adds system instructions requesting JSON-formatted responses.
|
|
526
|
+
|
|
527
|
+
### Asynchronous Support
|
|
528
|
+
|
|
529
|
+
Both synchronous and asynchronous APIs are available:
|
|
530
|
+
|
|
531
|
+
```python
|
|
532
|
+
# Synchronous
|
|
533
|
+
response = ChatCompletion.create(
|
|
534
|
+
model="openai/gpt-4",
|
|
535
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
536
|
+
)
|
|
537
|
+
|
|
538
|
+
# Asynchronous with fallbacks and retries
|
|
539
|
+
response = await ChatCompletion.acreate(
|
|
540
|
+
model="openai/gpt-4",
|
|
541
|
+
messages=[{"role": "user", "content": "Hello"}],
|
|
542
|
+
retries=2,
|
|
543
|
+
fallback_models=["anthropic/claude-3-opus"]
|
|
544
|
+
)
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
---
|
|
548
|
+
|
|
549
|
+
## π Migration from OpenAI
|
|
550
|
+
|
|
551
|
+
OneLLM provides multiple ways to migrate from the OpenAI client, including a fully compatible client interface:
|
|
552
|
+
|
|
553
|
+
### Option 1: Complete Drop-in Replacement (Identical Interface)
|
|
554
|
+
|
|
555
|
+
OneLLM is a complete drop-in replacement for the OpenAI client with the OpenAI library included as a default dependency:
|
|
556
|
+
|
|
557
|
+
```python
|
|
558
|
+
# Before
|
|
559
|
+
from openai import OpenAI
|
|
560
|
+
client = OpenAI()
|
|
561
|
+
response = client.chat.completions.create(
|
|
562
|
+
model="gpt-4",
|
|
563
|
+
messages=[{"role": "user", "content": "Hello world"}]
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
# After - 100% identical client interface
|
|
567
|
+
from onellm import OpenAI # or Client
|
|
568
|
+
client = OpenAI() # completely compatible with OpenAI's client
|
|
569
|
+
response = client.chat.completions.create(
|
|
570
|
+
model="gpt-4", # automatically adds "openai/" prefix when needed
|
|
571
|
+
messages=[{"role": "user", "content": "Hello world"}]
|
|
572
|
+
)
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
### Option 2: Streamlined Direct API (Fewer Lines)
|
|
576
|
+
|
|
577
|
+
```python
|
|
578
|
+
# Before
|
|
579
|
+
from openai import OpenAI
|
|
580
|
+
client = OpenAI()
|
|
581
|
+
response = client.chat.completions.create(
|
|
582
|
+
model="gpt-4",
|
|
583
|
+
messages=[{"role": "user", "content": "Hello world"}]
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
# After - more concise
|
|
587
|
+
from onellm import ChatCompletion
|
|
588
|
+
response = ChatCompletion.create(
|
|
589
|
+
model="openai/gpt-4", # explicitly using provider prefix
|
|
590
|
+
messages=[{"role": "user", "content": "Hello world"}]
|
|
591
|
+
)
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
### Option 3: Model Fallback (Enhanced Reliability)
|
|
595
|
+
|
|
596
|
+
```python
|
|
597
|
+
# Adding fallback options with ChatCompletion
|
|
598
|
+
from onellm import ChatCompletion
|
|
599
|
+
response = ChatCompletion.create(
|
|
600
|
+
model="openai/gpt-4",
|
|
601
|
+
messages=[{"role": "user", "content": "Hello world"}],
|
|
602
|
+
fallback_models=[
|
|
603
|
+
"anthropic/claude-3-haiku",
|
|
604
|
+
"openai/gpt-3.5-turbo"
|
|
605
|
+
],
|
|
606
|
+
# optional config
|
|
607
|
+
fallback_config={
|
|
608
|
+
"log_fallbacks": True
|
|
609
|
+
}
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
# Using retries with fallbacks for enhanced reliability
|
|
613
|
+
from onellm import ChatCompletion
|
|
614
|
+
response = ChatCompletion.create(
|
|
615
|
+
model="openai/gpt-4",
|
|
616
|
+
messages=[{"role": "user", "content": "Hello world"}],
|
|
617
|
+
retries=3, # Will retry gpt-4 up to 3 additional times before using fallbacks
|
|
618
|
+
fallback_models=[
|
|
619
|
+
"anthropic/claude-3-haiku",
|
|
620
|
+
"openai/gpt-3.5-turbo"
|
|
621
|
+
]
|
|
622
|
+
)
|
|
623
|
+
|
|
624
|
+
# Using fallback with the client interface
|
|
625
|
+
from onellm import OpenAI
|
|
626
|
+
client = OpenAI()
|
|
627
|
+
response = client.chat.completions.create(
|
|
628
|
+
model="openai/gpt-4",
|
|
629
|
+
messages=[{"role": "user", "content": "Hello world"}],
|
|
630
|
+
fallback_models=[
|
|
631
|
+
"anthropic/claude-3-opus",
|
|
632
|
+
"groq/llama3-70b"
|
|
633
|
+
]
|
|
634
|
+
)
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
---
|
|
638
|
+
|
|
639
|
+
## π·οΈ Model Naming Convention
|
|
640
|
+
|
|
641
|
+
Models are specified using a provider prefix to clearly identify the source:
|
|
642
|
+
|
|
643
|
+
<!-- Model naming examples -->
|
|
644
|
+
<table>
|
|
645
|
+
<tr>
|
|
646
|
+
<th>Provider</th>
|
|
647
|
+
<th>Format</th>
|
|
648
|
+
<th>Example</th>
|
|
649
|
+
</tr>
|
|
650
|
+
<tr>
|
|
651
|
+
<td>OpenAI</td>
|
|
652
|
+
<td><code>openai/{model}</code></td>
|
|
653
|
+
<td><code>openai/gpt-4</code></td>
|
|
654
|
+
</tr>
|
|
655
|
+
<tr>
|
|
656
|
+
<td>Google</td>
|
|
657
|
+
<td><code>google/{model}</code></td>
|
|
658
|
+
<td><code>google/gemini-pro</code></td>
|
|
659
|
+
</tr>
|
|
660
|
+
<tr>
|
|
661
|
+
<td>Anthropic</td>
|
|
662
|
+
<td><code>anthropic/{model}</code></td>
|
|
663
|
+
<td><code>anthropic/claude-3-opus</code></td>
|
|
664
|
+
</tr>
|
|
665
|
+
<tr>
|
|
666
|
+
<td>Groq</td>
|
|
667
|
+
<td><code>groq/{model}</code></td>
|
|
668
|
+
<td><code>groq/llama3-70b</code></td>
|
|
669
|
+
</tr>
|
|
670
|
+
<tr>
|
|
671
|
+
<td>Mistral</td>
|
|
672
|
+
<td><code>mistral/{model}</code></td>
|
|
673
|
+
<td><code>mistral/mistral-large</code></td>
|
|
674
|
+
</tr>
|
|
675
|
+
<tr>
|
|
676
|
+
<td>Local via Ollama</td>
|
|
677
|
+
<td><code>ollama/{model}</code></td>
|
|
678
|
+
<td><code>ollama/llama3</code></td>
|
|
679
|
+
</tr>
|
|
680
|
+
<tr>
|
|
681
|
+
<td>Via OpenRouter</td>
|
|
682
|
+
<td><code>openrouter/{provider}/{model}</code></td>
|
|
683
|
+
<td><code>openrouter/anthropic/claude-3</code></td>
|
|
684
|
+
</tr>
|
|
685
|
+
</table>
|
|
686
|
+
|
|
687
|
+
---
|
|
688
|
+
|
|
689
|
+
## βοΈ Configuration
|
|
690
|
+
|
|
691
|
+
OneLLM can be configured through environment variables or at runtime:
|
|
692
|
+
|
|
693
|
+
```python
|
|
694
|
+
# Environment variables
|
|
695
|
+
# OPENAI_API_KEY=sk-...
|
|
696
|
+
# ANTHROPIC_API_KEY=sk-...
|
|
697
|
+
|
|
698
|
+
# Runtime configuration
|
|
699
|
+
import onellm
|
|
700
|
+
|
|
701
|
+
onellm.openai_api_key = "sk-..." # OpenAI API key
|
|
702
|
+
onellm.anthropic_api_key = "sk-..." # Anthropic API key
|
|
703
|
+
|
|
704
|
+
# Configure fallback behavior
|
|
705
|
+
onellm.config.fallback = {
|
|
706
|
+
"enabled": True,
|
|
707
|
+
"default_chains": {
|
|
708
|
+
"chat": ["openai/gpt-4", "anthropic/claude-3-opus", "groq/llama3-70b"],
|
|
709
|
+
"embedding": ["openai/text-embedding-3-small", "cohere/embed-english"]
|
|
710
|
+
},
|
|
711
|
+
"retry_delay": 1.0,
|
|
712
|
+
"max_retries": 3
|
|
713
|
+
}
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
---
|
|
717
|
+
|
|
718
|
+
## π§ͺ Test Coverage
|
|
719
|
+
|
|
720
|
+
OneLLM maintains comprehensive test coverage to ensure reliability and compatibility:
|
|
721
|
+
|
|
722
|
+
<!-- Test coverage visualization -->
|
|
723
|
+
<table>
|
|
724
|
+
<tr>
|
|
725
|
+
<th>Coverage Metric</th>
|
|
726
|
+
<th>Value</th>
|
|
727
|
+
</tr>
|
|
728
|
+
<tr>
|
|
729
|
+
<td>Overall Package Coverage</td>
|
|
730
|
+
<td>96% (357 passing tests)</td>
|
|
731
|
+
</tr>
|
|
732
|
+
<tr>
|
|
733
|
+
<td>Modules with 100% Coverage</td>
|
|
734
|
+
<td>16 modules</td>
|
|
735
|
+
</tr>
|
|
736
|
+
<tr>
|
|
737
|
+
<td>Minimum Module Coverage</td>
|
|
738
|
+
<td>90%</td>
|
|
739
|
+
</tr>
|
|
740
|
+
<tr>
|
|
741
|
+
<td>Provider Implementations</td>
|
|
742
|
+
<td>93-95% coverage</td>
|
|
743
|
+
</tr>
|
|
744
|
+
</table>
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
This extensive test coverage ensures reliable operation across all supported providers and models.
|
|
748
|
+
|
|
749
|
+
---
|
|
750
|
+
|
|
751
|
+
## π Documentation
|
|
752
|
+
|
|
753
|
+
OneLLM uses a code-first documentation approach:
|
|
754
|
+
|
|
755
|
+
1. **Examples Directory**: The `examples/` directory contains well-documented example scripts that demonstrate all key features of the library:
|
|
756
|
+
- Each example includes detailed frontmatter explaining its purpose and relationship to the codebase
|
|
757
|
+
- Examples range from basic usage to advanced features like fallbacks, retries, and multi-modal capabilities
|
|
758
|
+
- Running the examples is the fastest way to understand the library's capabilities
|
|
759
|
+
|
|
760
|
+
2. **Code Docstrings**: All public APIs, classes, and methods have comprehensive docstrings:
|
|
761
|
+
- Detailed parameter descriptions
|
|
762
|
+
- Return value documentation
|
|
763
|
+
- Exception information
|
|
764
|
+
- Usage examples
|
|
765
|
+
|
|
766
|
+
3. **Type Annotations**: The codebase uses Python type annotations throughout:
|
|
767
|
+
- Provides IDE autocompletion support
|
|
768
|
+
- Makes argument requirements clear
|
|
769
|
+
- Enables static type checking
|
|
770
|
+
|
|
771
|
+
This approach keeps documentation tightly coupled with code, ensuring it stays up-to-date as the library evolves. To get started, we recommend examining the examples that match your use case.
|
|
772
|
+
|
|
773
|
+
---
|
|
774
|
+
|
|
775
|
+
## π€ Call for Contributions
|
|
776
|
+
|
|
777
|
+
We're building something amazing with OneLLM, and we'd love your help to make it even better! There are many ways to contribute:
|
|
778
|
+
|
|
779
|
+
- **Code contributions**: Add new providers, enhance existing ones, or improve core functionality
|
|
780
|
+
- **Bug reports**: Help us identify and fix issues
|
|
781
|
+
- **Documentation**: Improve examples, clarify API usage, or fix typos
|
|
782
|
+
- **Feature requests**: Share your ideas for making OneLLM more powerful
|
|
783
|
+
- **Testing**: Help ensure reliability across different environments and use cases
|
|
784
|
+
|
|
785
|
+
**Getting started is easy:**
|
|
786
|
+
|
|
787
|
+
1. Check out our [open issues](https://github.com/muxi-ai/onellm/issues) for good first contributions
|
|
788
|
+
2. Fork the repository and create a feature branch
|
|
789
|
+
3. Make your improvements and run tests
|
|
790
|
+
4. Submit a pull request with a clear description of your changes
|
|
791
|
+
|
|
792
|
+
For complete details on contribution guidelines, code style, provider development, testing standards, and our contributor license agreement, please read our [CONTRIBUTING.md](./CONTRIBUTING.md).
|
|
793
|
+
|
|
794
|
+
---
|
|
795
|
+
|
|
796
|
+
## π License
|
|
797
|
+
|
|
798
|
+
OneLLM is licensed under the [Apache-2.0 license](./LICENSE).
|
|
799
|
+
|
|
800
|
+
### Why Apache 2.0?
|
|
801
|
+
|
|
802
|
+
I chose the Apache 2.0 license to make OneLLM easy to adopt, integrate, and build on. This license:
|
|
803
|
+
|
|
804
|
+
- Allows you to freely use, modify, and distribute the library in both open-source and proprietary software
|
|
805
|
+
- Encourages wide adoption by individuals, startups, and enterprises alike
|
|
806
|
+
- Includes a clear patent grant for legal peace of mind
|
|
807
|
+
- Enables flexible usage without the complexity of copyleft restrictions
|
|
808
|
+
|
|
809
|
+
Whether youβre building internal tools or commercial applications, Apache 2.0 gives you the freedom to use OneLLM however you need β no strings attached.
|
|
810
|
+
|
|
811
|
+
---
|
|
812
|
+
|
|
813
|
+
## π Acknowledgements
|
|
814
|
+
|
|
815
|
+
This project stands on the shoulders of many excellent open-source projects and would not be possible without the collaborative spirit of the developer community.
|
|
816
|
+
|
|
817
|
+
Special thanks to all the LLM providers whose APIs this library integrates with, and to the early adopters who tested the library and provided crucial feedback.
|
|
818
|
+
|
|
819
|
+
## π Thank You
|
|
820
|
+
|
|
821
|
+
Thank you for trying out OneLLM! Your interest and support mean a lot to this project. Whether you're using it in your applications, experimenting with different LLM providers, or just exploring the capabilities, your participation helps drive this project forward.
|
|
822
|
+
|
|
823
|
+
If you find OneLLM useful in your work:
|
|
824
|
+
|
|
825
|
+
- Consider starring the repository on GitHub
|
|
826
|
+
- Share your experiences or use cases with the community
|
|
827
|
+
- Let us know how we can make it better for your needs
|
|
828
|
+
|
|
829
|
+
Thank you for your support!
|
|
830
|
+
|
|
831
|
+
~ **Ran Aroussi**<br>
|
|
832
|
+
π / [@aroussi](https://x.com/aroussi)
|