| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import List |
| 4 |
|
| 5 |
import httpx2 |
| 6 |
|
| 7 |
from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given |
| 8 |
from ..._utils import is_given, path_template, strip_not_given |
| 9 |
from ..._compat import cached_property |
| 10 |
from ..._resource import SyncAPIResource, AsyncAPIResource |
| 11 |
from ..._response import ( |
| 12 |
to_raw_response_wrapper, |
| 13 |
to_streamed_response_wrapper, |
| 14 |
async_to_raw_response_wrapper, |
| 15 |
async_to_streamed_response_wrapper, |
| 16 |
) |
| 17 |
from ...pagination import SyncPage, AsyncPage |
| 18 |
from ..._base_client import AsyncPaginator, make_request_options |
| 19 |
from ...types.anthropic_beta_param import AnthropicBetaParam |
| 20 |
from ...types.beta.beta_model_info import BetaModelInfo |
| 21 |
|
| 22 |
__all__ = ["Models", "AsyncModels"] |
| 23 |
|
| 24 |
|
| 25 |
class Models(SyncAPIResource): |
| 26 |
@cached_property |
| 27 |
def with_raw_response(self) -> ModelsWithRawResponse: |
| 28 |
""" |
| 29 |
This property can be used as a prefix for any HTTP method call to return |
| 30 |
the raw response object instead of the parsed content. |
| 31 |
|
| 32 |
For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers |
| 33 |
""" |
| 34 |
return ModelsWithRawResponse(self) |
| 35 |
|
| 36 |
@cached_property |
| 37 |
def with_streaming_response(self) -> ModelsWithStreamingResponse: |
| 38 |
""" |
| 39 |
An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 40 |
|
| 41 |
For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response |
| 42 |
""" |
| 43 |
return ModelsWithStreamingResponse(self) |
| 44 |
|
| 45 |
def retrieve( |
| 46 |
self, |
| 47 |
model_id: str, |
| 48 |
*, |
| 49 |
betas: List[AnthropicBetaParam] | Omit = omit, |
| 50 |
workspace_id: str | Omit = omit, |
| 51 |
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 52 |
# The extra values given here take precedence over values defined on the client or passed to this method. |
| 53 |
extra_headers: Headers | None = None, |
| 54 |
extra_query: Query | None = None, |
| 55 |
extra_body: Body | None = None, |
| 56 |
timeout: float | httpx2.Timeout | None | NotGiven = not_given, |
| 57 |
) -> BetaModelInfo: |
| 58 |
""" |
| 59 |
Get a specific model. |
| 60 |
|
| 61 |
The Models API response can be used to determine information about a specific |
| 62 |
model or resolve a model alias to a model ID. |
| 63 |
|
| 64 |
Args: |
| 65 |
model_id: Model identifier or alias. |
| 66 |
|
| 67 |
betas: Optional header to specify the beta version(s) you want to use. |
| 68 |
|
| 69 |
workspace_id: Optional header to select the Workspace for this request. The value is a |
| 70 |
Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`). |
| 71 |
|
| 72 |
Only needed for credentials that can act on more than one Workspace. A |
| 73 |
credential that belongs to a specific Workspace may omit it; if sent, it must |
| 74 |
match that Workspace. |
| 75 |
|
| 76 |
extra_headers: Send extra headers |
| 77 |
|
| 78 |
extra_query: Add additional query parameters to the request |
| 79 |
|
| 80 |
extra_body: Add additional JSON properties to the request |
| 81 |
|
| 82 |
timeout: Override the client-level default timeout for this request, in seconds |
| 83 |
""" |
| 84 |
if not model_id: |
| 85 |
raise ValueError(f"Expected a non-empty value for `model_id` but received {model_id!r}") |
| 86 |
extra_headers = { |
| 87 |
**strip_not_given( |
| 88 |
{ |
| 89 |
"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given, |
| 90 |
"anthropic-workspace-id": workspace_id, |
| 91 |
} |
| 92 |
), |
| 93 |
**(extra_headers or {}), |
| 94 |
} |
| 95 |
return self._get( |
| 96 |
path_template("/v1/models/{model_id}?beta=true", model_id=model_id), |
| 97 |
options=make_request_options( |
| 98 |
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 99 |
), |
| 100 |
cast_to=BetaModelInfo, |
| 101 |
) |
| 102 |
|
| 103 |
def list( |
| 104 |
self, |
| 105 |
*, |
| 106 |
after_id: str | Omit = omit, |
| 107 |
before_id: str | Omit = omit, |
| 108 |
limit: int | Omit = omit, |
| 109 |
betas: List[AnthropicBetaParam] | Omit = omit, |
| 110 |
workspace_id: str | Omit = omit, |
| 111 |
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 112 |
# The extra values given here take precedence over values defined on the client or passed to this method. |
| 113 |
extra_headers: Headers | None = None, |
| 114 |
extra_query: Query | None = None, |
| 115 |
extra_body: Body | None = None, |
| 116 |
timeout: float | httpx2.Timeout | None | NotGiven = not_given, |
| 117 |
) -> SyncPage[BetaModelInfo]: |
| 118 |
""" |
| 119 |
List available models. |
| 120 |
|
| 121 |
The Models API response can be used to determine which models are available for |
| 122 |
use in the API. More recently released models are listed first. |
| 123 |
|
| 124 |
Args: |
| 125 |
after_id: ID of the object to use as a cursor for pagination. When provided, returns the |
| 126 |
page of results immediately after this object. |
| 127 |
|
| 128 |
before_id: ID of the object to use as a cursor for pagination. When provided, returns the |
| 129 |
page of results immediately before this object. |
| 130 |
|
| 131 |
limit: Number of items to return per page. |
| 132 |
|
| 133 |
Defaults to `20`. Ranges from `1` to `1000`. |
| 134 |
|
| 135 |
betas: Optional header to specify the beta version(s) you want to use. |
| 136 |
|
| 137 |
workspace_id: Optional header to select the Workspace for this request. The value is a |
| 138 |
Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`). |
| 139 |
|
| 140 |
Only needed for credentials that can act on more than one Workspace. A |
| 141 |
credential that belongs to a specific Workspace may omit it; if sent, it must |
| 142 |
match that Workspace. |
| 143 |
|
| 144 |
extra_headers: Send extra headers |
| 145 |
|
| 146 |
extra_query: Add additional query parameters to the request |
| 147 |
|
| 148 |
extra_body: Add additional JSON properties to the request |
| 149 |
|
| 150 |
timeout: Override the client-level default timeout for this request, in seconds |
| 151 |
""" |
| 152 |
extra_headers = { |
| 153 |
**strip_not_given( |
| 154 |
{ |
| 155 |
"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given, |
| 156 |
"anthropic-workspace-id": workspace_id, |
| 157 |
} |
| 158 |
), |
| 159 |
**(extra_headers or {}), |
| 160 |
} |
| 161 |
return self._get_api_list( |
| 162 |
"/v1/models?beta=true", |
| 163 |
page=SyncPage[BetaModelInfo], |
| 164 |
options=make_request_options( |
| 165 |
extra_headers=extra_headers, |
| 166 |
extra_query=extra_query, |
| 167 |
extra_body=extra_body, |
| 168 |
timeout=timeout, |
| 169 |
query={ |
| 170 |
"after_id": after_id, |
| 171 |
"before_id": before_id, |
| 172 |
"limit": limit, |
| 173 |
}, |
| 174 |
), |
| 175 |
model=BetaModelInfo, |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
class AsyncModels(AsyncAPIResource): |
| 180 |
@cached_property |
| 181 |
def with_raw_response(self) -> AsyncModelsWithRawResponse: |
| 182 |
""" |
| 183 |
This property can be used as a prefix for any HTTP method call to return |
| 184 |
the raw response object instead of the parsed content. |
| 185 |
|
| 186 |
For more information, see https://www.github.com/anthropics/anthropic-sdk-python#accessing-raw-response-data-eg-headers |
| 187 |
""" |
| 188 |
return AsyncModelsWithRawResponse(self) |
| 189 |
|
| 190 |
@cached_property |
| 191 |
def with_streaming_response(self) -> AsyncModelsWithStreamingResponse: |
| 192 |
""" |
| 193 |
An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 194 |
|
| 195 |
For more information, see https://www.github.com/anthropics/anthropic-sdk-python#with_streaming_response |
| 196 |
""" |
| 197 |
return AsyncModelsWithStreamingResponse(self) |
| 198 |
|
| 199 |
async def retrieve( |
| 200 |
self, |
| 201 |
model_id: str, |
| 202 |
*, |
| 203 |
betas: List[AnthropicBetaParam] | Omit = omit, |
| 204 |
workspace_id: str | Omit = omit, |
| 205 |
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 206 |
# The extra values given here take precedence over values defined on the client or passed to this method. |
| 207 |
extra_headers: Headers | None = None, |
| 208 |
extra_query: Query | None = None, |
| 209 |
extra_body: Body | None = None, |
| 210 |
timeout: float | httpx2.Timeout | None | NotGiven = not_given, |
| 211 |
) -> BetaModelInfo: |
| 212 |
""" |
| 213 |
Get a specific model. |
| 214 |
|
| 215 |
The Models API response can be used to determine information about a specific |
| 216 |
model or resolve a model alias to a model ID. |
| 217 |
|
| 218 |
Args: |
| 219 |
model_id: Model identifier or alias. |
| 220 |
|
| 221 |
betas: Optional header to specify the beta version(s) you want to use. |
| 222 |
|
| 223 |
workspace_id: Optional header to select the Workspace for this request. The value is a |
| 224 |
Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`). |
| 225 |
|
| 226 |
Only needed for credentials that can act on more than one Workspace. A |
| 227 |
credential that belongs to a specific Workspace may omit it; if sent, it must |
| 228 |
match that Workspace. |
| 229 |
|
| 230 |
extra_headers: Send extra headers |
| 231 |
|
| 232 |
extra_query: Add additional query parameters to the request |
| 233 |
|
| 234 |
extra_body: Add additional JSON properties to the request |
| 235 |
|
| 236 |
timeout: Override the client-level default timeout for this request, in seconds |
| 237 |
""" |
| 238 |
if not model_id: |
| 239 |
raise ValueError(f"Expected a non-empty value for `model_id` but received {model_id!r}") |
| 240 |
extra_headers = { |
| 241 |
**strip_not_given( |
| 242 |
{ |
| 243 |
"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given, |
| 244 |
"anthropic-workspace-id": workspace_id, |
| 245 |
} |
| 246 |
), |
| 247 |
**(extra_headers or {}), |
| 248 |
} |
| 249 |
return await self._get( |
| 250 |
path_template("/v1/models/{model_id}?beta=true", model_id=model_id), |
| 251 |
options=make_request_options( |
| 252 |
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 253 |
), |
| 254 |
cast_to=BetaModelInfo, |
| 255 |
) |
| 256 |
|
| 257 |
def list( |
| 258 |
self, |
| 259 |
*, |
| 260 |
after_id: str | Omit = omit, |
| 261 |
before_id: str | Omit = omit, |
| 262 |
limit: int | Omit = omit, |
| 263 |
betas: List[AnthropicBetaParam] | Omit = omit, |
| 264 |
workspace_id: str | Omit = omit, |
| 265 |
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 266 |
# The extra values given here take precedence over values defined on the client or passed to this method. |
| 267 |
extra_headers: Headers | None = None, |
| 268 |
extra_query: Query | None = None, |
| 269 |
extra_body: Body | None = None, |
| 270 |
timeout: float | httpx2.Timeout | None | NotGiven = not_given, |
| 271 |
) -> AsyncPaginator[BetaModelInfo, AsyncPage[BetaModelInfo]]: |
| 272 |
""" |
| 273 |
List available models. |
| 274 |
|
| 275 |
The Models API response can be used to determine which models are available for |
| 276 |
use in the API. More recently released models are listed first. |
| 277 |
|
| 278 |
Args: |
| 279 |
after_id: ID of the object to use as a cursor for pagination. When provided, returns the |
| 280 |
page of results immediately after this object. |
| 281 |
|
| 282 |
before_id: ID of the object to use as a cursor for pagination. When provided, returns the |
| 283 |
page of results immediately before this object. |
| 284 |
|
| 285 |
limit: Number of items to return per page. |
| 286 |
|
| 287 |
Defaults to `20`. Ranges from `1` to `1000`. |
| 288 |
|
| 289 |
betas: Optional header to specify the beta version(s) you want to use. |
| 290 |
|
| 291 |
workspace_id: Optional header to select the Workspace for this request. The value is a |
| 292 |
Workspace ID (for example, `wrkspc_011CZkZaBF1tNoB5wlCeusgy`). |
| 293 |
|
| 294 |
Only needed for credentials that can act on more than one Workspace. A |
| 295 |
credential that belongs to a specific Workspace may omit it; if sent, it must |
| 296 |
match that Workspace. |
| 297 |
|
| 298 |
extra_headers: Send extra headers |
| 299 |
|
| 300 |
extra_query: Add additional query parameters to the request |
| 301 |
|
| 302 |
extra_body: Add additional JSON properties to the request |
| 303 |
|
| 304 |
timeout: Override the client-level default timeout for this request, in seconds |
| 305 |
""" |
| 306 |
extra_headers = { |
| 307 |
**strip_not_given( |
| 308 |
{ |
| 309 |
"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else not_given, |
| 310 |
"anthropic-workspace-id": workspace_id, |
| 311 |
} |
| 312 |
), |
| 313 |
**(extra_headers or {}), |
| 314 |
} |
| 315 |
return self._get_api_list( |
| 316 |
"/v1/models?beta=true", |
| 317 |
page=AsyncPage[BetaModelInfo], |
| 318 |
options=make_request_options( |
| 319 |
extra_headers=extra_headers, |
| 320 |
extra_query=extra_query, |
| 321 |
extra_body=extra_body, |
| 322 |
timeout=timeout, |
| 323 |
query={ |
| 324 |
"after_id": after_id, |
| 325 |
"before_id": before_id, |
| 326 |
"limit": limit, |
| 327 |
}, |
| 328 |
), |
| 329 |
model=BetaModelInfo, |
| 330 |
) |
| 331 |
|
| 332 |
|
| 333 |
class ModelsWithRawResponse: |
| 334 |
def __init__(self, models: Models) -> None: |
| 335 |
self._models = models |
| 336 |
|
| 337 |
self.retrieve = to_raw_response_wrapper( |
| 338 |
models.retrieve, |
| 339 |
) |
| 340 |
self.list = to_raw_response_wrapper( |
| 341 |
models.list, |
| 342 |
) |
| 343 |
|
| 344 |
|
| 345 |
class AsyncModelsWithRawResponse: |
| 346 |
def __init__(self, models: AsyncModels) -> None: |
| 347 |
self._models = models |
| 348 |
|
| 349 |
self.retrieve = async_to_raw_response_wrapper( |
| 350 |
models.retrieve, |
| 351 |
) |
| 352 |
self.list = async_to_raw_response_wrapper( |
| 353 |
models.list, |
| 354 |
) |
| 355 |
|
| 356 |
|
| 357 |
class ModelsWithStreamingResponse: |
| 358 |
def __init__(self, models: Models) -> None: |
| 359 |
self._models = models |
| 360 |
|
| 361 |
self.retrieve = to_streamed_response_wrapper( |
| 362 |
models.retrieve, |
| 363 |
) |
| 364 |
self.list = to_streamed_response_wrapper( |
| 365 |
models.list, |
| 366 |
) |
| 367 |
|
| 368 |
|
| 369 |
class AsyncModelsWithStreamingResponse: |
| 370 |
def __init__(self, models: AsyncModels) -> None: |
| 371 |
self._models = models |
| 372 |
|
| 373 |
self.retrieve = async_to_streamed_response_wrapper( |
| 374 |
models.retrieve, |
| 375 |
) |
| 376 |
self.list = async_to_streamed_response_wrapper( |
| 377 |
models.list, |
| 378 |
) |