{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2c7601bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import pkg_resources\n",
    "# import pip\n",
    "# installedPackages = {pkg.key for pkg in pkg_resources.working_set}\n",
    "# required = { 'openai','datasets', 'sklearn', 'tqdm'}\n",
    "# missing = required - installedPackages\n",
    "# if missing:\n",
    "#     !pip install openai\n",
    "#     !pip install datasets\n",
    "#     !pip install scikit-learn\n",
    "#     !pip install tqdm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "235112dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "08322695",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "from datasets import load_dataset\n",
    "from sklearn.metrics import accuracy_score, f1_score,confusion_matrix\n",
    "from tqdm import tqdm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "021b6ae1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#set your OpenAI API key\n",
    "openai.api_key='your api key'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "93d601c6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e1baee2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#load dataset and add prompt\n",
    "def get_dataset(n_limit=0):\n",
    "    dataset=load_dataset('financial_phrasebank','sentences_50agree')\n",
    "\n",
    "    dataset = dataset[\"train\"].train_test_split(test_size=0.2, seed=42)\n",
    "    dataset = dataset[\"test\"]\n",
    "\n",
    "    if n_limit>0:\n",
    "        dataset['train']=dataset['train'][:n_limit]\n",
    "    print(\"size of dataset: \", len( dataset['train']['sentence']))\n",
    "    text_inputs = dataset['train']['sentence']\n",
    "    process_inputs=[\n",
    "        f\"Human: Determine the sentiment of the financial news as negative, neutral or positive: {text_inputs[i]} Assistant: \"\n",
    "        for i in range(len(text_inputs))]\n",
    "    labels=dataset['train']['label']\n",
    "    return process_inputs, labels    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "28ce3f8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#ChatGPT API\n",
    "def chat_with_gpt(prompt):\n",
    "    response=openai.ChatCompletion.create(\n",
    "        model='gpt-3.5-turbo',\n",
    "        messages=[\n",
    "            {\"role\":\"system\", \"content\":\"Hello\"},\n",
    "            {\"role\": \"user\",\"content\":prompt}\n",
    "        ]\n",
    "    )\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a96b496a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Found cached dataset financial_phrasebank (/Users/bruce/.cache/huggingface/datasets/financial_phrasebank/sentences_50agree/1.0.0/550bde12e6c30e2674da973a55f57edde5181d53f5a5a34c1531c53f93b7e141)\n",
      "100%|████████████████████████████████████████████| 1/1 [00:00<00:00, 236.91it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size of dataset:  4846\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "sentences, labels = get_dataset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e3dbf2a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4846"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3739b840",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Human: Determine the sentiment of the financial news as negative, neutral or positive: According to Gran , the company has no plans to move all production to Russia , although that is where the company is growing . Assistant: '"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "senteces[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5842f5c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#positive: 2, negative: 0, neutral:1\n",
    "labels[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "15bf44f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#chatgpt response \n",
    "preds=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2386f981",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "5it [00:12,  2.45s/it]\n"
     ]
    }
   ],
   "source": [
    "#test run\n",
    "import time\n",
    "for prompt, label in tqdm(zip(sentences[0:5],labels)):\n",
    "    #print(prompt, label)\n",
    "    time.sleep(1)\n",
    "    response=chat_with_gpt(prompt)\n",
    "    if   \"negative\" in response:\n",
    "        preds.append(0)\n",
    "    elif \"neutral\" in response:\n",
    "        preds.append(1)\n",
    "    elif \"positive\" in response:\n",
    "        preds.append(2)\n",
    "    else:\n",
    "        preds.append(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a7b32a9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Neutral'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e5ec0ec3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "36830a9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 0, 2, 1]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "96f58756",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.8\n"
     ]
    }
   ],
   "source": [
    "print(f\"Accuracy: {accuracy_score(labels[0:5],preds[0:5])}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d67c2427",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "F1: 0.8222222222222223\n"
     ]
    }
   ],
   "source": [
    "print(f\"F1: {f1_score(labels[0:5],preds[0:5],average='macro')}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae4e90c2",
   "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
