ドキュメント

同期クライアント

同期クライアント

TypeSafeClient で質問を投げ、モデルを一覧し、同期の TypeSafe API リクエストを設定します。

typesafe_sdk.TypeSafeClient

TypeSafeClient(
    *,
    api_key: str | None = None,
    model: str | None = None,
    retry: RetryPolicy | None = None,
    timeout: float
    | httpx2.Timeout
    | None = None,
    headers: Mapping[str, str] | None = None,
    transport: httpx2.BaseTransport
    | None = None,
    http_client: httpx2.Client | None = None,
    base_url: str | None = None,
)

TypeSafe AI API 用の HTTP クライアントを作成します。

明示的なオプションは環境変数より優先されます。空、または空白文字だけの環境変数の値は無視されます。

パラメータ:

  • api_key (str | None, default: None ) –

    必須の API key。TYPESAFE_API_KEY 環境変数で設定できます。先頭と末尾の空白は除去されます。空のキー、内部の空白、制御文字、非 ASCII 文字は拒否されます。

  • model (str | None, default: None ) –

    モデル名。TYPESAFE_DEFAULT_MODEL 環境変数で設定できます。

  • retry (RetryPolicy | None, default: None ) –

    再試行の挙動を制御する RetryPolicy。利用できるオプションとその既定値は RetryPolicy を参照してください。再試行を無効にするには RetryPolicy(max_retries=0) を渡します。

  • timeout (float | httpx2.Timeout | None, default: None ) –

    HTTP 操作のタイムアウト。指定された場合は http_client.timeout を継承し、そうでなければ SDK の既定値を使います。

  • headers (Mapping[str, str] | None, default: None ) –

    追加で設定するリクエストヘッダー。

  • transport (httpx2.BaseTransport | None, default: None ) –

    任意のカスタム HTTP transport。この SDK クライアントを閉じるときに一緒に閉じられます。

  • http_client (httpx2.Client | None, default: None ) –

    任意の httpx2.Client。transport とは排他的です。この SDK クライアントを閉じるときに一緒に閉じられます。

  • base_url (str | None, default: None ) –

    API のルート。TYPESAFE_BASE_URL 環境変数で設定できます。

例外:

  • TypeSafeError –

    API key が欠けているか無効、またはタイムアウトが無効です。

  • ValueError –

    transport と http_client の両方が指定されています。

例:

from typesafe_sdk import Choice, Noul, TypeSafeClient

with TypeSafeClient() as client:
    result = client.system_one(
        state="I was charged twice. Please help.",
        questions={
            "billing": Noul(instructions="Is this about billing?"),
            "tone": Choice(
                instructions="What is the tone?",
                criteria={"calm": None, "angry": None},
            ),
        },
    )
    assert 0 <= result.nouls["billing"].noul <= 1
    assert result.choices["tone"].choice in {"calm", "angry"}

models

cached property

models: Models

Models API リソースへのアクセサ。

例:

with TypeSafeClient() as client:
    models = client.models.list()

system_one

system_one(
    state: JSONContent,
    questions: Mapping[str, Question],
    *,
    model: str | None = None,
    retry: RetryPolicy | None = None,
    timeout: float
    | httpx2.Timeout
    | None = None,
    extra_headers: Mapping[str, str]
    | None = None,
    extra_body: Mapping[str, JSONValue | None]
    | None = None,
    response_model: type[ResponseT]
    | None = None,
) -> SystemOneResponse | ResponseT
system_one(
    state: JSONContent,
    questions: Mapping[str, Question],
    *,
    model: str | None = None,
    retry: RetryPolicy | None = None,
    timeout: float
    | httpx2.Timeout
    | None = None,
    extra_headers: Mapping[str, str]
    | None = None,
    extra_body: Mapping[str, JSONValue | None]
    | None = None,
    response_model: None = None,
) -> SystemOneResponse
system_one(
    state: JSONContent,
    questions: Mapping[str, Question],
    *,
    model: str | None = None,
    retry: RetryPolicy | None = None,
    timeout: float
    | httpx2.Timeout
    | None = None,
    extra_headers: Mapping[str, str]
    | None = None,
    extra_body: Mapping[str, JSONValue | None]
    | None = None,
    response_model: type[ResponseT],
) -> ResponseT

テキストまたは構造化された state について、名前を付けた質問に答えます。

詳しくは System One を参照してください。

パラメータ:

  • state (JSONContent) –

    評価するテキスト、JSON オブジェクト、または配列。state を参照してください。

  • questions (Mapping[str, Question]) –

    名前から質問オブジェクトまたは生の辞書への、空でないマッピング。

  • model (str | None, default: None ) –

    モデルの上書き。None はクライアントの既定値を継承します。

  • retry (RetryPolicy | None, default: None ) –

    この呼び出しに限り、クライアントレベルの値を上書きする任意の再試行ポリシー。

  • timeout (float | httpx2.Timeout | None, default: None ) –

    この呼び出しに限り、クライアントレベルの値を上書きする、http 操作の任意のタイムアウト(秒)。

  • extra_headers (Mapping[str, str] | None, default: None ) –

    追加で設定するリクエストヘッダー。

  • extra_body (Mapping[str, JSONValue | None] | None, default: None ) –

    追加のトップレベルなリクエストボディのフィールド。state、model、questions を設定した後、ボディに浅くマージされます。マージは後書き優先です。state、model、questions と衝突するキーはそれらを上書きし、オブジェクトの値は深くマージされずに置き換えられます。

  • response_model (type[ResponseT] | None, default: None ) –

    JSON レスポンスボディを記述する任意の Pydantic BaseModel 型。入れ子になった答えのモデルも含みます。

戻り値:

  • SystemOneResponse | ResponseT –

    response_model のインスタンス、または質問ごとの答えを持つ SystemOneResponse

  • SystemOneResponse | ResponseT –

    カスタムモデルを指定しない場合は、質問名・モデル・token 使用量の詳細。

例外:

例:

名前付き引数で質問を作成します:

with TypeSafeClient() as client:
    result = client.system_one(
        state="I was charged twice. Please help.",
        questions={
            "billing": Noul(instructions="Is this about billing?"),
            "tone": Choice(
                instructions="What is the tone?",
                criteria={"calm": None, "angry": None},
            ),
        },
    )
    assert 0 <= result.nouls["billing"].noul <= 1
    assert result.choices["tone"].choice in {"calm", "angry"}

質問を辞書として渡します:

with TypeSafeClient() as client:
    result = client.system_one(
        state={"message": "I was charged twice. Please help."},
        questions={
            "billing": {"type": "noul", "instructions": "Is this about billing?"},
            "tone": {
                "type": "choice",
                "instructions": "What is the tone?",
                "criteria": {"calm": None, "angry": None},
            },
        },
    )
    assert 0 <= result.nouls["billing"].noul <= 1
    assert result.choices["tone"].choice in {"calm", "angry"}

close

close() -> None

ネットワークリソースを解放し、基盤となる HTTP クライアント(指定されたものも含む)を閉じます。

Models リソース

TypeSafeClient.models から到達します。

typesafe_sdk.Models

アカウントで利用できるモデルへのアクセス。TypeSafeClient.models から到達します。

list

list(
    *,
    retry: RetryPolicy | None = None,
    timeout: float
    | httpx2.Timeout
    | None = None,
    extra_headers: Mapping[str, str]
    | None = None,
) -> ListModelsResponse

アカウントで利用できるモデルを一覧します。

パラメータ:

  • retry (RetryPolicy | None, default: None ) –

    この呼び出しに限り、クライアントレベルの値を上書きする任意の再試行ポリシー。

  • timeout (float | httpx2.Timeout | None, default: None ) –

    操作ごとのタイムアウトの上書き。None はクライアントの設定を継承します。

  • extra_headers (Mapping[str, str] | None, default: None ) –

    追加のリクエストヘッダーの上書き。認証、SDK の識別、Accept は保護されたままです。

戻り値:

例外:

  • TypeSafeAPIError –

    再試行後もサーバーが失敗の HTTP レスポンスを返します。

  • TypeSafeAPIConnectionError –

    再試行後もリクエストが接続できないか、タイムアウトします。

例:

from typesafe_sdk import TypeSafeClient

with TypeSafeClient() as client:
    models = client.models.list()