Skip to main content

bfagent-llm

LLM integration for the BFAgent Hub ecosystem.

Features

  • Prompt Framework: DB-driven prompt templates with Jinja2 rendering
  • Secure Template Engine: Sandboxed rendering with security validations
  • Multi-Layer Caching: L1 (local) + L2 (Redis) + L3 (DB) caching
  • Resilient Service: Retry, circuit breaker, tier fallback
  • LLM Adapters: Gateway, OpenAI, Anthropic, Fallback

Installation

pip install bfagent-llm
# or with all providers
pip install "bfagent-llm[all]"
# or from git
pip install "bfagent-llm @ git+https://github.com/achimdehnert/platform.git#subdirectory=packages/bfagent-llm"

Quick Start

from bfagent_llm import PromptFramework

# Get singleton instance
framework = PromptFramework.get_instance()

# Execute a prompt template
result = await framework.execute(
    template_code="expert_hub.generate_content",
    context={"topic": "Python async programming"},
    tier="standard",
)
print(result.content)

Components

SecureTemplateEngine

Sandboxed Jinja2 rendering with security features:

  • Blocks dangerous patterns (eval, exec, __dunder__)
  • Context sanitization
  • JSON schema validation
  • Custom safe filters
from bfagent_llm import SecureTemplateEngine

engine = SecureTemplateEngine()
result = engine.render(
    template="Hello {{ name }}!",
    context={"name": "World"},
)
print(result.rendered)  # "Hello World!"

PromptRegistry

Multi-layer caching for prompt templates:

from bfagent_llm import CachedPromptRegistry

registry = CachedPromptRegistry()
template = registry.get("expert_hub.generate_content", tenant_id=tenant_id)

ResilientPromptService

LLM calls with resilience patterns:

from bfagent_llm import ResilientPromptService, GatewayLLMAdapter

adapter = GatewayLLMAdapter(base_url="http://llm-gateway:8100")
service = ResilientPromptService(llm_client=adapter)

result = await service.execute(
    system_prompt="You are a helpful assistant.",
    user_prompt="Explain quantum computing.",
    tier="standard",
)

LLM Adapters

from bfagent_llm import (
    GatewayLLMAdapter,    # BFAgent LLM Gateway
    OpenAILLMAdapter,     # Direct OpenAI API
    AnthropicLLMAdapter,  # Direct Anthropic API
    FallbackLLMAdapter,   # Chain with fallback
)

# Gateway (recommended for production)
gateway = GatewayLLMAdapter(base_url="http://llm-gateway:8100")

# Fallback chain
fallback = FallbackLLMAdapter([
    GatewayLLMAdapter(base_url="http://llm-gateway:8100"),
    OpenAILLMAdapter(api_key="..."),
])

Django Integration

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "bfagent_llm",
]

Settings:

# LLM Gateway
LLM_GATEWAY_URL = "http://llm-gateway:8100"
LLM_GATEWAY_TIMEOUT = 120.0

# Direct API (optional)
OPENAI_API_KEY = "..."
ANTHROPIC_API_KEY = "..."

# Caching
REDIS_URL = "redis://localhost:6379/0"

License

MIT

Metadata

Release files for iil-bfagent-llm 1.0.1

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-bfagent-llm 1.0.1
File Size Uploaded Provenance
iil_bfagent_llm-1.0.1.tar.gz 31.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for iil-bfagent-llm 1.0.1
File Interpreter ABI Platform
iil_bfagent_llm-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 63.6 kB

Release files / iil_bfagent_llm-1.0.1.tar.gz

Download URL iil_bfagent_llm-1.0.1.tar.gz
Size 31.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3f8af86aa23832f842895a8599e93d7c91a7b5009d168543b943793bfbff1d7e
BLAKE2b-256 checksum
How to use checksums
6490bd18abc3297abd2b2a0c8a3332a39b8e949f45351ddaba1613eadc1edf19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / iil_bfagent_llm-1.0.1-py3-none-any.whl

Download URL iil_bfagent_llm-1.0.1-py3-none-any.whl
Size 32.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92704f7649e32c9bf80b665da89e13c52f3d8a5faf7036ee52cc33108cdcde9a
BLAKE2b-256 checksum
How to use checksums
377e743b202c83c390c4b13617cde842f8810501f1976f48cdc4239d45818852
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for iil-bfagent-llm 1.0.1

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

1.0.1 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