Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ text:
```


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 the list of messages used to make calls to LLMs via LiteLLM.
A PDL program computes two 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 the list of messages used to make calls to LLMs via LiteLLM.

The PDL interpreter can also stream the background conversation instead of the result:

Expand Down
197 changes: 142 additions & 55 deletions docs/tutorial.md

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions examples/sdk/hello_dict.py

This file was deleted.

23 changes: 0 additions & 23 deletions examples/sdk/hello_prog.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/tutorial/calling_llm.pdl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
description: Hello world calling a model
description: Calling a model on the implicit background context
text:
- "Hello\n"
- model: ollama_chat/granite3.2:2b
parameters:
stop: ['!']
stop: ['!']
2 changes: 1 addition & 1 deletion examples/tutorial/calling_llm_with_input.pdl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: Hello world calling a model
description: Calling a model with an input text
text:
- "Hello\n"
- model: ollama_chat/granite3.2:2b
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/calling_llm_with_input_messages.pdl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: Hello world calling a model
description: Calling a model with an explicit list of messages
text:
- "Hello\n"
- model: ollama_chat/granite3.2:2b
Expand Down
10 changes: 6 additions & 4 deletions examples/tutorial/calling_llm_with_input_messages_var.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defs:
prompt:
array:
- role: system
content: You are a helpful software engineer. You write clear, concise, well-commented code.
content: You are a helpful assistant that is fluent in French.
- role: user
content: Write a Python function that implement merge sort.
model: ollama_chat/granite3.2:2b
input: ${ prompt }
content: Translate the word 'Hello' to French
text:
- "Hello\n"
- model: ollama_chat/granite3.2:2b
input: ${ prompt }
14 changes: 0 additions & 14 deletions examples/tutorial/defs-hello.pdl

This file was deleted.

34 changes: 14 additions & 20 deletions examples/tutorial/defs.pdl
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
description: Function def and call
defs:
translate:
function:
sentence: string
language: string
return:
text:
- "Hello\n"
- defs:
fr:
lastOf:
- "\nTranslate the sentence '${ sentence }' to ${ language }.\n"
- "\nTranslate to French\n"
- model: ollama_chat/granite3.2:2b
parameters:
stop: ["\n"]
text:
- call: ${ translate }
args:
sentence: I love Paris!
language: French
- "\n"
- call: ${ translate }
args:
sentence: I love Madrid!
language: Spanish
es:
lastOf:
- "\nTranslate to Spanish\n"
- model: ollama_chat/granite3.2:2b
text: |

In Fench: ${ fr }

In Spanish: ${ es }
2 changes: 1 addition & 1 deletion examples/tutorial/function_alias.pdl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: Hello function
description: Use a function as a value
defs:
hello:
function:
Expand Down
14 changes: 14 additions & 0 deletions examples/tutorial/function_call_in_jinja.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: Calling a PDL function from Jinja
defs:
translate:
function:
sentence: string
language: string
return:
lastOf:
- |
Translate the sentence '${ sentence }' to ${ language }.
Only give the result of the translation.
- model: ollama_chat/granite3.2:2b
text: |
The way to say hello in French is ${ translate("Hello", language="French") }.
24 changes: 12 additions & 12 deletions examples/tutorial/function_definition.pdl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
description: Function def and call
description: Function definition and call
defs:
translate:
function:
sentence: string
language: string
return:
lastOf:
- |
Translate the sentence '${ sentence }' to ${ language }.
Only give the result of the translation.
- model: ollama_chat/granite3.2:2b
text:
- def: translate
function:
sentence: string
language: string
return:
lastOf:
- "\nTranslate the sentence '${ sentence }' to ${ language }.\n"
- model: ollama_chat/granite3.2:2b
parameters:
stop: ["\n"]
temperature: 0
- call: ${ translate }
args:
sentence: I love Paris!
Expand Down
31 changes: 21 additions & 10 deletions examples/tutorial/function_empty_context.pdl
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
description: Hello world with function definition and call
description: Function call with an empty context
defs:
translate:
function:
sentence: string
language: string
return:
lastOf:
- |
Translate the sentence '${ sentence }' to ${ language }.
Only give the result of the translation.
- model: ollama_chat/granite3.2:2b
text:
- def: hello
function:
name: string
return:
text:
- Hello ${ name }!
- model: ollama_chat/granite3.2:8b
- call: ${ hello }
- call: ${ translate }
args:
name: World
sentence: I love Paris!
language: French
pdl_context: []
- "\n"
- call: ${ translate }
args:
sentence: I love Madrid!
language: Spanish
pdl_context: []
25 changes: 15 additions & 10 deletions examples/tutorial/function_optional_params.pdl
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
description: Hello world with function definition and call
description: Function with optional parameter
defs:
hello:
function:
name: string
lastName: {optional: string} # optional parameter
return:
if: ${ lastName is defined }
then: Hello ${ name } ${ lastName }!
else: Hello ${ name }!
text:
- def: hello
function:
name: string
lastName: {optional: string} # optional parameter
return:
if: ${ lastName is defined }
then: Hello ${ name } ${ lastName }!
else: Hello ${ name }!
- call: ${ hello }
args:
name: World
lastName: Universe
- "\n"
- call: ${ hello }
args:
name: Earth
lastName: Planet
8 changes: 8 additions & 0 deletions examples/tutorial/lastOf.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
description: Explicit use of role
role: user
text:
- "Hello\n"
- lastOf:
- role: system
text: "You are a polite assistant that likes to answer very formally."
- model: ollama_chat/granite3.2:2b
9 changes: 9 additions & 0 deletions examples/tutorial/local_computation.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
description: Local computations using defs
text:
- "Hello\n"
- defs:
GEN:
model: ollama_chat/granite3.2:2b
parameters:
stop: ['!']
- "The variable GEN is equal to: ${ GEN }"
28 changes: 9 additions & 19 deletions examples/tutorial/muting_block_output.pdl
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
description: Function def and call
defs:
translate:
function:
sentence: string
language: string
return:
text:
- text: "\nTranslate the sentence '${ sentence }' to ${ language }.\n"
contribute: [context]
- model: ollama_chat/granite3.2:2b
parameters:
stop: ["\n"]
description: Control block outputs with `contribute`
text:
- call: ${ translate }
- "Hello\n"
- text: "\nTranslate to French\n"
contribute: [context]
- model: ollama_chat/granite3.2:2b
contribute: []
def: FRENCH
args:
sentence: I love Paris!
language: French
- "The french sentence was: ${ FRENCH }"
def: fr
- |

In Fench: ${ fr }
7 changes: 7 additions & 0 deletions examples/tutorial/role.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: Explicit use of role
role: user
text:
- "Hello\n"
- role: system
text: "You are a polite assistant that likes to answer very formally."
- model: ollama_chat/granite3.2:2b
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/tutorial/sdk/hello_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pdl.pdl import exec_dict


def main():
hello = {
"text": [
"Hello\n",
{
"model": "ollama_chat/granite3.2:2b",
"parameters": {
"stop": ["!"],
},
},
]
}
result = exec_dict(hello)
print(result)


if __name__ == "__main__":
main()
File renamed without changes.
37 changes: 37 additions & 0 deletions examples/tutorial/sdk/hello_parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import concurrent.futures

from pdl.pdl import exec_str

HELLO = """
text:
- >+
Hello, my name is ${name}
- model: ollama_chat/granite3.2:2b
"""


def _run_agent(name):
pdl_output = exec_str(
HELLO,
scope={"name": name},
config={
"yield_result": False,
"yield_background": False,
"batch": 1, # disable streaming
},
)
return pdl_output


if __name__ == "__main__":
data = ["Alice", "Nicolas", "Rosa", "Remi"]
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = {executor.submit(_run_agent, name) for name in data}
executor.map(_run_agent, data)
for future in concurrent.futures.as_completed(futures):
try:
result = future.result()
except Exception as e:
print(f"Task raised an exception: {e}")
else:
print(result)
Loading