Skip to main content

UniLLM Python Client Library version

Overview

UniLLM is a unified Python client for accessing multiple large language model (LLM) providers (OpenAI, Anthropic, Gemini, Mistral, Cohere, and more) through a single, easy-to-use interface.
It abstracts away provider-specific details, allowing you to switch models and providers with minimal code changes.


Features

  • Unified API: One interface for all supported LLM providers and models.
  • Drop-in replacements: Use from unillm import openai or from unillm import anthropic_dropin as anthropic for seamless migration from official SDKs.
  • Provider/model switching: Change models/providers by changing a string.
  • API key management: Pass your UniLLM API key directly or via environment variable.
  • Custom error handling: Catch and handle errors with meaningful exceptions.
  • Health check: Programmatically check if your backend is up.
  • Extensible: Add new providers via adapters.

Installation

pip install unillm

Quick Start

1. Unified API Example

from unillm import UniLLM

client = UniLLM(api_key="YOUR_API_KEY", base_url="https://your-backend.url")
response = client.chat(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello, world!"}],
    temperature=0.7,
    max_tokens=100
)
print(response.content)

2. OpenAI Drop-in Replacement

from unillm import openai
openai.api_key = "YOUR_API_KEY"
openai.api_base = "https://your-backend.url"
response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    temperature=0.7,
    max_tokens=100
)
print(response["choices"][0]["message"]["content"])

3. Anthropic Drop-in Replacement

from unillm import anthropic_dropin as anthropic
anthropic.api_key = "YOUR_API_KEY"
anthropic.api_base = "https://your-backend.url"
response = anthropic.ChatCompletion.create(
    model="claude-3-5-sonnet-20240620",
    messages=[{"role": "user", "content": "Hello, Claude!"}],
    temperature=0.7,
    max_tokens=100
)
print(response["choices"][0]["message"]["content"])

4. Switching Providers/Models

Just change the model string:

response = client.chat(
    model="claude-3-5-sonnet-20240620",  # Anthropic model
    messages=[{"role": "user", "content": "Hello, Claude!"}]
)

5. Migration Notes

  • For OpenAI: Change import openai to from unillm import openai and set api_base/api_key.
  • For Anthropic: Change import anthropic to from unillm import anthropic_dropin as anthropic and set api_base/api_key.
  • All other code can remain the same for chat completions.

API Reference

UniLLM Class

UniLLM(api_key=None, base_url="http://localhost:8000")
  • api_key: Your UniLLM API key (or set UNILLM_API_KEY env var)
  • base_url: URL of your UniLLM backend

chat()

chat(
    model: str,
    messages: List[Dict[str, str]],
    temperature: Optional[float] = None,
    max_tokens: Optional[int] = None,
    stream: bool = False,
    **kwargs
) -> ChatResponse
  • model: Model name (e.g., "gpt-4", "claude-3-5-sonnet-20240620")
  • messages: List of messages ({"role": ..., "content": ...})
  • temperature, max_tokens, etc.: Standard LLM parameters

health_check()

health_check() -> bool

Returns True if backend is healthy.


ChatCompletion Drop-ins

  • openai.ChatCompletion.create(...)
  • anthropic.ChatCompletion.create(...)

Supported Models & Providers

  • OpenAI: "gpt-4", "gpt-3.5-turbo", etc.
  • Anthropic: "claude-3-5-sonnet-20240620", etc.
  • Gemini, Mistral, Cohere: (see full list in your backend/docs)

Troubleshooting

  • 502/connection errors: Check your backend URL and health.
  • Model not found: Ensure the model name is supported by your backend.
  • API key errors: Make sure your key is valid and set correctly.

Contributing

  • PRs and issues welcome!
  • See [CONTRIBUTING.md] for guidelines.

License

MIT

Metadata

Release files for unifyllm-sdk 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unifyllm-sdk 0.1.5
File Size Uploaded
unifyllm_sdk-0.1.5.tar.gz 23.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unifyllm-sdk 0.1.5
File Interpreter ABI Platform
unifyllm_sdk-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 51.3 kB

Release files / unifyllm_sdk-0.1.5.tar.gz

Download URL unifyllm_sdk-0.1.5.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
342166a41737b19fcde4c9dce085cb8ac11238d349c2c45ed05e68ac101d3fc1
BLAKE2b-256 checksum
How to use checksums
3fc83edc4e451d00c08342bbb929d55768cfc51fa28bb8f07557d0416e9d29d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.1

Release files / unifyllm_sdk-0.1.5-py3-none-any.whl

Download URL unifyllm_sdk-0.1.5-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b516c0517ded7756f95874458efe2c247af143f8a66ac14601d8d99186695e7a
BLAKE2b-256 checksum
How to use checksums
4ea38e06359e1618966a9369c3c2f74df9efa70634a52f1e91561fa2aba60fec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.1

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page