Skip to main content

A prompt programming language

Project description

banks

PyPI - Version PyPI - Python Version

Banks is a Python library to generate LLM prompts using a template language.


Table of Contents

Installation

pip install banks

To use lemmatization and text generation you also need to:

pip install openai simplemma

Examples

Generate a blog writing prompt

Given 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":

from banks import Prompt


p = 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:

Reuse templates from files

We 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:

from banks import Prompt


p = Prompt.from_template("blog.jinja")
topic = "retrogame computing"
print(p.text({"topic": topic}))

Generate a summarizer prompt

Instead of hardcoding the content to summarize in the prompt itself, we can generate it starting from a generic one:

from banks import Prompt


prompt_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 network
documents = [
    "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 template

Banks comes with predefined filters you can use to process data before generating the prompt. Say you want to use a lemmatizer on a document you want to summarize:

from banks import Prompt


prompt_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 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

License

banks is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

banks-0.0.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

banks-0.0.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file banks-0.0.2.tar.gz.

File metadata

  • Download URL: banks-0.0.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for banks-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9072ad48c211cd741e196d392bd7df55078e5c28941a39b8a816d2ab81fd3c86
MD5 d7e19fad06aeba111f7b5d1846c5be00
BLAKE2b-256 35b69c13879b037c0c803346f37898089c5a563746c1963ac7fc4979d3f6728b

See more details on using hashes here.

File details

Details for the file banks-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: banks-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for banks-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7970fb168e8b57ba5c2ff11b855962c659f3f09ab9725cdbafc8b45fbc0e8fc2
MD5 5148240055845fc0c921c1cf42a01091
BLAKE2b-256 3b85786170c08ce33fc0754ad63625dbc38ad446d002c18bee5415e375edce4c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page