-
Notifications
You must be signed in to change notification settings - Fork 118
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
增加模型列表获取的SDK #29
增加模型列表获取的SDK #29
Changes from 6 commits
1fe7704
16a98cb
f821fec
3666fe5
a009a0b
afb0300
fc8cde8
7d97965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,49 @@ | |
# 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 typing import List | ||
|
||
|
||
from appbuilder.utils.model_util import GetModelListRequest, Models, map_model_name | ||
|
||
|
||
def utils_get_user_agent(): | ||
return 'appbuilder-sdk-python/{}'.format("__version__") | ||
|
||
|
||
def get_model_list(secret_key: str = "", apiTypefilter: List[str] = [], is_available: bool = False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 函数入参apiTypefilter,可以统一写成下划线 |
||
""" | ||
返回用户的模型列表。 | ||
|
||
参数: | ||
secret_key(str,可选): 用户鉴权token, 默认从环境变量中获取: os.getenv("APPBUILDER_TOKEN", "")。 | ||
apiTypefilter(List[str], 可选): 根据apiType过滤,["chat", "completions", "embeddings", "text2image"],不填包括所有的。 | ||
is_available(bool, 可选): 返回可用模型列表, 默认返回所有模型。 | ||
|
||
返回: | ||
List[str]: 模型列表。 | ||
""" | ||
request = GetModelListRequest() | ||
request.apiTypefilter = apiTypefilter | ||
model = Models(secret_key=secret_key) | ||
response = model.list(request) | ||
models = [] | ||
if is_available: | ||
for common_model in response.result.common: | ||
if common_model.chargeStatus in ["OPENED", "FREE"]: | ||
mapped_name = map_model_name(common_model.name) | ||
models.append(mapped_name) | ||
|
||
for custom_model in response.result.custom: | ||
if custom_model.chargeStatus in ["OPENED", "FREE"]: | ||
mapped_name = map_model_name(custom_model.name) | ||
models.append(mapped_name) | ||
return models | ||
else: | ||
for common_model in response.result.common: | ||
mapped_name = map_model_name(common_model.name) | ||
models.append(mapped_name) | ||
|
||
for custom_model in response.result.custom: | ||
mapped_name = map_model_name(custom_model.name) | ||
models.append(mapped_name) | ||
return models | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以使用一个迭代器,将通用模型与定制化模型连接起来,再进行遍历 import itertools
for model in itertools.chain(response.result.common, response.result.custom):
if is_available and common_model.chargeStatus not in ["OPENED", "FREE"]:
continue
mapped_name = map_model_name(common_model.name)
models.append(mapped_name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import unittest | ||
|
||
import appbuilder | ||
from appbuilder.utils.model_util import GetModelListRequest, Models, GetModelListResponse | ||
|
||
|
||
class TestModels(unittest.TestCase): | ||
def setUp(self): | ||
""" | ||
设置环境变量。 | ||
|
||
Args: | ||
None | ||
|
||
Returns: | ||
None. | ||
""" | ||
self.model = Models() | ||
|
||
def get_model_list(self): | ||
""" | ||
get_model_list方法单测 | ||
|
||
Args: | ||
None | ||
|
||
Returns: | ||
None | ||
|
||
""" | ||
response = appbuilder.get_model_list(apiTypefilter=["chat"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.assertIsNotNone(response) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只验证 返回是否为空, 需要进一步校验返回值的正确性 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apiTypefilter=["chat"], filter的chat是什么含义 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 就是只返回千帆的对话Chat模型 |
||
|
||
def test_list(self): | ||
""" | ||
list方法单测 | ||
|
||
Args: | ||
None | ||
|
||
Returns: | ||
None | ||
|
||
""" | ||
|
||
request = GetModelListRequest() | ||
response = self.model.list(request) | ||
self.assertIsNotNone(response) | ||
self.assertIsInstance(response, GetModelListResponse) | ||
|
||
def test_check_service_error(self): | ||
""" | ||
check_service_error方法单测 | ||
|
||
Args: | ||
None | ||
|
||
Returns: | ||
None | ||
|
||
""" | ||
data = {'error_msg': 'Error', 'error_code': 1} | ||
request_id = "request_id" | ||
with self.assertRaises(appbuilder.AppBuilderServerException): | ||
self.model._check_service_error(request_id, data) | ||
data = {'error_msg': 'No Error', 'error_code': 0} | ||
self.assertIsNone(self.model._check_service_error(request_id, data)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_model_list函数的返回类型是不是也可以说明下