Package: lambdaprompt
λ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 lambdapromptA webserver (built onFastAPI) example repositoryEnvironment variables for using hosted modelsFor using openAI, set up API keys as environment variables or set after importing (also easy to just make a.envfile, since this usesdotenvpackage)OPENAI_API_KEY=...Creating a promptPrompts use JINJA templating to create a string, the string is passed to the LLM for completion.fromlambdapromptimportGPT3Promptexample=GPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")# then use it as a functionexample(number=12,thing="apples")Creating ChatGPT3 Conversational promptsEach 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.>>>importlambdapromptaslp>>>convo=lp.AsyncGPT3Chat([{'system':'You are a {{ type_of_bot }}'}])>>>awaitconvo("What should we get for lunch?",type_of_bot="pirate")Asapirate,Iwouldsuggestwehavesomeheartyseafoodsuchasfishandchipsoraseafoodplatter.Wecouldalsohavesomerumtowashitdown!Arrr!General prompt creationYou 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.fromlambdapromptimportprompt,GPT3Promptgenerate_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):")@promptdefget_tasks_and_rate_is_happy(n=3):results=[]fortaskingenerate_n_tasks(n=n).split(","):results.append((task,is_happy(task)))returnresultsprint(get_tasks_and_rate_is_happy())Async and SyncLambdaprompt works on both sync and async functions, and offers a sync and async templated prompt interfacefromlambdapromptimportGPT3Prompt,asyncGPT3Prompt#syncfirst=GPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")first(number=12,thing="apples")#asyncfirst=asyncGPT3Prompt("Sally had {{ number }} of {{ thing }}. Sally sold ")awaitfirst(number=12,thing="apples")fromlambdapromptimportprompt@promptdefsync_example(a):returna+"!"sync_example("hello")@promptasyncdefasync_example(a):returna+"!"awaitasync_example("hello")Some special propertiesFor 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 }}]?""")awaitbasic_qa("Is it safe to eat pizza with chopsticks?")Using lambdaprompt as a webserviceSimplypip install lambdaprompt[server]and then addfrom lambdaprompt.server.main import appto the top of your file!make a fileapp.pyfromlambdapromptimportAsyncGPT3Prompt,promptfromlambdaprompt.server.mainimportappAsyncGPT3Prompt("""Rewrite the following as a {{ target_author }}.```{{ source_text }}```Output:```""",name="rewrite_as",stop="```",)Then runuvicorn app:app --reloadbrowse tohttp://localhost:8000/docsto see the swagger docs generated for the prompts!Running inside dockerFirst, create an .env file with your OpenAI API key: (likeOPENAI_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)Response OptimizationIdeation, Scoring and SelectionError Correcting Language LoopsSummarization and AggregationsRollingFan-out-treeMeta-Prompting