You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial.md
+71-42Lines changed: 71 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ document:
46
46
```
47
47
48
48
In this program ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_llm.pdl)), the `document` starts with the word `Hello,`, and we call a model (`watsonx/ibm/granite-34b-code-instruct`) with this as input prompt. Notice the Watsonx model id on LiteLLM.
49
-
The model is passed some parameters including the `decoding_method` and `stop`, which corresponds to the `stop_sequences` parameter in Watsonx. The stop sequences are to be included in the output. Since the `input` field is not specified in the model call, the entire document up that point is passed to the model as input context.
49
+
The model is passed some parameters including the `decoding_method` and `stop`, which corresponds to the `stop_sequences` parameter in Watsonx. The stop sequences are to be included in the output. Since the `input` field is not specified in the model call, the entire document up that point is passed to the model as input context.
50
50
51
51
A PDL program computes 2 data structures. The first is a JSON corresponding to the result of the overall program, obtained by aggregating the results of each block. This is what is printed by default when we run the interpreter. The second is a conversational background context, which is a list of role/content pairs, where we implicitly keep track of roles and content for the purpose of communicating with models that support chat APIs. The contents in the latter correspond to the results of each block. The conversational background context is what is used to make calls to LLMs via LiteLLM.
52
52
@@ -64,7 +64,7 @@ description: Hello world calling a model
64
64
document:
65
65
- "Hello,"
66
66
- model: watsonx/ibm/granite-20b-multilingual
67
-
input:
67
+
input:
68
68
Translate the word 'world' to French
69
69
```
70
70
@@ -95,7 +95,7 @@ The user can override these defaults by explicitly including them in the model c
95
95
96
96
## Variable Definition and Use
97
97
98
-
Any block can have a variable definition using a `def: <var>` field. This means that the output of that block is assigned to the variable `<var>`, which may be reused at a later point in the document.
98
+
Any block can have a variable definition using a `def: <var>` field. This means that the output of that block is assigned to the variable `<var>`, which may be reused at a later point in the document.
99
99
100
100
Consider the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/variable_def_use.pdl)):
101
101
@@ -127,7 +127,7 @@ In PDL, we can declaratively chain models together as in the following example (
127
127
128
128
```yaml
129
129
description: Model chaining
130
-
document:
130
+
document:
131
131
- Hello,
132
132
- model: watsonx/ibm/granite-34b-code-instruct
133
133
parameters:
@@ -189,7 +189,7 @@ The translation of 'I love Paris!' to French is 'J'aime Paris!'.
189
189
The translation of 'I love Madrid!' to Spanish is 'Me encanta Madrid!'.
190
190
```
191
191
192
-
A function only contributes to the output document when it is called. So the definition itself results in `""`. When we call a function, we implicitly pass the current background context, and this is used as input to model calls inside the function body. In the above example, since the `input` field is omitted, the entire document produced at that point is passed as input to the granite model.
192
+
A function only contributes to the output document when it is called. So the definition itself results in `""`. When we call a function, we implicitly pass the current background context, and this is used as input to model calls inside the function body. In the above example, since the `input` field is omitted, the entire document produced at that point is passed as input to the granite model.
193
193
194
194
## Grouping Variable Definitions in Defs
195
195
@@ -264,7 +264,7 @@ Here are its possible values:
264
264
- `[result]: contribute to the final result but not the background context
265
265
266
266
- `[context]`: contribute to the background context but not the final result
267
-
267
+
268
268
- `[result, context]`: contribute to both, which is also the default setting.
269
269
270
270
## Input from File or Stdin
@@ -301,7 +301,7 @@ document:
301
301
multiline: true
302
302
```
303
303
304
-
Finally, the following example shows reading content in JSON format.
304
+
Finally, the following example shows reading content in JSON format.
305
305
306
306
Consider the JSON content in this [file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/input.json):
307
307
```json
@@ -310,7 +310,7 @@ Consider the JSON content in this [file](https://github.com/IBM/prompt-declarati
310
310
"address": {
311
311
"number": 87,
312
312
"street": "Smith Road",
313
-
"town": "Armonk",
313
+
"town": "Armonk",
314
314
"state": "NY",
315
315
"zip": 10504
316
316
}
@@ -350,7 +350,7 @@ description: Hello world showing call to python code
350
350
document:
351
351
- "Hello, "
352
352
- lan: python
353
-
code:
353
+
code:
354
354
|
355
355
import random
356
356
import string
@@ -423,7 +423,7 @@ document:
423
423
input:
424
424
|
425
425
Here is some info about the location of the function in the repo.
426
-
repo:
426
+
repo:
427
427
{{ CODE.repo_info.repo }}
428
428
path: {{ CODE.repo_info.path }}
429
429
Function_name: {{ CODE.repo_info.function_name }}
@@ -445,7 +445,7 @@ document:
445
445
{{ TRUTH }}
446
446
"""
447
447
result = textdistance.levenshtein.normalized_similarity(expl, truth)
448
-
- data:
448
+
- data:
449
449
input: "{{ CODE }}"
450
450
output: "{{ EXPLANATION }}"
451
451
metric: "{{ EVAL }}"
@@ -563,11 +563,11 @@ The first two blocks read math problem examples and include them in the document
563
563
564
564
In the body of the `repeat` block, the program first asks granite to generate a question and add it to the document. Next we print `Answer: Let's think step by step.\n`. The following block is a repeat-until: the document in `repeat` is repeated until the condition in the `until` field becomes true. Here the condition states that we stop the iteration when variable `REASON_OR_CALC` contains `<<`. That variable is defined in the first block of the repeat-until -- we prompt a granite model and stop at the character `<<`.
565
565
566
-
The next block is an if-then-else. We check if `REASON_OR_CALC` ends with `<<` and if so we prepare for the python call to perform the arithmetic calculation. First, we have the granite model generate an `EXPR` variable, which we then use inside the `code` of the following Python block.
566
+
The next block is an if-then-else. We check if `REASON_OR_CALC` ends with `<<` and if so we prepare for the python call to perform the arithmetic calculation. First, we have the granite model generate an `EXPR` variable, which we then use inside the `code` of the following Python block.
567
567
568
-
When we execute this program, we obtain 3 math problems like the ones in the [examples](https://github.com/IBM/prompt-declaration-language//blob/main/examples/arith/).
568
+
When we execute this program, we obtain 3 math problems like the ones in the [examples](https://github.com/IBM/prompt-declaration-language//blob/main/examples/arith/).
569
569
570
-
Notice that the `repeat` and `then` blocks are followed by `document`. This is because of the semantics of lists in PDL. If we want to aggregate the result by stringifying every element in the list and collating them together (which is the case of top-level programs in general), then we need the keyword `document` to preceed a list. If this is omitted then the list is treated as a programmatic sequence where all the blocks are executed in sequence but result of the overall list is the result of the {\em last} block in the sequence.
570
+
Notice that the `repeat` and `then` blocks are followed by `document`. This is because of the semantics of lists in PDL. If we want to aggregate the result by stringifying every element in the list and collating them together (which is the case of top-level programs in general), then we need the keyword `document` to preceed a list. If this is omitted then the list is treated as a programmatic sequence where all the blocks are executed in sequence but result of the overall list is the result of the {\em last} block in the sequence.
571
571
572
572
Similarly, the `read` block has an annotation `as: document`. This means that the result of each iteration is stringified and collated to the overall result of the `repeat` block which is of type string in this case.
573
573
@@ -581,7 +581,7 @@ description: for loop
581
581
document:
582
582
- for:
583
583
i: [1, 2, 3, 4]
584
-
repeat:
584
+
repeat:
585
585
"{{ i }}\n"
586
586
```
587
587
@@ -596,7 +596,7 @@ description: for loop
596
596
document:
597
597
- for:
598
598
i: [1, 2, 3, 4]
599
-
repeat:
599
+
repeat:
600
600
document:
601
601
- "{{ i }}\n"
602
602
as: document
@@ -663,16 +663,16 @@ document:
663
663
spec: {name: str, age: int}
664
664
input:
665
665
document:
666
-
- for:
666
+
- for:
667
667
question: "{{ data.questions }}"
668
668
answer: "{{ data.answers }}"
669
669
repeat:
670
670
- |
671
671
{{ question }}
672
672
{{ answer }}
673
673
as: document
674
-
- >
675
-
Question: Create a JSON object with fields 'name' and 'age'
674
+
- >
675
+
Question: Create a JSON object with fields 'name' and 'age'
676
676
and set them appropriately. Write the age in letters.
677
677
parser: yaml
678
678
parameters:
@@ -682,7 +682,7 @@ document:
682
682
```
683
683
684
684
Upon reading the data we use a parser to parse it into a YAML. The `spec` field indicates the expected type for the
685
-
data, which is an object with 2 fields: `questions` and `answers` that are a list of string and a list of objects,
685
+
data, which is an object with 2 fields: `questions` and `answers` that are a list of string and a list of objects,
686
686
respectively. When the interpreter is executed, it checks this type dynamically and throws errors if necessary.
687
687
688
688
Similarly, the output of the model call is parsed as YAML, and the `spec` indicates that we expect an object with
@@ -777,7 +777,7 @@ document:
777
777
- |-
778
778
Question: Bobby gave Alice 5 apples. Alice has 6 apples. How many apples did she have before?
779
779
780
-
Answer: Let's think step by step.
780
+
Answer: Let's think step by step.
781
781
- model: "ibm/granite-34b-code-instruct"
782
782
platform: bam
783
783
```
@@ -968,8 +968,8 @@ To produce an execution trace consumable by the Live Document, you can run the i
968
968
pdl --trace <file.json> <my-example>
969
969
```
970
970
971
-
This produces an additional file named `my-example_trace.json` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
972
-
in the right pane.
971
+
This produces an additional file named `my-example_trace.json` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
972
+
in the right pane.
973
973
974
974
This is similar to a spreadsheet for tabular data, where data is in the forefront and the user can inspect the formula that generates the data in each cell. In the Live Document, cells are not uniform but can take arbitrary extents. Clicking on them similarly reveals the part of the code that produced them.
975
975
@@ -988,13 +988,13 @@ document:
988
988
Several lines of text,
989
989
with some "quotes" of various 'types',
990
990
and also a blank line:
991
-
991
+
992
992
and some text with
993
993
extra indentation
994
994
on the next line,
995
995
plus another line at the end.
996
-
997
-
996
+
997
+
998
998
- "End."
999
999
```
1000
1000
@@ -1020,13 +1020,13 @@ document:
1020
1020
Several lines of text,
1021
1021
with some "quotes" of various 'types',
1022
1022
and also a blank line:
1023
-
1023
+
1024
1024
and some text with
1025
1025
extra indentation
1026
1026
on the next line,
1027
1027
plus another line at the end.
1028
-
1029
-
1028
+
1029
+
1030
1030
- "End."
1031
1031
```
1032
1032
@@ -1048,13 +1048,13 @@ document:
1048
1048
Several lines of text,
1049
1049
with some "quotes" of various 'types',
1050
1050
and also a blank line:
1051
-
1051
+
1052
1052
and some text with
1053
1053
extra indentation
1054
1054
on the next line,
1055
1055
plus another line at the end.
1056
-
1057
-
1056
+
1057
+
1058
1058
- "End."
1059
1059
```
1060
1060
@@ -1077,13 +1077,13 @@ document:
1077
1077
Several lines of text,
1078
1078
with some "quotes" of various 'types',
1079
1079
and also a blank line:
1080
-
1080
+
1081
1081
and some text with
1082
1082
extra indentation
1083
1083
on the next line,
1084
1084
plus another line at the end.
1085
-
1086
-
1085
+
1086
+
1087
1087
- "End."
1088
1088
```
1089
1089
@@ -1109,13 +1109,13 @@ document:
1109
1109
Several lines of text,
1110
1110
with some "quotes" of various 'types',
1111
1111
and also a blank line:
1112
-
1112
+
1113
1113
and some text with
1114
1114
extra indentation
1115
1115
on the next line,
1116
1116
plus another line at the end.
1117
-
1118
-
1117
+
1118
+
1119
1119
- "\n\n\n\n"
1120
1120
- "End."
1121
1121
```
@@ -1145,7 +1145,7 @@ document:
1145
1145
Several lines of text,
1146
1146
with some "quotes" of various 'types',
1147
1147
and also a blank line:
1148
-
1148
+
1149
1149
and some text with
1150
1150
extra indentation
1151
1151
on the next line.
@@ -1171,7 +1171,7 @@ PDL:
1171
1171
```
1172
1172
document: 'Several lines of text,
1173
1173
containing ''single quotes''. Escapes (like \n) don''t do anything.
1174
-
1174
+
1175
1175
Newlines can be added by leaving a blank line.
1176
1176
Leading whitespace on lines is ignored.'
1177
1177
```
@@ -1192,7 +1192,7 @@ document: "Several lines of text,
2. Run a model e.g., `ollama run granite-code:34b-instruct-q5_K_M`. See [the Ollama library for more models](https://ollama.com/library/granite-code/tags)
1232
+
3. An OpenAI style server is running locally at [http://localhost:11434/](http://localhost:11434/), see [the Ollama blog](https://ollama.com/blog/openai-compatibility) for more details.
Alternatively, one could also use Ollama's OpenAI-style endpoint using the `openai/` prefix instead of `ollama_chat/`. In this case, set the `OPENAI_API_BASE`, `OPENAI_API_KEY`, and `OPENAI_ORGANIZATION` (if necessary) environment variables. If you were using the official OpenAI API, you would only have to set the api key and possibly the organization. For local use e.g., using Ollama, this could look like so:
1228
1250
1251
+
```bash
1252
+
export OPENAI_API_BASE=http://localhost:11434/v1
1253
+
export OPENAI_API_KEY=ollama # required, but unused
0 commit comments