Skip to main content

Mantis SDK

Project description

Mantis SDK

Python SDK for sending chat completion requests to the Mantis LLM gateway /v1/chat/completions endpoint.

Requirements

  • Python 3.12+
  • A running Mantis service
  • A gateway API token

Install

pip install mantis_gw

Development

Use uv sync only when working on this repository locally. It uses the Python version in .python-version, creates the virtual environment, and installs this SDK from the local source tree with the dependencies pinned in uv.lock.

uv sync

Non-streaming

url should be the gateway base URL and should not include /v1/chat/completions. The SDK appends that endpoint path automatically.

import asyncio

from mantis_gw import gateway


async def main() -> None:
    client = gateway.Gateway(
        url="https://gateway.example.com",
        token="gw_token-id_token-secret",
    )

    response = await client.send(
        {
            "messages": [
                {"role": "user", "content": "Write a short project summary."},
            ],
            "stream": False,
            "temperature": 0.5,
            "max_tokens": 256,
            "system": "Answer clearly and concisely.",
        },
        metadata={"task-type": "summarization"},
    )

    print(response)


asyncio.run(main())

Streaming

When stream is True, send() returns an async iterator of text chunks.

import asyncio

from mantis_gw import gateway


async def main() -> None:
    client = gateway.Gateway(
        url="https://gateway.example.com",
        token="gw_token-id_token-secret"
    )

    chunks = await client.send(
        {
            "messages": [
                {"role": "user", "content": "Write a short project summary."},
            ],
            "stream": True,
            "temperature": 0.5,
            "max_tokens": 256,
            "system": "Answer clearly and concisely.",
        },
        metadata={"task-type": "summarization"},
    )

    async for chunk in chunks:
        print(chunk, end="")


asyncio.run(main())

Request Payload

The SDK sends the request payload to llm-gateway as-is. Gateway-side validation still applies to the endpoint:

  • messages is required and must contain at least one message.
  • Each message must only contain role and content.
  • role must be "user" or "assistant".
  • content must be a non-empty string after trimming whitespace.
  • Extra top-level request fields are rejected.
  • stream defaults to False.
  • temperature can be omitted, None, or a number from 0.0 to 2.0.
  • max_tokens can be omitted, None, or an integer greater than 0.
  • system can be omitted, None, or a non-empty string after trimming whitespace.

Metadata

Pass routing metadata with the metadata keyword argument. Keys and values must be strings. The SDK sends it as the gateway's metadata HTTP header.

await client.send(
    {
        "messages": [{"role": "user", "content": "Generate a Python function."}],
        "stream": False,
    },
    metadata={"task-type": "code_generation"},
)

Errors

Gateway responses with 4xx or 5xx status codes raise httpx.HTTPStatusError.

Integration Tests

The integration tests read gateway credentials from a local .env file in the repo root:

MANTIS_GATEWAY_URL=https://gateway.example.com
MANTIS_GATEWAY_TOKEN=gw_token-id_token-secret

Run them with:

uv run pytest

The .env file is ignored by Git so real credentials stay local.

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

mantis_gw-1.0.2.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

mantis_gw-1.0.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file mantis_gw-1.0.2.tar.gz.

File metadata

  • Download URL: mantis_gw-1.0.2.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mantis_gw-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9e2b7bb9bf95b3408a4baf0c02b8da404dd815b4098eec2c8a5f08765fe3c957
MD5 91bb87fd16cbcec89d6cca81ac6e78ee
BLAKE2b-256 127d4773f1c20948ee8323d22037d031c3954fb38a6d686199c55ec00b8e7012

See more details on using hashes here.

File details

Details for the file mantis_gw-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: mantis_gw-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mantis_gw-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 083ddc416405f408f9fa9c11079425feefd7ec2d5fa853563326cf1338131845
MD5 9a201059d77f65e6e82f95a2208de34e
BLAKE2b-256 9d053d35e09e6c5f250f2581a93728de2435ad50f65732d0a8486330140aa7f2

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