{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "31883cc69d2147dbadcfcbbc83297a8c": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HBoxModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HBoxModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HBoxView",
      "box_style": "",
      "children": [
       "IPY_MODEL_579a5f4533814940a14217c6fc60e170",
       "IPY_MODEL_f0c4ac746e6844cdb4f1b5a0f231836b",
       "IPY_MODEL_1320e19917fe4d2b9234fd628e0de37c"
      ],
      "layout": "IPY_MODEL_570c2be9707b43e1a152d0ac5f0b3e9b"
     }
    },
    "579a5f4533814940a14217c6fc60e170": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HTMLModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_8d4e121a0993436fa051bce8cc09fdcd",
      "placeholder": "​",
      "style": "IPY_MODEL_30d775cc466e468783b4f4dd0b5b7739",
      "value": "100%"
     }
    },
    "f0c4ac746e6844cdb4f1b5a0f231836b": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "FloatProgressModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "FloatProgressModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "ProgressView",
      "bar_style": "success",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_38ce581356f24c3cb64613c6410f21d6",
      "max": 10,
      "min": 0,
      "orientation": "horizontal",
      "style": "IPY_MODEL_da81bca2fe0c4b2094c9f390c1e5f7cb",
      "value": 10
     }
    },
    "1320e19917fe4d2b9234fd628e0de37c": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HTMLModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_8cfe7a82f6e0419086831ad53761c91c",
      "placeholder": "​",
      "style": "IPY_MODEL_1584ef7445394c43b2bb1aa593fe13fd",
      "value": " 10/10 [00:00&lt;00:00, 179.79it/s]"
     }
    },
    "570c2be9707b43e1a152d0ac5f0b3e9b": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "8d4e121a0993436fa051bce8cc09fdcd": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "30d775cc466e468783b4f4dd0b5b7739": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "DescriptionStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    },
    "38ce581356f24c3cb64613c6410f21d6": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "da81bca2fe0c4b2094c9f390c1e5f7cb": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "ProgressStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "ProgressStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "bar_color": null,
      "description_width": ""
     }
    },
    "8cfe7a82f6e0419086831ad53761c91c": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "1584ef7445394c43b2bb1aa593fe13fd": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "DescriptionStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    },
    "958730a9af4b48d0acb036d27ba720d5": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HBoxModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HBoxModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HBoxView",
      "box_style": "",
      "children": [
       "IPY_MODEL_18d7ae4959bd4e4cb749e6e516209b88",
       "IPY_MODEL_6753ffabb1a24f09b9a17222bd846032",
       "IPY_MODEL_0acbcd921ed3425d849115e2c79bdcc3"
      ],
      "layout": "IPY_MODEL_beb57a990c15409eb876c9b4700f1502"
     }
    },
    "18d7ae4959bd4e4cb749e6e516209b88": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HTMLModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_4dbbe9db382a4092a6ce5f6930f9a73c",
      "placeholder": "​",
      "style": "IPY_MODEL_f2f86e7411014436b22b5e08735e1174",
      "value": "100%"
     }
    },
    "6753ffabb1a24f09b9a17222bd846032": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "FloatProgressModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "FloatProgressModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "ProgressView",
      "bar_style": "success",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_7c0b28d71fe342ecaf0759a4d8c3e958",
      "max": 10,
      "min": 0,
      "orientation": "horizontal",
      "style": "IPY_MODEL_78f6c0f65a324c81aec164b7f9313179",
      "value": 10
     }
    },
    "0acbcd921ed3425d849115e2c79bdcc3": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "HTMLModel",
     "model_module_version": "1.5.0",
     "state": {
      "_dom_classes": [],
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "HTMLModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/controls",
      "_view_module_version": "1.5.0",
      "_view_name": "HTMLView",
      "description": "",
      "description_tooltip": null,
      "layout": "IPY_MODEL_fda0f4aa56064ccc89a27ea1e818a0fc",
      "placeholder": "​",
      "style": "IPY_MODEL_29c91f25b1854dfcade19dd10e4e5a8d",
      "value": " 10/10 [00:10&lt;00:00,  1.33it/s]"
     }
    },
    "beb57a990c15409eb876c9b4700f1502": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "4dbbe9db382a4092a6ce5f6930f9a73c": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "f2f86e7411014436b22b5e08735e1174": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "DescriptionStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    },
    "7c0b28d71fe342ecaf0759a4d8c3e958": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "78f6c0f65a324c81aec164b7f9313179": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "ProgressStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "ProgressStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "bar_color": null,
      "description_width": ""
     }
    },
    "fda0f4aa56064ccc89a27ea1e818a0fc": {
     "model_module": "@jupyter-widgets/base",
     "model_name": "LayoutModel",
     "model_module_version": "1.2.0",
     "state": {
      "_model_module": "@jupyter-widgets/base",
      "_model_module_version": "1.2.0",
      "_model_name": "LayoutModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "LayoutView",
      "align_content": null,
      "align_items": null,
      "align_self": null,
      "border": null,
      "bottom": null,
      "display": null,
      "flex": null,
      "flex_flow": null,
      "grid_area": null,
      "grid_auto_columns": null,
      "grid_auto_flow": null,
      "grid_auto_rows": null,
      "grid_column": null,
      "grid_gap": null,
      "grid_row": null,
      "grid_template_areas": null,
      "grid_template_columns": null,
      "grid_template_rows": null,
      "height": null,
      "justify_content": null,
      "justify_items": null,
      "left": null,
      "margin": null,
      "max_height": null,
      "max_width": null,
      "min_height": null,
      "min_width": null,
      "object_fit": null,
      "object_position": null,
      "order": null,
      "overflow": null,
      "overflow_x": null,
      "overflow_y": null,
      "padding": null,
      "right": null,
      "top": null,
      "visibility": null,
      "width": null
     }
    },
    "29c91f25b1854dfcade19dd10e4e5a8d": {
     "model_module": "@jupyter-widgets/controls",
     "model_name": "DescriptionStyleModel",
     "model_module_version": "1.5.0",
     "state": {
      "_model_module": "@jupyter-widgets/controls",
      "_model_module_version": "1.5.0",
      "_model_name": "DescriptionStyleModel",
      "_view_count": null,
      "_view_module": "@jupyter-widgets/base",
      "_view_module_version": "1.2.0",
      "_view_name": "StyleView",
      "description_width": ""
     }
    }
   }
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# CostEstimator Usage Example\n",
    "\n",
    "The `CostEstimator` class offers a convenient way to estimate the cost of using OpenAI's gpt-3.5 and gpt-4 models. It is intended to be used in a loop with `tqdm` to  track and display the cost estimation in real-time.\n",
    "\n",
    "**Home**: https://github.com/michaelachmann/gpt-cost-estimator\n",
    "\n",
    "**Licence**: [MIT Licence](https://github.com/michaelachmann/gpt-cost-estimator/blob/master/LICENSE)\n",
    "\n",
    "**Author**: [Michael Achmann](https://go.ur.de/michael-achmann)\n",
    "\n",
    "**PyPi Package** https://pypi.org/project/gpt-cost-estimator/"
   ],
   "metadata": {
    "id": "DBucnC6nww7h"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# Clone the git repository\n",
    "!pip install openai gpt-cost-estimator"
   ],
   "metadata": {
    "id": "UtsGaYnVqnoo"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "Import the `CostEstimator` class and define your custom `query_openai` method. Of course all OpenAI parameters may be added. Here I pass the `kwargs` on and remove the `CostEstimator` parameters beforehand."
   ],
   "metadata": {
    "id": "qoJKKWc-vht1"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from gpt_cost_estimator import CostEstimator\n",
    "from openai import OpenAI\n",
    "\n",
    "api_key = \"ENTER-YOUR-API-KEY\"\n",
    "\n",
    "# Initialize OpenAI using the key\n",
    "client = OpenAI(\n",
    "    api_key=api_key\n",
    ")\n",
    "\n",
    "@CostEstimator()\n",
    "def query_openai(model, messages, **kwargs):\n",
    "    args_to_remove = ['mock', 'completion_tokens']\n",
    "\n",
    "    for arg in args_to_remove:\n",
    "      if arg in kwargs:\n",
    "          del kwargs[arg]\n",
    "\n",
    "    return client.ChatCompletion.create(\n",
    "        model = model,\n",
    "        messages = messages,\n",
    "        **kwargs)"
   ],
   "metadata": {
    "id": "gsHm0Gcfq9uB"
   },
   "execution_count": 23,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Simulate OpenAI API Calls\n",
    "\n",
    "In this example I loop through a `range` from 0 to 10 and simulate an API call per loop. At the end, the total amount is displayed. Don't forget to `print()` an empty line after the loop make the last line (total price) visible."
   ],
   "metadata": {
    "id": "kt3_p7CVtcDe"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from tqdm.notebook import tqdm\n",
    "\n",
    "responses = []\n",
    "for i in tqdm(range(0,10)):\n",
    "  messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You're a helpful assistant.\"},\n",
    "    {\"role\": \"user\", \"content\": f\"Is the following number even or odd? Number: {i}\"},\n",
    "  ]\n",
    "\n",
    "  response = query_openai(\n",
    "      model=\"gpt-3.5-turbo-0613\",\n",
    "      messages = messages,\n",
    "      temperature=0,\n",
    "      mock=True,\n",
    "      completion_tokens=1\n",
    "  )\n",
    "\n",
    "  responses.append({\n",
    "      'input': i,\n",
    "      'output': response.choices[0].message.content\n",
    "    })\n",
    "\n",
    "\n",
    "print() # Empty line to display the total sum\n",
    "\n",
    "# Print the responses\n",
    "print(responses)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 106,
     "referenced_widgets": [
      "31883cc69d2147dbadcfcbbc83297a8c",
      "579a5f4533814940a14217c6fc60e170",
      "f0c4ac746e6844cdb4f1b5a0f231836b",
      "1320e19917fe4d2b9234fd628e0de37c",
      "570c2be9707b43e1a152d0ac5f0b3e9b",
      "8d4e121a0993436fa051bce8cc09fdcd",
      "30d775cc466e468783b4f4dd0b5b7739",
      "38ce581356f24c3cb64613c6410f21d6",
      "da81bca2fe0c4b2094c9f390c1e5f7cb",
      "8cfe7a82f6e0419086831ad53761c91c",
      "1584ef7445394c43b2bb1aa593fe13fd"
     ]
    },
    "id": "ymhaV0ZNrdz5",
    "outputId": "b7f33c2d-1606-44da-cb59-e85b4a36101d"
   },
   "execution_count": 25,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/plain": [
       "  0%|          | 0/10 [00:00<?, ?it/s]"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "31883cc69d2147dbadcfcbbc83297a8c"
      }
     },
     "metadata": {}
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Cost: $0.0001 | Total: $0.0032\rCost: $0.0001 | Total: $0.0032\rCost: $0.0001 | Total: $0.0033\rCost: $0.0001 | Total: $0.0033\rCost: $0.0001 | Total: $0.0034\rCost: $0.0001 | Total: $0.0035\rCost: $0.0001 | Total: $0.0035\rCost: $0.0001 | Total: $0.0036\rCost: $0.0001 | Total: $0.0037\rCost: $0.0001 | Total: $0.0037\r\n",
      "[{'input': 0, 'output': 'quisquam'}, {'input': 1, 'output': 'ipsa'}, {'input': 2, 'output': 'molestias'}, {'input': 3, 'output': 'consectetur'}, {'input': 4, 'output': 'repellendus'}, {'input': 5, 'output': 'neque'}, {'input': 6, 'output': 'recusandae'}, {'input': 7, 'output': 'praesentium'}, {'input': 8, 'output': 'adipisci'}, {'input': 9, 'output': 'magni'}]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Actual API Call\n",
    "\n",
    "Once you've optimized your prompt, just set `mock` to `False` and you're ready to go! While running through the API calls, the `CostEstimator` displays the current and total price of the OpenAI calls based on the token count provided in the API response."
   ],
   "metadata": {
    "id": "E8sYgPXOvcEv"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "from tqdm.notebook import tqdm\n",
    "\n",
    "responses = []\n",
    "for i in tqdm(range(0,10)):\n",
    "  messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You're a helpful assistant.\"},\n",
    "    {\"role\": \"user\", \"content\": f\"Is the following number even or odd? Number: {i}\"},\n",
    "  ]\n",
    "\n",
    "  response = query_openai(\n",
    "      model=\"gpt-3.5-turbo-0613\",\n",
    "      messages = messages,\n",
    "      temperature=0,\n",
    "      mock=False,\n",
    "      completion_tokens=1\n",
    "  )\n",
    "\n",
    "  responses.append({\n",
    "      'input': i,\n",
    "      'output': response.choices[0].message.content\n",
    "    })\n",
    "\n",
    "\n",
    "print() # Empty line to display the total sum\n",
    "\n",
    "# Print the responses\n",
    "print(responses)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 106,
     "referenced_widgets": [
      "958730a9af4b48d0acb036d27ba720d5",
      "18d7ae4959bd4e4cb749e6e516209b88",
      "6753ffabb1a24f09b9a17222bd846032",
      "0acbcd921ed3425d849115e2c79bdcc3",
      "beb57a990c15409eb876c9b4700f1502",
      "4dbbe9db382a4092a6ce5f6930f9a73c",
      "f2f86e7411014436b22b5e08735e1174",
      "7c0b28d71fe342ecaf0759a4d8c3e958",
      "78f6c0f65a324c81aec164b7f9313179",
      "fda0f4aa56064ccc89a27ea1e818a0fc",
      "29c91f25b1854dfcade19dd10e4e5a8d"
     ]
    },
    "id": "XMdDes4ZtlK5",
    "outputId": "97a880a6-7892-42c6-c7d9-f915623a62e3"
   },
   "execution_count": 24,
   "outputs": [
    {
     "output_type": "display_data",
     "data": {
      "text/plain": [
       "  0%|          | 0/10 [00:00<?, ?it/s]"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "958730a9af4b48d0acb036d27ba720d5"
      }
     },
     "metadata": {}
    },
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Cost: $0.0001 | Total: $0.0031\n",
      "[{'input': 0, 'output': 'The number 0 is an even number.'}, {'input': 1, 'output': 'The number 1 is an odd number.'}, {'input': 2, 'output': 'The number 2 is an even number.'}, {'input': 3, 'output': 'The number 3 is an odd number.'}, {'input': 4, 'output': 'The number 4 is an even number.'}, {'input': 5, 'output': 'The number 5 is an odd number.'}, {'input': 6, 'output': 'The number 6 is an even number.'}, {'input': 7, 'output': 'The number 7 is an odd number.'}, {'input': 8, 'output': 'The number 8 is an even number.'}, {'input': 9, 'output': 'The number 9 is an odd number.'}]\n"
     ]
    }
   ]
  }
 ]
}