Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prompt library #65

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
26 changes: 26 additions & 0 deletions examples/granite/fim.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
description: Example of fill-in-the middle using Granite
defs:
fim:
function:
prefix: str
suffix: str
return: "<fim_prefix>{{ prefix }}<fim_suffix>{{ suffix }}<fim_middle>"
text:
- |
def remove_non_ascii(s: str) -> str:
"""
- model: ibm/granite-34b-code-instruct
platform: bam
parameters:
stop_sequences:
- "<|endoftext|>"
include_stop_sequence: false
input:
- call: fim
args:
prefix: |
def remove_non_ascii(s: str) -> str:
"""
suffix: |
return result
- "return result"
68 changes: 68 additions & 0 deletions examples/prompt_library/CoT.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
description: CoT pattern introduced by Wei et al. (2022)
defs:
# Chain of Thought
cot_block:
function:
question: str
reasoning: str
answer: str
return: |+
Question: ${ question }
Answer: Let's think step by step. ${ reasoning }
The answer is ${ answer }

# Auto Chain of Thought Zhang et al. (2022)
# The idea is to use a _model_ to generate a reasoning path, even if not very accurate.
# It is best combined with some fewshot examples
auto_chain_of_thought:
function:
question: str
model: str
answer: str
return:
- |-
Question: ${ question }
- "Answer: Let's think step by step. "
- model: ${ model }
parameters:
decoding_method: "greedy"
stop_sequences:
- "The answer is"
include_stop_sequence: false
- "The answer is ${ answer }"

fewshot_cot:
function:
examples:
{ list: { obj: { question: str, reasoning: str, answer: str } } }
return:
for:
example: ${ examples }
repeat:
text:
call: cot_block
args:
question: ${ example.question }
reasoning: ${ example.reasoning }
answer: ${ example.answer }
as: text

chain_of_thought:
function:
question: str
model: str
examples:
{ list: { obj: { question: str, reasoning: str, answer: str } } }
return:
- call: fewshot_cot
args:
examples: ${ examples }
- |
Question: ${ question }
- "Answer: Let's think step by step. "
- model: ${ model }
parameters:
decoding_method: greedy
stop_sequences:
- "<|endoftext|>"
include_stop_sequence: false
227 changes: 227 additions & 0 deletions examples/prompt_library/PoT.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
description: Program of Thoughts pattern Chen (2022), TMLR
defs:
program_of_thought:
function:
question: str
model: str
return:
- |
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?
# Python code, return ans
total_eggs = 16
eaten_eggs = 3
baked_eggs = 4
sold_eggs = total_eggs - eaten_eggs - baked_eggs
dollars_per_egg = 2
result = sold_eggs * dollars_per_egg

Question: A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
# Python code, return ans
bolts_of_blue_fiber = 2
bolts_of_white_fiber = num_of_blue_fiber / 2
result = bolts_of_blue_fiber + bolts_of_white_fiber

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?
# Python code, return ans
cost_of_original_house = 80000
increase_rate = 150 / 100
value_of_house = (1 + increase_rate) * cost_of_original_house
cost_of_repair = 50000
result = value_of_house - cost_of_repair - cost_of_original_house

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?
# Python code, return ans
numb_of_chickens = 20
cups_for_each_chicken = 3
cups_for_all_chicken = num_of_chickens * cups_for_each_chicken
cups_in_the_morning = 15
cups_in_the_afternoon = 25
result = cups_for_all_chicken - cups_in_the_morning - cups_in_the_afternoon

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?
# Python code, return ans
num_glasses = 16
first_glass_cost = 5
second_glass_cost = 5 * 0.6
result = 0
for i in range(num_glasses):
if i % 2 == 0:
result += first_glass_cost
else:
result += second_glass_cost

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?
# Python code, return ans
average_mile_per_hour = 4
total_trail_miles = 12
remaining_miles = total_trail_miles - 4 - 2
total_hours = total_trail_miles / average_mile_per_hour
remaining_hours = total_hours - 2
result = remaining_miles / remaining_hours

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
e before he starts earning money on the lemon tree?
# Python code, return ans
total_cost = 90
cost_of_watering_and_feeding = 3
cost_of_each_lemon = 1.5
num_of_lemon_per_year = 7
result = 0
while total_cost > 0:
total_cost += cost_of_watering_and_feeding
total_cost -= num_of_lemon_per_year * cost_of_each_lemon
result += 1

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?
# Python code, return ans
lose_rate = 0.5
num_tomato_contained_in_per_ounce_sauce = 3 / 16
ounce_sauce_in_last_batch = 32
num_tomato_in_last_batch = ounce_sauce_in_last_batch * num_tomato_contained_in_per_ounce_sauce
result = num_tomato_in_last_batch / (1 - lose_rate)

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?
# Python code, return ans
minutes_to_make_batter = 20
minutes_to_bake_cake = 30
minutes_to_cool_cake = 2 * 60
minutes_to_frost_cake = 10
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
total_hours = total_minutes / 60
result = 5 - total_hours

Question: ${ question }
# Python code, return ans

- def: PROGRAM
model: ${ model }
parameters:
stop_sequences: ["\nAnswer: "]
include_stop_sequence: false
- def: ANSWER
lan: python
code: ${ PROGRAM }
- get: ANSWER

program_of_thought_backtick:
function:
question: str
model: str
return:
- |
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?
# Python code, return ans
```python
total_eggs = 16
eaten_eggs = 3
baked_eggs = 4
sold_eggs = total_eggs - eaten_eggs - baked_eggs
dollars_per_egg = 2
result = sold_eggs * dollars_per_egg
```

Question: A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
# Python code, return ans
```python
bolts_of_blue_fiber = 2
bolts_of_white_fiber = num_of_blue_fiber / 2
result = bolts_of_blue_fiber + bolts_of_white_fiber
```

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?
# Python code, return ans
```python
cost_of_original_house = 80000
increase_rate = 150 / 100
value_of_house = (1 + increase_rate) * cost_of_original_house
cost_of_repair = 50000
result = value_of_house - cost_of_repair - cost_of_original_house
```

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?
# Python code, return ans
```python
numb_of_chickens = 20
cups_for_each_chicken = 3
cups_for_all_chicken = num_of_chickens * cups_for_each_chicken
cups_in_the_morning = 15
cups_in_the_afternoon = 25
result = cups_for_all_chicken - cups_in_the_morning - cups_in_the_afternoon
```

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?
# Python code, return ans
```python
num_glasses = 16
first_glass_cost = 5
second_glass_cost = 5 * 0.6
result = 0
for i in range(num_glasses):
if i % 2 == 0:
result += first_glass_cost
else:
result += second_glass_cost
```

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?
# Python code, return ans
```python
average_mile_per_hour = 4
total_trail_miles = 12
remaining_miles = total_trail_miles - 4 - 2
total_hours = total_trail_miles / average_mile_per_hour
remaining_hours = total_hours - 2
result = remaining_miles / remaining_hours
```

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
e before he starts earning money on the lemon tree?
# Python code, return ans
```python
total_cost = 90
cost_of_watering_and_feeding = 3
cost_of_each_lemon = 1.5
num_of_lemon_per_year = 7
result = 0
while total_cost > 0:
total_cost += cost_of_watering_and_feeding
total_cost -= num_of_lemon_per_year * cost_of_each_lemon
result += 1
```

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?
# Python code, return ans
```python
lose_rate = 0.5
num_tomato_contained_in_per_ounce_sauce = 3 / 16
ounce_sauce_in_last_batch = 32
num_tomato_in_last_batch = ounce_sauce_in_last_batch * num_tomato_contained_in_per_ounce_sauce
result = num_tomato_in_last_batch / (1 - lose_rate)
```

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?
# Python code, return ans
```python
minutes_to_make_batter = 20
minutes_to_bake_cake = 30
minutes_to_cool_cake = 2 * 60
minutes_to_frost_cake = 10
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
total_hours = total_minutes / 60
result = 5 - total_hours
```

Question: ${ question }
# Python code, return ans
- def: PROGRAM
model: ${ model }
parser:
regex: '```.*\n((?:.|\n|$)*?)$\n\s*```' # extracts code from backtick blocks
mode: findall
parameters:
stop_sequences: ["\nAnswer: "]
include_stop_sequence: false
- def: ANSWER
lan: python
code: ${ PROGRAM|join('\n') }
- get: ANSWER
26 changes: 26 additions & 0 deletions examples/prompt_library/RAG.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
description: Retrieval-Augmented Generation (RAG) following Lewis et al.
defs:
# Corpus: Store the retrieval object in the PDL session
corpus:
function:
corpus: {list: str}
return:
- lan: python
code: |
from rank_bm25 import BM25Okapi
PDL_SESSION.corpus = corpus
PDL_SESSION.tokenized_corpus = [doc.split(" ") for doc in corpus]
PDL_SESSION.bm25_corpus = BM25Okapi(PDL_SESSION.tokenized_corpus)
result = None
# Retrieve from corpus in PDL session
retrieve:
function:
query: str
num_examples: int
spec: {list: str}
return:
- lan: python
code: |
from rank_bm25 import BM25Okapi
tokenized_query = query.split(" ")
result = PDL_SESSION.bm25_corpus.get_top_n(tokenized_query, PDL_SESSION.corpus, n=num_examples)
Loading