Skip to main content

Creative Services

Shared AI-powered creative writing services for BF Agent, Travel Beat, and other platform apps.

Overview

This package provides modular, reusable creative AI services:

  • Prompt Template System - Type-safe prompt management with Pydantic schemas
  • LLM Client - Unified client for OpenAI, Anthropic, Groq, Ollama
  • LLM Registry - DB-driven LLM configuration (switch models without code changes)
  • Dynamic Client - Tier-based LLM selection (economy/standard/premium)
  • Usage Tracker - Token and cost tracking
  • Security - Prompt injection detection and input sanitization
  • Caching - Redis and in-memory caching with TTL
  • Observability - Prometheus metrics and structured events

Installation

# Basic installation
pip install -e ../platform/packages/creative-services

# With OpenAI support
pip install -e ../platform/packages/creative-services[openai]

# With all LLM providers
pip install -e ../platform/packages/creative-services[all]

Quick Start

Simple Usage (Environment Variables)

from creative_services import DictRegistry, DynamicLLMClient, LLMTier
import asyncio

# Create registry from environment variables
registry = DictRegistry.from_env()
client = DynamicLLMClient(registry)

# Generate with tier-based selection
async def main():
    response = await client.generate(
        prompt="Write a short story about a traveler",
        system_prompt="You are a creative writer",
        tier=LLMTier.STANDARD,  # Uses GPT-4o-mini or Claude Sonnet
    )
    print(response.content)

asyncio.run(main())

Django Integration (BFAgent/TravelBeat)

from creative_services.adapters import DjangoLLMRegistry, BFAgentLLMBridge
from apps.bfagent.models import Llms

# Option 1: Use Django-backed registry
registry = DjangoLLMRegistry(Llms)
client = DynamicLLMClient(registry)

# Option 2: Use compatibility bridge (no code changes needed)
bridge = BFAgentLLMBridge.with_django_registry(Llms)
result = bridge.generate_text("Hello", tier="premium")

Backward Compatibility (Drop-in Replacement)

# Replace existing BFAgent imports:
# from apps.bfagent.services.llm_client import generate_text

# With:
from creative_services.adapters import generate_text

# Same API, uses new infrastructure
result = generate_text("Hello", tier="standard")

Architecture

creative_services/
├── prompts/                    # 🆕 Prompt Template System
│   ├── schemas/                # Pydantic schemas (frozen, immutable)
│   │   ├── variables.py        # PromptVariable, VariableType
│   │   ├── llm_config.py       # LLMConfig, RetryConfig
│   │   ├── template.py         # PromptTemplateSpec
│   │   └── execution.py        # PromptExecution, ExecutionStatus
│   ├── security/               # Injection detection & sanitization
│   ├── registry/               # Template storage backends
│   │   ├── memory.py           # InMemoryRegistry
│   │   ├── file.py             # FileRegistry (YAML/JSON)
│   │   └── django_registry.py  # DjangoRegistry
│   ├── execution/              # Execution engine
│   │   ├── executor.py         # PromptExecutor
│   │   ├── renderer.py         # Jinja2 template rendering
│   │   ├── cache.py            # InMemoryCache
│   │   ├── redis_cache.py      # RedisCache
│   │   └── retry.py            # Retry with backoff
│   ├── observability/          # Metrics & events
│   ├── migration/              # BFAgent compatibility
│   └── integration/            # Ready-to-use clients
│       ├── bfagent.py          # BFAgent integration
│       └── llm_clients.py      # OpenAI, Anthropic clients
├── core/                       # Core LLM infrastructure
├── adapters/                   # Django adapters
├── character/                  # Character generation
├── world/                      # World building
└── story/                      # Story generation

LLM Tiers

Tier Models Use Case
ECONOMY GPT-3.5, Claude Haiku, Groq Llama Simple tasks, high volume
STANDARD GPT-4o-mini, Claude Sonnet Balanced quality/cost
PREMIUM GPT-4o, Claude Opus Complex tasks, best quality
LOCAL Ollama (Llama, Mistral) Offline, privacy-sensitive

Supported LLM Providers

  • OpenAI (GPT-4o, GPT-4o-mini, GPT-3.5)
  • Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku)
  • Groq (Llama 3.3 70B - fast & free)
  • Ollama (Local models)

Migration Guide

For BFAgent

  1. Install creative-services: pip install -e ../platform/packages/creative-services
  2. Existing code continues to work (no changes needed)
  3. New code can use DynamicLLMClient directly
  4. Gradually migrate old code to new API

For TravelBeat

  1. Install creative-services
  2. Replace direct OpenAI calls with DynamicLLMClient
  3. Use LLMTier.STANDARD for story generation

Prompt Template System (New!)

Type-safe prompt management with 195 tests:

from creative_services.prompts import (
    PromptTemplateSpec,
    PromptVariable,
    PromptExecutor,
    InMemoryRegistry,
)
from creative_services.prompts.integration import OpenAIClient

# Define template
template = PromptTemplateSpec(
    template_key="greeting.v1",
    domain_code="examples",
    name="Greeting",
    system_prompt="You are friendly.",
    user_prompt="Say hello to {{ name }}!",
    variables=[PromptVariable(name="name", required=True)],
)

# Save and execute
registry = InMemoryRegistry()
registry.save(template)

executor = PromptExecutor(
    registry=registry,
    llm_client=OpenAIClient(),
    app_name="my_app",
)

result = await executor.execute(
    template_key="greeting.v1",
    variables={"name": "Alice"},
)
print(result.content)

BFAgent Integration

from creative_services.prompts.integration import create_bfagent_executor

executor = create_bfagent_executor()
result = await executor.execute(
    template_key="character.backstory.v1",
    variables={"name": "Elara", "genre": "fantasy"},
)

Documentation

Full documentation available at docs/:

cd docs && make html
# Open _build/html/index.html

License

MIT

Metadata

Release files for iil-creative-services 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iil-creative-services 0.3.0
File Size Uploaded Provenance
iil_creative_services-0.3.0.tar.gz 101.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for iil-creative-services 0.3.0
File Interpreter ABI Platform
iil_creative_services-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 200.5 kB

Release files / iil_creative_services-0.3.0.tar.gz

Download URL iil_creative_services-0.3.0.tar.gz
Size 101.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8dfa75c1f5541aed9e31f57f364558f4d3a5c2ac4d47e0dcaabb4d3c10ce4d36
BLAKE2b-256 checksum
How to use checksums
369de170b785e331440e8d1456bbc73750c56086b1e74a9f29b0431cd46d5be7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / iil_creative_services-0.3.0-py3-none-any.whl

Download URL iil_creative_services-0.3.0-py3-none-any.whl
Size 98.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee43fe920a05bb21264768901a89f1a4bb5aeecabf01b576c8b731be85a604a5
BLAKE2b-256 checksum
How to use checksums
fb315c5b3e32996fa0b265ec44f87a1980bebbf1c38ac9272f3f702469afe890
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for iil-creative-services 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page