LLM integration for BFAgent Hub ecosystem — DB-driven routing, LiteLLM backend, resilient adapters (ADR-089)
Project description
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
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 iil_bfagent_llm-1.0.1.tar.gz.
File metadata
- Download URL: iil_bfagent_llm-1.0.1.tar.gz
- Upload date:
- Size: 31.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f8af86aa23832f842895a8599e93d7c91a7b5009d168543b943793bfbff1d7e
|
|
| MD5 |
8c33503150ecdf091bbd6a35540ba1e2
|
|
| BLAKE2b-256 |
6490bd18abc3297abd2b2a0c8a3332a39b8e949f45351ddaba1613eadc1edf19
|
File details
Details for the file iil_bfagent_llm-1.0.1-py3-none-any.whl.
File metadata
- Download URL: iil_bfagent_llm-1.0.1-py3-none-any.whl
- Upload date:
- Size: 32.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92704f7649e32c9bf80b665da89e13c52f3d8a5faf7036ee52cc33108cdcde9a
|
|
| MD5 |
864badd5017bc6abcc399a65317e7a45
|
|
| BLAKE2b-256 |
377e743b202c83c390c4b13617cde842f8810501f1976f48cdc4239d45818852
|