|
1 | | -import asyncio |
| 1 | +from http import HTTPStatus |
2 | 2 |
|
3 | | -import aiohttp |
4 | 3 | from fastapi import APIRouter, Query |
| 4 | +from fastapi.responses import JSONResponse |
5 | 5 |
|
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, ModelConnectStatusEnum |
| 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 |
9 | 9 |
|
10 | 10 | router = APIRouter(prefix="/me") |
11 | 11 |
|
12 | 12 |
|
13 | | -@router.get("/model/list", response_model=ModelResponse) |
| 13 | +@router.get("/model/list") |
14 | 14 | async def get_me_models( |
15 | 15 | type: str = Query( |
16 | 16 | default="", description="Model type: embed/chat/rerank"), |
17 | 17 | timeout: int = Query( |
18 | 18 | default=2, description="Request timeout in seconds") |
19 | 19 | ): |
20 | 20 | try: |
21 | | - headers = { |
22 | | - 'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}', |
23 | | - } |
24 | | - |
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'] |
36 | | - |
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=[] |
| 21 | + filtered_result = await get_me_models_impl(timeout, type) |
| 22 | + return JSONResponse( |
| 23 | + status_code=HTTPStatus.OK, |
| 24 | + content={ |
| 25 | + "code": HTTPStatus.OK, |
| 26 | + "message": "Successfully retrieved", |
| 27 | + "data": filtered_result |
| 28 | + } |
64 | 29 | ) |
65 | 30 | except Exception as e: |
66 | | - return ModelResponse( |
67 | | - code=500, |
68 | | - message=f"Failed to get model list: {str(e)}", |
69 | | - data=[] |
| 31 | + return JSONResponse( |
| 32 | + status_code=HTTPStatus.OK, |
| 33 | + content={ |
| 34 | + "code": HTTPStatus.INTERNAL_SERVER_ERROR, |
| 35 | + "message": f"Failed to get model list: {str(e)}", |
| 36 | + "data": None |
| 37 | + } |
70 | 38 | ) |
71 | 39 |
|
72 | 40 |
|
73 | | -@router.get("/healthcheck", response_model=ModelResponse) |
| 41 | +@router.get("/healthcheck") |
74 | 42 | async def check_me_connectivity(timeout: int = Query(default=2, description="Timeout in seconds")): |
75 | 43 | 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 | | - |
| 44 | + message, code, status_data = await check_me_connectivity_impl(timeout) |
| 45 | + if code == HTTPStatus.OK: |
| 46 | + return JSONResponse( |
| 47 | + status_code=HTTPStatus.OK, |
| 48 | + content={ |
| 49 | + "code": HTTPStatus.OK, |
| 50 | + "message": message, |
| 51 | + "data": status_data |
| 52 | + } |
| 53 | + ) |
| 54 | + else: |
| 55 | + return JSONResponse( |
| 56 | + status_code=HTTPStatus.OK, |
| 57 | + content={ |
| 58 | + "code": code, |
| 59 | + "message": message, |
| 60 | + "data": status_data |
| 61 | + } |
| 62 | + ) |
109 | 63 | 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} |
| 64 | + return JSONResponse( |
| 65 | + status_code=HTTPStatus.OK, |
| 66 | + content={ |
| 67 | + "code": HTTPStatus.INTERNAL_SERVER_ERROR, |
| 68 | + "message": f"Connection failed: {str(e)}", |
| 69 | + "data": { |
| 70 | + "status": "Disconnected", |
| 71 | + "desc": f"Connection failed: {str(e)}", |
| 72 | + "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value |
| 73 | + } |
| 74 | + } |
115 | 75 | ) |
116 | 76 |
|
117 | 77 |
|
|
0 commit comments