Skip to content

Commit 0f08c6c

Browse files
[Fix] Multi Auth Token issue (#51)
* Fix Multi Auth Token issue * Fix VLLM models status in UI * Fixed graph config test cases --------- Co-authored-by: Vipul Mittal <[email protected]>
1 parent 3185749 commit 0f08c6c

File tree

13 files changed

+76
-64
lines changed

13 files changed

+76
-64
lines changed

apps/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async def initialize_model_statuses():
153153
)
154154
new_model_model = st.text_input("Model")
155155
new_model_url = st.text_input("URL")
156-
new_model_api_key = st.text_input("API Key / Auth Token", type="password")
156+
new_model_auth_token = st.text_input("Auth Token", type="password")
157157
new_model_api_version = st.text_input("API Version")
158158

159159
if st.form_submit_button("➕ Add New Parameter"):
@@ -214,7 +214,7 @@ async def initialize_model_statuses():
214214
"model_type": new_model_type,
215215
"model": new_model_model,
216216
"url": new_model_url,
217-
"api_key": new_model_api_key,
217+
"auth_token": new_model_auth_token,
218218
"api_version": new_model_api_version,
219219
**new_params,
220220
"parameters": {

apps/utils.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Install them with: pip install 'sygra[ui]'"
77
)
88
import httpx
9-
from openai import AsyncAzureOpenAI
9+
from openai import AsyncAzureOpenAI, AsyncOpenAI
1010
from mistralai_azure import MistralAzure
1111
from mistralai_azure.utils.retries import RetryConfig, BackoffStrategy
1212
import aiohttp
@@ -17,7 +17,7 @@ async def check_openai_status(session, model_name, model_data):
1717
try:
1818
client = AsyncAzureOpenAI(
1919
azure_endpoint=model_data["url"],
20-
api_key=model_data["api_key"],
20+
api_key=model_data["auth_token"],
2121
api_version=model_data["api_version"],
2222
timeout=model_data.get("timeout", 10),
2323
default_headers={"Connection": "close"},
@@ -110,32 +110,24 @@ async def check_tgi_status(session, model_name, model_data):
110110

111111
async def check_vllm_status(session, model_name, model_data):
112112
try:
113-
url = model_data["url"]
114-
auth_token = model_data.get("auth_token", "").replace("Bearer ", "")
115-
model_serving_name = model_data.get("model_serving_name", model_name)
113+
client = AsyncOpenAI(
114+
base_url=model_data["url"],
115+
api_key=model_data["auth_token"],
116+
timeout=model_data.get("timeout", 10),
117+
default_headers={"Connection": "close"},
118+
)
116119

117-
async with httpx.AsyncClient(
118-
http1=True, verify=True, timeout=model_data.get("timeout", 10)
119-
) as client:
120-
headers = {
121-
"Authorization": f"Bearer {auth_token}",
122-
"Content-Type": "application/json",
123-
"Connection": "close",
124-
}
125-
payload = {
126-
"model": model_serving_name,
127-
"prompt": "Hello!",
128-
"max_tokens": 5,
129-
"temperature": 0.1,
130-
}
131-
132-
response = await client.post(url, json=payload, headers=headers)
133-
134-
if response.status_code == 200:
135-
st.session_state["active_models"].append(model_name)
136-
return model_name, True
137-
else:
138-
return model_name, False
120+
# Sending test request
121+
completion = await client.chat.completions.create(
122+
model=model_data.get("model_serving_name", model_name),
123+
messages=[{"role": "system", "content": "Hello!"}],
124+
max_tokens=5,
125+
temperature=0.1,
126+
)
127+
128+
# If no exception, model is active
129+
st.session_state["active_models"].append(model_name)
130+
return model_name, True
139131

140132
except Exception as e:
141133
return model_name, False

docs/getting_started/model_configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ SYGRA_MIXTRAL_8X7B_CHAT_TEMPLATE={% for m in messages %} ... {% endfor %}
5858
| `ssl_verify` | *(Optional)* Verify SSL certificate (default: true) |
5959
| `ssl_cert` | *(Optional)* Path to SSL certificate file |
6060
> **Note:**
61-
> - Do **not** include `url`, `auth_token`, or `api_key` in your YAML config. These are sourced from environment variables as described above.<br>
61+
> - Do **not** include `url`, `auth_token` in your YAML config. These are sourced from environment variables as described above.<br>
6262
> - If you want to set **ssl_verify** to **false** globally, you can set `ssl_verify:false` under `model_config` section in config/configuration.yaml
6363
#### Customizable Model Parameters
6464

sygra/config/models.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mistralai:
2626
gpt4:
2727
model_type: azure_openai
2828
model: gpt-4-32k
29-
# URL and api_key should be defined at .env file as SYGRA_GPT4_URL and SYGRA_GPT4_TOKEN
29+
# URL and auth_token should be defined at .env file as SYGRA_GPT4_URL and SYGRA_GPT4_TOKEN
3030
api_version: 2024-05-01-preview
3131
parameters:
3232
max_tokens: 500
@@ -35,7 +35,7 @@ gpt4:
3535
gpt-4o:
3636
model_type: azure_openai
3737
model: gpt-4o
38-
# URL and api_key should be defined at .env file as SYGRA_GPT-4O_URL and SYGRA_GPT-4O_TOKEN
38+
# URL and auth_token should be defined at .env file as SYGRA_GPT-4O_URL and SYGRA_GPT-4O_TOKEN
3939
api_version: 2024-02-15-preview
4040
parameters:
4141
max_tokens: 500
@@ -44,15 +44,15 @@ gpt-4o:
4444
gpt-4o-mini:
4545
model_type: azure_openai
4646
model: gpt-4o-mini
47-
# URL and api_key should be defined at .env file as SYGRA_GPT-4O-MINI_URL and SYGRA_GPT-4O-MINI_TOKEN
47+
# URL and auth_token should be defined at .env file as SYGRA_GPT-4O-MINI_URL and SYGRA_GPT-4O-MINI_TOKEN
4848
api_version: 2024-08-01-preview
4949
parameters:
5050
max_tokens: 5000
5151
temperature: 0.0001
5252

5353
#QWEN VL 72b deployed in vllm
5454
qwen_vl_72b:
55-
# URL and api_key should be defined at .env file as SYGRA_QWEN_VL_72B_URL and SYGRA_QWEN_VL_72B_TOKEN
55+
# URL and auth_token should be defined at .env file as SYGRA_QWEN_VL_72B_URL and SYGRA_QWEN_VL_72B_TOKEN
5656
hf_chat_template_model_id: Qwen/Qwen2.5-VL-72B-Instruct
5757
model_type: vllm
5858
parameters:
@@ -61,7 +61,7 @@ qwen_vl_72b:
6161

6262
#QWEN 32B deployed in vllm
6363
qwen3_32b:
64-
# URL and api_key should be defined at .env file as SYGRA_QWEN3_32B_URL and SYGRA_QWEN3_32B_TOKEN
64+
# URL and auth_token should be defined at .env file as SYGRA_QWEN3_32B_URL and SYGRA_QWEN3_32B_TOKEN
6565
model_serving_name: qwen3_32b
6666
hf_chat_template_model_id: Qwen/Qwen3-32B
6767
model_type: vllm

sygra/core/models/client/client_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _create_openai_azure_client(
189189
"""
190190
model_config = utils.get_updated_model_config(model_config)
191191
utils.validate_required_keys(
192-
["url", "api_key", "api_version", "model"], model_config, "model"
192+
["url", "auth_token", "api_version", "model"], model_config, "model"
193193
)
194194
ssl_verify: bool = bool(model_config.get("ssl_verify", True))
195195
ssl_cert = model_config.get("ssl_cert")

sygra/core/models/custom_models.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ def name(self) -> str:
262262

263263
def _get_model_params(self) -> ModelParams:
264264
url = self.model_config.get("url", "")
265-
if "auth_token" in self.model_config:
266-
auth_token = self.model_config.get("auth_token", "")
267-
else:
268-
auth_token = self.model_config.get("api_key", "")
265+
auth_token = self.model_config.get("auth_token", "")
269266

270267
return_url = None
271268
return_auth_token = None
@@ -391,7 +388,7 @@ def ping(self) -> int:
391388
if returns 200, its success
392389
"""
393390
url_obj = self.model_config.get("url")
394-
auth_token = self.model_config.get("auth_token") or self.model_config.get("api_key")
391+
auth_token = self.model_config.get("auth_token")
395392
if isinstance(url_obj, list):
396393
for i, url in enumerate(url_obj):
397394
token = auth_token[i] if isinstance(auth_token, list) else auth_token
@@ -957,7 +954,7 @@ class CustomOpenAI(BaseCustomModel):
957954
def __init__(self, model_config: dict[str, Any]) -> None:
958955
super().__init__(model_config)
959956
utils.validate_required_keys(
960-
["url", "api_key", "api_version", "model"], model_config, "model"
957+
["url", "auth_token", "api_version", "model"], model_config, "model"
961958
)
962959
self.model_config = model_config
963960

sygra/core/models/langgraph/sygra_base_chat_model.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ def _get_model_params(self) -> ModelParams:
143143
ModelParams: The model parameters.
144144
"""
145145
url = self._config["url"]
146-
if "auth_token" in self._config:
147-
auth_token = self._config["auth_token"]
148-
else:
149-
auth_token = self._config["api_key"]
146+
auth_token = self._config["auth_token"]
150147

151148
return_url = None
152149
return_auth_token = None

sygra/utils/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ def load_model_config(config_path: Optional[str] = None) -> Any:
7777

7878
# Look for auth token/API key in environment variables
7979
if token := os.environ.get(f"{env_prefix}_TOKEN"):
80-
# Determine whether to use auth_token or api_key based on model_type
81-
model_type = config.get("model_type", "").lower()
82-
83-
# OpenAI models use api_key, others use auth_token
84-
if model_type == "azure_openai":
85-
config["api_key"] = token
80+
# Check if it contains the list separator (indicating a list of Auth Tokens)
81+
if constants.LIST_SEPARATOR in token:
82+
# Split by the separator and filter out any empty strings
83+
token_list = [t for t in token.split(constants.LIST_SEPARATOR) if t]
84+
config["auth_token"] = token_list
8685
else:
8786
config["auth_token"] = token
8887

tests/core/graph/test_graph_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,15 @@ def mock_conditional_subgraph_config():
472472
def mock_model_config():
473473
return {
474474
"gpt-4o": {
475-
"api_key": "dummy-keys",
475+
"auth_token": "dummy-keys",
476476
"api_version": "2024-02-15-preview",
477477
"model": "gpt-4o",
478478
"model_type": "azure_openai",
479479
"parameters": {"max_tokens": 500, "temperature": 1.0},
480480
"url": "https://test-url.com/",
481481
},
482482
"gpt4": {
483-
"api_key": "dummy-keys",
483+
"auth_token": "dummy-keys",
484484
"api_version": "2024-05-01-preview",
485485
"model": "gpt-4-32k",
486486
"model_type": "azure_openai",

tests/core/models/client/test_client_factory.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_create_openai_azure_client(self, mock_validate, mock_openai_client):
205205
model_config = {
206206
"model_type": "azure_openai",
207207
"url": model_url,
208-
"api_key": auth_token,
208+
"auth_token": auth_token,
209209
"api_version": "2023-05-15",
210210
"model": "gpt-4",
211211
"timeout": 90,
@@ -216,7 +216,7 @@ def test_create_openai_azure_client(self, mock_validate, mock_openai_client):
216216

217217
# Verify the client was created with the right parameters
218218
mock_validate.assert_called_once_with(
219-
["url", "api_key", "api_version", "model"], model_config, "model"
219+
["url", "auth_token", "api_version", "model"], model_config, "model"
220220
)
221221
self.assertIsNotNone(client)
222222
mock_openai_client.assert_called_once()
@@ -246,7 +246,7 @@ def test_create_openai_azure_client_multi_url(self, mock_validate, mock_openai_c
246246
model_config = {
247247
"model_type": "azure_openai",
248248
"url": [model_url1, model_url2],
249-
"api_key": [auth_token1, auth_token2],
249+
"auth_token": [auth_token1, auth_token2],
250250
"api_version": "2023-05-15",
251251
"model": "gpt-4",
252252
"timeout": 90,
@@ -257,7 +257,7 @@ def test_create_openai_azure_client_multi_url(self, mock_validate, mock_openai_c
257257

258258
# Verify the client was created with the right parameters
259259
mock_validate.assert_called_once_with(
260-
["url", "api_key", "api_version", "model"], model_config, "model"
260+
["url", "auth_token", "api_version", "model"], model_config, "model"
261261
)
262262
self.assertIsNotNone(client)
263263
mock_openai_client.assert_called_once()
@@ -288,7 +288,7 @@ def test_create_openai_azure_client_multi_url_single_auth_token(
288288
model_config = {
289289
"model_type": "azure_openai",
290290
"url": [model_url1, model_url2],
291-
"api_key": auth_token,
291+
"auth_token": auth_token,
292292
"api_version": "2023-05-15",
293293
"model": "gpt-4",
294294
"timeout": 90,
@@ -299,7 +299,7 @@ def test_create_openai_azure_client_multi_url_single_auth_token(
299299

300300
# Verify the client was created with the right parameters
301301
mock_validate.assert_called_once_with(
302-
["url", "api_key", "api_version", "model"], model_config, "model"
302+
["url", "auth_token", "api_version", "model"], model_config, "model"
303303
)
304304
self.assertIsNotNone(client)
305305
mock_openai_client.assert_called_once()

0 commit comments

Comments
 (0)