Beracles
commited on
Commit
·
427886e
1
Parent(s):
90abc51
delete cache.py
Browse files
cache.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
from pgsoft.pgdate.date_utils import beijing
|
| 4 |
-
|
| 5 |
-
cache_ai = {}
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def normalize_text(text: str) -> str:
|
| 9 |
-
text = text.lower()
|
| 10 |
-
tmp = text.split(" ")
|
| 11 |
-
tmp = [word for word in tmp if word != ""]
|
| 12 |
-
return " ".join(tmp)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def load_cache(filepath: str):
|
| 16 |
-
"""load cached ai calling from a json file"""
|
| 17 |
-
global cache_ai
|
| 18 |
-
if os.path.exists(filepath):
|
| 19 |
-
with open(filepath, "r+") as f:
|
| 20 |
-
cache_ai = json.load(f)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def add_cache(command: str, result: dict):
|
| 24 |
-
"""add a cache of ai calling"""
|
| 25 |
-
command = normalize_text(command)
|
| 26 |
-
result["command"] = command
|
| 27 |
-
|
| 28 |
-
global cache_ai
|
| 29 |
-
cache_ai[command] = result
|
| 30 |
-
print(f'[cache] added "{command}"')
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def get_cache(command: str) -> dict | None:
|
| 34 |
-
"""return a cached ai calling with new "timestamp" """
|
| 35 |
-
command = normalize_text(command)
|
| 36 |
-
outp = cache_ai.get(command)
|
| 37 |
-
if outp:
|
| 38 |
-
outp["timestamp"] = beijing().__str__()
|
| 39 |
-
outp["cache"] = True
|
| 40 |
-
return outp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|