From 530d35d22194314692c93fd6575fe6091b9cc36d Mon Sep 17 00:00:00 2001 From: N3N Date: Sat, 27 Apr 2024 19:31:14 -0700 Subject: [PATCH] feat: add sample calculator app that uses prompt parser --- BUILD.md | 24 ++++++++++++++ examples/jinja2/README.md | 52 +++++++++++++++++++++++++++++++ examples/jinja2/calculator.jinja2 | 12 +++++++ examples/jinja2/calculator.pml | 34 ++++++++++++++++++++ examples/jinja2/main.py | 21 +++++++++++++ examples/jinja2/requirements.txt | 2 ++ 6 files changed, 145 insertions(+) create mode 100644 BUILD.md create mode 100644 examples/jinja2/README.md create mode 100644 examples/jinja2/calculator.jinja2 create mode 100644 examples/jinja2/calculator.pml create mode 100644 examples/jinja2/main.py create mode 100644 examples/jinja2/requirements.txt diff --git a/BUILD.md b/BUILD.md new file mode 100644 index 0000000..74034d5 --- /dev/null +++ b/BUILD.md @@ -0,0 +1,24 @@ +## How to build `promptml` package locally ? + +We are in process of publishing this package to PyPi, until then you can build a Wheel package locally with below commands: + +From project root, in a command-line shell, run: + +```bash +pip install -r requirements_dev.txt + +hatch build +``` + +Once hatch command is executed, you will see a `dist/` directory with a `.whl` file. With your activated Python environment, run the following command to install the package: + +```bash + pip install dist/promptml-x.y.z-py3-none-any.whl +``` +were x.y.z is the version specified in `src/promptml/__about__.py` file. + +This will install promptml package to your Python environment. To uninstall, run command: + +```bash +pip uninstall promptml +``` diff --git a/examples/jinja2/README.md b/examples/jinja2/README.md new file mode 100644 index 0000000..f55f9d2 --- /dev/null +++ b/examples/jinja2/README.md @@ -0,0 +1,52 @@ +## Calculator App: A sample application of Prompt Markup Language (PromptML) + +This is an example of how to use PromptML to generate final Generative AI prompt using Jinja2. The steps are performed in this manner: + +1. Parse `calculator.pml` using promptml parser to generate a prompt context +2. Pass context to Jinja2 environment with a template called `calculator.jinja2` + +This will print an output of below: + +```txt +Context: You are a calculator app who can perform various kinds of mathematical operations. + +Follow the below instructions step-by-step for objective: +1. Think about how you can add two numbers together. +2. Return only the answer. + + +Audience: Education +Task Difficulty: Beginner +Reply in tone: Friendly + +Objective: What is the sum of 5 and 6? +``` + +This is the final prompt to be used for Generative AI systems. + +## Running instructions + +1. Create a virtual environment in this directory: + + ```bash + python3 -m venv .venv + ``` +2. Activate virtual environment + + ```bash + source .venv/bin/activate + ``` +3. Install Jinja2 and promptml + + ```bash + pip install -r requirements.txt + ``` + + Note: If `promptml` package is not available on PyPi, you can locally build the Wheel package and install it. See [BUILD.md](../../BUILD.md) file for instructions. + +4. Run the program + + ```bash + python main.py + ``` +5. Play around with `calculator.pml` and re-run the program to see the change in output. diff --git a/examples/jinja2/calculator.jinja2 b/examples/jinja2/calculator.jinja2 new file mode 100644 index 0000000..c2d208d --- /dev/null +++ b/examples/jinja2/calculator.jinja2 @@ -0,0 +1,12 @@ +Context: {{ prompt.context }} + +Follow the below instructions step-by-step for objective: +{% for instruction in prompt.instructions -%} + {{loop.index}}. {{ instruction }} +{% endfor %} + +Audience: {{ prompt.metadata.domain }} +Task Difficulty: {{ prompt.metadata.difficulty }} +Reply in tone: {{ prompt.constraints.tone }} + +Objective: {{ prompt.objective }} diff --git a/examples/jinja2/calculator.pml b/examples/jinja2/calculator.pml new file mode 100644 index 0000000..9db98b9 --- /dev/null +++ b/examples/jinja2/calculator.pml @@ -0,0 +1,34 @@ +@prompt + @context + You are a calculator app who can perform various kinds of mathematical operations. + @end + @objective + What is the sum of 5 and 6? + @end + @instructions + @step + Think about how you can add two numbers together. + @end + @step + Return only the answer. + @end + @end + @constraints + @length + min: 1 + max: 10 # Token size + @end + @tone + Friendly + @end + @end + @metadata + @domain + Education + @end + + @difficulty + Beginner + @end + @end +@end diff --git a/examples/jinja2/main.py b/examples/jinja2/main.py new file mode 100644 index 0000000..22d8a17 --- /dev/null +++ b/examples/jinja2/main.py @@ -0,0 +1,21 @@ +from promptml.parser import PromptParserFromFile + +from jinja2 import Environment, FileSystemLoader + + +def main(): + template_loader = FileSystemLoader(searchpath="./") + template_env = Environment(loader=template_loader) + template_file = "calculator.jinja2" + template = template_env.get_template(template_file) + + # Parse PromptML code and generate context + prompt_parser = PromptParserFromFile('calculator.pml') + prompt = prompt_parser.parse() + + # Render the template with the prompt context + template_output = template.render(prompt=prompt) + print(template_output) + +if __name__ == '__main__': + main() diff --git a/examples/jinja2/requirements.txt b/examples/jinja2/requirements.txt new file mode 100644 index 0000000..3f3835a --- /dev/null +++ b/examples/jinja2/requirements.txt @@ -0,0 +1,2 @@ +jinja2 +promptml \ No newline at end of file