Metadata-Version: 2.4
Name: drprompt
Version: 0.2.0
Summary: Python SDK for Dr-Prompt - A lightweight, version-controlled prompt management system
Author-email: Dr-Prompt Team <hello@doktur.ai>
License-Expression: MIT
Project-URL: Homepage, https://github.com/doktur-ai/dr-prompt
Project-URL: Documentation, https://github.com/doktur-ai/dr-prompt/tree/main/sdk/python
Project-URL: Repository, https://github.com/doktur-ai/dr-prompt
Project-URL: Issues, https://github.com/doktur-ai/dr-prompt/issues
Project-URL: Changelog, https://github.com/doktur-ai/dr-prompt/releases
Keywords: prompt,llm,ai,prompt-engineering,prompt-management,openai,gpt,claude
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.28.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: ruff>=0.1.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Requires-Dist: types-requests>=2.28.0; extra == "dev"
Dynamic: license-file

# drprompt

[![PyPI version](https://badge.fury.io/py/drprompt.svg)](https://pypi.org/project/drprompt/)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)

Python SDK for [Dr-Prompt](https://github.com/doktur-ai/dr-prompt) — a lightweight, version-controlled prompt management system.

Store, version, and serve your LLM prompts through a clean API. Use this SDK to fetch prompts at runtime, render templates, track token usage, and more.

## Installation

```bash
pip install drprompt
```

## Quick Start

```python
from drprompt import DrPrompt

# Authenticate with an API key (generate one from Settings > API Keys in the UI)
client = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")

# Fetch a prompt by UID
prompt = client.get_prompt("550e8400-e29b-41d4-a716-446655440000")

# Render template variables
rendered = prompt.render({"topic": "Python", "style": "concise"})
print(rendered)
```

## Usage with OpenAI

```python
from drprompt import DrPrompt
from openai import OpenAI

drp = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")
openai = OpenAI()

prompt = drp.get_prompt("550e8400-e29b-41d4-a716-446655440000")
system_prompt = prompt.render({"topic": "Python"})

response = openai.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "Explain decorators"}
    ]
)

# Report token usage back to Dr-Prompt
drp.report_usage(
    prompt=prompt.uid,
    model="gpt-4",
    input_tokens=response.usage.prompt_tokens,
    output_tokens=response.usage.completion_tokens
)
```

## Usage with Anthropic

```python
from drprompt import DrPrompt
import anthropic

drp = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")
claude = anthropic.Anthropic()

prompt = drp.get_prompt("550e8400-e29b-41d4-a716-446655440000")
system_prompt = prompt.render({"topic": "Python"})

response = claude.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    system=system_prompt,
    messages=[{"role": "user", "content": "Explain decorators"}]
)

drp.report_usage(
    prompt=prompt.uid,
    model="claude-sonnet-4-5",
    input_tokens=response.usage.input_tokens,
    output_tokens=response.usage.output_tokens
)
```

## Capture calls for evaluation

`log_call` records the LLM calls you make with a prompt — the variables you
rendered it with and the reply you got back. Captured calls can be exported
later and fed to an evaluation tool (for example when you are deciding whether
a newer or cheaper model can replace the one in production). Each prompt keeps
its most recent calls (5,000 by default, see `CAPTURE_MAX_CALLS_PER_PROMPT` on
the server).

`log_call` also records token usage, so for a given LLM call use **either**
`log_call` **or** `report_usage`, not both.

### One template, system + user

Keep the whole prompt in one Dr-Prompt entry, including the parts you would
otherwise hard-code around it. If you send the rendered template as the
*system* message and a separate *user* turn, pass that turn as `user_message`
so the export can reconstruct the full input the model saw.

### With OpenAI

```python
import time
from drprompt import DrPrompt
from openai import OpenAI

drp = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")
openai = OpenAI()

prompt = drp.get_prompt("550e8400-e29b-41d4-a716-446655440000")
variables = {"ticket": ticket_text, "tone": "neutral"}
rendered = prompt.render(variables)

started = time.monotonic()
response = openai.chat.completions.create(
    model="gpt-4o-2024-08-06",
    messages=[
        {"role": "system", "content": rendered},
        {"role": "user", "content": customer_message},
    ],
)

drp.log_call(
    prompt.uid,
    output=response.choices[0].message.content,
    variables=variables,
    version=prompt.current_version,
    model=response.model,
    input_tokens=response.usage.prompt_tokens,
    output_tokens=response.usage.completion_tokens,
    user_message=customer_message,
    latency_ms=int((time.monotonic() - started) * 1000),
)
```

### With Anthropic

```python
import time
from drprompt import DrPrompt
import anthropic

drp = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")
claude = anthropic.Anthropic()

prompt = drp.get_prompt("550e8400-e29b-41d4-a716-446655440000")
variables = {"ticket": ticket_text, "tone": "neutral"}
rendered = prompt.render(variables)

started = time.monotonic()
response = claude.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    system=rendered,
    messages=[{"role": "user", "content": customer_message}],
)

drp.log_call(
    prompt.uid,
    output=response.content[0].text,
    variables=variables,
    version=prompt.current_version,
    model=response.model,
    input_tokens=response.usage.input_tokens,
    output_tokens=response.usage.output_tokens,
    user_message=customer_message,
    latency_ms=int((time.monotonic() - started) * 1000),
)
```

### Export and clean up

```python
# Current version only, newest first (default limit 500, max 5000)
export = drp.export_calls(prompt.uid)
export["content"]      # the template of that version
export["variables"]    # its variable names, e.g. ["tone", "ticket"]
export["rows"][0]      # {"variables": {...}, "user_message": ..., "output": ..., "model": ..., ...}

# Every version, or one specific version
drp.export_calls(prompt.uid, version="all")
drp.export_calls(prompt.uid, version=2)

# Bare [{"input", "output", "model"}] with the template rendered per row,
# which generic trace importers accept as-is
rows = drp.export_calls(prompt.uid, format="generic")

# How many calls are captured (across all versions)
drp.get_prompt(prompt.uid).call_count

# Delete captured calls — all of them, or one version's
drp.delete_calls(prompt.uid)
drp.delete_calls(prompt.uid, version=1)
```

## Authentication

### API key (recommended)

Generate a key from the Dr-Prompt web UI under **Settings > API Keys**:

```python
client = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")
```

Or load from an environment variable:

```python
import os
client = DrPrompt("https://your-instance.com", api_key=os.environ["DRPROMPT_API_KEY"])
```

### Username / password

```python
client = DrPrompt("https://your-instance.com")
client.login("username", "password")
```

### JWT token

```python
client = DrPrompt("https://your-instance.com", token="your-jwt-token")
```

## Features

### Prompt CRUD

```python
# List all prompts
for p in client.list_prompts():
    print(f"{p.uid}: {p.name}")

# Create
prompt = client.create_prompt(
    name="Code Reviewer",
    content="You are a {{language}} code reviewer. Review for {{focus}}.",
    description="Reviews code for best practices"
)

# Update (creates a new version)
client.update_prompt(prompt.uid, content="Updated content...")

# Delete
client.delete_prompt(prompt.uid)
```

### Version History

```python
versions = client.get_prompt_versions(prompt.uid)
v1 = client.get_prompt_version(prompt.uid, version=1)
print(v1.content)
```

### Template Variables

```python
prompt = client.get_prompt("550e8400-e29b-41d4-a716-446655440000")

# Extract variable names
variables = prompt.get_variables()  # ["language", "focus"]

# Render
rendered = prompt.render({"language": "Python", "focus": "security"})
```

### Collections

```python
collections = client.list_collections()

collection = client.create_collection(
    name="Code Review Prompts",
    description="Prompts for code review tasks",
    color="#00ff88"
)

client.add_prompts_to_collection(collection.id, prompt_ids=[1, 2, 3])
```

### Usage Tracking

```python
# Report usage after an LLM call
client.report_usage(
    prompt=prompt.uid,
    model="gpt-4",
    input_tokens=150,
    output_tokens=320
)

# Get stats
stats = client.get_usage_stats()
print(f"Total calls: {stats.total_calls}")
print(f"Total tokens: {stats.total_tokens}")
```

### Smart Features

Requires an OpenAI API key configured on your Dr-Prompt instance.

```python
# Analyze prompt quality
analysis = client.analyze_prompt(prompt.uid)

# Get AI-suggested improvements
improved = client.improve_prompt(prompt.uid, focus="clarity")

# Generate variations
variations = client.generate_variations(prompt.uid, count=3, style="formal")
```

## Error Handling

```python
from drprompt import DrPrompt, AuthenticationError, NotFoundError, APIError

client = DrPrompt("https://your-instance.com", api_key="drp_your_api_key")

try:
    prompt = client.get_prompt("nonexistent")
except AuthenticationError:
    print("Invalid or expired API key")
except NotFoundError:
    print("Prompt not found")
except APIError as e:
    print(f"API error ({e.status_code}): {e.message}")
```

## Configuration

```python
client = DrPrompt(
    base_url="https://your-instance.com",
    api_key="drp_your_api_key",  # or token="jwt-token"
    timeout=30,                   # request timeout in seconds
)
```

## Links

- [Dr-Prompt GitHub](https://github.com/doktur-ai/dr-prompt) — self-host the server
- [API Docs](https://your-instance.com/docs) — Swagger UI on every instance at `/docs`
- [Issues](https://github.com/doktur-ai/dr-prompt/issues) — bug reports and feature requests

## License

MIT
