Skip to content

Commit 9c20383

Browse files
authored
refactor: react pattern in prompt library (#1162)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 1948f32 commit 9c20383

File tree

1 file changed

+70
-61
lines changed

1 file changed

+70
-61
lines changed

contrib/prompt_library/ReAct.pdl

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,26 @@ defs:
99
- for:
1010
trajectory: ${ trajectory }
1111
repeat:
12-
text:
13-
- defs:
14-
type:
15-
text: ${ trajectory.keys()|first }
16-
- match: ${ type }
17-
with:
18-
- case: question
19-
then: |
20-
Question: ${ trajectory[type]|trim }
21-
- case: task
22-
then: |
23-
Task: ${ trajectory[type]|trim }
24-
- case: thought
25-
then: |
26-
Tho: ${ trajectory[type]|trim }
27-
- case: action
28-
then: |
29-
Act: ${ trajectory[type]|trim }
30-
- case: observation
31-
then: |
32-
Obs: ${ trajectory[type]|trim }
33-
- if: ${ type not in ['question', 'task', 'thought', 'action', 'observation'] }
34-
then: "${ type }: ${ trajectory[type]|trim }"
12+
defs:
13+
type: ${ trajectory.keys()|first }
14+
match: ${ type }
15+
with:
16+
- case: question
17+
then: |
18+
Question: ${ trajectory[type]|trim }
19+
- case: task
20+
then: |
21+
Task: ${ trajectory[type]|trim }
22+
- case: thought
23+
then: |
24+
Tho: ${ trajectory[type]|trim }
25+
- case: action
26+
then: |
27+
Act: ${ trajectory[type]|trim }
28+
- case: observation
29+
then: |
30+
Obs: ${ trajectory[type]|trim }
31+
- then: "${ type }: ${ trajectory[type]|trim }"
3532
- "\n"
3633

3734
react:
@@ -56,7 +53,6 @@ defs:
5653
Respond in the format {"name": function name, "arguments": dictionary of argument name and its value}. Do not use variables.
5754

5855
${ tool_schema }
59-
contribute: [context]
6056
- "\n"
6157
- for:
6258
traj: ${ trajectories }
@@ -66,26 +62,24 @@ defs:
6662
args:
6763
trajectory: ${ traj }
6864
- ${ task }
69-
- def: prev_action
70-
contribute: []
71-
data: none
72-
- def: exit
73-
contribute: []
74-
data: False
75-
- def: tool_names
76-
contribute: []
77-
text: ${ tool_schema|map(attribute='name')|list }
78-
- repeat:
65+
- defs:
66+
prev_action:
67+
data: none
68+
exit:
69+
data: false
70+
tool_names:
71+
text: ${ tool_schema|map(attribute='name')|list }
72+
repeat:
7973
text:
8074
- "\nTho: "
81-
- def: thought
82-
model: "${ model }"
83-
contribute: []
84-
parameters:
85-
stop:
86-
- "Act:"
87-
max_tokens: 256
88-
include_stop_sequence: false
75+
- defs:
76+
thought:
77+
model: "${ model }"
78+
parameters:
79+
stop:
80+
- "Act:"
81+
max_tokens: 256
82+
include_stop_sequence: false
8983
- "${ thought|trim }"
9084
- "\nAct: "
9185
- def: action
@@ -96,26 +90,41 @@ defs:
9690
stop: ["\n", "Obs:", "<|eom_id|>"]
9791
include_stop_sequence: false
9892
spec: { name: string, arguments: object }
99-
- if: ${ action != prev_action }
100-
then:
101-
def: observation
102-
if: ${ action.name.lower() != "finish" }
93+
fallback:
94+
data:
95+
name: error
96+
arguments: failed to generate a valid action
97+
- match: ${ action.name.lower() }
98+
with:
99+
- case: finish
100+
then:
101+
def: exit
102+
data: true
103+
contribute: []
104+
- if: ${ action == prev_action }
103105
then:
106+
def: exit
107+
data: true
108+
contribute: []
109+
- if: ${ action.name.lower() in tools }
110+
then:
111+
def: observation
104112
text:
105113
- "\nObs: "
106-
- if: ${ action.name.lower() in tools }
107-
then:
108-
call: ${ tools[action.name.lower()] }
109-
args:
110-
arguments: ${ action.arguments }
111-
else: "Invalid action. Valid actions are ${ tool_names[:-1]|join(', ') }, and ${ tool_names[-1] }."
112-
else:
113-
def: exit
114-
contribute: []
115-
data: True
116-
- def: prev_action
117-
contribute: []
118-
data: ${ action }
119-
until: ${ action.name.lower() == "finish" or exit }
114+
- call: ${ tools[action.name.lower()] }
115+
args:
116+
arguments: ${ action.arguments }
117+
- then:
118+
def: observation
119+
text:
120+
- "\nObs: "
121+
- "Invalid action. Valid actions are ${ tool_names[:-1]|join(', ') }, and ${ tool_names[-1] }."
122+
123+
- defs:
124+
prev_action: ${ action }
125+
until: ${ exit }
120126
- data:
121-
answer: ${ (action.arguments.answer|default("No answer found."))|trim }
127+
answer: ${ action.arguments.answer | trim }
128+
fallback:
129+
data:
130+
answer: "No answer found."

0 commit comments

Comments
 (0)