客户端 API

您可以创建一个 BentoML 客户端 来与您的 BentoML 服务进行交互。

bentoml.SyncHTTPClient

class bentoml.SyncHTTPClient(url: str, *, token: str | None = None, timeout: float = 30, server_ready_timeout: float | None = None)[source]

BentoML 服务的同步客户端。

参数:
  • url (str) – BentoML 服务的 URL。

  • token (str, optional) – 认证令牌。默认为 None。

  • timeout (float, optional) – 客户端超时时间。默认为 30。

  • server_ready_timeout (float, optional) – 服务器就绪超时时间。如果未提供,将使用与 timeout 相同的值。

示例

with SyncHTTPClient("https://:3000") as client:
    resp = client.call("classify", input_series=[[1,2,3,4]])
    assert resp == [0]
    # Or using named method directly
    resp = client.classify(input_series=[[1,2,3,4]])
    assert resp == [0]
client_cls

别名 Client

is_ready(timeout: int | None = None) bool[source]

bentoml.AsyncHTTPClient

class bentoml.AsyncHTTPClient(url: str, *, token: str | None = None, timeout: float = 30, server_ready_timeout: float | None = None)[source]

BentoML 服务的异步客户端。

参数:
  • url (str) – BentoML 服务的 URL。

  • token (str, optional) – 认证令牌。默认为 None。

  • timeout (float, optional) – 客户端超时时间。默认为 30。

  • server_ready_timeout (float, optional) – 服务器就绪超时时间。如果未提供,将使用与 timeout 相同的值。

示例

async with AsyncHTTPClient("https://:3000") as client:
    resp = await client.call("classify", input_series=[[1,2,3,4]])
    assert resp == [0]
    # Or using named method directly
    resp = await client.classify(input_series=[[1,2,3,4]])
    assert resp == [0]

    # Streaming
    resp = client.stream(prompt="hello")
    async for data in resp:
        print(data)
client_cls

别名 AsyncClient

async is_ready(timeout: int | None = None) bool[source]