-
Notifications
You must be signed in to change notification settings - Fork 418
♻️ refactor me model module. #1154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c6f5cf
♻️ refactor me model module.
5054f92
♻️ refactor me model module.
fc9452d
♻️ refactor me model module.
df6cc0d
♻️ refactor me model module.
d8cb2d9
♻️ refactor me model module.
94564b0
Merge branch 'develop' into jpl/jpl_0829
porkpink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,122 +1,83 @@ | ||
| import asyncio | ||
| import logging | ||
| from http import HTTPStatus | ||
|
|
||
| import aiohttp | ||
| from fastapi import APIRouter, Query | ||
| from fastapi.responses import JSONResponse | ||
|
|
||
| from consts.const import MODEL_ENGINE_APIKEY, MODEL_ENGINE_HOST | ||
| from consts.model import ModelConnectStatusEnum, ModelResponse | ||
| from services.model_health_service import check_me_model_connectivity | ||
| from consts.exceptions import TimeoutException, NotFoundException, MEConnectionException | ||
| from consts.model import ModelConnectStatusEnum | ||
| from services.me_model_management_service import get_me_models_impl | ||
| from services.model_health_service import check_me_connectivity_impl | ||
|
|
||
| router = APIRouter(prefix="/me") | ||
|
|
||
|
|
||
| @router.get("/model/list", response_model=ModelResponse) | ||
| @router.get("/model/list") | ||
| async def get_me_models( | ||
| type: str = Query( | ||
| default="", description="Model type: embed/chat/rerank"), | ||
| timeout: int = Query( | ||
| default=2, description="Request timeout in seconds") | ||
| ): | ||
| """ | ||
| Get list of models from model engine API | ||
| """ | ||
| try: | ||
| headers = { | ||
| 'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}', | ||
| } | ||
|
|
||
| async with aiohttp.ClientSession( | ||
| timeout=aiohttp.ClientTimeout(total=timeout), | ||
| connector=aiohttp.TCPConnector(verify_ssl=False) | ||
| ) as session: | ||
| async with session.get( | ||
| f"{MODEL_ENGINE_HOST}/open/router/v1/models", | ||
| headers=headers | ||
| ) as response: | ||
| response.raise_for_status() | ||
| result_data = await response.json() | ||
| result: list = result_data['data'] | ||
|
|
||
| # Type filtering | ||
| filtered_result = [] | ||
| if type: | ||
| for data in result: | ||
| if data['type'] == type: | ||
| filtered_result.append(data) | ||
| if not filtered_result: | ||
| result_types = set(data['type'] for data in result) | ||
| return ModelResponse( | ||
| code=404, | ||
| message=f"No models found with type '{type}'. Available types: {result_types}", | ||
| data=[] | ||
| ) | ||
| else: | ||
| filtered_result = result | ||
|
|
||
| return ModelResponse( | ||
| code=200, | ||
| message="Successfully retrieved", | ||
| data=filtered_result | ||
| ) | ||
|
|
||
| except asyncio.TimeoutError: | ||
| return ModelResponse( | ||
| code=408, | ||
| message="Request timeout", | ||
| data=[] | ||
| filtered_result = await get_me_models_impl(timeout=timeout, type=type) | ||
| return JSONResponse( | ||
| status_code=HTTPStatus.OK, | ||
| content={ | ||
| "message": "Successfully retrieved", | ||
| "data": filtered_result | ||
| } | ||
| ) | ||
| except TimeoutException as e: | ||
| logging.error(f"Request me model timeout: {str(e)}") | ||
| return JSONResponse(status_code=HTTPStatus.REQUEST_TIMEOUT, content={ | ||
| "message": f"Request me model timeout: {str(e)}", | ||
| "data": [] | ||
| }) | ||
| except NotFoundException as e: | ||
| logging.error(f"Request me model not found: {str(e)}") | ||
| return JSONResponse(status_code=HTTPStatus.NOT_FOUND, content={ | ||
| "message": f"Request me model not found: {str(e)}", | ||
| "data": [] | ||
| }) | ||
| except Exception as e: | ||
| return ModelResponse( | ||
| code=500, | ||
| message=f"Failed to get model list: {str(e)}", | ||
| data=[] | ||
| ) | ||
| logging.error(f"Failed to get model list: {str(e)}") | ||
| return JSONResponse(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content={ | ||
| "message": f"Failed to get model list: {str(e)}", | ||
| "data": [] | ||
| }) | ||
|
|
||
|
|
||
| @router.get("/healthcheck", response_model=ModelResponse) | ||
| @router.get("/healthcheck") | ||
| async def check_me_connectivity(timeout: int = Query(default=2, description="Timeout in seconds")): | ||
| """ | ||
| Health check from model engine API | ||
| """ | ||
| try: | ||
| headers = {'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}'} | ||
|
|
||
| async with aiohttp.ClientSession( | ||
| timeout=aiohttp.ClientTimeout(total=timeout), | ||
| connector=aiohttp.TCPConnector(ssl=False) | ||
| ) as session: | ||
| try: | ||
| async with session.get( | ||
| f"{MODEL_ENGINE_HOST}/open/router/v1/models", | ||
| headers=headers | ||
| ) as response: | ||
| if response.status == 200: | ||
| return ModelResponse( | ||
| code=200, | ||
| message="Connection successful", | ||
| data={"status": "Connected", "desc": "Connection successful", | ||
| "connect_status": ModelConnectStatusEnum.AVAILABLE.value} | ||
| ) | ||
| else: | ||
| return ModelResponse( | ||
| code=response.status, | ||
| message=f"Connection failed, error code: {response.status}", | ||
| data={"status": "Disconnected", "desc": f"Connection failed, error code: {response.status}", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value} | ||
| ) | ||
| except asyncio.TimeoutError: | ||
| return ModelResponse( | ||
| code=408, | ||
| message="Connection timeout", | ||
| data={"status": "Disconnected", "desc": "Connection timeout", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value} | ||
| ) | ||
|
|
||
| except Exception as e: | ||
| return ModelResponse( | ||
| code=500, | ||
| message=f"Unknown error occurred: {str(e)}", | ||
| data={"status": "Disconnected", "desc": f"Unknown error occurred: {str(e)}", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value} | ||
| await check_me_connectivity_impl(timeout) | ||
| return JSONResponse( | ||
| status_code=HTTPStatus.OK, | ||
| content={ | ||
| "status": "Connected", | ||
| "desc": "Connection successful.", | ||
| "connect_status": ModelConnectStatusEnum.AVAILABLE.value | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| @router.get("/model/healthcheck", response_model=ModelResponse) | ||
| async def check_me_model_healthcheck( | ||
| model_name: str = Query(..., description="Model name to check") | ||
| ): | ||
| return await check_me_model_connectivity(model_name) | ||
| except MEConnectionException as e: | ||
| logging.error(f"Request me model connectivity failed: {str(e)}") | ||
| return JSONResponse(status_code=HTTPStatus.SERVICE_UNAVAILABLE, content={"status": "Disconnected", | ||
| "desc": f"Connection failed.", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}) | ||
| except TimeoutException as e: | ||
| logging.error(f"Request me model connectivity timeout: {str(e)}") | ||
| return JSONResponse(status_code=HTTPStatus.REQUEST_TIMEOUT, content={"status": "Disconnected", | ||
| "desc": "Connection timeout.", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}) | ||
| except Exception as e: | ||
| logging.error(f"Unknown error occurred: {str(e)}.") | ||
| return JSONResponse(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content={"status": "Disconnected", | ||
| "desc": f"Unknown error occurred: {str(e)}", | ||
| "connect_status": ModelConnectStatusEnum.UNAVAILABLE.value}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import asyncio | ||
| from typing import List | ||
|
|
||
| import aiohttp | ||
|
|
||
| from consts.const import MODEL_ENGINE_APIKEY, MODEL_ENGINE_HOST | ||
| from consts.exceptions import TimeoutException, NotFoundException | ||
|
|
||
|
|
||
| async def get_me_models_impl(timeout: int = 2, type: str = "") -> List: | ||
| """ | ||
| Fetches a list of models from the model engine API with response formatting. | ||
| Parameters: | ||
| timeout (int): The total timeout for the request in seconds. | ||
| type (str): The type of model to filter for. If empty, returns all models. | ||
| Returns: | ||
| - filtered_result: List of model data dictionaries | ||
| """ | ||
| try: | ||
| headers = { | ||
| 'Authorization': f'Bearer {MODEL_ENGINE_APIKEY}', | ||
| } | ||
| async with aiohttp.ClientSession( | ||
| timeout=aiohttp.ClientTimeout(total=timeout), | ||
| connector=aiohttp.TCPConnector(verify_ssl=False) | ||
| ) as session: | ||
| async with session.get( | ||
| f"{MODEL_ENGINE_HOST}/open/router/v1/models", | ||
| headers=headers | ||
| ) as response: | ||
| response.raise_for_status() | ||
| result_data = await response.json() | ||
| result: list = result_data['data'] | ||
|
|
||
| # Type filtering | ||
| filtered_result = [] | ||
| if type: | ||
| for data in result: | ||
| if data['type'] == type: | ||
| filtered_result.append(data) | ||
| if not filtered_result: | ||
| result_types = set(data['type'] for data in result) | ||
| raise NotFoundException( | ||
| f"No models found with type '{type}'. Available types: {result_types}.") | ||
| else: | ||
| filtered_result = result | ||
|
|
||
| return filtered_result | ||
| except asyncio.TimeoutError: | ||
| raise TimeoutException("Request timeout.") | ||
| except Exception as e: | ||
| raise Exception(f"Failed to get model list: {str(e)}.") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.