Templates
Templates allow to merge the user instructions with inputs from previous step before executing the agent. By using templates, you can ensure that your agents receive inputs in a controlled flow.
Writing Your Own Template
To create a custom template, your class should extend the Template abstract class and implement the apply_input and apply_output methods. While apply_output can be left unimplemented if not needed, apply_input is crucial for formatting the task input.
TextInputTemplate Example
The TextInputTemplate is a example for text-based inputs.
class TextInputTemplate(Template):
def __init__(self, template_text: str, previous_input_tag='PREVIOUS_ANALYSIS', user_request_tag='CURRENT_TASK'):
# Keep the plain instruction to use it alone when there is no input
self.instruction = None
# Ensure the template text includes placeholders for dynamic content
if '{0}' not in template_text:
self.instruction = template_text.strip()
context = previous_input_tag + ': {0}\n'
request = user_request_tag + ': ' + template_text
template_text = context + request
self.template_text = template_text.strip()
def apply_input(self, data):
# Without input, use the instruction alone
if data is None:
if self.instruction is not None:
return self.instruction
return self.template_text.replace('{0}', '')
# Put the input in place of the placeholder
return self.template_text.replace('{0}', str(data))
def apply_output(self, data):
# This method can be left unimplemented if output manipulation is not required
pass
Example
Prepare the template instance.
from intelli.flow.template.basic_template import TextInputTemplate
# define the template text, {0} is replaced with the input from the previous step
template_text = "Context: {0}\nuser request: analyze the sentiment of the context"
# create an instance of TextInputTemplate
text_template = TextInputTemplate(template_text)
Initiate the Task
from intelli.flow.tasks.task import Task
from intelli.flow.input.agent_input import TextAgentInput
from intelli.flow.agents.agent import Agent
# create the agent
text_agent = Agent(
agent_type='text',
provider='openai',
mission='analyze sentiment',
model_params={'key': 'YOUR_OPENAI_API_KEY', 'model': 'gpt-3'}
)
# create the task and pass the template
task = Task(task_input=TextAgentInput("This is a sample text."), agent=text_agent, template=text_template, log=True)
Get the next agent guide in your inbox.
Be the first to read new guides on AI agents, RAG and MCP apps.