Skip to content

Commit 0b3dbd9

Browse files
authored
dependent/independent context implementation (#945)
* dependent/independent context implementation Signed-off-by: Mandana Vaziri <[email protected]>
1 parent 1204d12 commit 0b3dbd9

28 files changed

+883
-185
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ jobs:
4444
- name: pre-commit checks
4545
run: pre-commit run -a
4646
- name: run tests
47-
run: py.test -v --capture=tee-sys --ignore=tests/test_examples_run.py --ignore=tests/test_schema.py tests
47+
run: py.test -v --capture=tee-sys --ignore=tests/test_examples_run.py --ignore=tests/test_optimizer.py --ignore=tests/test_schema.py tests
4848
if: matrix.python-version == '3.11'
4949
- name: run tests
50-
run: py.test -v --capture=tee-sys --ignore=tests/test_examples_run.py tests
50+
run: py.test -v --capture=tee-sys --ignore=tests/test_examples_run.py --ignore=tests/test_optimizer.py tests
5151
if: matrix.python-version != '3.11'
5252

examples/demo/9-react.pdl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,38 @@ text:
3838
What profession does Nicholas Ray and Elia Kazan have in common?
3939
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
4040
Action:
41-
<tool_call>[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
41+
[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
4242
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
4343
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
4444
Action:
45-
<tool_call>[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
45+
[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
4646
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
4747
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
4848
Action:
49-
<tool_call>[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
49+
[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
5050

5151

5252
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
5353
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
5454
Action:
55-
<tool_call>[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
55+
[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
5656
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) ...
5757
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
5858
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
5959
Action:
60-
<tool_call>[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
60+
[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
6161
contribute: [context]
6262

6363
- "How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.\n"
6464
- repeat:
6565
text:
6666
- def: thought
67-
model: ollama_chat/granite3.2:8b
67+
model: ollama_chat/granite3.3:8b
6868
parameters:
6969
stop: ["Action:"]
7070
- "Action:\n"
7171
- def: action
72-
model: ollama_chat/granite3.2:8b
72+
model: ollama_chat/granite3.3:8b
7373
parameters:
7474
stop: ["\n"]
7575
parser: json
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
defs:
2+
doc:
3+
data:
4+
text: |
5+
Audrey Faith McGraw (born September 21, 1967) is an American singer
6+
and record producer. She is one of the most successful country artists
7+
of all time, having sold more than 40 million albums worldwide. Hill is
8+
married to American singer Tim McGraw, with whom she has recorded several duets.
9+
Hill's first two albums, Take Me as I Am (1993) and It Matters to Me (1995),
10+
were major successes and placed a combined three number ones on Billboard's
11+
country charts. Hill's debut album was Take Me as I Am (1993); sales were strong,
12+
buoyed by the chart success of "Wild One". Hill became the first female country
13+
singer in 30 years to hold Billboard's number one position for four consecutive
14+
weeks when "Wild One" managed the feat in 1994. Her version of "Piece of My Heart",
15+
also went to the top of the country charts in 1994. The album sold a total of
16+
3 million copies. Other singles from the album include "Take Me as I Am". The recording
17+
of Faith's second album was delayed by surgery to repair a ruptured blood vessel on
18+
her vocal cords. It Matters to Me finally appeared in 1995 and was another
19+
success, with the title track becoming her third number-one country single.
20+
Several other top 10 singles followed, and more than 3 million copies of the
21+
album were sold. The fifth single from the album, "I Can't Do That Anymore",
22+
was written by country music artist Alan Jackson. Other singles from the album
23+
include "You Can't Lose Me", "Someone Else's Dream", and "Let's Go to Vegas".
24+
During this period, Hill appeared on the acclaimed PBS music program Austin City Limits.
25+
In spring 1996, Hill began the Spontaneous Combustion Tour with country singer Tim McGraw.
26+
At that time, Hill had recently become engaged to her former producer, Scott Hendricks,
27+
and McGraw had recently broken an engagement. McGraw and Hill were quickly
28+
attracted to each other and began an affair. After discovering that Hill was
29+
pregnant with their first child, the couple married on October 6, 1996. The
30+
couple have three daughters together: Gracie Katherine (born 1997), Maggie Elizabeth (born 1998)
31+
and Audrey Caroline (born 2001). Since their marriage, Hill and McGraw have endeavored
32+
never to be apart for more than three consecutive days. After the release of It Matters to Me,
33+
Hill took a three-year break from recording to give herself a rest from four years of touring
34+
and to begin a family with McGraw. During her break, she joined forces with her husband
35+
for their first duet, "It's Your Love". The song stayed at number one for six weeks,
36+
and won awards from both the Academy of Country Music and the Country Music Association.
37+
Hill has remarked that sometimes when they perform the song together,
38+
"it [doesn't] feel like anybody else was really watching."
39+
40+
text:
41+
- Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?
42+
- model: "granite3.2:2b"
43+
backend: openai
44+
parameters:
45+
documents:
46+
- ${ doc }
47+
controls:
48+
hallucinations: true
49+
modelResponse: output
50+
- "\nHallucinations:\n"
51+
- for:
52+
hallucination: ${ output.results[0].next_message.hallucinations }
53+
repeat:
54+
text:
55+
- "Hallucination Risk: ${ hallucination.risk }"
56+
- "\nSentence: ${ hallucination.response_text }"
57+
join:
58+
with: "\n"

examples/demos/react.pdl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
defs:
2+
tools:
3+
data:
4+
- type: function
5+
function:
6+
name: Calc
7+
description: Calculator function
8+
parameters:
9+
type: object
10+
properties:
11+
expr:
12+
type: string
13+
description: Arithmetic expression to calculate
14+
required:
15+
- expr
16+
- type: function
17+
function:
18+
name: Search
19+
description: Wikipedia search
20+
parameters:
21+
type: object
22+
properties:
23+
topic:
24+
type: string
25+
description: Topic to search
26+
required:
27+
- topic
28+
text:
29+
- role: system
30+
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
31+
contribute: [context]
32+
- role: tools
33+
content:
34+
text: ${ tools }
35+
contribute: [context]
36+
- text:
37+
|
38+
What profession does Nicholas Ray and Elia Kazan have in common?
39+
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
40+
Action:
41+
[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
42+
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
43+
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
44+
Action:
45+
[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
46+
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
47+
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
48+
Action:
49+
[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
50+
51+
52+
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
53+
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
54+
Action:
55+
[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
56+
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) ...
57+
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
58+
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
59+
Action:
60+
[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
61+
contribute: [context]
62+
63+
- "How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching for a birthday for a person, simply ask for the name of that person.\n"
64+
- repeat:
65+
text:
66+
- def: thought
67+
model: ollama_chat/granite3.3:8b
68+
parameters:
69+
stop: ["Action:"]
70+
- "Action:\n"
71+
- def: action
72+
model: ollama_chat/granite3.3:8b
73+
parameters:
74+
stop: ["\n"]
75+
parser: json
76+
- "\nObservation: "
77+
- match: ${ action[0].name }
78+
with:
79+
- case: 'Search'
80+
then:
81+
lang: python
82+
code: |
83+
import warnings, wikipedia
84+
warnings.simplefilter("ignore")
85+
try:
86+
result = wikipedia.summary("${ action[0].arguments.topic }")
87+
except wikipedia.WikipediaException as e:
88+
result = str(e)
89+
- case: 'Calc'
90+
then:
91+
lang: python
92+
code: result = ${ action[0].arguments.expr }
93+
- "\n"
94+
until: ${ action[0].name == "Finish" }

examples/demos/react_call.pdl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
description: Wikipedia example using the react_fun function definition
2+
text:
3+
- import: react_fun
4+
def: lib
5+
- call: ${ lib.react }
6+
args:
7+
question: "How many years ago was the discoverer of the Hudson River born? Keep in mind we are in 2025. When searching avoid using the words discovery or birthday.\n"
8+
model: ollama_chat/granite3.3:8b
9+
10+

examples/demos/react_fun.pdl

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
description: Function definition for react
2+
defs:
3+
react_inner:
4+
function:
5+
examples: [str]
6+
question: str
7+
model: str
8+
return:
9+
text:
10+
- defs:
11+
tools:
12+
data:
13+
- type: function
14+
function:
15+
name: Calc
16+
description: Calculator function
17+
parameters:
18+
type: object
19+
properties:
20+
expr:
21+
type: string
22+
description: Arithmetic expression to calculate
23+
required:
24+
- expr
25+
- type: function
26+
function:
27+
name: Search
28+
description: Wikipedia search
29+
parameters:
30+
type: object
31+
properties:
32+
topic:
33+
type: string
34+
description: Topic to search
35+
required:
36+
- topic
37+
- for:
38+
ex: ${ examples }
39+
repeat:
40+
"${ ex }\n"
41+
contribute: [context]
42+
- ${ question }
43+
- "\n"
44+
- role: system
45+
text: You are Granite, developed by IBM. You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.
46+
contribute: [context]
47+
- role: tools
48+
content:
49+
text: ${ tools }
50+
contribute: [context]
51+
- def: prev_action
52+
contribute: []
53+
data: none
54+
- def: exit
55+
contribute: []
56+
data: False
57+
- repeat:
58+
text:
59+
- def: thought
60+
model: ${ model }
61+
parameters:
62+
temperature: 0
63+
stop: ["Action:"]
64+
- "Action:\n"
65+
- def: action
66+
model: ${ model }
67+
parameters:
68+
temperature: 0
69+
stop: ["\n"]
70+
parser: json
71+
- if: ${ action != prev_action}
72+
then:
73+
def: observation
74+
if: ${ action[0].name == "Search" }
75+
then:
76+
text:
77+
- "\nObservation: "
78+
- lang: python
79+
code: |
80+
import warnings, wikipedia
81+
warnings.simplefilter("ignore")
82+
try:
83+
result = wikipedia.summary("${ action[0].arguments.topic }")
84+
except wikipedia.WikipediaException as e:
85+
result = str(e)
86+
- "\n"
87+
else:
88+
if: ${ action[0].name == "Calc" }
89+
then:
90+
text:
91+
- "\nObservation: "
92+
- lang: python
93+
code: result = ${ action[0].arguments.expr }
94+
- "\n"
95+
else:
96+
def: exit
97+
contribute: []
98+
data: True
99+
- def: prev_action
100+
contribute: []
101+
data: ${ action }
102+
until: ${ action[0].name == "Finish" or exit }
103+
104+
react:
105+
function:
106+
question: str
107+
model: str
108+
return:
109+
defs:
110+
examples:
111+
array:
112+
- text:
113+
|
114+
What profession does Nicholas Ray and Elia Kazan have in common?
115+
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
116+
Action:
117+
[{"name": "Search", "arguments": {"topic": "Nicholas Ray"}}]
118+
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
119+
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
120+
Action:
121+
[{"name": "Search", "arguments": {"topic": "Elia Kazan"}}]
122+
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
123+
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
124+
Action:
125+
[{"name": "Finish", "arguments": {"topic": "director, screenwriter, actor"}}]
126+
127+
128+
What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
129+
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado ...
130+
Action:
131+
[{"name": "Search", "arguments": {"topic": "Colorado orogeny"}}]
132+
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) ...
133+
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
134+
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
135+
Action:
136+
[{"name": "Finish", "arguments": {"topic": "1,800 to 7,000 ft"}}]
137+
138+
call: ${ react_inner }
139+
args:
140+
pdl_context: []
141+
examples: ${ examples }
142+
question: ${ question }
143+
model: ${ model }
144+
145+

0 commit comments

Comments
 (0)