Package: banks
banksBanksis the linguist professor who will help you generate meaningful LLM prompts using a template language that makes sense.
If you're still usingf-stringsfor the job, keep reading.Table of ContentsbanksInstallationExamplesGenerate a blog writing promptGenerate a summarizer promptLemmatize text while processing a templateUse a LLM to generate a text while rendering a promptGo meta: create a prompt andgenerateits responseGo meta(meta): process a LLM responseReuse templates from filesLicenseInstallationpip install banksExamplesGenerate a blog writing promptGiven a generic template to instruct an LLM to generate a blog article, we
use Banks to generate the actual prompt on our topic of choice, "retrogame computing":frombanksimportPromptp=Prompt("Write a 500-word blog post on {{ topic }}.\n\nBlog post:")topic="retrogame computing"print(p.text({"topic":topic}))This will print the following text, that can be pasted directly into Chat-GPT:Write a 500-word blog post on retrogame computing.

Blog post:Generate a summarizer promptInstead of hardcoding the content to summarize in the prompt itself, we can generate it
starting from a generic one:frombanksimportPromptprompt_template="""Summarize the following documents:{% for document in documents %}{{ document }}{% endfor %}Summary:"""# In a real-world scenario, these would be loaded as external resources from files or networkdocuments=["A first paragraph talking about AI","A second paragraph talking about climate change","A third paragraph talking about retrogaming"]p=Prompt(prompt_template)print(p.text({"documents":documents}))The resulting prompt:Summarize the following documents:

A first paragraph talking about AI

A second paragraph talking about climate change

A third paragraph talking about retrogaming

Summary:Lemmatize text while processing a templateBanks comes with predefined filters you can use to process data before generating the
prompt. Say you want to use a lemmatizer on a document before summarizing it, first
you need to installsimplemma:pipinstallsimplemmathen you can use thelemmatizefilter in your templates like this:frombanksimportPromptprompt_template="""Summarize the following document:{{ document | lemmatize }}Summary:"""p=Prompt(prompt_template)print(p.text({"document":"The cats are running"}))the output would be:Summarize the following document:
the cat be run
Summary:Use a LLM to generate a text while rendering a promptSometimes 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
calledOPENAI_API_KEYyou 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):frombanksimportPromptprompt_template="""Generate a tweet about the topic {{ topic }} with a positive sentiment.{#This is for illustration purposes only, there are better and cheaper waysto 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 #PositiveFutureGo meta: create a prompt andgenerateits responseWe can leverage Jinja's macro system to generate a prompt, send the result to OpenAI and get a response.
Let's bring back the blog writing example:frombanksimportPromptprompt_template="""{% from "banks_macros.jinja" import run_prompt with context %}{%- call run_prompt() -%}Write a 500-word blog post on {{ topic }}Blog post:{%- endcall -%}"""p=Prompt(prompt_template)print(p.text({"topic":"climate change"}))The snippet above won't print the prompt, instead will generate the prompt textWrite a 500-word blog post on climate change

Blog post:and will send it to OpenAI using thegenerateextension, eventually returning its response:Climate change is a phenomenon that has been gaining attention in recent years...
...Go meta(meta): process a LLM responseWhen generating a response from a prompt template, we can take a step further and
post-process the LLM response by assinging it to a variable and applying filters
to it:frombanksimportPromptprompt_template="""{% from "banks_macros.jinja" import run_prompt with context %}{%- set prompt_result %}{%- call run_prompt() -%}Write a 500-word blog post on {{ topic }}Blog post:{%- endcall -%}{%- endset %}{# nothing is returned at this point: the variable 'prompt_result' contains the result #}{# let's use the prompt_result variable now #}{{ prompt_result | upper }}"""p=Prompt(prompt_template)print(p.text({"topic":"climate change"}))The final answer from the LLM will be printed, this time all in uppercase.Reuse templates from filesWe can get the same result as the previous example loading the prompt template from file
instead of hardcoding it into the Python code. For convenience, Banks comes with a few
default templates distributed the package. We can load those templates from file like this:frombanksimportPromptp=Prompt.from_template("blog.jinja")topic="retrogame computing"print(p.text({"topic":topic}))Licensebanksis distributed under the terms of theMITlicense.