A production-ready template for building AI agents with streaming capabilities, conversation management, and enterprise-grade features.
- Simplified Streaming API: Clean, consistent event format for easy client integration
- Real-time Streaming: Server-Sent Events (SSE) with token and message streaming
- Multiple Client Examples: TypeScript, Python async, and Streamlit demo applications
- Conversation Management: Multi-turn conversations with thread persistence
- Enterprise Integration: Langfuse tracing, PostgreSQL checkpointing, SSO support
- Modular Architecture: AgentManager abstraction with clean separation of concerns
- Production Ready: Health checks, error handling, and comprehensive logging
- Google AI Integration: Built-in support for Google Generative AI models
graph TB
subgraph "Client"
UI[Web UI]
API[API Client]
end
subgraph "Template Agent"
subgraph "API Layer"
Health[Health Check]
Stream[Stream Chat]
History[Chat History]
Threads[Thread Management]
Feedback[Feedback]
end
subgraph "Core Layer"
Agent[Agent Engine]
Utils[Message Utils]
Prompt[Prompt Management]
end
subgraph "Data Layer"
DB[(PostgreSQL)]
Langfuse[Langfuse]
end
subgraph "External Services"
Google[Google AI]
SSO[SSO Auth]
end
end
UI --> Health
UI --> Stream
UI --> History
UI --> Threads
UI --> Feedback
API --> Health
API --> Stream
API --> History
API --> Threads
API --> Feedback
Stream --> Agent
Agent --> Utils
Agent --> Prompt
Agent --> Google
History --> DB
Threads --> DB
Agent --> DB
Agent --> Langfuse
Feedback --> Langfuse
The Template Agent now features a simplified streaming API that makes client integration easier while preserving all enterprise features:
POST /v1/stream
Content-Type: application/json
Accept: text/event-stream
{
"message": "User input message",
"thread_id": "conversation-thread-id",
"session_id": "session-id",
"user_id": "user-identifier",
"stream_tokens": true
}
{"type": "message", "content": {"type": "ai", "content": "", "tool_calls": [...]}}
{"type": "token", "content": "Hello"}
{"type": "token", "content": " world"}
{"type": "message", "content": {"type": "ai", "content": "Hello world"}}
[DONE]
Ready-to-use client examples are available in the examples/
directory:
- Streamlit Demo App - Interactive chat application
- Python Async Client - Server-to-server integration
See the examples README for detailed usage instructions.
- Python 3.12+
- PostgreSQL database
- Google AI API credentials
- Langfuse account (optional)
-
Clone the repository
git clone <repository-url> cd template-agent
-
Create virtual environment
uv venv source .venv/bin/activate
-
Install dependencies
uv pip install -e ".[dev]"
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration
-
Run template-mcp-server following https://github.com/redhat-data-and-ai/template-mcp-server
-
Run pgvector
podman-compose up pgvector # Edit .env with your configuration
-
Run the application
python -m template_agent.src.main
Endpoint | Method | Description |
---|---|---|
/health |
GET | Health check |
/v1/stream |
POST | Stream chat responses |
/v1/history/{thread_id} |
GET | Get conversation history |
/v1/threads/{user_id} |
GET | List user threads |
/v1/feedback |
POST | Record feedback |
curl -X POST "http://localhost:8081/v1/stream" \
-H "Content-Type: application/json" \
-d '{
"message": "Hello, how can you help me?",
"thread_id": "thread_123",
"user_id": "user_456",
"stream_tokens": true
}'
curl "http://localhost:8081/health"
# Response: {"status": "healthy", "service": "Template Agent"}
AGENT_HOST
: Server host (default: 0.0.0.0)AGENT_PORT
: Server port (default: 8081)PYTHON_LOG_LEVEL
: Logging level (default: INFO)
POSTGRES_USER
: Database username (default: pgvector)POSTGRES_PASSWORD
: Database password (default: pgvector)POSTGRES_DB
: Database name (default: pgvector)POSTGRES_HOST
: Database host (default: pgvector)POSTGRES_PORT
: Database port (default: 5432)
LANGFUSE_TRACING_ENVIRONMENT
: Langfuse environment (default: development)GOOGLE_SERVICE_ACCOUNT_FILE
: Google credentialsSSO_CALLBACK_URL
: SSO callback URLAGENT_SSL_KEYFILE
: SSL private key pathAGENT_SSL_CERTFILE
: SSL certificate path
# .env file
AGENT_HOST=0.0.0.0
AGENT_PORT=8081
PYTHON_LOG_LEVEL=INFO
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword
POSTGRES_DB=template_agent
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
LANGFUSE_TRACING_ENVIRONMENT=production
GOOGLE_SERVICE_ACCOUNT_FILE=/path/to/credentials.json
# Run all tests
pytest
# Run with coverage
pytest --cov=template_agent.src --cov-report=html
# Run specific test file
pytest tests/test_prompt.py -v
Current test coverage includes:
- β Core utilities (prompt, agent_utils)
- β Data models (schema)
- β Configuration (settings)
- β API endpoints (health, feedback)
- π Complex routes (history, stream, threads)
- π Application setup (api, main, agent)
# Start with Docker Compose
podman-compose up -d --build
- SSL/TLS: Configure SSL certificates for HTTPS
- Database: Use managed PostgreSQL service
- Monitoring: Set up Langfuse for tracing
- Scaling: Configure horizontal pod autoscaling
- Security: Implement proper authentication
template-agent/
βββ template_agent/
β βββ src/
β βββ core/ # Core agent functionality
β β βββ agent.py # Agent initialization
β β βββ agent_utils.py # Message utilities
β β βββ prompt.py # Prompt management
β βββ routes/ # API endpoints
β β βββ health.py # Health checks
β β βββ stream.py # Streaming chat
β β βββ history.py # Chat history
β β βββ threads.py # Thread management
β β βββ feedback.py # Feedback recording
β βββ api.py # FastAPI application
β βββ main.py # Application entry point
β βββ schema.py # Data models
β βββ settings.py # Configuration
βββ tests/ # Test suite
βββ README.md # This file
# Run linting
ruff check .
# Run type checking
mypy template_agent/src/
# Run formatting
ruff format .
# Run pre-commit hooks
pre-commit run --all-files
-
Create feature branch
git checkout -b feature/new-feature
-
Implement changes
- Follow Google docstring format
- Add type hints
- Write tests for new functionality
-
Run quality checks
pre-commit run --all-files pytest
-
Submit pull request
- Include tests
- Update documentation
- Follow commit message conventions
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Python: Follow PEP 8 and use type hints
- Documentation: Use Google docstring format
- Tests: Maintain >80% code coverage
- Commits: Use conventional commit messages
This template includes .cursor/rules.md
- a comprehensive development guide specifically designed to help AI coding assistants understand and work effectively with this MCP server template.
- Issues: GitHub Issues
- LangChain - LLM application framework
- LangGraph - Stateful LLM applications
- FastAPI - Modern web framework
- Langfuse - LLM observability platform
Built with β€οΈ by the Red Hat Data & AI team