Prompt Strategy and Prompt Template
The PromptStrategy
object is responsible for assembling the messages that are sent to the language model. The PromptStrategy
object is initialized with a prompt template.
class PromptStrategy(ABC):
"""
Interface for the prompt strategy object.
"""
def __init__(self,
system_template: List[PromptTemplate] | \
PromptTemplate
) -> None:
"""
Initialize the PromptStrategy object with
prompt template.
:param system_template: The system template
to use.
"""
if isinstance(system_template, PromptTemplate):
self._system_template = [system_template]
else:
self._system_template = system_template
@abstractmethod
def render(
self,
instruction: str,
history: List[Message] | None = None
) -> List[Message]:
"""
Prepare the list of message for the given
input and history using the system template.
:param instruction: The input to process.
:param history: The history of messages.
:return: The list of messages to send to LLM.
"""
Prompt Template and Templates
The PromptTemplate
object is responsible for defining the template that is used to assemble the messages.
class PromptTemplate(ABC):
"""
Abstract class for prompt templates.
Templates will be rendered using the
dictionary context that includes all the
variables needed for the template.
Two types of templates are supported: file
templates and string templates.
"""
def __init__(
self,
prompt_context: Dict[str, Any]
) -> None:
"""
Constructor for the PromptTemplate class.
The context is a dictionary that includes all
the variables needed for the template. This is
an abstract class and should not be
instantiated directly.
:param context: Dictionary with all
the variables needed for the template
"""
self._context = prompt_context
@abstractmethod
def render(self) -> str:
"""
Abstract method to render the template.
All variables in the template
will be replaced by the values in
the context dictionary.
"""
There are two types of templates supported: file templates and string templates. The file template is a template that is stored in a file and the string template is a template that is stored as a string.
class FileTemplate(PromptTemplate):
def __init__(self,
prompt_context: Dict[str, str],
file_name: str
) -> None
and class StringTemplate(PromptTemplate):
def __init__(self,
prompt_context: Dict[str, str],
template: str
) -> None
are two classes that inherit from the PromptTemplate
class. The FileTemplate
class is used to load the template from a file and the StringTemplate
class is used to load the template from a string.
Prompt Template Example
context = {
"agent_name": "ResearchAgent",
"agent_persona": "Researcher always following scientific method",
}
CHECK = """This is a test template for ResearchAgent. Researcher always following scientific method."""
# String template
TEMPLATE = "This is a test template for {agent_name}. {agent_persona}."
string_template = StringTemplate(
prompt_context=context,
template=TEMPLATE
)
final_string_prompt = string_template.render()
assert final_string_prompt == CHECK
In the example above, we show how given template defines variables that are rendered during the rendering. Both templates from string and file have the same functionality.
templates
directory includes sample template files, e.g. file Simple.template
is:
You are: {{ agent_persona }}.
Follow users instruction. Do this on a stepwise basis and double-check each step, one at a time.