Skip to content

Commit 8dd32b6

Browse files
authored
Merge pull request #60 from IBM/ollama-doc
Add ollama instructions
2 parents 818ffc0 + 4c98809 commit 8dd32b6

File tree

1 file changed

+70
-42
lines changed

1 file changed

+70
-42
lines changed

docs/tutorial.md

+70-42
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ text:
4646
```
4747

4848
In this program ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_llm.pdl)), the `text` 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
text:
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-
text:
130+
text:
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 @@ text:
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
text:
351351
- "Hello, "
352352
- lang: python
353-
code:
353+
code:
354354
|
355355
import random
356356
import string
@@ -393,7 +393,7 @@ text:
393393
- lang: python
394394
code: |
395395
import requests
396-
response = requests.get('https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q=${ LOCATION }')
396+
response = requests.get('https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q=${ LOCATION }')
397397
result = response.content
398398
def: WEATHER
399399
parser: json
@@ -427,7 +427,7 @@ text:
427427
input:
428428
|
429429
Here is some info about the location of the function in the repo.
430-
repo:
430+
repo:
431431
${ CODE.repo_info.repo }
432432
path: ${ CODE.repo_info.path }
433433
Function_name: ${ CODE.repo_info.function_name }
@@ -449,7 +449,7 @@ text:
449449
${ TRUTH }
450450
"""
451451
result = textdistance.levenshtein.normalized_similarity(expl, truth)
452-
- data:
452+
- data:
453453
input: ${ CODE }
454454
output: ${ EXPLANATION }
455455
metric: ${ EVAL }
@@ -564,9 +564,9 @@ The first two blocks read math problem examples and include them in the document
564564

565565
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 text 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 `<<`.
566566

567-
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+
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.
568568

569-
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+
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/).
570570

571571
Notice that the `repeat` and `then` blocks are followed by `text`. 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 `text` to precede 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. This behavior can be marked explicitly with a `lastOf` block.
572572

@@ -581,7 +581,7 @@ The following [example](https://github.com/IBM/prompt-declaration-language//blob
581581
description: for loop
582582
for:
583583
i: [1, 2, 3, 4]
584-
repeat:
584+
repeat:
585585
${ i }
586586
```
587587
@@ -595,7 +595,7 @@ To output a number of each line, we can specify which string to use to join the
595595
description: for loop
596596
for:
597597
i: [1, 2, 3, 4]
598-
repeat:
598+
repeat:
599599
${ i }
600600
join:
601601
with: "\n"
@@ -614,7 +614,7 @@ To creates an array as a result of iteration, we would write:
614614
description: for loop
615615
for:
616616
i: [1, 2, 3, 4]
617-
repeat:
617+
repeat:
618618
- ${ i }
619619
join:
620620
as: array
@@ -671,15 +671,15 @@ text:
671671
spec: {name: str, age: int}
672672
input:
673673
text:
674-
- for:
674+
- for:
675675
question: ${ data.questions }
676676
answer: ${ data.answers }
677677
repeat:
678678
- |
679679
${ question }
680680
${ answer }
681-
- >
682-
Question: Create a JSON object with fields 'name' and 'age'
681+
- >
682+
Question: Create a JSON object with fields 'name' and 'age'
683683
and set them appropriately. Write the age in letters.
684684
parser: yaml
685685
parameters:
@@ -689,7 +689,7 @@ text:
689689
```
690690
691691
Upon reading the data we use a parser to parse it into a YAML. The `spec` field indicates the expected type for the
692-
data, which is an object with 2 fields: `questions` and `answers` that are a list of string and a list of objects,
692+
data, which is an object with 2 fields: `questions` and `answers` that are a list of string and a list of objects,
693693
respectively. When the interpreter is executed, it checks this type dynamically and throws errors if necessary.
694694
695695
Similarly, the output of the model call is parsed as YAML, and the `spec` indicates that we expect an object with
@@ -754,8 +754,8 @@ To produce an execution trace consumable by the Live Document, you can run the i
754754
pdl --trace <my-example_trace.json> <my-example>
755755
```
756756
757-
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
758-
in the right pane.
757+
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
758+
in the right pane.
759759
760760
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.
761761
@@ -774,13 +774,13 @@ text:
774774
Several lines of text,
775775
with some "quotes" of various 'types',
776776
and also a blank line:
777-
777+
778778
and some text with
779779
extra indentation
780780
on the next line,
781781
plus another line at the end.
782-
783-
782+
783+
784784
- "End."
785785
```
786786
@@ -806,13 +806,13 @@ text:
806806
Several lines of text,
807807
with some "quotes" of various 'types',
808808
and also a blank line:
809-
809+
810810
and some text with
811811
extra indentation
812812
on the next line,
813813
plus another line at the end.
814-
815-
814+
815+
816816
- "End."
817817
```
818818
@@ -834,13 +834,13 @@ text:
834834
Several lines of text,
835835
with some "quotes" of various 'types',
836836
and also a blank line:
837-
837+
838838
and some text with
839839
extra indentation
840840
on the next line,
841841
plus another line at the end.
842-
843-
842+
843+
844844
- "End."
845845
```
846846
@@ -863,13 +863,13 @@ text:
863863
Several lines of text,
864864
with some "quotes" of various 'types',
865865
and also a blank line:
866-
866+
867867
and some text with
868868
extra indentation
869869
on the next line,
870870
plus another line at the end.
871-
872-
871+
872+
873873
- "End."
874874
```
875875
@@ -895,13 +895,13 @@ text:
895895
Several lines of text,
896896
with some "quotes" of various 'types',
897897
and also a blank line:
898-
898+
899899
and some text with
900900
extra indentation
901901
on the next line,
902902
plus another line at the end.
903-
904-
903+
904+
905905
- "\n\n\n\n"
906906
- "End."
907907
```
@@ -931,7 +931,7 @@ text:
931931
Several lines of text,
932932
with some "quotes" of various 'types',
933933
and also a blank line:
934-
934+
935935
and some text with
936936
extra indentation
937937
on the next line.
@@ -957,7 +957,7 @@ PDL:
957957
```
958958
text: 'Several lines of text,
959959
containing ''single quotes''. Escapes (like \n) don''t do anything.
960-
960+
961961
Newlines can be added by leaving a blank line.
962962
Leading whitespace on lines is ignored.'
963963
```
@@ -978,7 +978,7 @@ text: "Several lines of text,
978978
containing \"double quotes\". Escapes (like \\n) work.\nIn addition,
979979
newlines can be esc\
980980
aped to prevent them from being converted to a space.
981-
981+
982982
Newlines can also be added by leaving a blank line.
983983
Leading whitespace on lines is ignored."
984984
```
@@ -999,7 +999,7 @@ PDL:
999999
text: Several lines of text,
10001000
with some "quotes" of various 'types'.
10011001
Escapes (like \n) don't do anything.
1002-
1002+
10031003
Newlines can be added by leaving a blank line.
10041004
Additional leading whitespace is ignored.
10051005
```
@@ -1011,8 +1011,36 @@ Several lines of text, with some "quotes" of various 'types'. Escapes (like \n)
10111011
Newlines can be added by leaving a blank line. Additional leading whitespace is ignored.
10121012
```
10131013
1014+
## Using Ollama models
1015+
1016+
1. Install Ollama e.g., `brew install --cask ollama`
1017+
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)
1018+
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.
10141019
10151020
1021+
Example:
1022+
1023+
```
1024+
text:
1025+
- Hello,
1026+
- model: ollama_chat/granite-code:34b-instruct-q5_K_M
1027+
parameters:
1028+
stop:
1029+
- '!'
1030+
decoding_method: greedy
1031+
```
1032+
1033+
1034+
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:
1035+
1036+
```bash
1037+
export OPENAI_API_BASE=http://localhost:11434/v1
1038+
export OPENAI_API_KEY=ollama # required, but unused
1039+
export OPENAI_ORGANIZATION=ollama # not required
1040+
1041+
pdl <...>
1042+
```
1043+
10161044

10171045

10181046

0 commit comments

Comments
 (0)