-
Notifications
You must be signed in to change notification settings - Fork 596
[Feature] Models api #3073
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
Open
Yzc216
wants to merge
38
commits into
PaddlePaddle:develop
Choose a base branch
from
Yzc216:modelAPI
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−6
Open
[Feature] Models api #3073
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1c2e05a
add v1/models interface related
Yzc216 f421568
add model parameters
Yzc216 5446d4a
default model verification
Yzc216 a57341c
unit test
Yzc216 81ce789
check model err_msg
Yzc216 2cba65d
unit test
Yzc216 a1fe0fc
Merge branch 'develop' into modelAPI
Yzc216 0cf44da
type annotation
Yzc216 a214565
model parameter in response
Yzc216 bba5c0e
modify document description
Yzc216 ca08ee1
modify document description
Yzc216 5fb3957
Merge branch 'develop' into modelAPI
Yzc216 be13064
Merge remote-tracking branch 'upstream/develop' into modelAPI
Yzc216 b775512
Merge branch 'develop' into modelAPI
Yzc216 c1039f4
Merge branch 'develop' into modelAPI
Yzc216 08ef40f
unit test
Yzc216 7a5b686
Merge branch 'develop' into modelAPI
Yzc216 8653948
Merge branch 'develop' into modelAPI
Yzc216 a1090d6
Merge branch 'develop' into modelAPI
Yzc216 eaea619
Merge remote-tracking branch 'upstream/develop' into modelAPI
Yzc216 5ad7d0f
verification
Yzc216 99515ef
Merge branch 'develop' into modelAPI
Yzc216 748abc4
Merge branch 'develop' into modelAPI
Yzc216 ce47277
verification update
Yzc216 e2940c0
model_name
Yzc216 e5f4890
Merge branch 'develop' into modelAPI
Yzc216 24984ee
Merge branch 'develop' into modelAPI
Yzc216 bcf252e
Merge branch 'develop' into modelAPI
Yzc216 2205ec3
Merge branch 'develop' into modelAPI
Yzc216 2cc63c1
Merge branch 'develop' into modelAPI
Yzc216 4359200
Merge branch 'develop' into modelAPI
Yzc216 ac1de94
Merge branch 'develop' into modelAPI
Yzc216 25e2250
Merge branch 'develop' into modelAPI
LiqinruiG 8a282da
Merge branch 'develop' into modelAPI
LiqinruiG 7c51a55
Merge branch 'develop' into modelAPI
LiqinruiG ca68e24
pre-commit
LiqinruiG 6472ef7
update test case
LiqinruiG ab550e7
resolve conflict
LiqinruiG 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
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
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
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,96 @@ | ||
""" | ||
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License" | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
|
||
from dataclasses import dataclass | ||
from typing import List, Union | ||
|
||
from fastdeploy.entrypoints.openai.protocol import ( | ||
ErrorResponse, | ||
ModelInfo, | ||
ModelList, | ||
ModelPermission, | ||
) | ||
from fastdeploy.utils import api_server_logger, get_host_ip | ||
|
||
|
||
@dataclass | ||
class ModelPath: | ||
name: str | ||
model_path: str | ||
verification: bool = False | ||
|
||
|
||
class OpenAIServingModels: | ||
""" | ||
OpenAI-style models serving | ||
""" | ||
|
||
def __init__( | ||
self, | ||
model_paths: list[ModelPath], | ||
max_model_len: int, | ||
ips: Union[List[str], str], | ||
): | ||
self.model_paths = model_paths | ||
self.max_model_len = max_model_len | ||
self.master_ip = ips | ||
self.host_ip = get_host_ip() | ||
if self.master_ip is not None: | ||
if isinstance(self.master_ip, list): | ||
self.master_ip = self.master_ip[0] | ||
else: | ||
self.master_ip = self.master_ip.split(",")[0] | ||
|
||
def _check_master(self): | ||
if self.master_ip is None: | ||
return True | ||
if self.host_ip == self.master_ip: | ||
return True | ||
return False | ||
|
||
def is_supported_model(self, model_name) -> tuple[bool, str]: | ||
""" | ||
Check whether the specified model is supported. | ||
""" | ||
if self.model_paths[0].verification is False: | ||
return True, self.model_name() | ||
if model_name == "default": | ||
return True, self.model_name() | ||
return any(model.name == model_name for model in self.model_paths), model_name | ||
|
||
def model_name(self) -> str: | ||
""" | ||
Returns the current model name. | ||
""" | ||
return self.model_paths[0].name | ||
|
||
async def list_models(self) -> ModelList: | ||
""" | ||
Show available models. | ||
""" | ||
if not self._check_master(): | ||
err_msg = ( | ||
f"Only master node can accept models request, please send request to master node: {self.pod_ips[0]}" | ||
) | ||
api_server_logger.error(err_msg) | ||
return ErrorResponse(message=err_msg, code=400) | ||
model_infos = [ | ||
ModelInfo( | ||
id=model.name, max_model_len=self.max_model_len, root=model.model_path, permission=[ModelPermission()] | ||
) | ||
for model in self.model_paths | ||
] | ||
return ModelList(data=model_infos) |
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,51 @@ | ||
import asyncio | ||
import unittest | ||
|
||
from fastdeploy.entrypoints.openai.protocol import ModelInfo, ModelList | ||
from fastdeploy.entrypoints.openai.serving_models import ModelPath, OpenAIServingModels | ||
from fastdeploy.utils import get_host_ip | ||
|
||
MODEL_NAME = "baidu/ERNIE-4.5-0.3B-PT" | ||
MODEL_PATHS = [ModelPath(name=MODEL_NAME, model_path=MODEL_NAME)] | ||
MAX_MODEL_LEN = 2048 | ||
|
||
|
||
async def _async_serving_models_init() -> OpenAIServingModels: | ||
"""异步初始化 OpenAIServingModels 实例""" | ||
return OpenAIServingModels( | ||
model_paths=MODEL_PATHS, | ||
max_model_len=MAX_MODEL_LEN, | ||
ips=get_host_ip(), | ||
) | ||
|
||
|
||
class TestOpenAIServingModels(unittest.TestCase): | ||
"""测试 OpenAIServingModels 的 unittest 版本""" | ||
|
||
def test_serving_model_name(self): | ||
"""测试模型名称获取""" | ||
# 通过 asyncio.run() 执行异步初始化 | ||
serving_models = asyncio.run(_async_serving_models_init()) | ||
self.assertEqual(serving_models.model_name(), MODEL_NAME) | ||
|
||
def test_list_models(self): | ||
"""测试模型列表功能""" | ||
serving_models = asyncio.run(_async_serving_models_init()) | ||
|
||
# 通过 asyncio.run() 执行异步方法 | ||
result = asyncio.run(serving_models.list_models()) | ||
|
||
# 验证返回类型和内容 | ||
self.assertIsInstance(result, ModelList) | ||
self.assertEqual(len(result.data), 1) | ||
|
||
model_info = result.data[0] | ||
self.assertIsInstance(model_info, ModelInfo) | ||
self.assertEqual(model_info.id, MODEL_NAME) | ||
self.assertEqual(model_info.max_model_len, MAX_MODEL_LEN) | ||
self.assertEqual(model_info.root, MODEL_PATHS[0].model_path) | ||
self.assertEqual(result.object, "list") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.