Skip to content

Commit

Permalink
Merge pull request #3 from narenaryan/feat/add-example-calculator-app
Browse files Browse the repository at this point in the history
feat: add sample calculator app that uses prompt parser
  • Loading branch information
narenaryan authored Apr 28, 2024
2 parents f3a877f + 530d35d commit 72fa360
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
24 changes: 24 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
@@ -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
```
52 changes: 52 additions & 0 deletions examples/jinja2/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions examples/jinja2/calculator.jinja2
Original file line number Diff line number Diff line change
@@ -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 }}
34 changes: 34 additions & 0 deletions examples/jinja2/calculator.pml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions examples/jinja2/main.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions examples/jinja2/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jinja2
promptml

0 comments on commit 72fa360

Please sign in to comment.