Skip to main content

Python SDK for Inception Labs Mercury diffusion-LLM API

Project description

Mercury Client

Test Python 3.8+ License: MIT

A production-ready Python SDK for the Inception Labs Mercury diffusion-LLM API, providing both synchronous and asynchronous interfaces with full type safety.

Features

  • 🚀 Synchronous and Asynchronous Support - Use MercuryClient or AsyncMercuryClient based on your needs
  • 🔄 Automatic Retry Logic - Built-in exponential backoff with jitter for transient failures
  • 🎭 Full Type Safety - Complete type hints and runtime validation with Pydantic
  • 🌊 Streaming Support - Real-time streaming for chat completions
  • 🛡️ Robust Error Handling - Typed exceptions for different error scenarios
  • 🔧 Flexible Configuration - Customize timeouts, retries, and more
  • 📝 OpenAI-Compatible Interface - Familiar API design for easy migration

Installation

pip install mercury-client

Or install from source:

git clone https://github.com/hamzaamjad/mercury-client.git
cd mercury-client
pip install -e .

Getting Started

API Key Setup

First, obtain your API key from Inception Labs and set it as an environment variable:

# Using export (Linux/macOS)
export MERCURY_API_KEY="sk_your_api_key_here"

# Using set (Windows)
set MERCURY_API_KEY=sk_your_api_key_here

# Or add to your shell profile (.bashrc, .zshrc, etc.)
echo 'export MERCURY_API_KEY="sk_your_api_key_here"' >> ~/.bashrc

Alternatively, you can pass the API key directly when initializing the client:

from mercury_client import MercuryClient

client = MercuryClient(api_key="sk_your_api_key_here")

Quick Start

Basic Usage

from mercury_client import MercuryClient

# Initialize the client (uses MERCURY_API_KEY env var by default)
client = MercuryClient()

# Create a chat completion
response = client.chat_completion(
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is a diffusion model?"}
    ],
    model="mercury-coder-small"
)

print(response.choices[0].message.content)

Async Usage

import asyncio
from mercury_client import AsyncMercuryClient

async def main():
    async with AsyncMercuryClient() as client:
        response = await client.chat_completion(
            messages=[
                {"role": "user", "content": "Explain quantum computing"}
            ]
        )
        print(response.choices[0].message.content)

asyncio.run(main())

Streaming Chat Completion

# Synchronous streaming
for chunk in client.chat_completion_stream(
    messages=[{"role": "user", "content": "Write a story about AI"}],
    max_tokens=1000
):
    if chunk.choices[0].delta and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

# Async streaming
async for chunk in client.chat_completion_stream(
    messages=[{"role": "user", "content": "Write a poem"}]
):
    if chunk.choices[0].delta and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Fill-in-the-Middle (FIM) Completion

response = client.fim_completion(
    prompt="def fibonacci(",
    suffix="    return a + b",
    max_tokens=100
)

print(response.choices[0].text)

Advanced Usage

Custom Retry Configuration

from mercury_client import MercuryClient, RetryConfig

retry_config = RetryConfig(
    max_retries=5,
    initial_delay=2.0,
    max_delay=30.0,
    exponential_base=2.0,
    jitter=True
)

client = MercuryClient(
    api_key="your-api-key",
    retry_config=retry_config,
    timeout=60.0  # Request timeout in seconds
)

Error Handling

from mercury_client import MercuryClient
from mercury_client.exceptions import (
    AuthenticationError,
    RateLimitError,
    ServerError,
    EngineOverloadedError
)

try:
    response = client.chat_completion(
        messages=[{"role": "user", "content": "Hello"}]
    )
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limit exceeded. Retry after {e.retry_after} seconds")
except EngineOverloadedError:
    print("Service is temporarily overloaded")
except ServerError:
    print("Server error occurred")

Tool/Function Calling

from mercury_client.models import Tool, FunctionDefinition

tools = [
    Tool(
        type="function",
        function=FunctionDefinition(
            name="get_weather",
            description="Get current weather for a location",
            parameters={
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }
        )
    )
]

response = client.chat_completion(
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=tools,
    tool_choice="auto"
)

Configuration

Environment Variables

  • MERCURY_API_KEY - Your Mercury API key (primary)
  • INCEPTION_API_KEY - Alternative environment variable (for backward compatibility)

Client Options

Parameter Type Default Description
api_key str None API key for authentication
base_url str https://api.inceptionlabs.ai/v1 Base URL for the API
timeout float 30.0 Request timeout in seconds
retry_config RetryConfig Default config Retry behavior configuration

API Reference

MercuryClient / AsyncMercuryClient

Methods

  • chat_completion() - Create a chat completion
  • chat_completion_stream() - Create a streaming chat completion
  • fim_completion() - Create a fill-in-the-middle completion (coming soon)
  • close() - Close the HTTP client (also supports context manager)

Models

All models are fully typed with Pydantic:

  • ChatCompletionRequest / ChatCompletionResponse
  • FIMCompletionRequest / FIMCompletionResponse
  • Message, Tool, ToolCall, Usage, etc.

Exceptions

  • MercuryAPIError - Base exception for all API errors
  • AuthenticationError - Invalid or missing API key (401)
  • RateLimitError - Rate limit exceeded (429)
  • ServerError - Server error (500)
  • EngineOverloadedError - Service overloaded (503)

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/hamzaamjad/mercury-client.git
cd mercury-client

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=mercury_client --cov-report=html

# Run specific test file
pytest tests/test_client.py

# Run integration tests (requires MERCURY_API_KEY)
pytest tests/test_integration.py -v -m integration

Code Quality

# Format code
black mercury_client tests

# Sort imports
isort mercury_client tests

# Type checking
mypy mercury_client

# Linting
ruff mercury_client

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure:

  • All tests pass
  • Code is formatted with Black
  • Type hints are added for new code
  • Documentation is updated

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

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

mercury_api_client-0.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

mercury_api_client-0.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file mercury_api_client-0.1.0.tar.gz.

File metadata

  • Download URL: mercury_api_client-0.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for mercury_api_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d6f43dcbd33ad6afa6466e7d250b9c0502fe1fa7511b19c0eb578e2feaeb9d3
MD5 538c0e1288b5b56621e5809216c1ab38
BLAKE2b-256 070e0d359d503db71410f0731eef3215831e6bb7ec8497d456eaf534f431af13

See more details on using hashes here.

File details

Details for the file mercury_api_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mercury_api_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb459a51356ee776bb8024870de224adfcc17f848beccc1bfe73e2d369f1e1bd
MD5 bb00bb127ee8853cdb3c69b9dbc2dc41
BLAKE2b-256 983471173bb217ab876b3c3ffa6924458b3318a04cd4211733e26d82cede524b

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