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 the PromptTemplate 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
Prompt Templates are fundamental building blocks in the framework and are extensively used in agent implementations.
Create templates with basic variable substitution and type validation.
Copy
Ask AI
import sysimport tracebackfrom pydantic import BaseModelfrom beeai_framework.errors import FrameworkErrorfrom beeai_framework.template import PromptTemplatedef main() -> None: class UserMessage(BaseModel): label: str input: str template: PromptTemplate[UserMessage] = PromptTemplate( schema=UserMessage, template="""{{label}}: {{input}}""", ) prompt = template.render(label="Query", input="What interesting things happened on this day in history?") print(prompt)if __name__ == "__main__": try: main() except FrameworkError as e: traceback.print_exc() sys.exit(e.explain())
This example creates a simple template that formats a user message with a label and input text. The Pydantic model or Zod schema ensures type safety for the template variables.
This example shows how to work with nested objects in templates. The Mustache syntax allows for iterating through the responses array and accessing properties of each object.
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
Copy
Ask AI
import sysimport tracebackfrom typing import Anyfrom pydantic import BaseModelfrom beeai_framework.errors import FrameworkErrorfrom beeai_framework.template import PromptTemplate, PromptTemplateInputdef main() -> None: class OriginalSchema(BaseModel): name: str objective: str original: PromptTemplate[OriginalSchema] = PromptTemplate( schema=OriginalSchema, template="""You are a helpful assistant called {{name}}. Your objective is to {{objective}}.""", ) def customizer(temp_input: PromptTemplateInput[Any]) -> PromptTemplateInput[Any]: new_temp = temp_input.model_copy() new_temp.template = f"""{temp_input.template} Your answers must be concise.""" new_temp.defaults["name"] = "Bee" return new_temp modified = original.fork(customizer=customizer) # You are a helpful assistant called Bee. Your objective is to fulfill the user needs. Your answers must be concise. prompt = modified.render(objective="fulfill the user needs") print(prompt)if __name__ == "__main__": try: main() except FrameworkError as e: traceback.print_exc() sys.exit(e.explain())
The framework’s agents use specialized templates to structure their behavior. You can customize these templates to alter how agents operate:
Python
Copy
Ask AI
import sysimport tracebackfrom beeai_framework.agents.react.runners.default.prompts import ( SystemPromptTemplate, ToolDefinition,)from beeai_framework.errors import FrameworkErrorfrom beeai_framework.tools.weather import OpenMeteoToolfrom beeai_framework.utils.strings import to_jsondef main() -> None: tool = OpenMeteoTool() tool_def = ToolDefinition( name=tool.name, description=tool.description, input_schema=to_json(tool.input_schema.model_json_schema()), ) # Render the granite system prompt prompt = SystemPromptTemplate.render( instructions="You are a helpful AI assistant!", tools=[tool_def], tools_length=1 ) print(prompt)if __name__ == "__main__": try: main() except FrameworkError as e: traceback.print_exc() sys.exit(e.explain())
This example demonstrates how to create a system prompt for an agent with tool definitions, which enables the agent to use external tools like weather data retrieval.Source: python/examples/templates/system_prompt.py