Bento 和模型 API

此页面包含 Bento 和模型的管理 API。

管理 Bentos

bentoml.list(tag: Tag | str | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) t.List[Bento][source]
bentoml.get(tag: Tag | str, *, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) Bento[source]
bentoml.delete(tag: Tag | str, *, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>)[source]
bentoml.export_bento(tag: Tag | str, path: str, output_format: str | None = None, *, protocol: str | None = None, user: str | None = None, passwd: str | None = None, params: dict[str, str] | None = None, subpath: str | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) str[source]

导出 Bento。

要将 Bento 导出到 S3,必须安装带有 aws 附加功能的 BentoML

» pip install bentoml[aws]

示例

# exports 'my_bento' to '/path/to/folder/my_bento-version.bento' in BentoML's default format
bentoml.export_bento('my_bento:latest', '/path/to/folder')
# note that folders can only be passed if exporting to the local filesystem; otherwise the
# full path, including the desired filename, must be passed

# exports 'my_bento' to '/path/to/folder/my_bento.bento' in BentoML's default format
bentoml.export_bento('my_bento:latest', '/path/to/folder/my_bento')
bentoml.export_bento('my_bento:latest', '/path/to/folder/my_bento.bento')

# exports 'my_bento' to '/path/to/folder/my_bento.tar.gz' in gzip format
# currently supported formats are tar.gz ('gz'), tar.xz ('xz'), tar.bz2 ('bz2'), and zip
bentoml.export_bento('my_bento:latest', '/path/to/folder/my_bento.tar.gz')
# outputs a gzipped tarfile as 'my_bento.ext'
bentoml.export_bento('my_bento:latest', '/path/to/folder/my_bento.ext', 'gz')

# exports 'my_bento' to '/path/to/folder/my_bento/' as a folder
bentoml.export_bento('my_bento:latest', '/path/to/folder/my_bento', 'folder')

# exports 'my_bento' to the S3 bucket 'my_bucket' as 'folder/my_bento-version.bento'
bentoml.export_bento('my_bento:latest', 's3://my_bucket/folder')
bentoml.export_bento('my_bento:latest', 'my_bucket/folder', protocol='s3')
bentoml.export_bento('my_bento:latest', 'my_bucket', protocol='s3', subpath='folder')
bentoml.export_bento('my_bento:latest', 'my_bucket', protocol='s3', subpath='folder',
                     user='<AWS access key>', passwd='<AWS secret key>',
                     params={'acl': 'public-read', 'cache-control': 'max-age=2592000,public'})

有关每个关键字参数(protocoluserpasswdparamssubpath)含义的更全面描述,请参阅 FS URL 文档。

参数:
  • tag – 要导出的 Bento 的标签

  • path – 可以是以下两种之一:* 本地文件系统上的文件夹 * FS URL。例如,'s3://my_bucket/folder/my_bento.bento'

  • protocol – (专家) 导出时使用的 FS 协议。一些示例协议包括 'ftp''s3''userdata'

  • user – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • passwd – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • params – (专家) 要传递给用于导出的 FS 的参数映射,例如用于设置 FTP 代理的 {'proxy': 'myproxy.net'}

  • subpath – (专家) Bento 应导出到 FS 内部的路径

  • _bento_store – 将创建的 Bento 保存到此 BentoStore

返回值:

Bento 导出到的路径表示。如果导出到本地文件系统,

这将是导出 Bento 的 OS 路径。否则,它将是一个 FS URL。

返回类型:

str

bentoml.import_bento(path: str, input_format: str | None = None, *, protocol: str | None = None, user: str | None = None, passwd: str | None = None, params: t.Optional[t.Dict[str, str]] = None, subpath: str | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) Bento[source]

导入 Bento。

示例

# imports 'my_bento' from '/path/to/folder/my_bento.bento'
bentoml.import_bento('/path/to/folder/my_bento.bento')

# imports 'my_bento' from '/path/to/folder/my_bento.tar.gz'
# currently supported formats are tar.gz ('gz'),
# tar.xz ('xz'), tar.bz2 ('bz2'), and zip
bentoml.import_bento('/path/to/folder/my_bento.tar.gz')
# treats 'my_bento.ext' as a gzipped tarfile
bentoml.import_bento('/path/to/folder/my_bento.ext', 'gz')

# imports 'my_bento', which is stored as an
# uncompressed folder, from '/path/to/folder/my_bento/'
bentoml.import_bento('/path/to/folder/my_bento', 'folder')

# imports 'my_bento' from the S3 bucket 'my_bucket',
# path 'folder/my_bento.bento'
# requires `fs-s3fs <https://pypi.ac.cn/project/fs-s3fs/>`_
bentoml.import_bento('s3://my_bucket/folder/my_bento.bento')
bentoml.import_bento('my_bucket/folder/my_bento.bento', protocol='s3')
bentoml.import_bento('my_bucket', protocol='s3',
                     subpath='folder/my_bento.bento')
bentoml.import_bento('my_bucket', protocol='s3',
                     subpath='folder/my_bento.bento',
                     user='<AWS access key>', passwd='<AWS secret key>',
                     params={'acl': 'public-read',
                             'cache-control': 'max-age=2592000,public'})

有关每个关键字参数(protocoluserpasswd

paramssubpath)含义的更全面描述,请参阅

FS URL 文档.

参数:
  • tag – 要导出的 Bento 的标签

  • path –

    可以是以下两种之一:* 本地文件系统上的文件夹 * FS URL,

    例如 's3://my_bucket/folder/my_bento.bento'

  • protocol – (专家) 导出时使用的 FS 协议。一些示例协议包括 'ftp''s3''userdata'

  • user – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • passwd – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • params – (专家) 要传递给用于导出的 FS 的参数映射,例如用于设置 FTP 代理的 {'proxy': 'myproxy.net'}

  • subpath – (专家) Bento 应导出到 FS 内部的路径

  • _bento_store – 将 Bento 保存到的 Bento 存储

返回值:

导入的 Bento

返回类型:

Bento

bentoml.build(service: str, *, name: str | None = None, labels: dict[str, str] | None = None, description: str | None = None, include: t.List[str] | None = None, exclude: t.List[str] | None = None, envs: t.List[BentoEnvSchema] | None = None, docker: DockerOptions | dict[str, t.Any] | None = None, python: PythonOptions | dict[str, t.Any] | None = None, conda: CondaOptions | dict[str, t.Any] | None = None, models: t.List[ModelSpec | str | dict[str, t.Any]] | None = None, version: str | None = None, build_ctx: str | None = None, platform: str | None = None, args: dict[str, t.Any] | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) Bento[source]

用于构建 Bento 的面向用户 API。可用的构建选项与有效 ‘bentofile.yaml’ 文件中的键相同。

此 API 将不遵守任何 ‘bentofile.yaml’ 文件。构建选项应通过函数调用参数提供。

参数:
  • service – 用于查找 bentoml.Service 实例构建目标的导入字符串

  • labels – 用于携带上下文信息的可选不可变标签

  • description – 可选的 markdown 格式描述字符串

  • include – 文件路径和模式列表,指定要包含在 Bento 中的文件,默认情况下包含 build_ctx 下的所有文件,除了 exclude 参数中排除的文件或给定目录中的 .bentoignore 文件

  • exclude – 要从最终 Bento 归档中排除的文件路径和模式列表

  • docker – 用于配置 Bento 容器化过程的字典,详细信息请参阅 bentoml._internal.bento.build_config.DockerOptions

  • python – 用于配置 Bento Python 依赖项的字典,详细信息请参阅 bentoml._internal.bento.build_config.PythonOptions

  • conda – 用于配置 Bento Conda 依赖项的字典,详细信息请参阅 bentoml._internal.bento.build_config.CondaOptions

  • version – 覆盖默认自动生成的版本字符串

  • build_ctx – 构建上下文目录,用作

  • platform – 构建平台

返回值:

表示保存在 BentoStore 中的具体化 Bento 的 Bento 实例

返回类型:

Bento

示例

import bentoml

bentoml.build(
    service="fraud_detector.py:svc",
    version="any_version_label",  # override default version generator
    description=open("README.md").read(),
    include=['*'],
    exclude=[], # files to exclude can also be specified with a .bentoignore file
    labels={
        "foo": "bar",
        "team": "abc"
    },
    python=dict(
        packages=["tensorflow", "numpy"],
        # requirements_txt="./requirements.txt",
        index_url="http://<api token>:@mycompany.com/pypi/simple",
        trusted_host=["mycompany.com"],
        find_links=['thirdparty..'],
        extra_index_url=["..."],
        pip_args="ANY ADDITIONAL PIP INSTALL ARGS",
        wheels=["./wheels/*"],
        lock_packages=True,
    ),
    docker=dict(
        distro="amazonlinux2",
        setup_script="setup_docker_container.sh",
        python_version="3.8",
    ),
)
bentoml.bentos.build_bentofile(bentofile: str | None = None, *, service: str | None = None, name: str | None = None, version: str | None = None, labels: dict[str, str] | None = None, build_ctx: str | None = None, platform: str | None = None, bare: bool = False, reload: bool = False, args: dict[str, t.Any] | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>) Bento[source]

基于 bentofile.yaml 文件中指定的选项构建 Bento。

默认情况下,此函数将在当前工作目录中查找 bentofile.yaml 文件。

参数:
  • bentofile – 构建配置 yaml 文件的文件路径

  • version – 覆盖默认自动生成的版本字符串

  • build_ctx – 构建上下文目录,用作

  • bare – 是否构建不复制文件的 Bento

  • reload – 是否重新加载服务

返回值:

表示保存在 BentoStore 中的具体化 Bento 的 Bento 实例

返回类型:

Bento

加载模型

class bentoml.models.BentoModel(tag: Tag | str)[source]

继承自:Model[Model]

对 BentoML 模型的引用。

参数:

tag (Tag) – 模型标签。

返回值:

Bento 模型对象。

返回类型:

Model

classmethod from_info(info: BentoModelInfo) BentoModel[source]

从模型信息返回模型对象。

resolve(base_path: str | ~os.PathLike | ~fs.base.FS | None = None, global_model_store: ~bentoml._internal.models.model.ModelStore = <simple_di.providers.SingletonFactory object>) Model[source]

获取模型的实际对象。

to_info(alias: str | None = None) BentoModelInfo[source]

返回模型信息对象。

class bentoml.models.HuggingFaceModel(model_id: str, revision: str = 'main', endpoint: str | None = NOTHING, include: List[str] | None = None, exclude: List[str] | None = None)[source]

继承自:Model[str]

对 Hugging Face 模型的引用。

参数:
  • model_id (str) – 模型标签。例如,“google-bert/bert-base-uncased”。

  • revision (str, optional) – 要使用的修订版本。默认为“main”。

  • endpoint (str, optional) – 要使用的 Hugging Face 端点。默认为 https://hugging-face.cn。

  • include (List[str], optional) – 要包含的文件。默认为所有文件。

  • exclude (List[str], optional) – 要排除的文件。默认为不排除任何文件。

返回值:

下载的模型路径。

返回类型:

str

classmethod from_info(info: BentoModelInfo) HuggingFaceModel[source]

从模型信息返回模型对象。

resolve(base_path: str | PathLike | FS | None = None) str[source]

获取模型的实际对象。

to_info(alias: str | None = None) BentoModelInfo[source]

返回模型信息对象。

管理模型

bentoml.models.create(name: Tag | str, *, labels: dict[str, t.Any] | None = None, metadata: dict[str, t.Any] | None = None, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>, module: str = '', api_version: str = 'v1', signatures: ModelSignaturesType = {}, options: ModelOptions = ModelOptions(), custom_objects: dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, context: ModelContext = ModelContext(framework_name='', framework_versions={}, bentoml_version='1.4.14.post2+g7da959d4', python_version='3.11.12')) t.Generator[Model, None, None][source]

在 BentoML 模型存储中创建模型实例。

以下函数参数已弃用

module, api_version, signatures, options, custom_objects, external_modules, context

bentoml.models.list(tag: t.Optional[t.Union[Tag, str]] = None, *, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>) t.List['Model'][source]
bentoml.models.get(tag: t.Union[Tag, str], *, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>, model_aliases: t.Dict[str, str] = <simple_di.providers.Static object>) Model[source]

按标签获取模型。如果标签是字符串,则将在 model_aliases 字典中查找。

bentoml.models.delete(tag: t.Union[Tag, str], *, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>)[source]
bentoml.models.export_model(tag: t.Union[Tag, str], path: str, output_format: t.Optional[str] = None, *, protocol: t.Optional[str] = None, user: t.Optional[str] = None, passwd: t.Optional[str] = None, params: t.Optional[t.Dict[str, str]] = None, subpath: t.Optional[str] = None, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>) str[source]

导出 BentoML 模型。

示例

# exports 'my_model' to '/path/to/folder/my_model-version.bentomodel' in BentoML's default format
bentoml.models.export_model('my_model:latest', '/path/to/folder')
# note that folders can only be passed if exporting to the local filesystem; otherwise the
# full path, including the desired filename, must be passed

# exports 'my_model' to '/path/to/folder/my_model.bentomodel' in BentoML's default format
bentoml.models.export_model('my_model:latest', '/path/to/folder/my_model')
bentoml.models.export_model('my_model:latest', '/path/to/folder/my_model.bentomodel')

# exports 'my_model' to '/path/to/folder/my_model.tar.gz in gzip format
# currently supported formats are tar.gz ('gz'), tar.xz ('xz'), tar.bz2 ('bz2'), and zip
bentoml.models.export_model('my_model:latest', '/path/to/folder/my_model.tar.gz')
bentoml.models.export_model('my_model:latest', '/path/to/folder/my_model.tar.gz', 'gz')

# exports 'my_model' to '/path/to/folder/my_model/ as a folder
bentoml.models.export_model('my_model:latest', '/path/to/folder/my_model', 'folder')

# exports 'my_model' to the S3 bucket 'my_bucket' as 'folder/my_model-version.bentomodel'
bentoml.models.export_model('my_model:latest', 's3://my_bucket/folder')
bentoml.models.export_model('my_model:latest', 'my_bucket/folder', protocol='s3')
bentoml.models.export_model('my_model:latest', 'my_bucket', protocol='s3', subpath='folder')
bentoml.models.export_model('my_model:latest', 'my_bucket', protocol='s3', subpath='folder',
                            user='<AWS access key>', passwd='<AWS secret key>',
                            params={'acl': 'public-read', 'cache-control': 'max-age=2592000,public'})

有关每个关键字参数(protocoluserpasswdparamssubpath)含义的更全面描述,请参阅 FS URL 文档。

参数:
  • tag – 要导出的模型的标签

  • path –

    可以是以下两种之一:* 本地文件系统上的文件夹 * FS URL,例如,'s3://my_bucket/folder/my_model.bentomodel'

  • protocol – (专家) 导出时使用的 FS 协议。一些示例协议包括 'ftp''s3''userdata'

  • user – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • passwd – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • params – (专家) 要传递给用于导出的 FS 的参数映射,例如用于设置 FTP 代理的 {'proxy': 'myproxy.net'}

  • subpath – (专家) 模型应导出到 FS 内部的路径

  • _model_store – 从中获取要保存模型的模型存储

返回值:

模型导出到的路径表示。如果导出到本地文件系统,

这将是导出模型的 OS 路径。否则,它将是一个 FS URL。

返回类型:

str

bentoml.models.import_model(path: str, input_format: t.Optional[str] = None, *, protocol: t.Optional[str] = None, user: t.Optional[str] = None, passwd: t.Optional[str] = None, params: t.Optional[t.Dict[str, str]] = None, subpath: t.Optional[str] = None, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>) Model[source]

导入使用 bentoml.models.export_model 导出的 Bento 模型。要导入使用框架保存的模型,请参阅相关框架下的 save 函数,例如 bentoml.sklearn.save

示例

# imports 'my_model' from '/path/to/folder/my_model.bentomodel'
bentoml.models.import_model('/path/to/folder/my_model.bentomodel')

# imports 'my_model' from '/path/to/folder/my_model.tar.gz'
# currently supported formats are tar.gz ('gz'), tar.xz ('xz'), tar.bz2 ('bz2'), and zip
bentoml.models.import_model('/path/to/folder/my_model.tar.gz')
# treats 'my_model.ext' as a gzipped tarfile
bentoml.models.import_model('/path/to/folder/my_model.ext', 'gz')

# imports 'my_model', which is stored as an uncompressed folder, from '/path/to/folder/my_model/'
bentoml.models.import_model('/path/to/folder/my_model', 'folder')

# imports 'my_model' from the S3 bucket 'my_bucket', path 'folder/my_model.bentomodel'
# requires `fs-s3fs <https://pypi.ac.cn/project/fs-s3fs/>`_ ('pip install fs-s3fs')
bentoml.models.import_model('s3://my_bucket/folder/my_model.bentomodel')
bentoml.models.import_model('my_bucket/folder/my_model.bentomodel', protocol='s3')
bentoml.models.import_model('my_bucket', protocol='s3', subpath='folder/my_model.bentomodel')
bentoml.models.import_model('my_bucket', protocol='s3', subpath='folder/my_model.bentomodel',
                            user='<AWS access key>', passwd='<AWS secret key>',
                            params={'acl': 'public-read', 'cache-control': 'max-age=2592000,public'})

有关每个关键字参数(protocoluserpasswdparamssubpath)含义的更全面描述,请参阅 FS URL 文档。

参数:
  • tag – 要导出的模型的标签

  • path –

    可以是以下两种之一:* 本地文件系统上的文件夹 * FS URL,例如 's3://my_bucket/folder/my_model.bentomodel'

  • protocol – (专家) 导出时使用的 FS 协议。一些示例协议包括 'ftp''s3''userdata'

  • user – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • passwd – (专家) 如果需要身份验证,用于身份验证的用户名,例如用于 FTP

  • params – (专家) 要传递给用于导出的 FS 的参数映射,例如用于设置 FTP 代理的 {'proxy': 'myproxy.net'}

  • subpath – (专家) 模型应导出到 FS 内部的路径

  • _model_store – 将模型保存到的模型存储

返回值:

导入的模型

返回类型:

Model

使用 BentoCloud

bentoml.push(tag: Tag | str, *, force: bool = False, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>, _cloud_client: BentoCloudClient = <simple_di.providers.SingletonFactory object>)[source]

将 Bento 推送到 Yatai 服务器。

bentoml.pull(tag: Tag | str, *, force: bool = False, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>, _cloud_client: BentoCloudClient = <simple_di.providers.SingletonFactory object>)[source]
bentoml.models.push(tag: t.Union[Tag, str], *, force: bool = False, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>, _cloud_client: BentoCloudClient = <simple_di.providers.SingletonFactory object>)[source]
bentoml.models.pull(tag: t.Union[Tag, str], *, force: bool = False, _cloud_client: BentoCloudClient = <simple_di.providers.SingletonFactory object>) Model | None[source]