Spaces:
Running
Running
Automated deployment update from ML build
Browse files- services/llm_proxy_service.py +26 -21
services/llm_proxy_service.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os,subprocess,json
|
| 2 |
from pathlib import Path
|
| 3 |
from loguru import logger
|
|
@@ -41,30 +44,30 @@ def _load_keys():
|
|
| 41 |
if A:logger.info(f"{PREFIX} Parsed {len(A)} keys from LLM_KEYS env variable")
|
| 42 |
return A
|
| 43 |
def _build_config():
|
| 44 |
-
|
| 45 |
-
if not
|
| 46 |
-
|
| 47 |
-
for(A,B,
|
| 48 |
-
if B=='*'or B==f"{A}/*":
|
| 49 |
litellm_params:
|
| 50 |
model: {A}/*
|
| 51 |
-
api_key: "{
|
| 52 |
model_info:
|
| 53 |
owned_by: "{A}"
|
| 54 |
'''
|
| 55 |
else:
|
| 56 |
-
if B.startswith(f"{A}/"):
|
| 57 |
-
else:
|
| 58 |
-
|
| 59 |
litellm_params:
|
| 60 |
-
model: {
|
| 61 |
-
api_key: "{
|
| 62 |
model_info:
|
| 63 |
owned_by: "{A}"
|
| 64 |
'''
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
{
|
| 68 |
router_settings:
|
| 69 |
routing_strategy: least-busy
|
| 70 |
num_retries: 3
|
|
@@ -76,11 +79,13 @@ litellm_settings:
|
|
| 76 |
|
| 77 |
general_settings:
|
| 78 |
drop_params: true
|
| 79 |
-
|
| 80 |
-
{J}'''
|
| 81 |
def start():
|
| 82 |
-
A=True;os.makedirs(METRICS_DIR,exist_ok=A);
|
| 83 |
-
if not
|
| 84 |
-
Path(CONFIG_PATH).write_text(
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
_C='postgresql://'
|
| 2 |
+
_B='postgres://'
|
| 3 |
+
_A='DATABASE_URL'
|
| 4 |
import os,subprocess,json
|
| 5 |
from pathlib import Path
|
| 6 |
from loguru import logger
|
|
|
|
| 44 |
if A:logger.info(f"{PREFIX} Parsed {len(A)} keys from LLM_KEYS env variable")
|
| 45 |
return A
|
| 46 |
def _build_config():
|
| 47 |
+
D=_load_keys()
|
| 48 |
+
if not D:return''
|
| 49 |
+
E=[]
|
| 50 |
+
for(A,B,F)in D:
|
| 51 |
+
if B=='*'or B==f"{A}/*":G=f''' - model_name: "{A}/*"
|
| 52 |
litellm_params:
|
| 53 |
model: {A}/*
|
| 54 |
+
api_key: "{F}"
|
| 55 |
model_info:
|
| 56 |
owned_by: "{A}"
|
| 57 |
'''
|
| 58 |
else:
|
| 59 |
+
if B.startswith(f"{A}/"):H=B
|
| 60 |
+
else:H=f"{A}/{B}"
|
| 61 |
+
G=f''' - model_name: "{B}"
|
| 62 |
litellm_params:
|
| 63 |
+
model: {H}
|
| 64 |
+
api_key: "{F}"
|
| 65 |
model_info:
|
| 66 |
owned_by: "{A}"
|
| 67 |
'''
|
| 68 |
+
E.append(G)
|
| 69 |
+
J=''.join(E);I=os.environ.get('LITELLM_MASTER_KEY','').strip();K=f' master_key: "{I}"\n'if I else'';C=os.environ.get(_A,'').strip();L=C.startswith(_B)or C.startswith(_C);M=f' database_url: "{C}"\n'if L else'';return f"""model_list:
|
| 70 |
+
{J}
|
| 71 |
router_settings:
|
| 72 |
routing_strategy: least-busy
|
| 73 |
num_retries: 3
|
|
|
|
| 79 |
|
| 80 |
general_settings:
|
| 81 |
drop_params: true
|
| 82 |
+
{M}{K}"""
|
|
|
|
| 83 |
def start():
|
| 84 |
+
A=True;os.makedirs(METRICS_DIR,exist_ok=A);C=_build_config()
|
| 85 |
+
if not C:logger.warning(f"{PREFIX} No API keys loaded or LLM_KEYS not set — skipping llm_proxy");return
|
| 86 |
+
Path(CONFIG_PATH).write_text(C);logger.info(f"{PREFIX} Config written to {CONFIG_PATH}");D=os.path.join(METRICS_DIR,'ui');E=os.path.join(METRICS_DIR,'assets');F=os.path.join(METRICS_DIR,'prisma');os.makedirs(D,exist_ok=A);os.makedirs(E,exist_ok=A);os.makedirs(F,exist_ok=A);os.environ['LITELLM_UI_PATH']=D;os.environ['LITELLM_ASSETS_PATH']=E;os.environ['LITELLM_MIGRATION_DIR']=F;B=os.environ.get(_A,'').strip();H=B.startswith(_B)or B.startswith(_C)
|
| 87 |
+
if H:os.environ[_A]=B
|
| 88 |
+
else:os.environ.pop(_A,None)
|
| 89 |
+
I=['litellm','--config',CONFIG_PATH,'--port',str(PORT),'--host','127.0.0.1']
|
| 90 |
+
with open(LOG_PATH,'a')as G:J=subprocess.Popen(I,stdout=G,stderr=G)
|
| 91 |
+
logger.success(f"{PREFIX} litellm proxy started on 127.0.0.1:{PORT} (pid {J.pid})")
|