Skip to content

Commit 8b668f8

Browse files
committed
Add ollama instructions
Signed-off-by: Claudio Spiess <[email protected]>
1 parent 8c60414 commit 8b668f8

File tree

1 file changed

+71
-42
lines changed

1 file changed

+71
-42
lines changed

docs/tutorial.md

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ document:
4646
```
4747

4848
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.
5050

5151
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.
5252

@@ -64,7 +64,7 @@ description: Hello world calling a model
6464
document:
6565
- "Hello,"
6666
- model: watsonx/ibm/granite-20b-multilingual
67-
input:
67+
input:
6868
Translate the word 'world' to French
6969
```
7070

@@ -95,7 +95,7 @@ The user can override these defaults by explicitly including them in the model c
9595

9696
## Variable Definition and Use
9797

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.
9999

100100
Consider the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/variable_def_use.pdl)):
101101

@@ -127,7 +127,7 @@ In PDL, we can declaratively chain models together as in the following example (
127127

128128
```yaml
129129
description: Model chaining
130-
document:
130+
document:
131131
- Hello,
132132
- model: watsonx/ibm/granite-34b-code-instruct
133133
parameters:
@@ -189,7 +189,7 @@ The translation of 'I love Paris!' to French is 'J'aime Paris!'.
189189
The translation of 'I love Madrid!' to Spanish is 'Me encanta Madrid!'.
190190
```
191191

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.
193193

194194
## Grouping Variable Definitions in Defs
195195

@@ -264,7 +264,7 @@ Here are its possible values:
264264
- `[result]: contribute to the final result but not the background context
265265

266266
- `[context]`: contribute to the background context but not the final result
267-
267+
268268
- `[result, context]`: contribute to both, which is also the default setting.
269269

270270
## Input from File or Stdin
@@ -301,7 +301,7 @@ document:
301301
multiline: true
302302
```
303303

304-
Finally, the following example shows reading content in JSON format.
304+
Finally, the following example shows reading content in JSON format.
305305

306306
Consider the JSON content in this [file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/input.json):
307307
```json
@@ -310,7 +310,7 @@ Consider the JSON content in this [file](https://github.com/IBM/prompt-declarati
310310
"address": {
311311
"number": 87,
312312
"street": "Smith Road",
313-
"town": "Armonk",
313+
"town": "Armonk",
314314
"state": "NY",
315315
"zip": 10504
316316
}
@@ -350,7 +350,7 @@ description: Hello world showing call to python code
350350
document:
351351
- "Hello, "
352352
- lan: python
353-
code:
353+
code:
354354
|
355355
import random
356356
import string
@@ -423,7 +423,7 @@ document:
423423
input:
424424
|
425425
Here is some info about the location of the function in the repo.
426-
repo:
426+
repo:
427427
{{ CODE.repo_info.repo }}
428428
path: {{ CODE.repo_info.path }}
429429
Function_name: {{ CODE.repo_info.function_name }}
@@ -445,7 +445,7 @@ document:
445445
{{ TRUTH }}
446446
"""
447447
result = textdistance.levenshtein.normalized_similarity(expl, truth)
448-
- data:
448+
- data:
449449
input: "{{ CODE }}"
450450
output: "{{ EXPLANATION }}"
451451
metric: "{{ EVAL }}"
@@ -563,11 +563,11 @@ The first two blocks read math problem examples and include them in the document
563563

564564
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 `<<`.
565565

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.
567567

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/).
569569

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.
571571

572572
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.
573573

@@ -581,7 +581,7 @@ description: for loop
581581
document:
582582
- for:
583583
i: [1, 2, 3, 4]
584-
repeat:
584+
repeat:
585585
"{{ i }}\n"
586586
```
587587
@@ -596,7 +596,7 @@ description: for loop
596596
document:
597597
- for:
598598
i: [1, 2, 3, 4]
599-
repeat:
599+
repeat:
600600
document:
601601
- "{{ i }}\n"
602602
as: document
@@ -663,16 +663,16 @@ document:
663663
spec: {name: str, age: int}
664664
input:
665665
document:
666-
- for:
666+
- for:
667667
question: "{{ data.questions }}"
668668
answer: "{{ data.answers }}"
669669
repeat:
670670
- |
671671
{{ question }}
672672
{{ answer }}
673673
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'
676676
and set them appropriately. Write the age in letters.
677677
parser: yaml
678678
parameters:
@@ -682,7 +682,7 @@ document:
682682
```
683683
684684
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,
686686
respectively. When the interpreter is executed, it checks this type dynamically and throws errors if necessary.
687687
688688
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:
777777
- |-
778778
Question: Bobby gave Alice 5 apples. Alice has 6 apples. How many apples did she have before?
779779

780-
Answer: Let's think step by step.
780+
Answer: Let's think step by step.
781781
- model: "ibm/granite-34b-code-instruct"
782782
platform: bam
783783
```
@@ -968,8 +968,8 @@ To produce an execution trace consumable by the Live Document, you can run the i
968968
pdl --trace <file.json> <my-example>
969969
```
970970
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.
973973
974974
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.
975975
@@ -988,13 +988,13 @@ document:
988988
Several lines of text,
989989
with some "quotes" of various 'types',
990990
and also a blank line:
991-
991+
992992
and some text with
993993
extra indentation
994994
on the next line,
995995
plus another line at the end.
996-
997-
996+
997+
998998
- "End."
999999
```
10001000
@@ -1020,13 +1020,13 @@ document:
10201020
Several lines of text,
10211021
with some "quotes" of various 'types',
10221022
and also a blank line:
1023-
1023+
10241024
and some text with
10251025
extra indentation
10261026
on the next line,
10271027
plus another line at the end.
1028-
1029-
1028+
1029+
10301030
- "End."
10311031
```
10321032
@@ -1048,13 +1048,13 @@ document:
10481048
Several lines of text,
10491049
with some "quotes" of various 'types',
10501050
and also a blank line:
1051-
1051+
10521052
and some text with
10531053
extra indentation
10541054
on the next line,
10551055
plus another line at the end.
1056-
1057-
1056+
1057+
10581058
- "End."
10591059
```
10601060
@@ -1077,13 +1077,13 @@ document:
10771077
Several lines of text,
10781078
with some "quotes" of various 'types',
10791079
and also a blank line:
1080-
1080+
10811081
and some text with
10821082
extra indentation
10831083
on the next line,
10841084
plus another line at the end.
1085-
1086-
1085+
1086+
10871087
- "End."
10881088
```
10891089
@@ -1109,13 +1109,13 @@ document:
11091109
Several lines of text,
11101110
with some "quotes" of various 'types',
11111111
and also a blank line:
1112-
1112+
11131113
and some text with
11141114
extra indentation
11151115
on the next line,
11161116
plus another line at the end.
1117-
1118-
1117+
1118+
11191119
- "\n\n\n\n"
11201120
- "End."
11211121
```
@@ -1145,7 +1145,7 @@ document:
11451145
Several lines of text,
11461146
with some "quotes" of various 'types',
11471147
and also a blank line:
1148-
1148+
11491149
and some text with
11501150
extra indentation
11511151
on the next line.
@@ -1171,7 +1171,7 @@ PDL:
11711171
```
11721172
document: 'Several lines of text,
11731173
containing ''single quotes''. Escapes (like \n) don''t do anything.
1174-
1174+
11751175
Newlines can be added by leaving a blank line.
11761176
Leading whitespace on lines is ignored.'
11771177
```
@@ -1192,7 +1192,7 @@ document: "Several lines of text,
11921192
containing \"double quotes\". Escapes (like \\n) work.\nIn addition,
11931193
newlines can be esc\
11941194
aped to prevent them from being converted to a space.
1195-
1195+
11961196
Newlines can also be added by leaving a blank line.
11971197
Leading whitespace on lines is ignored."
11981198
```
@@ -1213,7 +1213,7 @@ PDL:
12131213
document: Several lines of text,
12141214
with some "quotes" of various 'types'.
12151215
Escapes (like \n) don't do anything.
1216-
1216+
12171217
Newlines can be added by leaving a blank line.
12181218
Additional leading whitespace is ignored.
12191219
```
@@ -1225,7 +1225,36 @@ Several lines of text, with some "quotes" of various 'types'. Escapes (like \n)
12251225
Newlines can be added by leaving a blank line. Additional leading whitespace is ignored.
12261226
```
12271227
1228+
## Using Ollama models
1229+
1230+
1. Install Ollama e.g., `brew install --cask ollama`
1231+
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.
1233+
1234+
1235+
Example:
1236+
1237+
```
1238+
document:
1239+
- role: user
1240+
document: Hello,
1241+
- model: ollama_chat/granite-code:34b-instruct-q5_K_M
1242+
parameters:
1243+
stop:
1244+
- '!'
1245+
decoding_method: greedy
1246+
```
1247+
1248+
1249+
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:
12281250
1251+
```bash
1252+
export OPENAI_API_BASE=http://localhost:11434/v1
1253+
export OPENAI_API_KEY=ollama # required, but unused
1254+
export OPENAI_ORGANIZATION=ollama # not required
1255+
1256+
pdl <...>
1257+
```
12291258

12301259

12311260

0 commit comments

Comments
 (0)