| 172 | } |
| 173 | |
| 174 | constructPrompt(goal) { |
| 175 | const toolDescriptions = Array.from(this.tools.values()) |
| 176 | .map(t => `${t.name}: ${t.description}`) |
| 177 | .join('\n'); |
| 178 | |
| 179 | let prompt = `Answer the following questions as best you can. You have access to the following tools: |
| 180 | |
| 181 | ${toolDescriptions} |
| 182 | |
| 183 | Use the following format: |
| 184 | |
| 185 | Question: the input question you must answer |
| 186 | Thought: you should always think about what to do |
| 187 | Action: the action to take, should be one of [${Array.from(this.tools.keys()).join(', ')}] |
| 188 | Action Input: the input to the action |
| 189 | Observation: the result of the action |
| 190 | ... (this Thought/Action/Action Input/Observation can repeat N times) |
| 191 | Thought: I now know the final answer |
| 192 | Final Answer: the final answer to the original input question |
| 193 | |
| 194 | Begin! |
| 195 | |
| 196 | Question: ${goal} |
| 197 | `; |
| 198 | |
| 199 | for (const step of this.history) { |
| 200 | if (step.type === 'step') { |
| 201 | prompt += `Thought: ${step.thought}\n`; |
| 202 | prompt += `Action: ${step.action.tool}\n`; |
| 203 | prompt += `Action Input: ${step.action.args}\n`; |
| 204 | prompt += `Observation: ${step.observation}\n`; |
| 205 | } else if (step.type === 'thought') { |
| 206 | prompt += `Thought: ${step.content}\n`; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Prompt for the next step |
| 211 | prompt += `Thought:`; |
| 212 | |
| 213 | return prompt; |
| 214 | } |
| 215 | |
| 216 | parseResponse(response) { |
| 217 | // Handle case where LLM includes "Thought:" prefix in the response |