문서

동기 클라이언트

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 키로, 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 ) –

    이 SDK 클라이언트가 닫힐 때 함께 닫히는 선택적 사용자 정의 HTTP 전송입니다.

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

    선택적 httpx2.Client로, transport와 함께 사용할 수 없습니다. 이 SDK 클라이언트가 닫힐 때 함께 닫힙니다.

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

    API 루트로, TYPESAFE_BASE_URL 환경 변수로 설정할 수 있습니다.

예외:

  • TypeSafeError –

    API 키가 없거나 유효하지 않거나, 타임아웃이 유효하지 않습니다.

  • 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

텍스트나 구조화된 상태에 대해 이름 있는 질문에 답합니다.

자세한 내용은 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 –

    사용자 정의 모델을 제공하지 않으면 이름, 모델, 토큰 사용량 세부 정보입니다.

예외:

예시:

이름 있는 인수로 질문을 생성합니다:

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는 보호된 상태로 유지됩니다.

반환값:

예외:

예시:

from typesafe_sdk import TypeSafeClient

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