Overview
Templates are predefined structures used to create consistent outputs. In the context of AI applications, prompt templates provide structured guidance for language models to generate targeted responses. They include placeholders that can be filled with specific information at runtime. The Framework implements this functionality through thePromptTemplate class, which uses Mustache-style syntax (via the chevron library) for variable substitution. The implementation adds type safety and validation using Pydantic or Zod schemas.
At its core, the PromptTemplate class:
- Validates input data against a Pydantic model schema
- Handles template variable substitution
- Supports dynamic content generation through callable functions
- Provides default values for optional fields
- Enables template customization through forking
Supported in Python and TypeScript.
Basic usage
Simple template
Create templates with basic variable substitution and type validation.Template functions
Add dynamic content to templates using custom functions.- The
format_metafunction returns the date and author in a readable string - Functions can be called directly from the template using Mustache-style syntax
Working with objects
Handle complex nested data structures in templates with proper type validation.Working with arrays
Process collections of data within templates for dynamic list generation.Template forking
The fork() method allows you to create new templates based on existing ones, with customizations. Template forking is useful for:- Creating variations of templates while maintaining core functionality
- Adding new fields or functionality to existing templates
- Specializing generic templates for specific use cases
Python
Default values
Provide default values for template variables that can be overridden at runtime.Using templates with agents
The framework’s agents use specialized templates to structure their behavior. You can customize these templates to alter how agents operate:Python
Examples
Python
Explore reference template implementations in Python
TypeScript
Explore reference template implementations in TypeScript