同期クライアント
同期クライアント
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環境変数で設定できます。
例外:
-
API key が欠けているか無効、またはタイムアウトが無効です。
-
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 使用量の詳細。
例外:
-
質問が空、または score 質問の criteria リストが空です。
-
再試行後もサーバーが失敗の HTTP レスポンスを返します。
-
再試行後もリクエストが接続できないか、タイムアウトします。
-
TypeSafeAPIResponseValidationError–レスポンスボディがレスポンスモデルと一致しません。
例:
名前付き引数で質問を作成します:
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は保護されたままです。
戻り値:
-
ListModelsResponseのmodelsは各モデルの名前・説明、そして -
リリース日を保持します。
例外:
-
再試行後もサーバーが失敗の HTTP レスポンスを返します。
-
再試行後もリクエストが接続できないか、タイムアウトします。
例:
from typesafe_sdk import TypeSafeClient
with TypeSafeClient() as client:
models = client.models.list()