A simple Python implementation of the ReAct pattern for LLMs Simon Willison published a from-scratch Python implementation of the ReAct (Reason+Act) pattern on top of his tiny ChatGPT API wrapper, giving the LLM three callable actions: `wikipedia: `, `simon_blog_search: `, and `calculate: `, the last evaluated with Python's `eval()` — which Willison flagged as "so dangerous!" and said should use a safer mechanism. In worked examples, the model answered "England shares borders with Wales and Scotland" via a Wikipedia lookup and resolved "Fifteen * twenty five" by requesting the calculate action. Willison built the implementation himself rather than using langchain, which Matt Webb had used for his version of the loop. A popular nightmare scenario for AI is giving it access to tools, so it can make API calls and execute its own code and generally break free of the constraints of its initial environment. Let's do that now The ReAct pattern for Reason+Act is described in this paper https://react-lm.github.io/ . It's a pattern where you implement additional actions that an LLM can take - searching Wikipedia or running calculations for example - and then teach it how to request that those actions are run, then feed their results back into the LLM. See also my post Could you train a ChatGPT-beating model for $85,000 and run it in a browser? https://simonwillison.net/2023/Mar/17/beat-chatgpt-in-a-browser/ react-pattern . Matt Webb wrote a great piece about that here: The surprising ease and effectiveness of AI in a loop https://interconnected.org/home/2023/03/16/singularity . In it Matt points out that the 1/10 price reduction on ChatGPT compared to GPT-3 makes it an ideal candidate for cheaply implementing this pattern. Matt used langchain https://github.com/hwchase17/langchain for his implementation. I decided to try building it from scratch on top of my tiny Python wrapper https://til.simonwillison.net/gpt3/chatgpt-api for the ChatGPT API. Here's my first attempt at a very rough implementation. It gains access to three new actions: wikipedia: