-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.py
200 lines (157 loc) · 5.39 KB
/
config.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import streamlit as st
import json
import requests
import time
from typing import List, Dict, Tuple
base_url = "https://api.unify.ai/v0"
def init_session_state() -> None:
'''
Initialize session state if it doesn't exist yet
'''
if 'selections' not in st.session_state:
st.session_state['selections'] = {
'LLM1': {}, 'LLM2': {}, 'Judge': {}
}
@st.cache_data
def load_models() -> List[Dict[str, str]]:
'''
Load models from models.json
Returns:
List[Dict[str, str]]: List of models
'''
with open("models.json", "r") as f:
data = json.load(f)
return data["models"]
def get_providers(model: str) -> List[str]:
'''
Get providers for a given model
Args:
model (str): The model name
Returns:
List[str]: List of providers
'''
url = f"{base_url}/endpoints"
headers = {
'Authorization': f'Bearer {st.session_state["previous_api_key"]}'
}
response = requests.get(url, params={"model": model}, headers=headers)
if response.status_code == 200:
providers = [provider.split("@")[1]
for provider in json.loads(response.text)]
return providers
else:
st.error("Failed to fetch providers from API.")
return []
def get_summary_string(key: str) -> str:
"""
Generate summary string for the expander based on current selections.
Args:
key (str): The key for LLM1, LLM2, or Judge
Returns:
str: A formatted summary string
"""
selection = st.session_state["selections"][key]
if selection.get("name") and selection.get("provider"):
return f"{key}: {selection['name']}@{selection['provider']}"
return f"{key} Configuration"
def update_models() -> None:
'''
Update models.json with providers
'''
model_names = list_models()
models = []
if not model_names:
st.error("No models found to update.")
return
progress_bar = st.progress(0)
message_placeholder = st.empty() # Placeholder for the message
total_models = len(model_names)
for index, model in enumerate(model_names):
message_placeholder.text(f"Searching providers for {model}...")
providers = get_providers(model)
models.append({"name": model, "providers": providers})
# Update the progress bar
progress_bar.progress((index + 1) / total_models)
with open("models.json", "w") as f:
json.dump({"models": models}, f, indent=4)
st.success("Models and providers updated successfully!")
message_placeholder.empty() # Clear the message placeholder
def list_models() -> List[str]:
'''
List models from Unify API
Returns:
List[str]: List of models
'''
url = f"{base_url}/models"
headers = {
'Authorization': f'Bearer {st.session_state["previous_api_key"]}'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return json.loads(response.text)
else:
st.error("Failed to fetch models from API.")
return []
def select_model_provider(key: str, models: List[Dict[str, str]]) -> str:
"""
Render model and provider selection boxes and update session state.
Args:
key (str): The key for LLM1, LLM2, or Judge
models (List[Dict[str, str]]): List of models and their providers
Returns:
str: The endpoint string constructed from selected model and provider
"""
model_names = [model["name"] for model in models]
selected_model = st.selectbox(
"Model",
options=model_names,
key=f"{key}_model",
)
st.session_state["selections"][key]["name"] = selected_model
if selected_model:
model_details = next(
(item for item in models if item["name"] == selected_model), None
)
if model_details:
providers = model_details["providers"]
selected_provider = st.selectbox(
"Provider",
options=providers,
key=f"{key}_provider",
)
st.session_state["selections"][key]["provider"] = selected_provider
return f"{selected_model}@{selected_provider}"
return ""
def input_fields() -> Tuple[str, Dict[str, str]]:
"""
Input fields for the app
Returns:
Tuple[str, Dict[str, str]]: API key, endpoints
"""
init_session_state()
with st.sidebar:
st.header("Configuration")
api_key = st.text_input(
"Unify API Key*",
type="password",
placeholder="Enter Unify API Key"
)
models = load_models()
endpoints = {}
for key in ["LLM1", "LLM2", "Judge"]:
with st.expander(get_summary_string(key), expanded=False):
endpoint = select_model_provider(key, models)
if endpoint:
endpoints[key] = endpoint
if st.session_state["Valid Key"] and st.sidebar.toggle(
"Show Credit Balance", value=False, key='show_credit'
):
if "credits" in st.session_state:
st.sidebar.write(
f"Credit Balance: ${st.session_state['credits']:.2f}"
)
if st.button("Update Models from API"):
update_models()
time.sleep(1) # Add a delay to indicate the update is complete
st.rerun()
return api_key, endpoints