|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, List, Mapping, cast, Optional, Dict |
| 4 | +from ...types.audio import transcriptions_create_param |
| 5 | + |
| 6 | +import httpx |
| 7 | +import logging |
| 8 | +from typing_extensions import Literal |
| 9 | + |
| 10 | +from ...core import BaseAPI, deepcopy_minimal, maybe_transform, drop_prefix_image_data |
| 11 | +from ...core import make_request_options |
| 12 | +from ...core import StreamResponse |
| 13 | +from ...types.chat.chat_completion import Completion |
| 14 | +from ...types.chat.chat_completion_chunk import ChatCompletionChunk |
| 15 | +from ...types.sensitive_word_check import SensitiveWordCheckRequest |
| 16 | +from ...core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes |
| 17 | +from zhipuai.core._utils import extract_files |
| 18 | + |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from ..._client import ZhipuAI |
| 24 | + |
| 25 | + |
| 26 | +__all__ = ["Transcriptions"] |
| 27 | + |
| 28 | +class Transcriptions(BaseAPI): |
| 29 | + def __init__(self, client: "ZhipuAI") -> None: |
| 30 | + super().__init__(client) |
| 31 | + |
| 32 | + def create( |
| 33 | + self, |
| 34 | + *, |
| 35 | + file: FileTypes, |
| 36 | + model: str, |
| 37 | + request_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 38 | + user_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 39 | + stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN, |
| 40 | + temperature: Optional[float] | NotGiven = NOT_GIVEN, |
| 41 | + sensitive_word_check: Optional[SensitiveWordCheckRequest] | NotGiven = NOT_GIVEN, |
| 42 | + extra_headers: Headers | None = None, |
| 43 | + extra_body: Body | None = None, |
| 44 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN |
| 45 | + ) -> Completion | StreamResponse[ChatCompletionChunk]: |
| 46 | + if temperature is not None and temperature != NOT_GIVEN: |
| 47 | + if temperature <= 0: |
| 48 | + temperature = 0.01 |
| 49 | + if temperature >= 1: |
| 50 | + temperature = 0.99 |
| 51 | + |
| 52 | + body = deepcopy_minimal({ |
| 53 | + "model": model, |
| 54 | + "file": file, |
| 55 | + "request_id": request_id, |
| 56 | + "user_id": user_id, |
| 57 | + "temperature": temperature, |
| 58 | + "sensitive_word_check": sensitive_word_check, |
| 59 | + "stream": stream |
| 60 | + }) |
| 61 | + files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) |
| 62 | + if files: |
| 63 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 64 | + return self._post( |
| 65 | + "/audio/transcriptions", |
| 66 | + body=maybe_transform(body, transcriptions_create_param.TranscriptionsParam), |
| 67 | + files=files, |
| 68 | + options=make_request_options( |
| 69 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 70 | + ), |
| 71 | + cast_type=Completion, |
| 72 | + stream=stream or False, |
| 73 | + stream_cls=StreamResponse[ChatCompletionChunk], |
| 74 | + ) |
0 commit comments