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 ADDED
@@ -0,0 +1 @@
1
+ 0.0.6
onellm/__init__.py ADDED
@@ -0,0 +1,123 @@
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
+ OneLLM: A lightweight, provider-agnostic Python library that offers a unified interface
23
+ for interacting with large language models (LLMs) from various providers.
24
+
25
+ This module serves as the main entry point for the OneLLM library, exposing all
26
+ public APIs and functionality to users. It provides a consistent interface for working
27
+ with different LLM providers while maintaining compatibility with the OpenAI API format.
28
+ """
29
+
30
+ import os
31
+
32
+ # Public API imports - core functionality
33
+ from .chat_completion import ChatCompletion
34
+ from .completion import Completion
35
+ from .embedding import Embedding
36
+
37
+ # Media handling
38
+ from .audio import AudioTranscription, AudioTranslation
39
+ from .files import File
40
+ from .image import Image
41
+ from .speech import Speech
42
+
43
+ # Configuration and providers
44
+ from .config import get_api_key, get_provider_config, set_api_key
45
+ from .providers import get_provider, list_providers, register_provider
46
+ from .providers.base import parse_model_name
47
+
48
+ # Client interface (OpenAI compatibility)
49
+ from .client import Client, OpenAI
50
+
51
+ # Error handling
52
+ from .errors import (
53
+ MuxiLLMError,
54
+ APIError,
55
+ AuthenticationError,
56
+ RateLimitError,
57
+ InvalidRequestError,
58
+ )
59
+
60
+
61
+ def get_version() -> str:
62
+ """
63
+ Read and return the package version from the .version file.
64
+
65
+ Returns:
66
+ str: The current version of the package
67
+
68
+ Note:
69
+ The .version file should be located in the same directory as this file.
70
+ The version string is stripped of any whitespace to ensure clean formatting.
71
+ """
72
+ version_file = os.path.join(os.path.dirname(__file__), ".version")
73
+ with open(version_file, "r", encoding="utf-8") as f:
74
+ return f.read().strip()
75
+
76
+
77
+ # Initialize package version from .version file
78
+ __version__ = get_version()
79
+
80
+ # Package metadata
81
+ __author__ = "Ran Aroussi"
82
+ __license__ = "Apache-2.0"
83
+ __url__ = "https://github.com/muxi-ai/onellm"
84
+
85
+ # Module exports - defines the public API of the package
86
+ # This controls what gets imported when using "from onellm import *"
87
+ __all__ = [
88
+ # Core functionality
89
+ "ChatCompletion", # Chat-based completions (conversations)
90
+ "Completion", # Text completions
91
+ "Embedding", # Vector embeddings for text
92
+
93
+ # Media handling
94
+ "File", # File operations for models
95
+ "AudioTranscription", # Convert audio to text
96
+ "AudioTranslation", # Translate audio to text
97
+ "Speech", # Text-to-speech synthesis
98
+ "Image", # Image generation and manipulation
99
+
100
+ # Client interface (OpenAI compatibility)
101
+ "Client", # Generic client for any provider
102
+ "OpenAI", # OpenAI-compatible client
103
+
104
+ # Configuration and providers
105
+ "set_api_key", # Set API key for a provider
106
+ "get_api_key", # Get API key for a provider
107
+ "get_provider", # Get provider instance by name
108
+ "list_providers", # List available providers
109
+ "register_provider", # Register a new provider
110
+ "parse_model_name", # Parse provider from model name
111
+ "get_provider_config", # Get configuration for a provider
112
+
113
+ # Error handling
114
+ "MuxiLLMError", # Base error class
115
+ "APIError", # API-related errors
116
+ "AuthenticationError", # Authentication failures
117
+ "RateLimitError", # Rate limit exceeded
118
+ "InvalidRequestError", # Invalid request parameters
119
+ ]
120
+
121
+ # Provider-specific API keys can be accessed as globals after they're set:
122
+ # e.g., from onellm import openai_api_key, anthropic_api_key
123
+ # This allows for a cleaner import experience when working with multiple providers
onellm/audio.py ADDED
@@ -0,0 +1,218 @@
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 audio capabilities for transcription and translation.
23
+
24
+ This module provides a high-level API for OpenAI's audio capabilities.
25
+ It includes classes for audio transcription and translation to English,
26
+ with support for fallback models if the primary model fails.
27
+ """
28
+
29
+ import asyncio
30
+ from typing import Any, Dict, IO, List, Optional, Union
31
+
32
+ from .providers.base import get_provider_with_fallbacks
33
+ from .utils.fallback import FallbackConfig
34
+
35
+
36
+ class AudioTranscription:
37
+ """
38
+ API class for audio transcription.
39
+
40
+ This class provides methods to transcribe audio files to text using
41
+ various provider models with fallback support.
42
+ """
43
+
44
+ @classmethod
45
+ async def create(
46
+ cls,
47
+ file: Union[str, bytes, IO[bytes]],
48
+ model: str = "openai/whisper-1",
49
+ fallback_models: Optional[List[str]] = None,
50
+ fallback_config: Optional[dict] = None,
51
+ **kwargs
52
+ ) -> Dict[str, Any]:
53
+ """
54
+ Transcribe audio to text.
55
+
56
+ This async method takes an audio file and transcribes it to text using
57
+ the specified model. If the primary model fails, it can fall back to
58
+ alternative models if provided.
59
+
60
+ Args:
61
+ file: Audio file to transcribe (path, bytes, or file-like object)
62
+ model: Model ID in format "provider/model" (default: "openai/whisper-1")
63
+ fallback_models: Optional list of models to try if the primary model fails
64
+ fallback_config: Optional configuration for fallback behavior
65
+ **kwargs: Additional parameters:
66
+ - language: Optional language code (e.g., "en")
67
+ - prompt: Optional text to guide transcription
68
+ - response_format: Format of the response ("json", "text", "srt",
69
+ "verbose_json", "vtt")
70
+ - temperature: Temperature for sampling
71
+
72
+ Returns:
73
+ Transcription result
74
+ """
75
+ # Process fallback configuration - convert dict to FallbackConfig object if provided
76
+ fb_config = None
77
+ if fallback_config:
78
+ fb_config = FallbackConfig(**fallback_config)
79
+
80
+ # Get provider with fallbacks or a regular provider
81
+ # This returns both the provider instance and the specific model name to use
82
+ provider, model_name = get_provider_with_fallbacks(
83
+ primary_model=model,
84
+ fallback_models=fallback_models,
85
+ fallback_config=fb_config,
86
+ )
87
+
88
+ # Delegate the actual transcription to the provider implementation
89
+ return await provider.create_transcription(file, model_name, **kwargs)
90
+
91
+ @classmethod
92
+ def create_sync(
93
+ cls,
94
+ file: Union[str, bytes, IO[bytes]],
95
+ model: str = "openai/whisper-1",
96
+ fallback_models: Optional[List[str]] = None,
97
+ fallback_config: Optional[dict] = None,
98
+ **kwargs
99
+ ) -> Dict[str, Any]:
100
+ """
101
+ Synchronous version of create().
102
+
103
+ This method provides a synchronous interface to the async create() method
104
+ by running it in an event loop. It has the same functionality but can be
105
+ called from synchronous code.
106
+
107
+ Args:
108
+ file: Audio file to transcribe (path, bytes, or file-like object)
109
+ model: Model ID in format "provider/model" (default: "openai/whisper-1")
110
+ fallback_models: Optional list of models to try if the primary model fails
111
+ fallback_config: Optional configuration for fallback behavior
112
+ **kwargs: Additional parameters as in create()
113
+
114
+ Returns:
115
+ Transcription result
116
+ """
117
+ # Use asyncio.run to execute the async create method in a new event loop
118
+ return asyncio.run(
119
+ cls.create(
120
+ file=file,
121
+ model=model,
122
+ fallback_models=fallback_models,
123
+ fallback_config=fallback_config,
124
+ **kwargs
125
+ )
126
+ )
127
+
128
+
129
+ class AudioTranslation:
130
+ """
131
+ API class for audio translation to English.
132
+
133
+ This class provides methods to translate audio files to English text
134
+ using various provider models with fallback support.
135
+ """
136
+
137
+ @classmethod
138
+ async def create(
139
+ cls,
140
+ file: Union[str, bytes, IO[bytes]],
141
+ model: str = "openai/whisper-1",
142
+ fallback_models: Optional[List[str]] = None,
143
+ fallback_config: Optional[dict] = None,
144
+ **kwargs
145
+ ) -> Dict[str, Any]:
146
+ """
147
+ Translate audio to English text.
148
+
149
+ This async method takes an audio file in any language and translates it
150
+ to English text using the specified model. If the primary model fails,
151
+ it can fall back to alternative models if provided.
152
+
153
+ Args:
154
+ file: Audio file to translate (path, bytes, or file-like object)
155
+ model: Model ID in format "provider/model" (default: "openai/whisper-1")
156
+ fallback_models: Optional list of models to try if the primary model fails
157
+ fallback_config: Optional configuration for fallback behavior
158
+ **kwargs: Additional parameters:
159
+ - prompt: Optional text to guide translation
160
+ - response_format: Format of the response ("json", "text", "srt",
161
+ "verbose_json", "vtt")
162
+ - temperature: Temperature for sampling
163
+
164
+ Returns:
165
+ Translation result with text in English
166
+ """
167
+ # Process fallback configuration - convert dict to FallbackConfig object if provided
168
+ fb_config = None
169
+ if fallback_config:
170
+ fb_config = FallbackConfig(**fallback_config)
171
+
172
+ # Get provider with fallbacks or a regular provider
173
+ # This returns both the provider instance and the specific model name to use
174
+ provider, model_name = get_provider_with_fallbacks(
175
+ primary_model=model,
176
+ fallback_models=fallback_models,
177
+ fallback_config=fb_config,
178
+ )
179
+
180
+ # Delegate the actual translation to the provider implementation
181
+ return await provider.create_translation(file, model_name, **kwargs)
182
+
183
+ @classmethod
184
+ def create_sync(
185
+ cls,
186
+ file: Union[str, bytes, IO[bytes]],
187
+ model: str = "openai/whisper-1",
188
+ fallback_models: Optional[List[str]] = None,
189
+ fallback_config: Optional[dict] = None,
190
+ **kwargs
191
+ ) -> Dict[str, Any]:
192
+ """
193
+ Synchronous version of create().
194
+
195
+ This method provides a synchronous interface to the async create() method
196
+ by running it in an event loop. It has the same functionality but can be
197
+ called from synchronous code.
198
+
199
+ Args:
200
+ file: Audio file to translate (path, bytes, or file-like object)
201
+ model: Model ID in format "provider/model" (default: "openai/whisper-1")
202
+ fallback_models: Optional list of models to try if the primary model fails
203
+ fallback_config: Optional configuration for fallback behavior
204
+ **kwargs: Additional parameters as in create()
205
+
206
+ Returns:
207
+ Translation result with text in English
208
+ """
209
+ # Use asyncio.run to execute the async create method in a new event loop
210
+ return asyncio.run(
211
+ cls.create(
212
+ file=file,
213
+ model=model,
214
+ fallback_models=fallback_models,
215
+ fallback_config=fallback_config,
216
+ **kwargs
217
+ )
218
+ )