{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## ReAct Prompting" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "# update or install the necessary libraries\n", "!pip install --upgrade openai\n", "!pip install --upgrade langchain\n", "!pip install --upgrade python-dotenv\n", "!pip install google-search-results\n", "\n", "# import libraries\n", "import openai\n", "import os\n", "from langchain.llms import OpenAI\n", "from langchain.agents import load_tools\n", "from langchain.agents import initialize_agent\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "# load API keys; you will need to obtain these if you haven't yet\n", "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\n", "os.environ[\"SERPER_API_KEY\"] = os.getenv(\"SERPER_API_KEY\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(model_name=\"text-davinci-003\" ,temperature=0)\n", "tools = load_tools([\"google-serper\", \"llm-math\"], llm=llm)\n", "agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n", "Action: Search\n", "Action Input: \"Olivia Wilde boyfriend\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mOlivia Wilde started dating Harry Styles after ending her years-long engagement to Jason Sudeikis — see their relationship timeline.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to find out Harry Styles' age.\n", "Action: Search\n", "Action Input: \"Harry Styles age\"\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3m29 years\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 29 raised to the 0.23 power.\n", "Action: Calculator\n", "Action Input: 29^0.23\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.169459462491557\n", "\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n", "Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169459462491557.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "\"Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169459462491557.\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# run the agent\n", "agent.run(\"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "langchain", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }