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 that provides a unified interface for interacting with various Large Language Model (LLM) providers. It simplifies the process of working with different LLM APIs by offering a consistent set of methods across providers.
Features
- Unified API for multiple LLM providers
- Flexible usage patterns: specify model per call or set a default model
- Easy-to-use interface for common LLM operations
- Support for both synchronous and streaming text generation
- Token counting functionality
- File upload capability (where supported)
- Proxy setting for API calls
- Usage statistics retrieval (where available)
Installation
pip install llm-onesdk
Quick Start
OneSDK supports two main usage patterns:
Pattern 1: Specify model for each call
from llm-onesdk import OneSDK
# Initialize the SDK with your chosen provider and credentials
sdk = OneSDK("anthropic", {"api_key": "your-api-key"})
# Generate text
response = sdk.generate(
model="claude-2",
messages=[{"role": "user", "content": "Tell me a joke about programming."}]
)
print(response['choices'][0]['message']['content'])
Pattern 2: Set a default model
from llm-onesdk import OneSDK
# Initialize the SDK and set a default model
sdk = OneSDK("anthropic", {"api_key": "your-api-key"})
sdk.set_model("claude-2")
# Generate text using the default model
response = sdk.generate(
messages=[{"role": "user", "content": "Tell me a joke about programming."}]
)
print(response['choices'][0]['message']['content'])
Streaming Generation
Both patterns support streaming generation:
for chunk in sdk.stream_generate(
model="claude-2", # or omit if using set_model()
messages=[{"role": "user", "content": "Write a short story about AI."}]
):
print(chunk['choices'][0]['message']['content'], end='', flush=True)
Other Operations
# List available models
models = sdk.list_models()
print(models)
# Count tokens
token_count = sdk.count_tokens(
model="claude-2",
messages=[{"role": "user", "content": "How many tokens is this?"}]
)
print(f"Token count: {token_count}")
Supported Providers
You can list all available providers using:
print(OneSDK.list_providers())
Methods
set_model(model): Set the default model for subsequent API calls.list_models(): List available models for the current provider.get_model_info(model_id): Get information about a specific model.generate(messages, model=None, **kwargs): Generate a response using the specified or default model.stream_generate(messages, model=None, **kwargs): Generate a streaming response.count_tokens(model, messages): Count the number of tokens in the input messages.create_completion(model, prompt, **kwargs): Create a completion using the legacy API (if supported).upload_file(file_path): Upload a file and return a reference.set_proxy(proxy_url): Set a proxy for API calls.get_usage(): Get usage statistics for the current account.
Error Handling
OneSDK uses custom exception classes for error handling. The base exception class is InvokeError, with specific subclasses for different error types (e.g., InvokeModelNotFoundError).
Contributing
We welcome contributions, especially new provider integrations! Please see our Contributing Guide for more information on how to get started.
License
This project is licensed 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.1.tar.gz.
File metadata
- Download URL: llm_onesdk-0.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
245c603b8f66f41f60fc010d58eeb000e5544fd1b5f287a7fac10a7646ee62e0
|
|
| MD5 |
a7124be1c6e4da22fe56a6b7bd1da89a
|
|
| BLAKE2b-256 |
a4979c75bd53eb98a1d62564b66e6dc12df47fc14ca9bee7fd8010682d3153a4
|
File details
Details for the file llm_onesdk-0.0.1-py3-none-any.whl.
File metadata
- Download URL: llm_onesdk-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.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 |
bbee1b0e3c461daa55ebdf06608ab41d1107dda457f3b6ce2d3ab273ba6accca
|
|
| MD5 |
a9bffe40bdb2329ab127677858b1655b
|
|
| BLAKE2b-256 |
be3f91016397679c825ca602c1162b203d38fb8258340e6917489f29a8df2c3c
|