Skip to content

Commit

Permalink
Merge pull request #12 from drudilorenzo/update-demo
Browse files Browse the repository at this point in the history
Update demo file.
  • Loading branch information
drudilorenzo authored May 28, 2024
2 parents de46029 + 7a5db83 commit afb1136
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 45 deletions.
148 changes: 104 additions & 44 deletions demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"Requirement already satisfied: cycler>=0.10 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (0.11.0)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (4.42.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (1.4.5)\n",
"Requirement already satisfied: numpy>=1.19 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (1.26.4)\n",
"Requirement already satisfied: numpy>=1.19 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (1.25.2)\n",
"Requirement already satisfied: packaging>=20.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (23.1)\n",
"Requirement already satisfied: pillow>=6.2.0 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (10.2.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib==3.6.3->-r requirements.txt (line 2)) (3.1.1)\n",
Expand Down Expand Up @@ -73,42 +73,55 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: openai-cost-logger in /home/drudao/.local/lib/python3.10/site-packages (0.0.2)\n",
"Requirement already satisfied: matplotlib in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger) (3.6.3)\n",
"Requirement already satisfied: openai in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger) (1.13.3)\n",
"Requirement already satisfied: pandas in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger) (2.1.1)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (1.1.1)\n",
"Requirement already satisfied: cycler>=0.10 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (0.11.0)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (4.42.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (1.4.5)\n",
"Requirement already satisfied: numpy>=1.19 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (1.26.4)\n",
"Requirement already satisfied: packaging>=20.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (23.1)\n",
"Requirement already satisfied: pillow>=6.2.0 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (10.2.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (3.1.1)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger) (2.8.2)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (4.3.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from openai->openai-cost-logger) (1.9.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (0.27.0)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (2.6.4)\n",
"Requirement already satisfied: sniffio in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (1.3.1)\n",
"Requirement already satisfied: tqdm>4 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (4.66.2)\n",
"Requirement already satisfied: typing-extensions<5,>=4.7 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger) (4.10.0)\n",
"Requirement already satisfied: pytz>=2020.1 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from pandas->openai-cost-logger) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.1 in /home/drudao/.local/lib/python3.10/site-packages (from pandas->openai-cost-logger) (2023.3)\n",
"Requirement already satisfied: idna>=2.8 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai->openai-cost-logger) (3.6)\n",
"Requirement already satisfied: exceptiongroup>=1.0.2 in /home/drudao/.local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai->openai-cost-logger) (1.1.3)\n",
"Requirement already satisfied: certifi in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai->openai-cost-logger) (2024.2.2)\n",
"Requirement already satisfied: httpcore==1.* in /home/drudao/.local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai->openai-cost-logger) (1.0.4)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /home/drudao/.local/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai->openai-cost-logger) (0.14.0)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /home/drudao/.local/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->openai->openai-cost-logger) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in /home/drudao/.local/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->openai->openai-cost-logger) (2.16.3)\n",
"Requirement already satisfied: six>=1.5 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib->openai-cost-logger) (1.16.0)\n",
"Collecting openai-cost-logger==0.5.0\n",
" Downloading openai-cost-logger-0.5.0.tar.gz (7.8 kB)\n",
" Preparing metadata (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25hRequirement already satisfied: openai in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger==0.5.0) (1.13.3)\n",
"Requirement already satisfied: pandas in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger==0.5.0) (2.1.1)\n",
"Requirement already satisfied: matplotlib in /home/drudao/.local/lib/python3.10/site-packages (from openai-cost-logger==0.5.0) (3.6.3)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (1.1.1)\n",
"Requirement already satisfied: cycler>=0.10 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (0.11.0)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (4.42.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (1.4.5)\n",
"Requirement already satisfied: numpy>=1.19 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (1.25.2)\n",
"Requirement already satisfied: packaging>=20.0 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (23.1)\n",
"Requirement already satisfied: pillow>=6.2.0 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (10.2.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (3.1.1)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /home/drudao/.local/lib/python3.10/site-packages (from matplotlib->openai-cost-logger==0.5.0) (2.8.2)\n",
"Requirement already satisfied: anyio<5,>=3.5.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (4.3.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (1.9.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (0.27.0)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (2.6.4)\n",
"Requirement already satisfied: sniffio in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (1.3.1)\n",
"Requirement already satisfied: tqdm>4 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (4.66.2)\n",
"Requirement already satisfied: typing-extensions<5,>=4.7 in /home/drudao/.local/lib/python3.10/site-packages (from openai->openai-cost-logger==0.5.0) (4.10.0)\n",
"Requirement already satisfied: pytz>=2020.1 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from pandas->openai-cost-logger==0.5.0) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.1 in /home/drudao/.local/lib/python3.10/site-packages (from pandas->openai-cost-logger==0.5.0) (2023.3)\n",
"Requirement already satisfied: idna>=2.8 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai->openai-cost-logger==0.5.0) (3.6)\n",
"Requirement already satisfied: exceptiongroup>=1.0.2 in /home/drudao/.local/lib/python3.10/site-packages (from anyio<5,>=3.5.0->openai->openai-cost-logger==0.5.0) (1.1.3)\n",
"Requirement already satisfied: certifi in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai->openai-cost-logger==0.5.0) (2024.2.2)\n",
"Requirement already satisfied: httpcore==1.* in /home/drudao/.local/lib/python3.10/site-packages (from httpx<1,>=0.23.0->openai->openai-cost-logger==0.5.0) (1.0.4)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /home/drudao/.local/lib/python3.10/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai->openai-cost-logger==0.5.0) (0.14.0)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /home/drudao/.local/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->openai->openai-cost-logger==0.5.0) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in /home/drudao/.local/lib/python3.10/site-packages (from pydantic<3,>=1.9.0->openai->openai-cost-logger==0.5.0) (2.16.3)\n",
"Requirement already satisfied: six>=1.5 in /home/drudao/anaconda3/envs/modern_nlp/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib->openai-cost-logger==0.5.0) (1.16.0)\n",
"Building wheels for collected packages: openai-cost-logger\n",
" Building wheel for openai-cost-logger (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for openai-cost-logger: filename=openai_cost_logger-0.5.0-py3-none-any.whl size=8764 sha256=fb2536ca63b38118fbeadc2c3fb7ad00f0d7fd0f7992524f8ce7b2f5fa1a6607\n",
" Stored in directory: /home/drudao/.cache/pip/wheels/66/a3/10/a497133f6b2a588c19bc46372fb0e1cdb6dac6d29ece1a96c0\n",
"Successfully built openai-cost-logger\n",
"Installing collected packages: openai-cost-logger\n",
" Attempting uninstall: openai-cost-logger\n",
" Found existing installation: openai-cost-logger 0.0.2\n",
" Uninstalling openai-cost-logger-0.0.2:\n",
" Successfully uninstalled openai-cost-logger-0.0.2\n",
"Successfully installed openai-cost-logger-0.5.0\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install openai-cost-logger"
"%pip install openai-cost-logger==0.5.0"
]
},
{
Expand All @@ -120,7 +133,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -130,21 +143,14 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"imported openai-cost-logger\n"
]
}
],
"outputs": [],
"source": [
"from openai_cost_logger import OpenAICostLogger\n",
"from openai_cost_logger import OpenAICostLoggerViz\n",
"from openai_cost_logger import OpenAICostLoggerUtils\n",
"from openai_cost_logger import OpenAICostLogger_Singleton\n",
"from openai_cost_logger import DEFAULT_LOG_PATH, MODELS_COST"
]
},
Expand Down Expand Up @@ -177,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -346,6 +352,60 @@
"OpenAICostLoggerViz.print_experiment_cost(experiment=\"demo\", path=log_folder)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7. Thread-Safe Logger"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"cost_logger_singleton = OpenAICostLogger_Singleton(\n",
" experiment_name = experiment_name,\n",
" log_folder = log_folder,\n",
" cost_upperbound = cost_upperbound\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current cost: 1.4000000000000001e-05\n"
]
}
],
"source": [
"# THREAD-SAFE CHAT COMPLETION EXAMPLE\n",
"model = \"gpt-3-5-turbo\"\n",
"input_cost = MODELS_COST[model][\"input\"]\n",
"output_cost = MODELS_COST[model][\"output\"]\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Who won the euro 2020?\"},\n",
"]\n",
"\n",
"# Get the model response\n",
"response = client.chat.completions.create(model=model, messages=messages, max_tokens=1, temperature=0)\n",
"\n",
"# In case `input_cost` or `output_cost` are not passed, the object will look for the model in the `MODELS_COST` dictionary.\n",
"# If the model is not found, it will raise an exception.\n",
"# The costs should be per million tokens.\n",
"cost_logger_singleton.update_cost(response=response, input_cost=input_cost, output_cost=output_cost)\n",
"\n",
"print(\"Current cost: \", cost_logger_singleton.get_current_cost())"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
with open('README.rst') as f:
long_description = f.read()

version_number = '0.5.0'
version_number = '0.5.1'

setup(
name='openai-cost-logger',
Expand Down

0 comments on commit afb1136

Please sign in to comment.