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
This has the same output as the previous program. An alternative way of writing this is [this](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_llm_with_input_messages_var.pdl) program.
76
76
77
77
### Parameter defaults for watsonx Granite models
78
78
@@ -104,7 +104,7 @@ Consider the following example ([file](https://github.com/IBM/prompt-declaration
104
104
```
105
105
106
106
Here we assign the output of the model to variable `GEN` using the `def` field. The last line of the program prints out the value of `GEN`. Notice the notation `${ }` for accessing the value of a variable. Any [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) expression is allowed to be used inside these braces. These expressions
107
-
are also used to specify conditions for loops and conditionals. See for example this [file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/conditionals_loops.pdl).
107
+
are also used to specify conditions for loops and conditionals. See for example this [file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/chatbot.pdl).
108
108
109
109
When we execute this program, we obtain:
110
110
```
@@ -115,10 +115,10 @@ GEN is equal to: Hello
115
115
116
116
## Model Chaining
117
117
118
-
In PDL, we can declaratively chain models together as in the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/model_chaining.pdl)):
118
+
In PDL, we can declaratively chain models together as in the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_llm_chaining.pdl)):
In this program, the first call is to a Granite model with the prompt `"Hello\n"`. The following block in the program prints out the sentence: `"\nDid you just say Hello?\n"`. The final line of the program takes the entire context produced so far and passes it as input to the Granite model. Notice that the input passed to this model is the context up to that point, represented as a conversation. This makes it easy to chain models together and continue building on previous interactions. Notice how the conversational context is accumulated implicitly without requiring the user to explicitly manage messages.
@@ -159,12 +159,18 @@ To reset the context when calling a function, we can pass the special argument:
159
159
160
160
Notice that the arguments of function calls are expressions and cannot be arbitrary PDL blocks.
161
161
162
+
A function name can be aliased (see [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/function_alias.pdl)).
163
+
164
+
The context inherited by a function can be reset at the call site (see [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/function_empty_context.pdl)).
165
+
166
+
Functions can be declared with optional parameters (see [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/function_optional_params.pdl)).
167
+
162
168
## Grouping Variable Definitions in Defs
163
169
164
-
In PDL, the above program can be written more neatly by grouping certain variable definitions into a `defs` section, as follows ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/grouping_definitions.pdl)):
170
+
In PDL, the above program can be written more neatly by grouping certain variable definitions into a `defs` section, as follows ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/defs.pdl)):
Here is another example using a regular expression:
323
+
```yaml
313
324
--8<-- "./examples/tutorial/parser_regex.pdl"
314
325
```
315
326
327
+
316
328
We support the following operations with the`regex` parser (indicated with the `mode` field):
317
329
318
330
-`fullmatch` (default)
@@ -334,40 +346,43 @@ See [here](https://docs.python.org/3/library/re.html) for more information on ho
334
346
335
347
## Calling code
336
348
337
-
The following script shows how to execute python code ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_code.pdl)). The python code is executed locally (or in a containerized way if using `pdl --sandbox`). In principle, PDL is agnostic of any specific programming language, but we currently only support Python, Jinja, and shell commands. Variables defined in PDL are copied into the global scope of the Python code, so those variables can be used directly in the code. However, mutating variables in Python has no effect on the variables in the PDL program. The result of the code must be assigned to the variable `result` internally to be propagated to the result of the block. A variable `def` on the code block will then be set to this result.
349
+
The following script shows how to execute python code ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/code_python.pdl)). The python code is executed locally (or in a containerized way if using `pdl --sandbox`). In principle, PDL is agnostic of any specific programming language, but we currently only support Python, Jinja, and shell commands. Variables defined in PDL are copied into the global scope of the Python code, so those variables can be used directly in the code. However, mutating variables in Python has no effect on the variables in the PDL program. The result of the code must be assigned to the variable `result` internally to be propagated to the result of the block. A variable `def` on the code block will then be set to this result.
338
350
339
351
In order to define variables that are carried over to the next Python code block, a special variable `PDL_SESSION` can be used, and
340
352
variables assigned to it as fields.
341
-
See for example: ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/rag/tfidf_rag.pdl)).
353
+
See for example: ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/tfidf_rag.pdl)).
342
354
343
355
```yaml
344
-
--8<-- "./examples/tutorial/calling_code.pdl"
356
+
--8<-- "./examples/tutorial/code_python.pdl"
345
357
```
346
358
347
359
This results in the following output (for example):
348
360
```
349
361
Hello, r!
350
362
```
351
363
352
-
PDL also supports Jinja code blocks, as well as PDL code blocks for meta-cycle programming.
364
+
PDL also supports Jinja code blocks, shell commands, as well as PDL code blocks for meta-cycle programming. For more examples, see
PDL programs can contain calls to REST APIs with Python code. Consider a simple weather app ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/calling_apis.pdl)):
371
+
PDL programs can contain calls to REST APIs with Python code. Consider a simple weather app ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/weather.pdl)):
357
372
358
373
```yaml
359
-
--8<-- "./examples/tutorial/calling_apis.pdl"
374
+
--8<-- "./examples/tutorial/programs/weather.pdl"
360
375
```
361
376
362
377
In this program, we first define a query about the weather in some location (assigned to variable `QUERY`). The next block is a call to a Granite model with few-shot examples to extract the location, which we assign to variable `LOCATION`. The next block makes an API call with Python (mocked in this example). Here the `LOCATION` is appended to the `url`. The result is a JSON object, which may be hard to interpret for a human user. So we make a final call to an LLM to interpret the JSON in terms of weather. Notice that many blocks have `contribute` set to `[]` to hide intermediate results.
363
378
364
379
365
380
## Data Block
366
381
367
-
PDL offers the ability to create JSON data as illustrated by the following example (described in detail in the [Overview](https://ibm.github.io/prompt-declaration-language/#overview) section). The `data` block can gather previously defined variables into a JSON structure. This feature is useful for data generation. Programs such as this one can be generalized to read jsonl files to generate data en masse by piping into another jsonl file ([file](https://github.com/IBM/prompt-declaration-language/blob/main/examples/tutorial/data_block.pdl)).
382
+
PDL offers the ability to create JSON data as illustrated by the following example (described in detail in the [Overview](https://ibm.github.io/prompt-declaration-language/#overview) section). The `data` block can gather previously defined variables into a JSON structure. This feature is useful for data generation. Programs such as this one can be generalized to read jsonl files to generate data en masse by piping into another jsonl file ([file](https://github.com/IBM/prompt-declaration-language/blob/main/examples/tutorial/programs/code-json.pdl)).
Notice that in the `data` block the values are interpreted as Jinja expressions. If values need to be PDL programs to be interpreted, then you need to use
@@ -409,11 +424,11 @@ The `import` block means that the PDL code at that file is executed and its scop
409
424
410
425
## Conditionals and Loops
411
426
412
-
PDL supports conditionals and loops as illustrated in the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/conditionals_loops.pdl)), which implements a chatbot.
427
+
PDL supports conditionals and loops as illustrated in the following example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/chatbot.pdl)), which implements a chatbot.
The first block prompts the user for a query, and this is contributed to the background context. The next
@@ -432,6 +447,8 @@ Notice that the `repeat` and `then` blocks are followed by `text`. This is becau
432
447
The way that the result of each iteration is collated with other iterations can be customized in PDL using
433
448
the `join` feature (see the following section).
434
449
450
+
Another simple example of using an `if` statement is [this](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/if.pdl).
451
+
435
452
### For Loops
436
453
437
454
PDL also offers `for` loops over lists.
@@ -536,7 +553,7 @@ as soon as one of the exit conditions is satisfied:
536
553
### Match block
537
554
538
555
PDL provides a match block for convenience.
539
-
Consider the [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/intrinsics/demo-hallucination.pdl). This shows retrieved RAG documents
556
+
Consider the [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/demo-hallucination.pdl). This shows retrieved RAG documents
540
557
that are then submitted with a query to a RAG Granite model.
541
558
The output contains an answer to the query together with hallucination
542
559
score and possibly a citation.
@@ -565,7 +582,7 @@ The `match` field indicates an expression to match on. The cases follow the `wit
565
582
566
583
## Roles and Chat Templates
567
584
568
-
Consider again the chatbot example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/conditionals_loops.pdl)). By default blocks have role `user`, except for model call blocks, which have role `assistant`.
585
+
Consider again the chatbot example ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/programs/chatbot.pdl)). By default blocks have role `user`, except for model call blocks, which have role `assistant`.
569
586
If we write roles explicitly for the chatbot, we obtain:
570
587
571
588
@@ -624,12 +641,12 @@ parameters:
624
641
625
642
## Type Checking
626
643
627
-
Consider the following PDL program ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/gen-data.pdl)). It first reads the data
628
-
found [here](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/gen-data.yaml) to form few-shot examples. These demonstrations show how to create
644
+
Consider the following PDL program ([file](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/type_checking.pdl)). It first reads the data
645
+
found [here](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/type_checking_data.yaml) to form few-shot examples. These demonstrations show how to create
629
646
some JSON data.
630
647
631
648
```yaml
632
-
--8<-- "./examples/tutorial/gen-data.pdl"
649
+
--8<-- "./examples/tutorial/type_checking.pdl"
633
650
```
634
651
635
652
Upon reading the data we use a parser to parse it into a YAML. The `spec` field indicates the expected type for the
@@ -641,9 +658,9 @@ Similarly, the output of the model call is parsed as YAML, and the `spec` indica
641
658
642
659
When we run this program, we obtain the output:
643
660
```
644
-
gen-data.pdl:8 - Type errors during spec checking:
645
-
gen-data.pdl:8 - 30 should be of type <class 'int'>
646
661
{'name': 'John', 'age': '30'}
662
+
type_checking.pdl:9 - Type errors during spec checking:
663
+
type_checking.pdl:9 - twentyfive should be of type <class 'int'>
647
664
```
648
665
649
666
Notice that since we asked the age to be produced in letters, we got a string back and this causes a type error indicated above.
@@ -670,9 +687,24 @@ the examples below:
670
687
- `[{question: str, answer: str}]`: same as above
671
688
- `{enum: [red, green, blue]}`: an enumeration
672
689
690
+
Another example of type checking a list can be found [here](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/type_list.pdl).
691
+
673
692
## Structured Decoding
674
693
675
-
When a type is specified in a PDL block, it is used for structured decoding with models that support it. The fields `guided_json` and `response_format` are added automatically by the interpreter with a JSON Schema value obtained from the type. Models that support structured decoding will then use this to generate JSON of the correct format.
694
+
When a type is specified in a PDL block, it is used for structured decoding with models that support it. The fields `guided_json` and `response_format` are added automatically by the interpreter with a JSON Schema value obtained from the type. Models on platforms that support structured decoding will then use this to generate JSON of the correct format.
695
+
696
+
The following [program](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/structured-decoding.pdl):
0 commit comments