Python SDK for Mandatum - Prompt engineering platform with automatic LLM request logging
Project description
Mandatum Python SDK
Python SDK for Mandatum - prompt management with code-level customization.
Installation
pip install mandatum
# Or install with specific provider support
pip install mandatum[openai]
pip install mandatum[anthropic]
pip install mandatum[all] # All providers
Quick Start
OpenAI Integration
from mandatum import Mandatum
# Initialize Mandatum client
mandatum = Mandatum(api_key="md_xxxxx")
# Get wrapped OpenAI client
OpenAI = mandatum.openai.OpenAI
client = OpenAI()
# Use exactly like normal OpenAI SDK - automatically logged!
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello, world!"}],
# Optional: Mandatum-specific metadata
md_tags=["production", "feature-x"],
md_environment="production",
md_metadata={"user_id": "123"}
)
print(response.choices[0].message.content)
Anthropic Integration
from mandatum import Mandatum
mandatum = Mandatum(api_key="md_xxxxx")
# Get wrapped Anthropic client
Anthropic = mandatum.anthropic.Anthropic
client = Anthropic()
# Use normally - automatically logged!
response = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello, Claude!"}],
# Mandatum metadata
md_tags=["test"],
md_environment="development"
)
print(response.content[0].text)
Async Support
import asyncio
from mandatum import Mandatum
mandatum = Mandatum(api_key="md_xxxxx")
# Get wrapped async client
AsyncOpenAI = mandatum.openai.AsyncOpenAI
client = AsyncOpenAI()
async def main():
response = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
asyncio.run(main())
Configuration
Environment Variables
# Required
export MANDATUM_API_KEY="md_xxxxx"
# Optional
export MANDATUM_BASE_URL="https://api.mandatum.io/api/v1" # Default: http://localhost:8000/api/v1
Initialization Options
mandatum = Mandatum(
api_key="md_xxxxx", # API key (or set MANDATUM_API_KEY)
base_url="http://localhost:8000/api/v1", # Mandatum API URL
organization_id="org_xxxxx", # Optional organization ID
async_logging=True, # Log asynchronously (default: True)
debug=False # Enable debug logging (default: False)
)
Metadata Parameters
Add Mandatum-specific metadata to any LLM call:
md_tags: List of tags for filtering/grouping (e.g.,["production", "feature-x"])md_environment: Environment name (e.g.,"production","staging","development")md_metadata: Custom JSON metadata (e.g.,{"user_id": "123", "session_id": "abc"})md_prompt_id: UUID of associated prompt in Mandatummd_version_tag: Version tag for the promptmd_parent_request_id: UUID of parent request (for chained calls)
Features
- Transparent Wrapping: Use OpenAI/Anthropic SDKs normally with zero code changes
- Automatic Logging: All requests logged to Mandatum automatically
- Cost Tracking: Automatic cost calculation per request
- Latency Monitoring: Track request duration for every call
- Token Counting: Input/output token counts captured
- Error Logging: Failed requests logged with error details
- Async Support: Full async/await support for both OpenAI and Anthropic
- Background Logging: Non-blocking async logging doesn't slow down LLM calls
How It Works
- Mandatum wraps the native OpenAI/Anthropic SDK clients
- When you call
.create(), the wrapper intercepts the request - The actual LLM API call is made (locally, nothing sent to Mandatum yet)
- Response metadata (tokens, latency, cost) is captured
- Request data is sent to Mandatum API in a background thread (non-blocking)
- Original response is returned to your code immediately
Your API keys for OpenAI/Anthropic are never sent to Mandatum - all LLM requests are made directly from your machine.
Examples
With Prompt Management
from mandatum import Mandatum
import uuid
mandatum = Mandatum(api_key="md_xxxxx")
OpenAI = mandatum.openai.OpenAI
client = OpenAI()
# Link request to a Mandatum prompt
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Translate to French: Hello"}],
md_prompt_id=str(uuid.UUID("prompt-uuid-here")),
md_version_tag="v1.2.0"
)
With Request Chains
# First request
response1 = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What is Python?"}]
)
# Get request ID from Mandatum dashboard or API
parent_request_id = "request-uuid-from-first-call"
# Chained request
response2 = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Give me a code example"}],
md_parent_request_id=parent_request_id,
md_tags=["chained-request"]
)
Development
Local Installation
cd sdk/python
pip install -e .
Testing
# Set up environment
export MANDATUM_API_KEY="your_test_key"
export OPENAI_API_KEY="your_openai_key"
# Run test script
python test_sdk.py
Support
- Documentation: https://docs.mandatum.io
- Issues: https://github.com/mandatum/mandatum/issues
- Email: support@mandatum.io
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mandatum_sdk-0.1.0.tar.gz.
File metadata
- Download URL: mandatum_sdk-0.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c84547cfd1d38ce85e6f0341cee66dec919572619b37e78fc01ed05c6545dd2b
|
|
| MD5 |
bacc7e83ff2c12bd11fa6fe404adeb77
|
|
| BLAKE2b-256 |
10666e7d66b520fea96884182ab3f7f9f1ad94652c0018220a1b4a18cff355a8
|
File details
Details for the file mandatum_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mandatum_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9cf02a253f7454fd88172fb403157102938732028c560b47d7ed160663207c3
|
|
| MD5 |
01f1a78e7d480f6b7252a5fb3115ca5f
|
|
| BLAKE2b-256 |
ab194c6bfbb4563c4ecda87f098f2552185fdcfe4ab291e628fcd47a51d7c72a
|