The ReAct action being in Lesson 6 Python Agent is “Action: Use the sorted() function” when it should be “Action: Python REPL”. There is an issue in the default prefix ReAct instruction prompt but that is not displayed in the verbose output.
I was able to get it to work by modifying the default LangChain prefix from:
prefix=‘You are an agent designed to write and execute python code to answer questions.\nYou have access to a python REPL, which you can use to execute python code.\nIf you get an error, debug your code and try again.\nOnly use the output of your code to answer the question. \nYou might know the answer without running any code, but you should still run the code to get the answer.\nIf it does not seem like you can write code to answer the question, just return “I don't know” as the answer.\n’
to:
prefix=‘You are an agent designed to write and execute python code to answer questions.\nYou have access to a python REPL, which you can use to execute python code by taking issuing the “PythonREPL” aciton.\nIf you get an error, debug your code and try again.\nOnly use the output of your code to answer the question. \nYou might know the answer without running any code, but you should still run the code to get the answer.\nIf it does not seem like you can write code to answer the question, just return “I don't know” as the answer.\n’
Hi! Thanks for flagging this. Indeed, by default, the agent doesn’t know it’s supposed to call "PythonREPL" unless that’s clearly stated in the prompt.
Great fix by editing the prefix to mention the "PythonREPL" action explicitly. We’ll consider that as an update for our short course! Thank you!
from langchain.prompts import PromptTemplate
custom_prompt=PromptTemplate(
input_variables=["input"],
template="You are an agent designed to write and execute python REPL tool to answer questions.\nYou have access to a python REPL, which you can use to answer.\nIf you get an error, debug your code and try again.\nOnly use the output of your code to answer the question. \nYou might know the answer without running any code, but you should still run the code to get the answer.\nIf it does not seem like you can write code to answer the question, just return \"I don't know\" as the answer.\n\n\nPython REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Python REPL]\nAction Input: the {input} to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\n"
)
here mention to execute python REPL tool instead of execute python code
and also mention the {input} in the Input:
and this is the output