Spaces:
Sleeping
Sleeping
Create logging.py
Browse files- app/utils/logging.py +22 -0
app/utils/logging.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from app.db.database import SessionLocal
|
| 4 |
+
from app.models.llm_log import LLMLog
|
| 5 |
+
|
| 6 |
+
def log_llm_usage(user_id: int, model: str, prompt_tokens: int, completion_tokens: int, response_time: float):
|
| 7 |
+
db = SessionLocal()
|
| 8 |
+
try:
|
| 9 |
+
new_log = LLMLog(
|
| 10 |
+
user_id=user_id,
|
| 11 |
+
model=model,
|
| 12 |
+
prompt_tokens=prompt_tokens,
|
| 13 |
+
completion_tokens=completion_tokens,
|
| 14 |
+
total_tokens=prompt_tokens + completion_tokens,
|
| 15 |
+
response_time=response_time
|
| 16 |
+
)
|
| 17 |
+
db.add(new_log)
|
| 18 |
+
db.commit()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Failed to log LLM usage: {e}")
|
| 21 |
+
finally:
|
| 22 |
+
db.close()
|