{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PAL: Code as Reasoning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "from datetime import datetime\n",
    "from dateutil.relativedelta import relativedelta\n",
    "import os\n",
    "from langchain.llms import OpenAI\n",
    "from dotenv import load_dotenv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv()\n",
    "\n",
    "# API configuration\n",
    "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "# for LangChain\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model_name='text-davinci-003', temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I adopted prompt template from here: https://github.com/reasoning-machines/pal/blob/main/pal/prompt/date_understanding_prompt.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATE_UNDERSTANDING_PROMPT = \"\"\"\n",
    "# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?\n",
    "# If 2015 is coming in 36 hours, then today is 36 hours before.\n",
    "today = datetime(2015, 1, 1) - relativedelta(hours=36)\n",
    "# One week from today,\n",
    "one_week_from_today = today + relativedelta(weeks=1)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = one_week_from_today.strftime('%m/%d/%Y')\n",
    "# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?\n",
    "# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.\n",
    "today = datetime(2019, 1, 1) + relativedelta(days=6)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = today.strftime('%m/%d/%Y')\n",
    "# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?\n",
    "# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.\n",
    "today = datetime(1943, 6, 1) + relativedelta(days=1)\n",
    "# 10 days ago,\n",
    "ten_days_ago = today - relativedelta(days=10)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = ten_days_ago.strftime('%m/%d/%Y')\n",
    "# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?\n",
    "# It is 4/19/1969 today.\n",
    "today = datetime(1969, 4, 19)\n",
    "# 24 hours later,\n",
    "later = today + relativedelta(hours=24)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = today.strftime('%m/%d/%Y')\n",
    "# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?\n",
    "# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.\n",
    "today = datetime(2002, 3, 12)\n",
    "# 24 hours later,\n",
    "later = today + relativedelta(hours=24)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = later.strftime('%m/%d/%Y')\n",
    "# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?\n",
    "# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.\n",
    "today = datetime(2001, 2, 28) + relativedelta(years=16)\n",
    "# Yesterday,\n",
    "yesterday = today - relativedelta(days=1)\n",
    "# The answer formatted with %m/%d/%Y is\n",
    "answer = yesterday.strftime('%m/%d/%Y')\n",
    "# Q: {question}\n",
    "\"\"\".strip() + '\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# If today is 27 February 2023 and I was born exactly 25 years ago, then I was born 25 years before.\n",
      "today = datetime(2023, 2, 27)\n",
      "# I was born 25 years before,\n",
      "born = today - relativedelta(years=25)\n",
      "# The answer formatted with %m/%d/%Y is\n",
      "answer = born.strftime('%m/%d/%Y')\n"
     ]
    }
   ],
   "source": [
    "llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))\n",
    "print(llm_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "02/27/1998\n"
     ]
    }
   ],
   "source": [
    "exec(llm_out)\n",
    "print(answer)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "promptlecture",
   "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,
  "vscode": {
   "interpreter": {
    "hash": "f38e0373277d6f71ee44ee8fea5f1d408ad6999fda15d538a69a99a1665a839d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
