local-llm-server/server.py

75 lines
2.3 KiB
Python
Raw Normal View History

2023-08-21 21:28:52 -06:00
import os
import sys
from pathlib import Path
import tiktoken
from flask import Flask, current_app, jsonify
from llm_server import opts
from llm_server.config import ConfigLoader
from llm_server.database import init_db
from llm_server.helpers import resolve_path
from llm_server.llm.info import get_running_model
2023-08-21 21:28:52 -06:00
from llm_server.routes.cache import cache
2023-08-21 22:49:44 -06:00
from llm_server.routes.helpers.http import cache_control
2023-08-21 21:28:52 -06:00
from llm_server.routes.v1 import bp
2023-08-21 23:07:12 -06:00
script_path = os.path.dirname(os.path.realpath(__file__))
2023-08-21 21:28:52 -06:00
config_path_environ = os.getenv("CONFIG_PATH")
if config_path_environ:
config_path = config_path_environ
else:
2023-08-21 23:07:12 -06:00
config_path = Path(script_path, 'config', 'config.yml')
2023-08-21 21:28:52 -06:00
2023-08-22 16:50:49 -06:00
default_vars = {'mode': 'oobabooga', 'log_prompts': False, 'database_path': './proxy-server.db', 'auth_required': False, 'concurrent_gens': 3, 'frontend_api_client': ''}
required_vars = ['token_limit']
2023-08-21 21:28:52 -06:00
config_loader = ConfigLoader(config_path, default_vars, required_vars)
success, config, msg = config_loader.load_config()
if not success:
print('Failed to load config:', msg)
sys.exit(1)
opts.backend_url = config['backend_url'].strip('/')
2023-08-21 23:07:12 -06:00
# Resolve relative directory to the directory of the script
if config['database_path'].startswith('./'):
config['database_path'] = resolve_path(script_path, config['database_path'].strip('./'))
2023-08-21 21:28:52 -06:00
opts.database_path = resolve_path(config['database_path'])
init_db(opts.database_path)
if config['mode'] not in ['oobabooga', 'hf-textgen']:
print('Unknown mode:', config['mode'])
opts.mode = config['mode']
opts.auth_required = config['auth_required']
opts.log_prompts = config['log_prompts']
2023-08-22 00:26:46 -06:00
opts.concurrent_gens = config['concurrent_gens']
2023-08-22 16:50:49 -06:00
opts.frontend_api_client = config['frontend_api_client']
opts.token_limit = config['token_limit']
2023-08-21 21:28:52 -06:00
app = Flask(__name__)
cache.init_app(app)
cache.clear() # clear redis cache
2023-08-21 21:28:52 -06:00
# with app.app_context():
# current_app.tokenizer = tiktoken.get_encoding("cl100k_base")
app.register_blueprint(bp, url_prefix='/api/v1/')
2023-08-21 22:49:44 -06:00
# print(app.url_map)
2023-08-21 21:28:52 -06:00
@app.route('/')
@app.route('/<first>')
@app.route('/<first>/<path:rest>')
2023-08-21 22:49:44 -06:00
@cache_control(-1)
2023-08-21 21:28:52 -06:00
def fallback(first=None, rest=None):
return jsonify({
'error': 404,
'msg': 'not found'
}), 404
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)