2023-09-17 18:33:57 -06:00
|
|
|
import math
|
2023-09-23 23:14:22 -06:00
|
|
|
import re
|
2023-08-24 18:59:52 -06:00
|
|
|
from collections import OrderedDict
|
2023-08-21 21:28:52 -06:00
|
|
|
from pathlib import Path
|
2023-09-25 09:32:23 -06:00
|
|
|
from typing import Union
|
2023-08-21 21:28:52 -06:00
|
|
|
|
2023-09-23 23:14:22 -06:00
|
|
|
import simplejson as json
|
2023-09-16 17:37:43 -06:00
|
|
|
from flask import make_response
|
|
|
|
|
2023-09-23 23:14:22 -06:00
|
|
|
from llm_server import opts
|
|
|
|
from llm_server.routes.cache import redis
|
|
|
|
|
2023-09-20 21:14:12 -06:00
|
|
|
|
2023-08-21 21:28:52 -06:00
|
|
|
def resolve_path(*p: str):
|
|
|
|
return Path(*p).expanduser().resolve().absolute()
|
2023-08-22 20:28:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
def safe_list_get(l, idx, default):
|
|
|
|
"""
|
|
|
|
https://stackoverflow.com/a/5125636
|
|
|
|
:param l:
|
|
|
|
:param idx:
|
|
|
|
:param default:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return l[idx]
|
|
|
|
except IndexError:
|
|
|
|
return default
|
2023-08-24 18:59:52 -06:00
|
|
|
|
|
|
|
|
|
|
|
def deep_sort(obj):
|
|
|
|
if isinstance(obj, dict):
|
2023-09-25 17:20:21 -06:00
|
|
|
return OrderedDict((k, deep_sort(v)) for k, v in sorted(obj.items()))
|
2023-08-24 18:59:52 -06:00
|
|
|
if isinstance(obj, list):
|
2023-09-25 17:20:21 -06:00
|
|
|
return sorted(deep_sort(x) for x in obj)
|
2023-08-24 18:59:52 -06:00
|
|
|
return obj
|
2023-08-29 13:46:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
def indefinite_article(word):
|
|
|
|
if word[0].lower() in 'aeiou':
|
|
|
|
return 'an'
|
|
|
|
else:
|
|
|
|
return 'a'
|
2023-09-16 17:37:43 -06:00
|
|
|
|
|
|
|
|
2023-09-25 09:32:23 -06:00
|
|
|
def jsonify_pretty(json_dict: Union[list, dict], status=200, indent=4, sort_keys=True):
|
2023-09-16 17:37:43 -06:00
|
|
|
response = make_response(json.dumps(json_dict, indent=indent, sort_keys=sort_keys))
|
|
|
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
|
|
|
response.headers['mimetype'] = 'application/json'
|
|
|
|
response.status_code = status
|
|
|
|
return response
|
2023-09-17 18:33:57 -06:00
|
|
|
|
|
|
|
|
|
|
|
def round_up_base(n, base):
|
2023-09-28 03:46:01 -06:00
|
|
|
if base == 0:
|
|
|
|
print('round_up_base DIVIDE BY ZERO ERROR????', n, base)
|
|
|
|
return 0
|
2023-09-17 18:33:57 -06:00
|
|
|
return math.ceil(n / base) * base
|
2023-09-23 23:14:22 -06:00
|
|
|
|
|
|
|
|
2023-09-23 23:24:08 -06:00
|
|
|
def auto_set_base_client_api(request):
|
2023-09-23 23:14:22 -06:00
|
|
|
http_host = redis.get('http_host', str)
|
|
|
|
host = request.headers.get("Host")
|
|
|
|
if http_host and not re.match(r'((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}', http_host):
|
|
|
|
# If the current http_host is not an IP, don't do anything.
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
redis.set('http_host', host)
|
|
|
|
redis.set('base_client_api', f'{host}/{opts.frontend_api_client.strip("/")}')
|