Skip to content

Commit 65f71a4

Browse files
author
jinhaiyang
committed
Merge remote-tracking branch 'origin/main'
2 parents 7683e40 + 842c9aa commit 65f71a4

File tree

9 files changed

+176
-1
lines changed

9 files changed

+176
-1
lines changed

tests/integration_tests/asr1.wav

346 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from zhipuai import ZhipuAI
2+
import zhipuai
3+
4+
import logging
5+
import logging.config
6+
7+
8+
def test_transcriptions(logging_conf):
9+
logging.config.dictConfig(logging_conf) # type: ignore
10+
client = ZhipuAI() # 填写您自己的APIKey
11+
try:
12+
with open("asr1.wav", "rb") as audio_file:
13+
transcriptResponse = client.audio.transcriptions.create(
14+
model="glm-asr",
15+
file=audio_file,
16+
stream=False
17+
)
18+
print(transcriptResponse)
19+
except zhipuai.core._errors.APIRequestFailedError as err:
20+
print(err)
21+
except zhipuai.core._errors.APIInternalError as err:
22+
print(err)
23+
except zhipuai.core._errors.APIStatusError as err:
24+
print(err)
25+
26+
27+
def test_transcriptions_stream(logging_conf):
28+
logging.config.dictConfig(logging_conf) # type: ignore
29+
client = ZhipuAI() # 填写您自己的APIKey
30+
try:
31+
with open("asr1.wav", "rb") as audio_file:
32+
transcriptResponse = client.audio.transcriptions.create(
33+
model="glm-asr",
34+
file=audio_file,
35+
stream=True
36+
)
37+
for item in transcriptResponse:
38+
print(item)
39+
except zhipuai.core._errors.APIRequestFailedError as err:
40+
print(err)
41+
except zhipuai.core._errors.APIInternalError as err:
42+
print(err)
43+
except zhipuai.core._errors.APIStatusError as err:
44+
print(err)
45+

zhipuai/_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(
6363
self.videos = api_resource.Videos(self)
6464
self.assistant = api_resource.Assistant(self)
6565
self.web_search = api_resource.WebSearchApi(self)
66+
self.audio = api_resource.audio.Audio(self)
6667

6768
@property
6869
@override

zhipuai/api_resource/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
from .assistant import (
3434
Assistant,
3535
)
36+
from .audio import (
37+
Audio
38+
)
3639

3740
from .web_search import (
3841
WebSearchApi
@@ -52,5 +55,5 @@
5255
'Knowledge',
5356
'Tools',
5457
'Assistant',
55-
58+
'Audio'
5659
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .audio import (
2+
Audio
3+
)
4+
5+
from .transcriptions import (
6+
Transcriptions
7+
)
8+
9+
10+
__all__ = [
11+
'Audio',
12+
'Transcriptions'
13+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from __future__ import annotations
2+
3+
from ...core import BaseAPI, cached_property
4+
from .transcriptions import Transcriptions
5+
6+
7+
8+
__all__ = ["Audio"]
9+
class Audio(BaseAPI):
10+
@cached_property
11+
def transcriptions(self) -> Transcriptions:
12+
return Transcriptions(self._client)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
)

zhipuai/types/audio/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
from .transcriptions_create_param import(
3+
TranscriptionsParam
4+
)
5+
6+
__all__ = ["TranscriptionsParam"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import annotations
2+
3+
from typing import List, Optional
4+
5+
from typing_extensions import Literal, Required, TypedDict
6+
__all__ = ["TranscriptionsParam"]
7+
8+
from ..sensitive_word_check import SensitiveWordCheckRequest
9+
10+
class TranscriptionsParam(TypedDict, total=False):
11+
model: str
12+
"""模型编码"""
13+
temperature:float
14+
"""采样温度"""
15+
stream: bool
16+
"""是否流式输出"""
17+
sensitive_word_check: Optional[SensitiveWordCheckRequest]
18+
request_id: str
19+
"""由用户端传参,需保证唯一性;用于区分每次请求的唯一标识,用户端不传时平台会默认生成。"""
20+
user_id: str
21+
"""用户端。"""

0 commit comments

Comments
 (0)