Skip to main content

λprompt - Build, compose and call templated LLM prompts!

Write LLM prompts with jinja templates, compose them in python as functions, and call them directly or use them as a webservice!

We believe that large language model prompts are a lot like "functions" in a programming sense and would benefit greatly by the power of an interpreted language. lambdaprompt is a library to offer an interface to back that belief up. This library allows for building full large language model based "prompt machines", including ones that self-edit to correct and even self-write their own execution code.

pip install lambdaprompt

Open In Colab

A webserver (built on FastAPI) example repository

Environment variables for using hosted models

For using openAI, set up API keys as environment variables or set after importing (also easy to just make a .env file, since this uses dotenv package)

OPENAI_API_KEY=...

Creating a prompt

Prompts use JINJA templating to create a string, the string is passed to the LLM for completion.

from lambdaprompt import GPT3Prompt

example = GPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")
# then use it as a function
example(number=12, thing="apples")

Creating ChatGPT3 Conversational prompts

Each prompt can be thought of as a parameterizable conversation, and executing the prompt with an input will apply that as "the next line of conversation" and then generate the response.

In order to update the memory state of the prompt, call the .add() method on the prompt, which can be used to add steps to a conversation and make the prompt "remember" what has been said.

>>> import lambdaprompt as lp

>>> convo = lp.AsyncGPT3Chat([{'system': 'You are a {{ type_of_bot }}'}])
>>> await convo("What should we get for lunch?", type_of_bot="pirate")
As a pirate, I would suggest we have some hearty seafood such as fish and chips or a seafood platter. We could also have some rum to wash it down! Arrr!

General prompt creation

You can also turn any function into a prompt (useful for composing prompts, or creating programs out of prompts. This is commonly called "prompt chaining". See how you can achieve this with simple python composition.

from lambdaprompt import prompt, GPT3Prompt

generate_n_tasks = GPT3Prompt("Today I will do {{ n }} things (comma separated) [", stop="]")
is_happy = GPT3Prompt("The task {{ task_detail }} is a task that will make me happy? (y/n):")

@prompt
def get_tasks_and_rate_is_happy(n=3):
    results = []
    for task in generate_n_tasks(n=n).split(","):
        results.append((task, is_happy(task)))
    return results

print(get_tasks_and_rate_is_happy())

Async and Sync

Lambdaprompt works on both sync and async functions, and offers a sync and async templated prompt interface

from lambdaprompt import GPT3Prompt, asyncGPT3Prompt

#sync
first = GPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")
first(number=12, thing="apples")

#async
first = asyncGPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")
await first(number=12, thing="apples")
from lambdaprompt import prompt

@prompt
def sync_example(a):
    return a + "!"

sync_example("hello")

@prompt
async def async_example(a):
    return a + "!"

await async_example("hello")

Some special properties

For templated prompts with only template variable, can directly call with the variable as positional argument (no need to define in kwarg)

basic_qa = asyncGPT3Prompt("basic_qa", """What is the answer to the question [{{ question }}]?""")

await basic_qa("Is it safe to eat pizza with chopsticks?")

Using lambdaprompt as a webservice

Simply pip install lambdaprompt[server] and then add from lambdaprompt.server.main import app to the top of your file!

make a file

app.py

from lambdaprompt import AsyncGPT3Prompt, prompt
from lambdaprompt.server.main import app

AsyncGPT3Prompt(
    """Rewrite the following as a {{ target_author }}. 
```
{{ source_text }}
```
Output:
```
""",
    name="rewrite_as",
    stop="```",
)

Then run

uvicorn app:app --reload

browse to http://localhost:8000/docs to see the swagger docs generated for the prompts!

Running inside docker

First, create an .env file with your OpenAI API key: (like OPENAI_API_KEY=sk-dskj32094klsaj9024lkjsa)

docker build . -t lambdaprompt:0.0.1
docker run -it --env-file .env lambdaprompt:0.0.1  bash -c "python two.py"

This will output something like this:

docker run -it --env-file .env lambdaprompt:0.0.1  bash -c "python two.py"
[('example: go for a walk', '\n\nYes. Going for a walk can be a great way to boost your mood and get some fresh air.'), (' read a book', '\n\nYes'), (' call a friend', '\n\nYes')]

docker run -it --env-file .env lambdaprompt:0.0.1  bash -c "python two.py"
[(' edit ', '\n\nNo. Editing can be a tedious and time-consuming task, so it may not necessarily make you happy.')]

Design Patterns (TODO)

Metadata

Release files for lambdaprompt 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lambdaprompt 0.6.1
File Size Uploaded
lambdaprompt-0.6.1.tar.gz 18.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lambdaprompt 0.6.1
File Interpreter ABI Platform
lambdaprompt-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.5 kB

Release files / lambdaprompt-0.6.1.tar.gz

Download URL lambdaprompt-0.6.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5571d6235e0b32940ac17f5b2e38b4f09df618ed6b48e6209d5d05a3c4351699
BLAKE2b-256 checksum
How to use checksums
91472f6dba24e16a7f896ad193543874deb1aa9f6cd1a40689c31b9bb52afa50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / lambdaprompt-0.6.1-py3-none-any.whl

Download URL lambdaprompt-0.6.1-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
304a58e8033f31ef13e817ce9be46d0e4d3bdc96fa304cc0afacadbc29fab416
BLAKE2b-256 checksum
How to use checksums
8c2815dc6cadf0f5362cbcf37f57c87077422278847242ba847382c1abdeca3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.3.5

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page