forked from xiaozheyao/llm-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_utils.py
42 lines (38 loc) · 1.44 KB
/
user_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import secrets
from datetime import datetime
from sqlmodel import SQLModel, Field, Session, create_engine, select
class APIKey(SQLModel, table=True):
key: str = Field(primary_key=True)
budget: int = Field(default=1000)
created_at: datetime = Field(default=datetime.now())
updated_at: datetime = Field(default=datetime.now())
owner_email: str = Field(default="")
def get_or_create_apikey(engine, owner_email: str) -> APIKey:
with Session(engine) as session:
api_key = session.exec(
select(APIKey).where(APIKey.owner_email == owner_email)
).first()
if api_key is None:
key = f"sk-rc-{secrets.token_urlsafe(16)}"
api_key = APIKey(key=key, owner_email=owner_email)
session.add(api_key)
session.commit()
session.refresh(api_key)
return api_key
def rotate_key(engine, key: str) -> APIKey:
with Session(engine) as session:
api_key = session.exec(
select(APIKey).where(APIKey.key == key)
).first()
if api_key is None:
raise ValueError("Invalid key")
api_key.key = f"sk-rc-{secrets.token_urlsafe(16)}"
session.add(api_key)
session.commit()
session.refresh(api_key)
return api_key
if __name__ =="__main__":
pg_host = os.environ.get("PG_HOST", "localhost")
engine = create_engine(pg_host, echo=True)
SQLModel.metadata.create_all(engine)