Recently I've been exploring ORM solutions for SQL databases, and during my research, I had an amusing thought. One of the reasons SQL ORMs like drizzle are popular is because they streamline the process of writing out SQL query strings, making writing queries faster and nigh-immune to misspellings and other common mishaps. What if the same concept could be applied to the prompts that we write for LLMs like ChatGPT?
After all, a prompt is just a collection of english sentences, and those sentences can be broken down into unique parts. For example, you might have a prompt that says the following:
I want you to act as a travel guide.
I will write you my location and you will suggest a place to visit near my location.
The first sentence here is setting up the model to assume a persona, a "travel guide" in this case. Then, the next sentence is the instruction the model must fulfill.
This is a very simple example, more advanced prompts will take advantage of numerous other techniques such as few-shot prompting where the user gives exapmles of successful completions to the model, making it learn how to respond. Another technique might be to explicitly outline the steps the model must take to reach its response, thus giving the model time to think about its answer, and often resulting in more reaonsable completions.
Theory
Given that we can formulate a prompt using such techniques, and template prompts already exist on the internet as proof of the constructable nature of prompts, then it would be reasonable to assume there could be some API that enables a developer to construct a prompt using their favourite programming language. Naturally, some text input is still required from the user, it is not possible to entirely eliminate that from the construction process like it would be for something like SQL queries, simply because a spoken language like English is highly complex, with an unfathomable set of word combinations that can be made. Having said that, what is able to be abstracted away are the techniques used to make more advanced prompts themselves.
So I began looking into all the techniques available to make LLM prompts. I took the DeepLearning.AI course on prompt engineering for developers, deconstructed the fantastic prompts of this github repo, and in about two days, I had a prototype that could generate prompts using TypeScript. The API looks something like this:
const prompt = new Prompto()
.withPersona('Openai engineer')
.withInstruction('Explain how to write effective ChatGPT prompts')
.inFormat('step-by-step guide')
.generatePrompt();
console.log(prompt);
Implementation
For the implementation, I decided to build the package for TypeScript for two reasons. Firstly, it could be seamlessly published on NPM to reach the largest audience of developers. Secondly, many developers use LLM APIs like the OpenAI API to integrate LLM models in their web applications, and having the package in TypeScript would enable such developers to utilise it for their apps.
The package is essentially a TypeScript class called Prompto, that once constructed will initialise an empty request. Then,
the builder pattern is leveraged to allow for class methods to be chained together to construct the prompt, similar to
SQL query builders. The prompt is then generated via the generatePrompt()
method, which uses template literals to construct
a prompt inspired by the format of the prompts seen here. The implementation supports a wide variety of prompting techniques such as setting examples, setting the steps,
the format of the response and much more.
By the way, in case you were wondering, the name Prompto is a play on the words pronto, meaning without delay, and prompt, so it could be interpreted as "quick prompts".
The package is still very much a work in progress, and I do plan on adding more features and refining the prompt generation, but I would like to see this evolve into a tool that is a staple in web apps powered by LLMs. If nothing else, it was certainly an interesting experiment.