Skip to main content

Unified Python client for multiple LLM providers (OpenAI, Anthropic, etc.) with drop-in compatibility.

Project description

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

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

unifyllm_sdk-0.1.2.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unifyllm_sdk-0.1.2-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file unifyllm_sdk-0.1.2.tar.gz.

File metadata

  • Download URL: unifyllm_sdk-0.1.2.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for unifyllm_sdk-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4c868f7fbedb5a21f5970527f1c562024e18ab34480a1b009218a2680222cee2
MD5 f070b01549df70ec10b77ad52417c131
BLAKE2b-256 410f8da635c235468dea5fd8fede77af7d0f7cfcb489e721b6e4a7b941f41cd9

See more details on using hashes here.

File details

Details for the file unifyllm_sdk-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: unifyllm_sdk-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for unifyllm_sdk-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 513d0fb40c355931c0ccd3428be6df6a7ae32fc02a1116fbd7351afdb8a8e5e2
MD5 540d6074693b96dc53bb85e70a7ca543
BLAKE2b-256 6f8a94597b4418c77ec39fe85ab0f05c9d21bbc2ce49bbaecb93ad7af253b24b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page