OneSDK is a Python library that provides a unified interface for interacting with various Large Language Model (LLM) providers.
Project description
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
- File upload capability (provider-dependent)
- Proxy setting for API calls
- Usage statistics retrieval (provider-dependent)
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("anthropic", {"api_key": "your-api-key"})
response = sdk.generate(
model="claude-3-opus-20240229",
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("anthropic", {"api_key": "your-api-key"})
sdk.set_model("claude-3-opus-20240229")
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="claude-3-opus-20240229", # 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
models = sdk.list_models()
print(models)
# Count tokens
token_count = sdk.count_tokens(
model="claude-3-opus-20240229",
messages=[{"role": "user", "content": "How many tokens is this?"}]
)
print(f"Token count: {token_count}")
Supported Providers and Methods
The following table shows the supported providers, their default models, and the methods they support:
| Provider | list_models | generate | stream_generate | count_tokens | create_embedding | create_image |
|---|---|---|---|---|---|---|
| Anthropic | ✓ | ✓ | ✓ | ✓ | ✗ | ✗ |
| Qwen | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ |
| Cohere | ✗ | ✓ | ✓ | ✓ | ✓ | ✗ |
| Doubao | ✗ | ✓ | ✓ | ✓ | ✓ | ✗ |
| Gemini | ✗ | ✓ | ✓ | ✗ | ✗ | ✗ |
| Minimax | ✗ | ✓ | ✓ | ✓ | ✓ | ✓ |
| OpenAI | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Wenxin | ✗ | ✓ | ✓ | ✓ | ✗ | ✗ |
✓: Supported, ✗: Not supported
Key Methods
set_model(model): Set default modellist_models(): List available modelsget_model_info(model_id): Get model informationgenerate(messages, model=None, **kwargs): Generate responsestream_generate(messages, model=None, **kwargs): Stream responsecount_tokens(model, messages): Count tokenscreate_completion(model, prompt, **kwargs): Legacy API completioncreate_embedding(model, input, **kwargs): Create embeddingscreate_image(prompt, **kwargs): Create image (for supported providers)upload_file(file_path): Upload fileset_proxy(proxy_url): Set proxyget_usage(): Get usage statistics
Error Handling
OneSDK uses custom exceptions inheriting from InvokeError (e.g., InvokeModelNotFoundError).
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_onesdk-0.0.2.tar.gz.
File metadata
- Download URL: llm_onesdk-0.0.2.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19180dfc3ac45d84840e838a53ebafee72e153de40024c681087d18c013a66dc
|
|
| MD5 |
0127fcb61cb04023c38f24148a221ee8
|
|
| BLAKE2b-256 |
725658afb564e15a9e4438b32c3a60901f5914b349961e0e3995e32be6869fb6
|
File details
Details for the file llm_onesdk-0.0.2-py3-none-any.whl.
File metadata
- Download URL: llm_onesdk-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5478a09fa81ce080e04e596c5f6c6b555cf34053226a59fc58b051c38d6727f0
|
|
| MD5 |
dc751a66120815cb635809f683f78333
|
|
| BLAKE2b-256 |
792bc9f8b30cc1215e016c2d0343d2710151c8b53df260c6a60159e3f7acbc9a
|