Skip to content

Commit 6c6f5cf

Browse files
author
jiangpeiling
committed
♻️ refactor me model module.
1 parent 1e302e8 commit 6c6f5cf

File tree

7 files changed

+1106
-458
lines changed

7 files changed

+1106
-458
lines changed

.github/workflows/auto-unit-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ on:
1717
- 'backend/**'
1818
- 'sdk/**'
1919
- 'test/**'
20+
- '.github/workflows/**'
2021
push:
2122
branches: [develop]
2223
paths:
2324
- 'backend/**'
2425
- 'sdk/**'
2526
- 'test/**'
27+
- '.github/workflows/**'
2628

2729
jobs:
2830
test:

backend/apps/me_model_managment_app.py

Lines changed: 32 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,52 @@
1-
import asyncio
1+
from http import HTTPStatus
22

3-
import aiohttp
43
from fastapi import APIRouter, Query
4+
from fastapi.responses import JSONResponse
55

6-
from consts.const import MODEL_ENGINE_APIKEY, MODEL_ENGINE_HOST
7-
from consts.model import ModelConnectStatusEnum, ModelResponse
8-
from services.model_health_service import check_me_model_connectivity
6+
from consts.model import ModelResponse
7+
from services.me_model_management_service import get_me_models_impl
8+
from services.model_health_service import check_me_model_connectivity, check_me_connectivity_impl
99

1010
router = APIRouter(prefix="/me")
1111

1212

13-
@router.get("/model/list", response_model=ModelResponse)
13+
@router.get("/model/list")
1414
async def get_me_models(
1515
type: str = Query(
1616
default="", description="Model type: embed/chat/rerank"),
1717
timeout: int = Query(
1818
default=2, description="Request timeout in seconds")
1919
):
20-
try:
21-
headers = {
22-
'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}',
20+
"""
21+
Get list of models from model engine API
22+
"""
23+
# Call service function to get business logic result
24+
code, message, data = await get_me_models_impl(timeout=timeout, type=type)
25+
return JSONResponse(
26+
status_code=HTTPStatus.OK,
27+
content={
28+
"code": code,
29+
"message": message,
30+
"data": data
2331
}
32+
)
2433

25-
async with aiohttp.ClientSession(
26-
timeout=aiohttp.ClientTimeout(total=timeout),
27-
connector=aiohttp.TCPConnector(verify_ssl=False)
28-
) as session:
29-
async with session.get(
30-
f"{MODEL_ENGINE_HOST}/open/router/v1/models",
31-
headers=headers
32-
) as response:
33-
response.raise_for_status()
34-
result_data = await response.json()
35-
result: list = result_data['data']
3634

37-
# Type filtering
38-
filtered_result = []
39-
if type:
40-
for data in result:
41-
if data['type'] == type:
42-
filtered_result.append(data)
43-
if not filtered_result:
44-
result_types = set(data['type'] for data in result)
45-
return ModelResponse(
46-
code=404,
47-
message=f"No models found with type '{type}'. Available types: {result_types}",
48-
data=[]
49-
)
50-
else:
51-
filtered_result = result
52-
53-
return ModelResponse(
54-
code=200,
55-
message="Successfully retrieved",
56-
data=filtered_result
57-
)
58-
59-
except asyncio.TimeoutError:
60-
return ModelResponse(
61-
code=408,
62-
message="Request timeout",
63-
data=[]
64-
)
65-
except Exception as e:
66-
return ModelResponse(
67-
code=500,
68-
message=f"Failed to get model list: {str(e)}",
69-
data=[]
70-
)
71-
72-
73-
@router.get("/healthcheck", response_model=ModelResponse)
35+
@router.get("/healthcheck")
7436
async def check_me_connectivity(timeout: int = Query(default=2, description="Timeout in seconds")):
75-
try:
76-
headers = {'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}'}
77-
78-
async with aiohttp.ClientSession(
79-
timeout=aiohttp.ClientTimeout(total=timeout),
80-
connector=aiohttp.TCPConnector(ssl=False)
81-
) as session:
82-
try:
83-
async with session.get(
84-
f"{MODEL_ENGINE_HOST}/open/router/v1/models",
85-
headers=headers
86-
) as response:
87-
if response.status == 200:
88-
return ModelResponse(
89-
code=200,
90-
message="Connection successful",
91-
data={"status": "Connected", "desc": "Connection successful",
92-
"connect_status": ModelConnectStatusEnum.AVAILABLE.value}
93-
)
94-
else:
95-
return ModelResponse(
96-
code=response.status,
97-
message=f"Connection failed, error code: {response.status}",
98-
data={"status": "Disconnected", "desc": f"Connection failed, error code: {response.status}",
99-
"connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}
100-
)
101-
except asyncio.TimeoutError:
102-
return ModelResponse(
103-
code=408,
104-
message="Connection timeout",
105-
data={"status": "Disconnected", "desc": "Connection timeout",
106-
"connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}
107-
)
108-
109-
except Exception as e:
110-
return ModelResponse(
111-
code=500,
112-
message=f"Unknown error occurred: {str(e)}",
113-
data={"status": "Disconnected", "desc": f"Unknown error occurred: {str(e)}",
114-
"connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}
115-
)
37+
"""
38+
Health check from model engine API
39+
"""
40+
# Call service function to health check
41+
code, message, data = await check_me_connectivity_impl(timeout)
42+
return JSONResponse(
43+
status_code=HTTPStatus.OK,
44+
content={
45+
"code": code,
46+
"message": message,
47+
"data": data
48+
}
49+
)
11650

11751

11852
@router.get("/model/healthcheck", response_model=ModelResponse)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
from http import HTTPStatus
3+
from typing import List, Dict
4+
5+
import aiohttp
6+
7+
from consts.const import MODEL_ENGINE_APIKEY, MODEL_ENGINE_HOST
8+
9+
10+
async def get_me_models_impl(timeout: int = 2, type: str = "") -> tuple[int, str, List[Dict]]:
11+
"""
12+
Fetches a list of models from the model engine API with response formatting.
13+
Parameters:
14+
timeout (int): The total timeout for the request in seconds.
15+
type (str): The type of model to filter for. If empty, returns all models.
16+
Returns:
17+
tuple: (code, message, data) where:
18+
- code: HTTP status code
19+
- message: Response message
20+
- data: List of model data dictionaries
21+
"""
22+
try:
23+
headers = {
24+
'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}',
25+
}
26+
async with aiohttp.ClientSession(
27+
timeout=aiohttp.ClientTimeout(total=timeout),
28+
connector=aiohttp.TCPConnector(verify_ssl=False)
29+
) as session:
30+
async with session.get(
31+
f"{MODEL_ENGINE_HOST}/open/router/v1/models",
32+
headers=headers
33+
) as response:
34+
response.raise_for_status()
35+
result_data = await response.json()
36+
result: list = result_data['data']
37+
38+
# Type filtering
39+
filtered_result = []
40+
if type:
41+
for data in result:
42+
if data['type'] == type:
43+
filtered_result.append(data)
44+
if not filtered_result:
45+
result_types = set(data['type'] for data in result)
46+
return HTTPStatus.NOT_FOUND, f"No models found with type '{type}'. Available types: {result_types}.", []
47+
else:
48+
filtered_result = result
49+
50+
return HTTPStatus.OK, "Successfully retrieved", filtered_result
51+
except asyncio.TimeoutError:
52+
return HTTPStatus.REQUEST_TIMEOUT, "Request timeout", []
53+
except Exception as e:
54+
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Failed to get model list: {str(e)}", []

0 commit comments

Comments
 (0)