OneSDK: Unified LLM API Interface
OneSDK is a Python library providing a unified interface for various Large Language Model (LLM) providers. It simplifies interactions with different LLM APIs through a consistent set of methods.
Features
- Unified API for multiple LLM providers
- Flexible usage: per-call model specification or default model setting
- Intuitive interface for common LLM operations
- Synchronous and streaming text generation support
- Token counting functionality
- Embedding creation (for supported providers)
- Image generation (for supported providers)
- File operations (for supported providers)
- Proxy setting for API calls
- Usage statistics retrieval (for supported providers)
Installation
pip install llm_onesdk
Quick Start
OneSDK supports two main usage patterns:
1. Specify model for each call
from llm_onesdk import OneSDK
sdk = OneSDK("openai", {"api_key": "your-api-key"})
response = sdk.generate(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Tell me a joke about programming."}]
)
print(response['choices'][0]['message']['content'])
2. Set a default model
from llm_onesdk import OneSDK
sdk = OneSDK("openai", {"api_key": "your-api-key"})
sdk.set_model("gpt-3.5-turbo")
response = sdk.generate(
messages=[{"role": "user", "content": "Tell me a joke about programming."}]
)
print(response['choices'][0]['message']['content'])
Streaming Generation
for chunk in sdk.stream_generate(
model="gpt-3.5-turbo", # Optional if using set_model()
messages=[{"role": "user", "content": "Write a short story about AI."}]
):
print(chunk['choices'][0]['message']['content'], end='', flush=True)
Additional Operations
# List models (for supported providers)
models = sdk.list_models()
print(models)
# Count tokens
token_count = sdk.count_tokens(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "How many tokens is this?"}]
)
print(f"Token count: {token_count}")
# Create embeddings (for supported providers)
embeddings = sdk.create_embedding(
model="text-embedding-ada-002",
input="Hello, world!"
)
print(embeddings)
# Generate image (for supported providers)
image_response = sdk.create_image("A futuristic city with flying cars")
print(image_response)
Supported Providers and Core Methods
The following table shows the supported providers, their core method support, and additional features:
| Provider | list_models | generate | stream_generate | count_tokens | create_embedding | create_image | Additional Features |
|---|---|---|---|---|---|---|---|
| Anthropic | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | Context creation and management |
| Qwen (通义千问) | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | Multimodal generation |
| Cohere* | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | Text classification, Summarization |
| Doubao | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | Knowledge base management, Speech synthesis |
| Gemini* | ✗ | ✓ | ✓ | ✓ | ✓ | ✗ | Multimodal understanding |
| Kimi | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ | File operations, Context caching |
| MiniMax | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ | Audio processing, Knowledge base management |
| Ollama* | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ | Local model management |
| OpenAI | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | Audio transcription, Model fine-tuning |
| Wenxin (文心一言) | ✗ | ✓ | ✓ | ✓ | ✗ | ✗ | Custom model settings |
✓: Supported, ✗: Not supported
Notes:
- Some providers may have additional provider-specific methods. Refer to individual provider documentation for details.
- Providers marked with * (Ollama, Gemini, and Cohere) are currently not fully tested. The documentation for these providers is for reference only and may not be entirely accurate or up-to-date. We are working on improving these integrations and will provide more accurate information in future updates.
- The "Additional Features" column summarizes some unique or extra functionalities of each provider. The availability and usage of specific features may change over time; please refer to the latest official documentation.
Key Methods
set_model(model): Set default modellist_models(): List available models (if supported)generate(messages, model=None, **kwargs): Generate responsestream_generate(messages, model=None, **kwargs): Stream responsecount_tokens(model, messages): Count tokenscreate_embedding(model, input, **kwargs): Create embeddings (if supported)create_image(prompt, **kwargs): Create image (if supported)upload_file(file_path): Upload file (if supported)set_proxy(proxy_url): Set proxy for API calls
Error Handling
OneSDK uses custom exceptions inheriting from InvokeError. Always wrap API calls in try-except blocks:
from llm_onesdk.utils.error_handler import InvokeError
try:
response = sdk.generate(model, messages)
except InvokeError as e:
print(f"An error occurred: {str(e)}")
Documentation
For detailed information on each provider's capabilities and usage, please refer to the individual documentation files in the docs/ directory.
Contributing
We welcome contributions, especially new provider integrations! See our Contributing Guide for details.
License
This project is under the MIT License. See the LICENSE file for details.
Metadata
Release files for llm-onesdk 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| llm_onesdk-0.0.7.tar.gz | 42.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llm_onesdk-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 112.4 kB
Release files / llm_onesdk-0.0.7.tar.gz
| Download URL | llm_onesdk-0.0.7.tar.gz |
|---|---|
| Size | 42.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79635a963b186f0f53f560e391707b1f8b323d97a2ccd86ea361fce6dec84a7d
|
|
BLAKE2b-256 checksum How to use checksums |
07e086e611a5104e13622794b8f42482317d831d89e5c0d362c07201764cc144
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / llm_onesdk-0.0.7-py3-none-any.whl
| Download URL | llm_onesdk-0.0.7-py3-none-any.whl |
|---|---|
| Size | 70.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3940bb39702fe6865f9c3d508bb95b8d3563712233220aa8eeb806374b031569
|
|
BLAKE2b-256 checksum How to use checksums |
c421cd88584e7e53746376b2a6ad367ca3dacddec00ff9dfb780c07a5de4da7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Security for llm-onesdk 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.