Skip to content

Commit d62cad5

Browse files
authored
Add contrib. prompt library (#927)
* Initial prompt library Signed-off-by: Claudio Spiess <[email protected]> * Documentation and fixes Signed-off-by: Claudio Spiess <[email protected]> * Finish prompt lib docs Signed-off-by: Claudio Spiess <[email protected]> * Address feedback Signed-off-by: Claudio Spiess <[email protected]> --------- Signed-off-by: Claudio Spiess <[email protected]>
1 parent 5ddd784 commit d62cad5

File tree

8 files changed

+892
-61
lines changed

8 files changed

+892
-61
lines changed

contrib/prompt_library/CoT.pdl

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
fewshot_cot:
15+
function:
16+
examples:
17+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
18+
return:
19+
text:
20+
- for:
21+
example: ${ examples }
22+
repeat:
23+
call: ${ cot_block }
24+
args:
25+
question: ${ example.question }
26+
reasoning: ${ example.reasoning }
27+
answer: ${ example.answer }
28+
join:
29+
with: "\n\n"
30+
31+
chain_of_thought:
32+
function:
33+
question: str
34+
model: str
35+
examples:
36+
{ list: { obj: { question: str, reasoning: str, answer: str } } }
37+
return:
38+
lastOf:
39+
- call: ${ fewshot_cot }
40+
args:
41+
examples: ${ examples }
42+
- "Question: ${ question }\n"
43+
- "Answer: Let's think step by step. "
44+
- model: ${ model }
45+
def: answer
46+
parameters:
47+
max_tokens: 1024
48+
temperature: 0
49+
stop:
50+
- "<|endoftext|>"
51+
- "Question:"
52+
include_stop_sequence: false
53+
- data:
54+
answer: ${ answer|trim }

contrib/prompt_library/ReAct.pdl

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
description: ReAct pattern from Yao et al., [ICLR 2023](https://openreview.net/forum?id=WE_vluYUL-X)
2+
# See alternative implementation here: https://smith.langchain.com/hub/hwchase17/react-chat
3+
defs:
4+
react_block:
5+
function:
6+
trajectory: { list: obj }
7+
return:
8+
text:
9+
- for:
10+
trajectory: ${ trajectory }
11+
repeat:
12+
text:
13+
- def: type
14+
text: ${ trajectory.keys()|first }
15+
contribute: []
16+
- if: ${ type == 'question'}
17+
then: |
18+
Question: ${ trajectory[type]|trim }
19+
- if: ${ type == 'task'}
20+
then: |
21+
Task: ${ trajectory[type]|trim }
22+
- if: ${ type == 'thought'}
23+
then: |
24+
Tho: ${ trajectory[type]|trim }
25+
- if: ${ type == 'action'}
26+
then: |
27+
Act: ${ trajectory[type]|trim }
28+
- if: ${ type == 'observation'}
29+
then: |
30+
Obs: ${ trajectory[type]|trim }
31+
- if: ${ type not in ['question', 'task', 'thought', 'action', 'observation'] }
32+
then: "${ type }: ${ trajectory[type]|trim }"
33+
- "\n"
34+
35+
react:
36+
function:
37+
task: str
38+
model: str
39+
tool_schema: { list: obj }
40+
tools: obj
41+
trajectories: { list: list }
42+
return:
43+
lastOf:
44+
- role: system
45+
text:
46+
- "Today's Date: "
47+
- lang: python
48+
code: |
49+
from datetime import datetime
50+
result = datetime.today().strftime('%B %d, %Y.\n')
51+
- |
52+
You are a helpful assistant with access to the following function calls. Your task is to produce a sequence of function calls necessary to generate response to the user utterance. Use the following function calls as required.
53+
54+
Respond in the format {"name": function name, "arguments": dictionary of argument name and its value}. Do not use variables.
55+
56+
${ tool_schema }
57+
contribute: [context]
58+
- "\n"
59+
- for:
60+
traj: ${ trajectories }
61+
repeat:
62+
text:
63+
call: ${ react_block }
64+
args:
65+
trajectory: ${ traj }
66+
- ${ task }
67+
- def: prev_action
68+
contribute: []
69+
data: none
70+
- def: exit
71+
contribute: []
72+
data: False
73+
- def: tool_names
74+
contribute: []
75+
text: ${ tool_schema|map(attribute='name')|list }
76+
- repeat:
77+
text:
78+
- "\nTho: "
79+
- def: thought
80+
model: "${ model }"
81+
contribute: []
82+
parameters:
83+
stop:
84+
- "Act:"
85+
max_tokens: 256
86+
include_stop_sequence: false
87+
- "${ thought|trim }"
88+
- "\nAct: "
89+
- def: action
90+
model: "${ model }"
91+
parser: json
92+
parameters:
93+
temperature: 0
94+
stop: ["\n", "Obs:", "<|eom_id|>"]
95+
include_stop_sequence: false
96+
spec: { name: str, arguments: obj }
97+
- if: ${ action != prev_action }
98+
then:
99+
def: observation
100+
if: ${ action.name.lower() != "finish" }
101+
then:
102+
text:
103+
- "\nObs: "
104+
- if: ${ action.name in tools }
105+
then:
106+
call: ${ tools[action.name] }
107+
args:
108+
arguments: ${ action.arguments }
109+
else: "Invalid action. Valid actions are ${ tool_names[:-1]|join(', ') }, and ${ tool_names[-1] }."
110+
# - "\n"
111+
else:
112+
def: exit
113+
contribute: []
114+
data: True
115+
- def: prev_action
116+
contribute: []
117+
data: ${ action }
118+
until: ${ action.name.lower() == "finish" or exit }
119+
- data:
120+
answer: ${ (action.arguments.answer|default("No answer found."))|trim }

contrib/prompt_library/ReWoo.pdl

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
description: ReWOO (Reasoning without observation) pattern from Xu et al., (http://arxiv.org/abs/2305.18323)
2+
# Compared to ReAct, reduced token consumption (and thus execution time),
3+
# by generating full chain of tools in a single pass
4+
# see: https://github.com/langchain-ai/langgraph/blob/main/examples/rewoo/rewoo.ipynb
5+
defs:
6+
rewoo_block:
7+
function:
8+
trajectory: { list: obj }
9+
return:
10+
text:
11+
- defs:
12+
i:
13+
data: 1
14+
- for:
15+
trajectory: ${ trajectory }
16+
repeat:
17+
text:
18+
- defs:
19+
type:
20+
text: ${ trajectory.keys()|first }
21+
content:
22+
text: ${ trajectory.values()|first }
23+
- if: ${ type in ['task', 'question'] }
24+
then: |-
25+
Task: ${ content|trim }
26+
- if: ${ type == 'thought'}
27+
then: |-
28+
29+
Plan: ${ content|trim }
30+
- if: ${ type == 'action'}
31+
then:
32+
text:
33+
- " #E${ i } = ${ content|trim }"
34+
- defs:
35+
i:
36+
data: ${ i+1 }
37+
- if: ${ type == 'observation'}
38+
then: ""
39+
- if: ${ type not in ['question', 'task', 'thought', 'action', 'observation'] }
40+
then: "${ type }: ${ content|trim }\n"
41+
- "\n"
42+
43+
rewoo:
44+
function:
45+
task: str
46+
model: str
47+
tool_schema: { list: obj }
48+
tools: obj
49+
trajectories: { list: list }
50+
show_plans: bool
51+
return:
52+
lastOf:
53+
- |
54+
For the following task, make plans that can solve the problem step by step. For each plan, indicate which external tool together with tool input to retrieve evidence. You can store the evidence into a variable #E that can be called by later tools. (Plan, #E1, Plan, #E2, Plan, ...)
55+
56+
Tools can be one of the following:
57+
${ tool_schema }
58+
- "\n"
59+
- for:
60+
traj: ${ trajectories }
61+
repeat:
62+
text:
63+
- call: ${ rewoo_block }
64+
args:
65+
trajectory: ${ traj }
66+
- "\n"
67+
- |
68+
Begin!
69+
Describe your plans with rich details. Each Plan should be followed by only one #E.
70+
71+
${ task }
72+
- def: PLANS
73+
model: ${ model }
74+
contribute: []
75+
parser: # plan, step_name, tool, tool_input
76+
regex: 'Plan:\s*(?P<plan>(?:.|\n)*?)\s*(?P<step_name>#E\d+)\s*=\s*(?P<act>\{.+\})'
77+
mode: findall
78+
parameters:
79+
temperature: 0
80+
stop:
81+
- "<|endoftext|>"
82+
- "\n\n"
83+
- "Task:"
84+
include_stop_sequence: false
85+
max_tokens: 1024
86+
- if: ${ show_plans }
87+
contribute: [result, context]
88+
then:
89+
text:
90+
- "\n\n--- Raw plans ---\n"
91+
- ${ PLANS }
92+
- "\n\n--- Extracted Blueprint ---\n"
93+
- for:
94+
plan: ${ PLANS }
95+
repeat:
96+
text:
97+
- "Plan: ${ plan[0] }\n"
98+
- "${ plan[1] } = ${ plan[2] }\n"
99+
- "----------------------------\n\n"
100+
- defs:
101+
SOLUTION:
102+
text: "No plans found."
103+
output:
104+
data: {}
105+
plans:
106+
for:
107+
plan: ${ PLANS }
108+
repeat:
109+
lastOf:
110+
- defs:
111+
PLAN: ${ plan[0] }
112+
ID: ${ plan[1] }
113+
ACTION_RAW: ${ plan[2] }
114+
ACTION:
115+
parser: json
116+
lang: python
117+
code: |-
118+
for k,v in output.items():
119+
if k in ACTION_RAW:
120+
ACTION_RAW = ACTION_RAW.replace(k, v)
121+
result = ACTION_RAW
122+
tool_output:
123+
if: ${ ACTION.name in tools }
124+
then:
125+
call: ${ tools[ACTION.name] }
126+
args:
127+
arguments: ${ ACTION.arguments }
128+
else: "Invalid action. Valid actions are ${ tools.keys() }"
129+
- def: output
130+
lang: python
131+
contribute: []
132+
code: |
133+
output[ID] = str(tool_output)
134+
result = output
135+
- |
136+
Plan: ${ PLAN }
137+
Evidence: ${ tool_output }
138+
- if: ${ plans is not none and plans|length > 0 }
139+
then:
140+
text:
141+
- "\n\n"
142+
- def: solution_input
143+
text: |-
144+
Solve the following task or problem. To solve the problem, we have made step-by-step Plan and retrieved corresponding Evidence to each Plan. Use them with caution since long evidence might contain irrelevant information.
145+
146+
${ plans|join }
147+
Now solve the question or task according to provided Evidence above. Respond with the answer directly with no extra words.
148+
149+
${ task }
150+
Response:
151+
- def: SOLUTION
152+
model: ${ model }
153+
parameters:
154+
temperature: 0
155+
stop:
156+
- "<|endoftext|>"
157+
include_stop_sequence: false
158+
max_tokens: 1024
159+
input:
160+
text: ${ solution_input }
161+
- data:
162+
answer: ${ SOLUTION|trim }

0 commit comments

Comments
 (0)