Skip to content

Commit

Permalink
Optionally pass timestamp when adding to costs db
Browse files Browse the repository at this point in the history
  • Loading branch information
paulovcmedeiros committed Nov 12, 2023
1 parent 2a29e22 commit 1a03d6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 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
13 changes: 10 additions & 3 deletions pyrobbot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import sqlite3
from pathlib import Path
from typing import Optional

import pandas as pd
import tiktoken
Expand Down Expand Up @@ -61,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 @@ -83,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 @@ -110,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 1a03d6b

Please sign in to comment.