|
| 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