forked from xiaozheyao/llm-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
63 lines (54 loc) · 2.18 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import functools
import requests
import json
from dotenv import load_dotenv
load_dotenv()
@functools.lru_cache(maxsize=None)
def getenv(key, default=0):
return type(default)(os.getenv(key, default))
def set_env_variables(data):
try:
if "env_variables" in data:
env_variables = data["env_variables"]
for key in env_variables:
os.environ[key] = env_variables[key]
data.pop("env_variables")
except:
pass
def get_all_models(endpoint: str):
available_models = []
providers = requests.get(endpoint).json()
for provider in providers.keys():
services = providers[provider]['service']
try:
for svc in services:
if svc['name'] == "llm":
# identity groups are in the form of "key=value", we split into tuples
identity_groups = [tuple(x.split("=")) for x in svc['identity_group']]
# filter out the tuple that has the key "model"
model = dict(filter(lambda x: x[0] == "model", identity_groups))['model']
available_models.append(model)
except Exception as e:
pass
available_models = list(set(available_models))
return available_models
def get_online_models(endpoint: str):
online_models = []
providers = requests.get(endpoint).json()
for provider in providers.keys():
services = providers[provider]['service']
try:
for svc in services:
if svc['name'] == "llm" and svc['status'] == "online":
identity_groups = [tuple(x.split("=")) for x in svc['identity_group']]
model = dict(filter(lambda x: x[0] == "model", identity_groups))['model']
metrics_url = f"http://148.187.108.172:8092/v1/p2p{provider}/v1/_service/llm/metrics" # TO CHANGE, USE VARIABLE INSTEAD OF IP
online_models.append({
"model_name": model,
"metrics_url": metrics_url,
})
except Exception as e:
print(f"Error: {e}")
pass
return online_models