|
43 | 43 | from otari._client.api.responses_api import ResponsesApi |
44 | 44 | from otari._client.exceptions import ApiException |
45 | 45 | from otari._client.models.chat_completion_request import ChatCompletionRequest |
| 46 | +from otari._client.models.count_tokens_request import CountTokensRequest |
46 | 47 | from otari._client.models.create_batch_request import CreateBatchRequest |
47 | 48 | from otari._client.models.embedding_request import EmbeddingRequest |
48 | 49 | from otari._client.models.messages_request import MessagesRequest |
|
57 | 58 |
|
58 | 59 | from otari._client.models.chat_completion import ChatCompletion |
59 | 60 | from otari._client.models.chat_completion_chunk import ChatCompletionChunk |
| 61 | + from otari._client.models.count_tokens_response import CountTokensResponse |
60 | 62 | from otari._client.models.create_embedding_response import CreateEmbeddingResponse |
61 | 63 | from otari._client.models.model_object import ModelObject |
62 | 64 | from otari._client.models.moderation_response import ModerationResponse |
@@ -254,6 +256,32 @@ def message( |
254 | 256 | request = build_request(MessagesRequest, body) |
255 | 257 | return self._call(lambda: self._messages.create_message_v1_messages_post(request)) |
256 | 258 |
|
| 259 | + def count_tokens( |
| 260 | + self, |
| 261 | + *, |
| 262 | + model: str, |
| 263 | + messages: list[dict[str, Any]], |
| 264 | + **kwargs: Any, |
| 265 | + ) -> CountTokensResponse: |
| 266 | + """Count input tokens for an Anthropic-style message request. |
| 267 | +
|
| 268 | + Calls the gateway ``/v1/messages/count_tokens`` endpoint, which counts |
| 269 | + the tokens a ``/messages`` request would consume without generating a |
| 270 | + response. Returns a typed |
| 271 | + :class:`~otari._client.models.count_tokens_response.CountTokensResponse`. |
| 272 | +
|
| 273 | + Args: |
| 274 | + model: Model identifier (e.g. ``"anthropic:claude-3-5-sonnet"``). |
| 275 | + messages: Anthropic-style message list. |
| 276 | + **kwargs: Additional count-tokens parameters (``system``, ``tools``, |
| 277 | + ``tool_choice``, ``thinking``, ...). |
| 278 | + """ |
| 279 | + request = build_request(CountTokensRequest, {"model": model, "messages": messages, **kwargs}) |
| 280 | + result = self._call( |
| 281 | + lambda: self._messages.count_message_tokens_v1_messages_count_tokens_post(request), |
| 282 | + ) |
| 283 | + return cast("CountTokensResponse", result) |
| 284 | + |
257 | 285 | # -- Embeddings --------------------------------------------------------- |
258 | 286 |
|
259 | 287 | def embedding( |
|
0 commit comments