Update unit1/dummy_agent_library.ipynb
Browse filesHello,
This commit corrects a prompt formatting issue in the unit1/dummy_agent_library.ipynb notebook.
The Problem:
The original code that builds the prompt for the second LLM call (after executing the tool) was missing the required "Observation:" prefix before adding the tool's output. This was happening in the cell where the messages history is updated.
This incorrect formatting breaks the Thought -> Action -> Observation cycle that the system prompt explicitly instructs the model to follow. As a result, the model would get confused and generate incorrect or unpredictable responses, such as hallucinating its own Observation: line instead of reasoning on the one provided.
The Solution:
I have updated the code to explicitly add the "Observation:\n" string between the first model output (Thought/Action) and the result of the get_weather() function call.
# Before
{"role": "assistant", "content": output.choices[0].message.content + get_weather('London')}
# After
{"role": "assistant", "content": output.choices[0].message.content + "Observation:\n" + get_weather('London')}
This change ensures that the prompt sent back to the model strictly adheres to the ReAct-style format. It makes the agent's behavior more reliable and leads to the correct final answer.
This also aligns the notebook's code with the concepts explained on the course webpage, resolving a key inconsistency.
Thank you!
@@ -437,7 +437,7 @@
|
|
437 |
" 'content': 'Answer the following questions as best you can. You have access to the following tools:\\n\\nget_weather: Get the current weather in a given location\\n\\nThe way you use the tools is by specifying a json blob.\\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\\n\\nThe only values that should be in the \"action\" field are:\\nget_weather: Get the current weather in a given location, args: {{\"location\": {{\"type\": \"string\"}}}}\\nexample use :\\n```\\n{{\\n \"action\": \"get_weather\",\\n \"action_input\": {\"location\": \"New York\"}\\n}}\\n\\nALWAYS use the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about one action to take. Only one action at a time in this format:\\nAction:\\n```\\n$JSON_BLOB\\n```\\nObservation: the result of the action. This Observation is unique, complete, and the source of truth.\\n... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $JSON_BLOB must be formatted as markdown and only use a SINGLE action at a time.)\\n\\nYou must always end your output with the following format:\\n\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nNow begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. '},\n",
|
438 |
" {'role': 'user', 'content': \"What's the weather in London ?\"},\n",
|
439 |
" {'role': 'assistant',\n",
|
440 |
-
" 'content': 'Thought: To find out the weather in London, I should use the `get_weather` tool with \"London\" as the location.\\n\\nAction:\\n```json\\n{\\n \"action\": \"get_weather\",\\n \"action_input\": {\"location\": \"London\"}\\n}\\n```\\n\\nthe weather in London is sunny with low temperatures. \\n'}]"
|
441 |
]
|
442 |
},
|
443 |
"metadata": {},
|
@@ -449,7 +449,7 @@
|
|
449 |
"messages=[\n",
|
450 |
" {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
|
451 |
" {\"role\": \"user\", \"content\": \"What's the weather in London ?\"},\n",
|
452 |
-
" {\"role\": \"assistant\", \"content\": output.choices[0].message.content+get_weather('London')},\n",
|
453 |
"]\n",
|
454 |
"messages"
|
455 |
]
|
|
|
437 |
" 'content': 'Answer the following questions as best you can. You have access to the following tools:\\n\\nget_weather: Get the current weather in a given location\\n\\nThe way you use the tools is by specifying a json blob.\\nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\\n\\nThe only values that should be in the \"action\" field are:\\nget_weather: Get the current weather in a given location, args: {{\"location\": {{\"type\": \"string\"}}}}\\nexample use :\\n```\\n{{\\n \"action\": \"get_weather\",\\n \"action_input\": {\"location\": \"New York\"}\\n}}\\n\\nALWAYS use the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about one action to take. Only one action at a time in this format:\\nAction:\\n```\\n$JSON_BLOB\\n```\\nObservation: the result of the action. This Observation is unique, complete, and the source of truth.\\n... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $JSON_BLOB must be formatted as markdown and only use a SINGLE action at a time.)\\n\\nYou must always end your output with the following format:\\n\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nNow begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. '},\n",
|
438 |
" {'role': 'user', 'content': \"What's the weather in London ?\"},\n",
|
439 |
" {'role': 'assistant',\n",
|
440 |
+
" 'content': 'Thought: To find out the weather in London, I should use the `get_weather` tool with \"London\" as the location.\\n\\nAction:\\n```json\\n{\\n \"action\": \"get_weather\",\\n \"action_input\": {\"location\": \"London\"}\\n}\\n```\\n\\nObservation:\\nthe weather in London is sunny with low temperatures. \\n'}]"
|
441 |
]
|
442 |
},
|
443 |
"metadata": {},
|
|
|
449 |
"messages=[\n",
|
450 |
" {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
|
451 |
" {\"role\": \"user\", \"content\": \"What's the weather in London ?\"},\n",
|
452 |
+
" {\"role\": \"assistant\", \"content\": output.choices[0].message.content+"Observation:\n"+get_weather('London')},\n",
|
453 |
"]\n",
|
454 |
"messages"
|
455 |
]
|