-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from narenaryan/feat/add-example-calculator-app
feat: add sample calculator app that uses prompt parser
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jinja2 | ||
promptml |