Skip to content

Commit 23c25d4

Browse files
committed
Initial prompt library
Signed-off-by: Claudio Spiess <[email protected]>
1 parent 61ef436 commit 23c25d4

File tree

9 files changed

+1198
-0
lines changed

9 files changed

+1198
-0
lines changed

contrib/prompt_library/CoT.pdl

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
description: CoT pattern introduced by Wei et al. (2022)
2+
defs:
3+
# Chain of Thought
4+
cot_block:
5+
function:
6+
question: str
7+
reasoning: str
8+
answer: str
9+
return: |-
10+
Question: ${ question }
11+
Answer: Let's think step by step. ${ reasoning }
12+
The answer is ${ answer }
13+
14+
# Auto Chain of Thought Zhang et al. (2022)
15+
# The idea is to use a _model_ to generate a reasoning path, even if not very accurate.
16+
# It is best combined with some fewshot examples
17+
auto_chain_of_thought:
18+
function:
19+
question: str
20+
model: str
21+
answer: str
22+
return:
23+
text:
24+
- |-
25+
Question: ${ question }
26+
- "Answer: Let's think step by step. "
27+
- model: ${ model }
28+
parameters:
29+
# decoding_method: "greedy"
30+
temperature: 0
31+
stop:
32+
- "The answer is"
33+
include_stop_sequence: false
34+
- "The answer is ${ answer }"
35+
36+
fewshot_cot:
37+
function:
38+
examples:
39+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
40+
return:
41+
text:
42+
- for:
43+
example: ${ examples }
44+
repeat:
45+
text:
46+
- call: ${ cot_block }
47+
args:
48+
question: ${ example.question }
49+
reasoning: ${ example.reasoning }
50+
answer: ${ example.answer }
51+
- "\n\n"
52+
join:
53+
with: ""
54+
55+
chain_of_thought:
56+
function:
57+
question: str
58+
model: str
59+
examples:
60+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
61+
return:
62+
text:
63+
- call: ${ fewshot_cot }
64+
args:
65+
examples: ${ examples }
66+
- "Question: ${ question }\n"
67+
- "Answer: Let's think step by step. "
68+
- model: ${ model }
69+
parameters:
70+
temperature: 0
71+
stop:
72+
- "<|endoftext|>"
73+
include_stop_sequence: false
74+
75+
claim_cot:
76+
function:
77+
question: str
78+
reasoning: str
79+
answer: str
80+
return: |-
81+
Task: On June 2017, the following claim was made: ${ question }
82+
Q: Was this claim true or false?
83+
Thought: Let's think step by step. ${ reasoning|trim }
84+
Answer: The claim is ${ answer }
85+
86+
fewshot_cot_claim:
87+
function:
88+
examples:
89+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
90+
return:
91+
text:
92+
- for:
93+
example: ${ examples }
94+
repeat:
95+
text:
96+
- call: ${ claim_cot }
97+
args:
98+
question: ${ example.question }
99+
reasoning: ${ example.reasoning }
100+
answer: ${ example.answer }
101+
- "\n\n"
102+
join:
103+
with: ""
104+
105+
chain_of_thought_claim:
106+
function:
107+
question: str
108+
model: str
109+
examples:
110+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
111+
return:
112+
text:
113+
- call: ${ fewshot_cot_claim }
114+
args:
115+
examples: ${ examples }
116+
- "${ question }\n"
117+
- "Thought: Let's think step by step. "
118+
- model: ${ model }
119+
parameters:
120+
temperature: 0
121+
stop:
122+
- "<|endoftext|>"
123+
max_tokens: 1024
124+
include_stop_sequence: false

contrib/prompt_library/PoT.pdl

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
description: Program of Thoughts pattern Chen (2022), TMLR
2+
defs:
3+
program_of_thought:
4+
function:
5+
question: str
6+
model: str
7+
return:
8+
- |
9+
Question: Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?
10+
# Python code, return ans
11+
total_eggs = 16
12+
eaten_eggs = 3
13+
baked_eggs = 4
14+
sold_eggs = total_eggs - eaten_eggs - baked_eggs
15+
dollars_per_egg = 2
16+
result = sold_eggs * dollars_per_egg
17+
18+
Question: A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
19+
# Python code, return ans
20+
bolts_of_blue_fiber = 2
21+
bolts_of_white_fiber = num_of_blue_fiber / 2
22+
result = bolts_of_blue_fiber + bolts_of_white_fiber
23+
24+
Question: Josh decides to try flipping a house. He buys a house for $80,000 and then puts in $50,000 in repairs. This increased the value of the house by 150%. How much profit did he make?
25+
# Python code, return ans
26+
cost_of_original_house = 80000
27+
increase_rate = 150 / 100
28+
value_of_house = (1 + increase_rate) * cost_of_original_house
29+
cost_of_repair = 50000
30+
result = value_of_house - cost_of_repair - cost_of_original_house
31+
32+
Question: Every day, Wendi feeds each of her chickens three cups of mixed chicken feed, containing seeds, mealworms and vegetables to help keep them healthy. She gives the chickens their feed in three separate meals. In the morning, she gives her flock of chickens 15 cups of feed. In the afternoon, she gives her chickens another 25 cups of feed. How many cups of feed does she need to give her chickens in the final meal of the day if the size of Wendi's flock is 20 chickens?
33+
# Python code, return ans
34+
numb_of_chickens = 20
35+
cups_for_each_chicken = 3
36+
cups_for_all_chicken = num_of_chickens * cups_for_each_chicken
37+
cups_in_the_morning = 15
38+
cups_in_the_afternoon = 25
39+
result = cups_for_all_chicken - cups_in_the_morning - cups_in_the_afternoon
40+
41+
Question: Kylar went to the store to buy glasses for his new apartment. One glass costs $5, but every second glass costs only 60% of the price. Kylar wants to buy 16 glasses. How much does he need to pay for them?
42+
# Python code, return ans
43+
num_glasses = 16
44+
first_glass_cost = 5
45+
second_glass_cost = 5 * 0.6
46+
result = 0
47+
for i in range(num_glasses):
48+
if i % 2 == 0:
49+
result += first_glass_cost
50+
else:
51+
result += second_glass_cost
52+
53+
Question: Marissa is hiking a 12-mile trail. She took 1 hour to walk the first 4 miles, then another hour to walk the next two miles. If she wants her average speed to be 4 miles per hour, what speed (in miles per hour) does she need to walk the remaining distance?
54+
# Python code, return ans
55+
average_mile_per_hour = 4
56+
total_trail_miles = 12
57+
remaining_miles = total_trail_miles - 4 - 2
58+
total_hours = total_trail_miles / average_mile_per_hour
59+
remaining_hours = total_hours - 2
60+
result = remaining_miles / remaining_hours
61+
62+
Question: Carlos is planting a lemon tree. The tree will cost $90 to plant. Each year it will grow 7 lemons, which he can sell for $1.5 each. It costs $3 a year to water and feed the tree. How many years will it tak
63+
e before he starts earning money on the lemon tree?
64+
# Python code, return ans
65+
total_cost = 90
66+
cost_of_watering_and_feeding = 3
67+
cost_of_each_lemon = 1.5
68+
num_of_lemon_per_year = 7
69+
result = 0
70+
while total_cost > 0:
71+
total_cost += cost_of_watering_and_feeding
72+
total_cost -= num_of_lemon_per_year * cost_of_each_lemon
73+
result += 1
74+
75+
Question: When Freda cooks canned tomatoes into sauce, they lose half their volume. Each 16 ounce can of tomatoes that she uses contains three tomatoes. Freda's last batch of tomato sauce made 32 ounces of sauce. How many tomatoes did Freda use?
76+
# Python code, return ans
77+
lose_rate = 0.5
78+
num_tomato_contained_in_per_ounce_sauce = 3 / 16
79+
ounce_sauce_in_last_batch = 32
80+
num_tomato_in_last_batch = ounce_sauce_in_last_batch * num_tomato_contained_in_per_ounce_sauce
81+
result = num_tomato_in_last_batch / (1 - lose_rate)
82+
83+
Question: Jordan wanted to surprise her mom with a homemade birthday cake. From reading the instructions, she knew it would take 20 minutes to make the cake batter and 30 minutes to bake the cake. The cake would require 2 hours to cool and an additional 10 minutes to frost the cake. If she plans to make the cake all on the same day, what is the latest time of day that Jordan can start making the cake to be ready to serve it at 5:00 pm?
84+
# Python code, return ans
85+
minutes_to_make_batter = 20
86+
minutes_to_bake_cake = 30
87+
minutes_to_cool_cake = 2 * 60
88+
minutes_to_frost_cake = 10
89+
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
90+
total_hours = total_minutes / 60
91+
result = 5 - total_hours
92+
93+
Question: ${ question }
94+
# Python code, return ans
95+
96+
- def: PROGRAM
97+
model: ${ model }
98+
parameters:
99+
stop: ["\nAnswer: "]
100+
include_stop_sequence: false
101+
- def: ANSWER
102+
lang: python
103+
code: ${ PROGRAM }
104+
- get: ANSWER
105+
106+
program_of_thought_backtick:
107+
function:
108+
question: str
109+
model: str
110+
return:
111+
- |
112+
Question: Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers' market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers' market?
113+
# Python code, return ans
114+
```python
115+
total_eggs = 16
116+
eaten_eggs = 3
117+
baked_eggs = 4
118+
sold_eggs = total_eggs - eaten_eggs - baked_eggs
119+
dollars_per_egg = 2
120+
result = sold_eggs * dollars_per_egg
121+
```
122+
123+
Question: A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
124+
# Python code, return ans
125+
```python
126+
bolts_of_blue_fiber = 2
127+
bolts_of_white_fiber = num_of_blue_fiber / 2
128+
result = bolts_of_blue_fiber + bolts_of_white_fiber
129+
```
130+
131+
Question: Josh decides to try flipping a house. He buys a house for $80,000 and then puts in $50,000 in repairs. This increased the value of the house by 150%. How much profit did he make?
132+
# Python code, return ans
133+
```python
134+
cost_of_original_house = 80000
135+
increase_rate = 150 / 100
136+
value_of_house = (1 + increase_rate) * cost_of_original_house
137+
cost_of_repair = 50000
138+
result = value_of_house - cost_of_repair - cost_of_original_house
139+
```
140+
141+
Question: Every day, Wendi feeds each of her chickens three cups of mixed chicken feed, containing seeds, mealworms and vegetables to help keep them healthy. She gives the chickens their feed in three separate meals. In the morning, she gives her flock of chickens 15 cups of feed. In the afternoon, she gives her chickens another 25 cups of feed. How many cups of feed does she need to give her chickens in the final meal of the day if the size of Wendi's flock is 20 chickens?
142+
# Python code, return ans
143+
```python
144+
numb_of_chickens = 20
145+
cups_for_each_chicken = 3
146+
cups_for_all_chicken = num_of_chickens * cups_for_each_chicken
147+
cups_in_the_morning = 15
148+
cups_in_the_afternoon = 25
149+
result = cups_for_all_chicken - cups_in_the_morning - cups_in_the_afternoon
150+
```
151+
152+
Question: Kylar went to the store to buy glasses for his new apartment. One glass costs $5, but every second glass costs only 60% of the price. Kylar wants to buy 16 glasses. How much does he need to pay for them?
153+
# Python code, return ans
154+
```python
155+
num_glasses = 16
156+
first_glass_cost = 5
157+
second_glass_cost = 5 * 0.6
158+
result = 0
159+
for i in range(num_glasses):
160+
if i % 2 == 0:
161+
result += first_glass_cost
162+
else:
163+
result += second_glass_cost
164+
```
165+
166+
Question: Marissa is hiking a 12-mile trail. She took 1 hour to walk the first 4 miles, then another hour to walk the next two miles. If she wants her average speed to be 4 miles per hour, what speed (in miles per hour) does she need to walk the remaining distance?
167+
# Python code, return ans
168+
```python
169+
average_mile_per_hour = 4
170+
total_trail_miles = 12
171+
remaining_miles = total_trail_miles - 4 - 2
172+
total_hours = total_trail_miles / average_mile_per_hour
173+
remaining_hours = total_hours - 2
174+
result = remaining_miles / remaining_hours
175+
```
176+
177+
Question: Carlos is planting a lemon tree. The tree will cost $90 to plant. Each year it will grow 7 lemons, which he can sell for $1.5 each. It costs $3 a year to water and feed the tree. How many years will it tak
178+
e before he starts earning money on the lemon tree?
179+
# Python code, return ans
180+
```python
181+
total_cost = 90
182+
cost_of_watering_and_feeding = 3
183+
cost_of_each_lemon = 1.5
184+
num_of_lemon_per_year = 7
185+
result = 0
186+
while total_cost > 0:
187+
total_cost += cost_of_watering_and_feeding
188+
total_cost -= num_of_lemon_per_year * cost_of_each_lemon
189+
result += 1
190+
```
191+
192+
Question: When Freda cooks canned tomatoes into sauce, they lose half their volume. Each 16 ounce can of tomatoes that she uses contains three tomatoes. Freda's last batch of tomato sauce made 32 ounces of sauce. How many tomatoes did Freda use?
193+
# Python code, return ans
194+
```python
195+
lose_rate = 0.5
196+
num_tomato_contained_in_per_ounce_sauce = 3 / 16
197+
ounce_sauce_in_last_batch = 32
198+
num_tomato_in_last_batch = ounce_sauce_in_last_batch * num_tomato_contained_in_per_ounce_sauce
199+
result = num_tomato_in_last_batch / (1 - lose_rate)
200+
```
201+
202+
Question: Jordan wanted to surprise her mom with a homemade birthday cake. From reading the instructions, she knew it would take 20 minutes to make the cake batter and 30 minutes to bake the cake. The cake would require 2 hours to cool and an additional 10 minutes to frost the cake. If she plans to make the cake all on the same day, what is the latest time of day that Jordan can start making the cake to be ready to serve it at 5:00 pm?
203+
# Python code, return ans
204+
```python
205+
minutes_to_make_batter = 20
206+
minutes_to_bake_cake = 30
207+
minutes_to_cool_cake = 2 * 60
208+
minutes_to_frost_cake = 10
209+
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
210+
total_hours = total_minutes / 60
211+
result = 5 - total_hours
212+
```
213+
214+
Question: ${ question }
215+
# Python code, return ans
216+
- def: PROGRAM
217+
model: ${ model }
218+
parser:
219+
regex: '```.*\n((?:.|\n|$)*?)$\n\s*```' # extracts code from backtick blocks
220+
mode: findall
221+
parameters:
222+
stop: ["\nAnswer: "]
223+
include_stop_sequence: false
224+
- def: ANSWER
225+
lang: python
226+
code: ${ PROGRAM|join('\n') }
227+
- get: ANSWER

contrib/prompt_library/RAG.pdl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
description: Retrieval-Augmented Generation (RAG) following Lewis et al.
2+
defs:
3+
# Corpus: Store the retrieval object in the PDL session
4+
corpus:
5+
function:
6+
corpus: {list: str}
7+
return:
8+
- lang: python
9+
code: |
10+
from rank_bm25 import BM25Okapi
11+
PDL_SESSION.corpus = corpus
12+
PDL_SESSION.tokenized_corpus = [doc.split(" ") for doc in corpus]
13+
PDL_SESSION.bm25_corpus = BM25Okapi(PDL_SESSION.tokenized_corpus)
14+
result = None
15+
# Retrieve from corpus in PDL session
16+
retrieve:
17+
function:
18+
query: str
19+
num_examples: int
20+
spec: {list: str}
21+
return:
22+
- lang: python
23+
code: |
24+
from rank_bm25 import BM25Okapi
25+
tokenized_query = query.split(" ")
26+
result = PDL_SESSION.bm25_corpus.get_top_n(tokenized_query, PDL_SESSION.corpus, n=num_examples)

0 commit comments

Comments
 (0)