Prompt API
Filters
Filters are Python functions that are called during the rendering of a certain tag. For example, if a prompt template contains the tag:
a Python function named upper
(in this case provided by Jinja) will be called passing the string 'hello' as a
parameter, and its return value will replace the tag in the final text:
In addition to all the builtin filters provided by Jinja, Banks supports the following ones, specific for prompt engineering.
tool
Inspect a Python callable and generates a JSON-schema ready for LLM function calling.
Important
This filter only works when used within a {% completion %}
block.
cache_control
Wrap the filtered value into a ContentBlock with the proper cache_control field set.
The resulting ChatMessage will have the field content
populated with a list of ContentBlock objects.
Example
Important
this filter marks the content to cache by surrounding it with <content_block>
and
</content_block>
, so it's only useful when used within a {% chat %}
block.
image
Wrap the filtered value into a ContentBlock of type image.
The resulting ChatMessage will have the field content
populated with a list of ContentBlock objects.
Important
this filter marks the content to cache by surrounding it with <content_block>
and
</content_block>
, so it's only useful when used within a {% chat %}
block.
lemmatize
Extensions
Extensions are custom functions that can be used to add new tags to the template engine. Banks supports the following ones, specific for prompt engineering.
chat
completion
completion
can be used to send to the LLM the content of the block in form of messages.
The rendered value of the block can be assigned to a variable and accessed from another section of the prompt.
canary_word
Insert into the prompt a canary word that can be checked later with Prompt.canary_leaked()
to ensure the original prompt was not leaked.
Example:
from banks import Prompt
p = Prompt("{{canary_word}}Hello, World!")
p.text() ## outputs 'BANKS[5f0bbba4]Hello, World!'
Macros
Macros are a way to implement complex logic in the template itself, think about defining functions but using Jinja code instead of Python. Banks provides a set of macros out of the box that are useful in prompt engineering, for example to generate a prompt and call OpenAI on-the-fly, during the template rendering. Before using Banks' macros, you have to import them in your templates, see the examples below.