This repository has been archived on 2024-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
local-llm-server/llm_server/database.py

82 lines
2.6 KiB
Python
Raw Normal View History

2023-08-21 21:28:52 -06:00
import json
import sqlite3
import time
from pathlib import Path
import tiktoken
from llm_server import opts
tokenizer = tiktoken.get_encoding("cl100k_base")
def init_db(db_path):
if not Path(db_path).exists():
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''
CREATE TABLE prompts (
ip TEXT,
token TEXT DEFAULT NULL,
prompt TEXT,
prompt_tokens INTEGER,
response TEXT,
response_tokens INTEGER,
response_status INTEGER,
2023-08-21 21:28:52 -06:00
parameters TEXT CHECK (parameters IS NULL OR json_valid(parameters)),
headers TEXT CHECK (headers IS NULL OR json_valid(headers)),
timestamp INTEGER
)
''')
c.execute('''
CREATE TABLE token_auth
2023-08-21 22:49:44 -06:00
(token TEXT, type TEXT NOT NULL, uses INTEGER, max_uses INTEGER, expire INTEGER, disabled BOOLEAN default 0)
2023-08-21 21:28:52 -06:00
''')
# c.execute('''
# CREATE TABLE leeches
# (url TEXT, online TEXT)
# ''')
2023-08-21 21:28:52 -06:00
conn.commit()
conn.close()
def log_prompt(db_path, ip, token, prompt, response, parameters, headers, backend_response_code):
2023-08-21 21:28:52 -06:00
prompt_tokens = len(tokenizer.encode(prompt))
response_tokens = len(tokenizer.encode(response))
if not opts.log_prompts:
prompt = response = None
timestamp = int(time.time())
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute("INSERT INTO prompts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(ip, token, prompt, prompt_tokens, response, response_tokens, backend_response_code, json.dumps(parameters), json.dumps(headers), timestamp))
2023-08-21 21:28:52 -06:00
conn.commit()
conn.close()
def is_valid_api_key(api_key):
conn = sqlite3.connect(opts.database_path)
cursor = conn.cursor()
2023-08-21 22:49:44 -06:00
cursor.execute("SELECT token, uses, max_uses, expire, disabled FROM token_auth WHERE token = ?", (api_key,))
2023-08-21 21:28:52 -06:00
row = cursor.fetchone()
if row is not None:
2023-08-21 22:49:44 -06:00
token, uses, max_uses, expire, disabled = row
disabled = bool(disabled)
if (uses is None or uses < max_uses) and (expire is None or expire > time.time()) and not disabled:
2023-08-21 21:28:52 -06:00
return True
return False
def increment_uses(api_key):
conn = sqlite3.connect(opts.database_path)
cursor = conn.cursor()
cursor.execute("SELECT token FROM token_auth WHERE token = ?", (api_key,))
row = cursor.fetchone()
if row is not None:
cursor.execute("UPDATE token_auth SET uses = COALESCE(uses, 0) + 1 WHERE token = ?", (api_key,))
conn.commit()
return True
return False