2023-09-25 09:32:23 -06:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from flask import jsonify
|
2023-09-12 16:40:09 -06:00
|
|
|
|
2023-10-27 19:19:22 -06:00
|
|
|
from llm_server.custom_redis import ONE_MONTH_SECONDS, flask_cache, redis
|
2023-09-12 16:40:09 -06:00
|
|
|
from . import openai_bp
|
|
|
|
from ..stats import server_start_time
|
2024-01-10 15:01:26 -07:00
|
|
|
from ...cluster.cluster_config import get_a_cluster_backend, cluster_config
|
2024-05-07 12:20:53 -06:00
|
|
|
from ...config.global_config import GlobalConfig
|
2023-09-25 09:32:23 -06:00
|
|
|
from ...helpers import jsonify_pretty
|
2023-10-27 19:19:22 -06:00
|
|
|
from ...llm.openai.transform import generate_oai_string
|
2023-09-12 16:40:09 -06:00
|
|
|
|
|
|
|
|
|
|
|
@openai_bp.route('/models', methods=['GET'])
|
2023-09-26 22:09:11 -06:00
|
|
|
@flask_cache.cached(timeout=60, query_string=True)
|
2023-09-12 16:40:09 -06:00
|
|
|
def openai_list_models():
|
2023-10-27 19:19:22 -06:00
|
|
|
model_name = cluster_config.get_backend(get_a_cluster_backend()).get('model')
|
|
|
|
if not model_name:
|
2023-09-12 16:40:09 -06:00
|
|
|
response = jsonify({
|
|
|
|
'code': 502,
|
|
|
|
'msg': 'failed to reach backend',
|
|
|
|
}), 500 # return 500 so Cloudflare doesn't intercept us
|
|
|
|
else:
|
2023-10-27 19:19:22 -06:00
|
|
|
running_model = redis.get('running_model', 'ERROR', dtype=str)
|
2023-09-24 21:45:30 -06:00
|
|
|
oai = fetch_openai_models()
|
2023-10-27 19:19:22 -06:00
|
|
|
r = {
|
|
|
|
"object": "list",
|
|
|
|
"data": oai
|
|
|
|
}
|
|
|
|
# TODO: verify this works
|
2024-05-07 12:20:53 -06:00
|
|
|
if GlobalConfig.get().openai_expose_our_model:
|
2023-10-27 19:19:22 -06:00
|
|
|
r["data"].insert(0, {
|
|
|
|
"id": running_model,
|
|
|
|
"object": "model",
|
|
|
|
"created": int(server_start_time.timestamp()),
|
2024-05-07 12:20:53 -06:00
|
|
|
"owned_by": GlobalConfig.get().llm_middleware_name,
|
2023-10-27 19:19:22 -06:00
|
|
|
"permission": [
|
2023-09-25 09:32:23 -06:00
|
|
|
{
|
2023-09-26 13:32:33 -06:00
|
|
|
"id": running_model,
|
2023-10-27 19:19:22 -06:00
|
|
|
"object": "model_permission",
|
2023-09-25 09:32:23 -06:00
|
|
|
"created": int(server_start_time.timestamp()),
|
2023-10-27 19:19:22 -06:00
|
|
|
"allow_create_engine": False,
|
|
|
|
"allow_sampling": False,
|
|
|
|
"allow_logprobs": False,
|
|
|
|
"allow_search_indices": False,
|
|
|
|
"allow_view": True,
|
|
|
|
"allow_fine_tuning": False,
|
|
|
|
"organization": "*",
|
|
|
|
"group": None,
|
|
|
|
"is_blocking": False
|
2023-09-25 09:32:23 -06:00
|
|
|
}
|
2023-10-27 19:19:22 -06:00
|
|
|
],
|
|
|
|
"root": None,
|
|
|
|
"parent": None
|
|
|
|
})
|
|
|
|
response = jsonify_pretty(r), 200
|
2023-09-12 16:40:09 -06:00
|
|
|
return response
|
2023-09-24 21:45:30 -06:00
|
|
|
|
|
|
|
|
2023-09-26 22:09:11 -06:00
|
|
|
@flask_cache.memoize(timeout=ONE_MONTH_SECONDS)
|
2023-09-24 21:45:30 -06:00
|
|
|
def fetch_openai_models():
|
2024-05-07 12:20:53 -06:00
|
|
|
if GlobalConfig.get().openai_api_key:
|
2023-09-25 09:32:23 -06:00
|
|
|
try:
|
2024-05-07 12:20:53 -06:00
|
|
|
response = requests.get('https://api.openai.com/v1/models', headers={'Authorization': f"Bearer {GlobalConfig.get().openai_api_key}"}, timeout=10)
|
2023-10-27 19:19:22 -06:00
|
|
|
j = response.json()['data']
|
|
|
|
|
|
|
|
# The "modelperm" string appears to be user-specific, so we'll
|
|
|
|
# randomize it just to be safe.
|
|
|
|
for model in range(len(j)):
|
|
|
|
for p in range(len(j[model]['permission'])):
|
|
|
|
j[model]['permission'][p]['id'] = f'modelperm-{generate_oai_string(24)}'
|
|
|
|
return j
|
2023-09-25 09:32:23 -06:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return []
|