{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5b771260-e38c-43dd-b27a-40da209e5549",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv())\n",
    "openai_api_key = os.environ[\"OPENAI_API_KEY\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a48d340a-e66d-4561-9e3e-9b4cd5bb9f06",
   "metadata": {},
   "source": [
    "## Few Shot Prompt Template"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9ae5070-d02f-4b70-b493-489c52e2be83",
   "metadata": {},
   "source": [
    "Basic prompting strategies:\n",
    "* Zero Shot Prompt: \"Classify the sentiment of this review: ...\"\n",
    "* Few Shot Prompt: \"Classify the sentiment of this review based on these examples: ...\"\n",
    "* Chain Of Thought Prompt: \"Classify the sentiment of this review based on these examples and explanations of the reasoning behind: ...\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "237e7a73-6682-4a4e-b2e0-2d91cccd5cc8",
   "metadata": {},
   "source": [
    "## Zero Shot Prompting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "40f7b3ee-d618-4eef-9d70-570db33b636e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d54139f-ab00-4e70-acac-d4a5d3a4f80b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts.prompt import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "98b0f6a9-5365-489f-95f3-cab4be868fb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_template = \"\"\"\n",
    "Classify the sentiment of this review.\n",
    "Sentiment: Is the review positive, neutral or negative?\n",
    "\n",
    "Review: {review}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "caf11c1a-8b85-4391-906d-bd71f1afbf92",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bde15ab8-8850-4106-8c87-2cc8df7f6c8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_prompt = PromptTemplate(\n",
    "    template=my_template,\n",
    "    input_variables=[\"review\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dce90a04-9baf-4300-ac64-aeee00fe3803",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nClassify the sentiment of this review.\\nSentiment: Is the review positive, neutral or negative?\\n\\nReview: I love this course!\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_prompt.format(review=\"I love this course!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26873a56-3438-4192-9db6-0f3d1f4a2aab",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = my_prompt.format(review=\"I love this course!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "14165d9b-ec12-43b7-b36c-0a82a612f1c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/juliocolomer/.pyenv/versions/3.11.4/envs/venv020124/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Positive\n"
     ]
    }
   ],
   "source": [
    "print(llm(question))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5f55b02-a220-4354-9206-4991817643e0",
   "metadata": {},
   "source": [
    "## Few Shot Prompting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "31b6e23e-dc84-4684-adc5-9a7b74884525",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts.few_shot import FewShotPromptTemplate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c7c1f7a-4b9b-407b-b2ec-b885ae276130",
   "metadata": {},
   "source": [
    "**Option 1: in the template**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fe4adbaf-4fa6-4ae5-a504-fab833c3ec3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_few_shot_template_verbose = \"\"\"\n",
    "Classify the sentiment of this review.\n",
    "Sentiment: Is the review positive, neutral or negative?\n",
    "\n",
    "Review: {review}\n",
    "\n",
    "Examples:\n",
    "review: \"I love this course!\"\n",
    "sentiment: positive\n",
    "\n",
    "review: \"What a waste of time!\"\n",
    "sentiment: negative\n",
    "\n",
    "review: \"So so. Not so good.\"\n",
    "sentiment: neutral\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88e02428-8d68-42ba-8374-481f52b964f8",
   "metadata": {},
   "source": [
    "**Option2: out ot the template**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b6ad14d7-6d98-40de-92bc-ee77fc9598b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_examples = [\n",
    "    {\n",
    "        \"review\": \"I love this course!\",\n",
    "        \"response\": \"sentiment: positive\" \n",
    "    },\n",
    "    {\n",
    "        \"review\": \"What a waste of time!\",\n",
    "        \"response\": \"sentiment: negative\" \n",
    "    },\n",
    "    {\n",
    "        \"review\": \"So so. Not so good.\",\n",
    "        \"response\": \"sentiment: neutral\" \n",
    "    },\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ec48685c-ae4e-494c-9dae-6afccfec575a",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_few_shot_template_ok = \"\"\"\n",
    "Classify the sentiment of this review.\n",
    "Sentiment: Is the review positive, neutral or negative?\n",
    "\n",
    "Review: {review}\n",
    "{response}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c88c11ab-d252-4aec-97e8-87f8d6b778ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "example_prompt = PromptTemplate(\n",
    "     input_variables=[\"review\", \"response\"],\n",
    "     template=my_few_shot_template_ok\n",
    " )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1cd8eb4d-8ece-45b6-a3f7-42de9d7a15a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_few_shot_prompt = FewShotPromptTemplate(\n",
    "    examples=my_examples,\n",
    "    example_prompt=example_prompt,\n",
    "    suffix=\"Review: {review}\",\n",
    "    input_variables=[\"review\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dc216351-2951-4c9d-81c3-e47618ef363d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "sentiment: negative\n"
     ]
    }
   ],
   "source": [
    "print(llm(my_few_shot_prompt.format(review=\"What a piece of shit!\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a494f806-0d57-4c52-87d4-09e9f620904c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
