A prompt programming language
Project description
banks
Banks is the linguist professor who will help you generate meaningful
LLM prompts using a template language that makes sense. If you're still using f-strings
for the job, keep reading.
Docs are available here.
Table of Contents
Installation
pip install banks
Features
Prompts are instrumental for the success of any LLM application, and Banks focuses around specific areas of their lifecycle:
- :orange_book: Templating: Banks provides tools and functions to build prompts text and chat messages from generic blueprints.
- :tickets: Versioning and metadata: Banks supports attaching metadata to prompts to ease their management, and versioning is first-class citizen.
- :file_cabinet: Management: Banks provides ways to store prompts on disk along with their metadata.
Cookbooks
- :blue_book: In-prompt chat completion
- :blue_book: Prompt caching with Anthropic
- :blue_book: Prompt versioning
Examples
For a more extensive set of code examples, see the documentation page.
:point_right: Use a LLM to generate a text while rendering a prompt
Sometimes it might be useful to ask another LLM to generate examples for you in a
few-shot prompt. Provided you have a valid OpenAI API key stored in an env var
called OPENAI_API_KEY
you can ask Banks to do something like this (note we can
annotate the prompt using comments - anything within {# ... #}
will be removed
from the final prompt):
from banks import Prompt
prompt_template = """
Generate a tweet about the topic {{ topic }} with a positive sentiment.
{#
This is for illustration purposes only, there are better and cheaper ways
to generate examples for a few-shots prompt.
#}
Examples:
{% for number in range(3) %}
- {% generate "write a tweet with positive sentiment" "gpt-3.5-turbo" %}
{% endfor %}
"""
p = Prompt(prompt_template)
print(p.text({"topic": "climate change"}))
The output would be something similar to the following:
Generate a tweet about the topic climate change with a positive sentiment.
Examples:
- "Feeling grateful for the amazing capabilities of #GPT3.5Turbo! It's making my work so much easier and efficient. Thank you, technology!" #positivity #innovation
- "Feeling grateful for all the opportunities that come my way! With #GPT3.5Turbo, I am able to accomplish tasks faster and more efficiently. #positivity #productivity"
- "Feeling grateful for all the wonderful opportunities and experiences that life has to offer! #positivity #gratitude #blessed #gpt3.5turbo"
If you paste Banks' output into ChatGPT you would get something like this:
Climate change is a pressing global issue, but together we can create positive change! Let's embrace renewable energy, protect our planet, and build a sustainable future for generations to come. 🌍💚 #ClimateAction #PositiveFuture
[!IMPORTANT] The
generate
extension uses LiteLLM under the hood, and provided you have the proper environment variables set, you can use any model from the supported model providers.
[!NOTE] Banks uses a cache to avoid generating text again for the same template with the same context. By default the cache is in-memory but it can be customized.
:point_right: Render a prompt template as chat messages
You'll find yourself feeding an LLM a list of chat messages instead of plain text more often than not. Banks will help you remove the boilerplate by defining the messages already at the prompt level.
from banks import Prompt
prompt_template = """
{% chat role="system" %}
You are a {{ persona }}.
{% endchat %}
{% chat role="user" %}
Hello, how are you?
{% endchat %}
"""
p = Prompt(prompt_template)
print(p.chat_messages({"persona": "helpful assistant"}))
# Output:
# [
# ChatMessage(role='system', content='You are a helpful assistant.\n'),
# ChatMessage(role='user', content='Hello, how are you?\n')
# ]
:point_right: Use prompt caching from Anthropic
Several inference providers support prompt caching to save time and costs, and Anthropic in particular offers fine-grained control over the parts of the prompt that we want to cache. With Banks this is as simple as using a template filter:
prompt_template = """
{% chat role="user" %}
Analyze this book:
{# Only this part of the chat message (the book content) will be cached #}
{{ book | cache_control("ephemeral") }}
What is the title of this book? Only output the title.
{% endchat %}
"""
p = Prompt(prompt_template)
print(p.chat_messages({"book":"This is a short book!"}))
# Output:
# [
# ChatMessage(role='user', content=[
# ContentBlock(type='text', text='Analyze this book:\n\n'),
# ContentBlock(type='text', cache_control=CacheControl(type='ephemeral'), text='This is a short book!'),
# ContentBlock(type='text', text='\n\nWhat is the title of this book? Only output the title.\n')
# ])
# ]
The output of p.chat_messages()
can be fed to the Anthropic client directly.
Reuse templates from registries
We can get the same result as the previous example loading the prompt template from a registry
instead of hardcoding it into the Python code. For convenience, Banks comes with a few registry types
you can use to store your templates. For example, the DirectoryTemplateRegistry
can load templates
from a directory in the file system. Suppose you have a folder called templates
in the current path,
and the folder contains a file called blog.jinja
. You can load the prompt template like this:
from banks import Prompt
from banks.registries import DirectoryTemplateRegistry
registry = DirectoryTemplateRegistry(populated_dir)
prompt = registry.get(name="blog")
print(prompt.text({"topic": "retrogame computing"}))
Async support
To run banks within an asyncio
loop you have to do two things:
- set the environment variable
BANKS_ASYNC_ENABLED=true
. - use the
AsyncPrompt
class that has an awaitablerun
method.
Example:
from banks import AsyncPrompt
async def main():
p = AsyncPrompt("Write a blog article about the topic {{ topic }}")
result = await p.text({"topic": "AI frameworks"})
print(result)
asyncio.run(main())
License
banks
is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file banks-1.3.0.tar.gz
.
File metadata
- Download URL: banks-1.3.0.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac3f465bd0b2a9cf559d9ddcd781ff7e7a22b5b9c6dfe520b05199f50df66b31 |
|
MD5 | f7078edebeef207fc851fd2e2b2741ba |
|
BLAKE2b-256 | 7b822740ea7d03def2d646aa47887b0a204dbee4c6d6158936e325b54a678dca |
File details
Details for the file banks-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: banks-1.3.0-py3-none-any.whl
- Upload date:
- Size: 24.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f23c42026a705666e542df044be87a3204c772229a1ab9f7477b340d7c24780 |
|
MD5 | fbacdd03afc4013ec5291813e85583c0 |
|
BLAKE2b-256 | 1616189d935da7157e8bce16f47b664185602b8564512a90d29c8f31fb9939a2 |