Skip to content

Commit

Permalink
Sort costs table by total costs, not by tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
paulovcmedeiros committed Nov 12, 2023
2 parents 3937ed9 + 1a03d6b commit beb0dd6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license = "MIT"
name = "pyrobbot"
readme = "README.md"
version = "0.1.3"
version = "0.1.4"

[build-system]
build-backend = "poetry.core.masonry.api"
Expand Down
21 changes: 17 additions & 4 deletions pyrobbot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
import datetime
import sqlite3
from pathlib import Path
from typing import Optional

import pandas as pd
import tiktoken

# See <https://openai.com/pricing> for the latest prices.
PRICE_PER_K_TOKENS = {
"gpt-3.5-turbo": {"input": 0.0015, "output": 0.002},
"gpt-3.5-turbo-0613": {"input": 0.0015, "output": 0.002},
"gpt-3.5-turbo-16k": {"input": 0.001, "output": 0.002},
"gpt-3.5-turbo-16k-0613": {"input": 0.001, "output": 0.002},
"gpt-3.5-turbo-1106": {"input": 0.001, "output": 0.002},
"gpt-4-1106-preview": {"input": 0.03, "output": 0.06},
"gpt-4-1106-preview": {"input": 0.01, "output": 0.03},
"gpt-4": {"input": 0.03, "output": 0.06},
"gpt-4-0613": {"input": 0.03, "output": 0.06},
"gpt-4-32k": {"input": 0.06, "output": 0.12},
"text-embedding-ada-002": {"input": 0.0001, "output": 0.0},
"text-embedding-ada-002-v2": {"input": 0.0001, "output": 0.0},
"text-davinci:002": {"input": 0.0020, "output": 0.020},
"full-history": {"input": 0.0, "output": 0.0},
}

Expand Down Expand Up @@ -55,7 +62,13 @@ def create(self):
conn.commit()
conn.close()

def insert_data(self, model: str, n_input_tokens: int = 0, n_output_tokens: int = 0):
def insert_data(
self,
model: str,
n_input_tokens: int = 0,
n_output_tokens: int = 0,
timestamp: Optional[int] = None,
):
"""Insert the data into the token_costs table."""
if model is None:
return
Expand All @@ -77,7 +90,7 @@ def insert_data(self, model: str, n_input_tokens: int = 0, n_output_tokens: int
VALUES (?, ?, ?, ?, ?, ?)
""",
(
int(datetime.datetime.utcnow().timestamp()),
timestamp or int(datetime.datetime.utcnow().timestamp()),
model,
n_input_tokens,
n_output_tokens,
Expand All @@ -104,7 +117,7 @@ def get_usage_balance_dataframe(self):
SUM(cost_input_tokens + cost_output_tokens) AS "Cost ($): Tot."
FROM token_costs
GROUP BY model
ORDER BY "Tokens: Tot." DESC
ORDER BY "Cost ($): Tot." DESC
"""

usage_df = pd.read_sql_query(query, con=conn)
Expand Down

0 comments on commit beb0dd6

Please sign in to comment.