llm-onesdk


Namellm-onesdk JSON
Version 0.0.7 PyPI version JSON
download
home_pagehttps://onesdk.llmpages.cn/
SummaryOneSDK is a Python library that provides a unified interface for interacting with various Large Language Model (LLM) providers.
upload_time2025-01-03 00:59:54
maintainerNone
docs_urlNone
authoranycodes
requires_python>=3.7
licenseMIT
keywords llm api sdk nlp ai
VCS
bugtrack_url
requirements requests python-dotenv typing-extensions unittest2
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install llm_onesdk
```

## Quick Start

OneSDK supports two main usage patterns:

### 1. Specify model for each call

```python
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

```python
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

```python
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

```python
# 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](docs/anthropic.md) | ✓           | ✓        | ✓               | ✓            | ✗                | ✗            | Context creation and management |
| [Qwen (通义千问)](docs/qwen.md)      | ✓           | ✓        | ✓               | ✓            | ✓                | ✗            | Multimodal generation |
| [Cohere](docs/cohere.md)*    | ✓           | ✓        | ✓               | ✓            | ✓                | ✗            | Text classification, Summarization |
| [Doubao](docs/doubao.md)    | ✓           | ✓        | ✓               | ✓            | ✓                | ✗            | Knowledge base management, Speech synthesis |
| [Gemini](docs/gemini.md)*    | ✗           | ✓        | ✓               | ✓            | ✓                | ✗            | Multimodal understanding |
| [Kimi](docs/kimi.md)      | ✓           | ✓        | ✓               | ✓            | ✗                | ✗            | File operations, Context caching |
| [MiniMax](docs/minimax.md) | ✗           | ✓        | ✓               | ✓            | ✓                | ✓            | Audio processing, Knowledge base management |
| [Ollama](docs/ollama.md)*    | ✓           | ✓        | ✓               | ✓            | ✓                | ✗            | Local model management |
| [OpenAI](docs/openai.md)    | ✓           | ✓        | ✓               | ✓            | ✓                | ✓            | Audio transcription, Model fine-tuning |
| [Wenxin (文心一言)](docs/wenxin.md)    | ✗           | ✓        | ✓               | ✓            | ✗                | ✗            | Custom model settings |

✓: Supported, ✗: Not supported

Notes:
1. Some providers may have additional provider-specific methods. Refer to individual provider documentation for details.
2. 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.
3. 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 model
- `list_models()`: List available models (if supported)
- `generate(messages, model=None, **kwargs)`: Generate response
- `stream_generate(messages, model=None, **kwargs)`: Stream response
- `count_tokens(model, messages)`: Count tokens
- `create_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:

```python
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](CONTRIBUTING.md) for details.

## License

This project is under the MIT License. See the [LICENSE](LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://onesdk.llmpages.cn/",
    "name": "llm-onesdk",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "LLM API SDK NLP AI",
    "author": "anycodes",
    "author_email": "liuyu@xmail.tech",
    "download_url": "https://files.pythonhosted.org/packages/07/e0/86e611a5104e13622794b8f42482317d831d89e5c0d362c07201764cc144/llm_onesdk-0.0.7.tar.gz",
    "platform": null,
    "description": "# OneSDK: Unified LLM API Interface\n\nOneSDK 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.\n\n## Features\n\n- Unified API for multiple LLM providers\n- Flexible usage: per-call model specification or default model setting\n- Intuitive interface for common LLM operations\n- Synchronous and streaming text generation support\n- Token counting functionality\n- Embedding creation (for supported providers)\n- Image generation (for supported providers)\n- File operations (for supported providers)\n- Proxy setting for API calls\n- Usage statistics retrieval (for supported providers)\n\n## Installation\n\n```bash\npip install llm_onesdk\n```\n\n## Quick Start\n\nOneSDK supports two main usage patterns:\n\n### 1. Specify model for each call\n\n```python\nfrom llm_onesdk import OneSDK\n\nsdk = OneSDK(\"openai\", {\"api_key\": \"your-api-key\"})\n\nresponse = sdk.generate(\n    model=\"gpt-3.5-turbo\",\n    messages=[{\"role\": \"user\", \"content\": \"Tell me a joke about programming.\"}]\n)\nprint(response['choices'][0]['message']['content'])\n```\n\n### 2. Set a default model\n\n```python\nfrom llm_onesdk import OneSDK\n\nsdk = OneSDK(\"openai\", {\"api_key\": \"your-api-key\"})\nsdk.set_model(\"gpt-3.5-turbo\")\n\nresponse = sdk.generate(\n    messages=[{\"role\": \"user\", \"content\": \"Tell me a joke about programming.\"}]\n)\nprint(response['choices'][0]['message']['content'])\n```\n\n## Streaming Generation\n\n```python\nfor chunk in sdk.stream_generate(\n    model=\"gpt-3.5-turbo\",  # Optional if using set_model()\n    messages=[{\"role\": \"user\", \"content\": \"Write a short story about AI.\"}]\n):\n    print(chunk['choices'][0]['message']['content'], end='', flush=True)\n```\n\n## Additional Operations\n\n```python\n# List models (for supported providers)\nmodels = sdk.list_models()\nprint(models)\n\n# Count tokens\ntoken_count = sdk.count_tokens(\n    model=\"gpt-3.5-turbo\",\n    messages=[{\"role\": \"user\", \"content\": \"How many tokens is this?\"}]\n)\nprint(f\"Token count: {token_count}\")\n\n# Create embeddings (for supported providers)\nembeddings = sdk.create_embedding(\n    model=\"text-embedding-ada-002\",\n    input=\"Hello, world!\"\n)\nprint(embeddings)\n\n# Generate image (for supported providers)\nimage_response = sdk.create_image(\"A futuristic city with flying cars\")\nprint(image_response)\n```\n\n## Supported Providers and Core Methods\n\nThe following table shows the supported providers, their core method support, and additional features:\n\n| Provider  | list_models | generate | stream_generate | count_tokens | create_embedding | create_image | Additional Features |\n|-----------|-------------|----------|-----------------|--------------|------------------|--------------|---------------------|\n| [Anthropic](docs/anthropic.md) | \u2713           | \u2713        | \u2713               | \u2713            | \u2717                | \u2717            | Context creation and management |\n| [Qwen (\u901a\u4e49\u5343\u95ee)](docs/qwen.md)      | \u2713           | \u2713        | \u2713               | \u2713            | \u2713                | \u2717            | Multimodal generation |\n| [Cohere](docs/cohere.md)*    | \u2713           | \u2713        | \u2713               | \u2713            | \u2713                | \u2717            | Text classification, Summarization |\n| [Doubao](docs/doubao.md)    | \u2713           | \u2713        | \u2713               | \u2713            | \u2713                | \u2717            | Knowledge base management, Speech synthesis |\n| [Gemini](docs/gemini.md)*    | \u2717           | \u2713        | \u2713               | \u2713            | \u2713                | \u2717            | Multimodal understanding |\n| [Kimi](docs/kimi.md)      | \u2713           | \u2713        | \u2713               | \u2713            | \u2717                | \u2717            | File operations, Context caching |\n| [MiniMax](docs/minimax.md) | \u2717           | \u2713        | \u2713               | \u2713            | \u2713                | \u2713            | Audio processing, Knowledge base management |\n| [Ollama](docs/ollama.md)*    | \u2713           | \u2713        | \u2713               | \u2713            | \u2713                | \u2717            | Local model management |\n| [OpenAI](docs/openai.md)    | \u2713           | \u2713        | \u2713               | \u2713            | \u2713                | \u2713            | Audio transcription, Model fine-tuning |\n| [Wenxin (\u6587\u5fc3\u4e00\u8a00)](docs/wenxin.md)    | \u2717           | \u2713        | \u2713               | \u2713            | \u2717                | \u2717            | Custom model settings |\n\n\u2713: Supported, \u2717: Not supported\n\nNotes:\n1. Some providers may have additional provider-specific methods. Refer to individual provider documentation for details.\n2. 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.\n3. 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.\n\n## Key Methods\n\n- `set_model(model)`: Set default model\n- `list_models()`: List available models (if supported)\n- `generate(messages, model=None, **kwargs)`: Generate response\n- `stream_generate(messages, model=None, **kwargs)`: Stream response\n- `count_tokens(model, messages)`: Count tokens\n- `create_embedding(model, input, **kwargs)`: Create embeddings (if supported)\n- `create_image(prompt, **kwargs)`: Create image (if supported)\n- `upload_file(file_path)`: Upload file (if supported)\n- `set_proxy(proxy_url)`: Set proxy for API calls\n\n## Error Handling\n\nOneSDK uses custom exceptions inheriting from `InvokeError`. Always wrap API calls in try-except blocks:\n\n```python\nfrom llm_onesdk.utils.error_handler import InvokeError\n\ntry:\n    response = sdk.generate(model, messages)\nexcept InvokeError as e:\n    print(f\"An error occurred: {str(e)}\")\n```\n\n## Documentation\n\nFor detailed information on each provider's capabilities and usage, please refer to the individual documentation files in the `docs/` directory.\n\n## Contributing\n\nWe welcome contributions, especially new provider integrations! See our [Contributing Guide](CONTRIBUTING.md) for details.\n\n## License\n\nThis project is under the MIT License. See the [LICENSE](LICENSE) file for details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "OneSDK is a Python library that provides a unified interface for interacting with various Large Language Model (LLM) providers.",
    "version": "0.0.7",
    "project_urls": {
        "GitHub": "https://github.com/LLMPages/onesdk",
        "Homepage": "https://onesdk.llmpages.cn/"
    },
    "split_keywords": [
        "llm",
        "api",
        "sdk",
        "nlp",
        "ai"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c421cd88584e7e53746376b2a6ad367ca3dacddec00ff9dfb780c07a5de4da7c",
                "md5": "2ca826fd9e90bf21408b5ae5d66e329f",
                "sha256": "3940bb39702fe6865f9c3d508bb95b8d3563712233220aa8eeb806374b031569"
            },
            "downloads": -1,
            "filename": "llm_onesdk-0.0.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2ca826fd9e90bf21408b5ae5d66e329f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 70314,
            "upload_time": "2025-01-03T00:59:52",
            "upload_time_iso_8601": "2025-01-03T00:59:52.401748Z",
            "url": "https://files.pythonhosted.org/packages/c4/21/cd88584e7e53746376b2a6ad367ca3dacddec00ff9dfb780c07a5de4da7c/llm_onesdk-0.0.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "07e086e611a5104e13622794b8f42482317d831d89e5c0d362c07201764cc144",
                "md5": "eebd44dab419baf0f150b9473934382c",
                "sha256": "79635a963b186f0f53f560e391707b1f8b323d97a2ccd86ea361fce6dec84a7d"
            },
            "downloads": -1,
            "filename": "llm_onesdk-0.0.7.tar.gz",
            "has_sig": false,
            "md5_digest": "eebd44dab419baf0f150b9473934382c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 42090,
            "upload_time": "2025-01-03T00:59:54",
            "upload_time_iso_8601": "2025-01-03T00:59:54.682940Z",
            "url": "https://files.pythonhosted.org/packages/07/e0/86e611a5104e13622794b8f42482317d831d89e5c0d362c07201764cc144/llm_onesdk-0.0.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-03 00:59:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "LLMPages",
    "github_project": "onesdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "requests",
            "specs": [
                [
                    ">=",
                    "2.25.1"
                ]
            ]
        },
        {
            "name": "python-dotenv",
            "specs": [
                [
                    ">=",
                    "0.19.0"
                ]
            ]
        },
        {
            "name": "typing-extensions",
            "specs": [
                [
                    ">=",
                    "3.7.4"
                ]
            ]
        },
        {
            "name": "unittest2",
            "specs": [
                [
                    ">=",
                    "1.1.0"
                ]
            ]
        }
    ],
    "lcname": "llm-onesdk"
}
        
Elapsed time: 0.44756s