import math import re from collections import OrderedDict from pathlib import Path from typing import Union import simplejson as json from flask import make_response from llm_server.config.global_config import GlobalConfig from llm_server.custom_redis import redis def resolve_path(*p: str): return Path(*p).expanduser().resolve().absolute() def deep_sort(obj): if isinstance(obj, dict): return OrderedDict((k, deep_sort(v)) for k, v in sorted(obj.items())) if isinstance(obj, list): return sorted(deep_sort(x) for x in obj) return obj def indefinite_article(word): if word[0].lower() in 'aeiou': return 'an' else: return 'a' def jsonify_pretty(json_dict: Union[list, dict], status=200, indent=4, sort_keys=True): 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 def round_up_base(n, base): if base == 0: # TODO: I don't think passing (0, 0) to this function is a sign of any underlying issues. # print('round_up_base DIVIDE BY ZERO ERROR????', n, base) return 0 return math.ceil(n / base) * base def auto_set_base_client_api(request): http_host = redis.get('http_host', dtype=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}/{GlobalConfig.get().frontend_api_client.strip("/")}')