Skip to content

Commit 4f86b8f

Browse files
author
zhansheng.lzs
committed
feat: tag x-dashscope-sdk-client with module on all request paths
Derive the module segment from the API class package (threads grouped into assistants), tag /tasks/* operations as "tasks", add sdk headers to standalone WebSocket classes (tts_v2, qwen_omni, qwen_tts_realtime, multimodal_dialog, tingwu_realtime) and to encryption/reinforcement/ http-tts paths, and default acli TongyiProvider module to "app".
1 parent 30e7cb5 commit 4f86b8f

16 files changed

Lines changed: 481 additions & 39 deletions

File tree

‎dashscope/acli/providers/tongyi.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(
4848
request_timeout: int = 60,
4949
protocol: str = "openai",
5050
base_url: str | None = None,
51-
module: str = "",
51+
module: str = "app",
5252
):
5353
self.model = model
5454
self.api_key = api_key

‎dashscope/api_entities/api_request_factory.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def _build_api_request( # pylint: disable=too-many-branches
5353
task_id: Optional[str] = None,
5454
enable_encryption: bool = False,
5555
pre_task_id: Optional[str] = None,
56+
# SDK module tag for the x-dashscope-sdk-client header
57+
sdk_module: str = "",
5658
# Additional parameters for API request data
5759
**kwargs,
5860
):
@@ -192,7 +194,7 @@ def _build_api_request( # pylint: disable=too-many-branches
192194
"websocket]",
193195
)
194196

195-
merged_headers = dict(get_sdk_headers())
197+
merged_headers = dict(get_sdk_headers(module=sdk_module))
196198
if headers is not None:
197199
merged_headers.update(headers)
198200
if merged_headers:

‎dashscope/api_entities/encryption.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
DEFAULT_REQUEST_TIMEOUT_SECONDS,
1919
)
2020
from dashscope.common.logging import logger
21+
from dashscope.common.utils import get_sdk_headers
2122

2223

2324
class Encryption:
@@ -90,6 +91,7 @@ def _get_public_keys():
9091
url = dashscope.base_http_api_url + "/public-keys/latest"
9192
headers = {
9293
"Authorization": f"Bearer {dashscope.api_key}",
94+
**get_sdk_headers(module="utils"),
9395
}
9496

9597
response = requests.get(

‎dashscope/app/application.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def call( # type: ignore[override]
240240
workspace=workspace,
241241
api_key=api_key,
242242
is_service=False,
243+
sdk_module="app",
243244
**parameters,
244245
)
245246
# call request service.

‎dashscope/audio/http_tts/http_speech_synthesizer.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _http_call(
203203

204204
# Get base URL
205205
import dashscope
206-
from dashscope.common.utils import join_url
206+
from dashscope.common.utils import get_sdk_headers, join_url
207207

208208
if url:
209209
base_url = url
@@ -233,6 +233,9 @@ def _http_call(
233233
stream=stream,
234234
)
235235

236+
# Tag sdk module (custom headers below may still override)
237+
request.add_headers(get_sdk_headers(module="audio"))
238+
236239
# Add custom headers
237240
if headers:
238241
request.add_headers(headers)

‎dashscope/audio/qwen_omni/omni_realtime.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import dashscope
1515
from dashscope.common.error import ModelRequired
1616
from dashscope.common.logging import logger
17-
from dashscope.common.utils import get_user_agent
17+
from dashscope.common.utils import get_sdk_headers, get_user_agent
1818

1919

2020
class OmniRealtimeCallback:
@@ -259,6 +259,7 @@ def _get_websocket_header(self):
259259
headers = {
260260
"user-agent": ua,
261261
"Authorization": "Bearer " + self.apikey,
262+
**get_sdk_headers(module="audio"),
262263
}
263264
if self.user_headers:
264265
headers = {**self.user_headers, **headers}

‎dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import dashscope
1313
from dashscope.common.error import ModelRequired
1414
from dashscope.common.logging import logger
15-
from dashscope.common.utils import get_user_agent
15+
from dashscope.common.utils import get_sdk_headers, get_user_agent
1616

1717

1818
class QwenTtsRealtimeCallback:
@@ -117,6 +117,7 @@ def _get_websocket_header(self):
117117
headers = {
118118
"user-agent": ua,
119119
"Authorization": "Bearer " + self.apikey,
120+
**get_sdk_headers(module="audio"),
120121
}
121122
if self.user_headers:
122123
headers = {**self.user_headers, **headers}

‎dashscope/audio/tts_v2/speech_synthesizer.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
RequestFailure,
2323
)
2424
from dashscope.common.logging import logger
25-
from dashscope.common.utils import get_user_agent
25+
from dashscope.common.utils import get_sdk_headers, get_user_agent
2626
from dashscope.protocol.websocket import (
2727
ACTION_KEY,
2828
ERROR_MESSAGE,
@@ -183,6 +183,7 @@ def get_websocket_headers(self, headers, workspace):
183183
self.headers = {
184184
"user-agent": ua,
185185
"Authorization": "Bearer " + self.apikey,
186+
**get_sdk_headers(module="audio"),
186187
}
187188
if headers:
188189
self.headers = {**self.headers, **headers}

‎dashscope/client/base_api.py‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
_handle_http_response,
2929
_handle_http_stream_response,
3030
default_headers,
31+
get_api_module,
3132
join_url,
3233
)
3334

@@ -69,7 +70,7 @@ def _handle_kwargs(
6970
custom_headers = kwargs.pop("headers", None)
7071
headers = {
7172
**_workspace_header(workspace),
72-
**default_headers(api_key),
73+
**default_headers(api_key, module="tasks"),
7374
}
7475
if custom_headers:
7576
headers = {
@@ -133,6 +134,7 @@ async def async_call(
133134
task=task,
134135
function=function,
135136
api_key=api_key,
137+
sdk_module=get_api_module(cls.__module__),
136138
**kwargs,
137139
)
138140
# call request service.
@@ -373,7 +375,7 @@ async def list(
373375
api_key = get_default_api_key()
374376
headers = {
375377
**_workspace_header(workspace),
376-
**default_headers(api_key),
378+
**default_headers(api_key, module="tasks"),
377379
}
378380
async with aiohttp.ClientSession(trust_env=True) as session:
379381
response = await session.get(
@@ -486,6 +488,7 @@ async def call(
486488
task=task,
487489
function=function,
488490
api_key=api_key,
491+
sdk_module=get_api_module(cls.__module__),
489492
**kwargs,
490493
)
491494
# call request service.
@@ -552,6 +555,7 @@ def call(
552555
task=task,
553556
function=function,
554557
api_key=api_key,
558+
sdk_module=get_api_module(cls.__module__),
555559
**kwargs,
556560
)
557561
# call request service.
@@ -588,7 +592,7 @@ def _get(
588592
custom_headers = kwargs.pop("headers", None)
589593
headers = {
590594
**_workspace_header(workspace),
591-
**default_headers(api_key),
595+
**default_headers(api_key, module="tasks"),
592596
}
593597
if custom_headers:
594598
headers = {
@@ -687,7 +691,7 @@ def cancel(
687691
url,
688692
headers={
689693
**_workspace_header(workspace),
690-
**default_headers(api_key),
694+
**default_headers(api_key, module="tasks"),
691695
},
692696
)
693697
return _handle_http_response(response)
@@ -751,7 +755,7 @@ def list(
751755
params=params,
752756
headers={
753757
**_workspace_header(workspace),
754-
**default_headers(api_key),
758+
**default_headers(api_key, module="tasks"),
755759
},
756760
)
757761
if response.status_code == HTTPStatus.OK:
@@ -934,6 +938,7 @@ def async_call(
934938
api_key=api_key,
935939
async_request=True,
936940
query=False,
941+
sdk_module=get_api_module(cls.__module__),
937942
**kwargs,
938943
)
939944
return request.call()
@@ -946,6 +951,7 @@ def _get(
946951
api_key=None,
947952
flattened_output=False,
948953
workspace: str = None,
954+
module: str = "",
949955
**kwargs,
950956
) -> Union[DashScopeAPIResponse, Dict]:
951957
timeout = kwargs.pop(
@@ -958,7 +964,7 @@ def _get(
958964
url,
959965
headers={
960966
**_workspace_header(workspace),
961-
**default_headers(api_key),
967+
**default_headers(api_key, module=module),
962968
**kwargs.pop("headers", {}),
963969
},
964970
params=params,
@@ -1025,6 +1031,7 @@ def list(
10251031
params=params,
10261032
api_key=api_key,
10271033
workspace=workspace,
1034+
module=get_api_module(cls.__module__),
10281035
**kwargs,
10291036
)
10301037

@@ -1060,6 +1067,7 @@ def list(
10601067
params=params,
10611068
api_key=api_key,
10621069
workspace=workspace,
1070+
module=get_api_module(cls.__module__),
10631071
**kwargs,
10641072
)
10651073

@@ -1099,6 +1107,7 @@ def logs( # pylint: disable=unused-argument
10991107
params=params,
11001108
api_key=api_key,
11011109
workspace=workspace,
1110+
module=get_api_module(cls.__module__),
11021111
**kwargs,
11031112
)
11041113

@@ -1142,6 +1151,7 @@ def get(
11421151
params=params,
11431152
flattened_output=flattened_output,
11441153
workspace=workspace,
1154+
module=get_api_module(cls.__module__),
11451155
**kwargs,
11461156
)
11471157

@@ -1181,6 +1191,7 @@ def get(
11811191
api_key=api_key,
11821192
flattened_output=flattened_output,
11831193
workspace=workspace,
1194+
module=get_api_module(cls.__module__),
11841195
**kwargs,
11851196
)
11861197

@@ -1225,7 +1236,7 @@ def delete(
12251236
url,
12261237
headers={
12271238
**_workspace_header(workspace),
1228-
**default_headers(api_key),
1239+
**default_headers(api_key, module=get_api_module(cls.__module__)),
12291240
**kwargs.pop("headers", {}),
12301241
},
12311242
timeout=timeout,
@@ -1275,7 +1286,7 @@ def call(
12751286
headers={
12761287
"Content-Type": "application/json; charset=utf-8",
12771288
**_workspace_header(workspace),
1278-
**default_headers(api_key),
1289+
**default_headers(api_key, module=get_api_module(cls.__module__)),
12791290
**kwargs.pop("headers", {}),
12801291
},
12811292
timeout=timeout,
@@ -1332,6 +1343,7 @@ def update(
13321343
flattened_output = kwargs.pop("flattened_output", False)
13331344
import json as _json # pylint: disable=reimported
13341345

1346+
module = get_api_module(cls.__module__)
13351347
with requests.Session() as session:
13361348
logger.debug("Starting request: %s", url)
13371349
body = _json.dumps(json, ensure_ascii=False).encode("utf-8")
@@ -1342,7 +1354,7 @@ def update(
13421354
headers={
13431355
"Content-Type": "application/json; charset=utf-8",
13441356
**_workspace_header(workspace),
1345-
**default_headers(api_key),
1357+
**default_headers(api_key, module=module),
13461358
**kwargs.pop("headers", {}),
13471359
},
13481360
timeout=timeout,
@@ -1354,7 +1366,7 @@ def update(
13541366
headers={
13551367
"Content-Type": "application/json; charset=utf-8",
13561368
**_workspace_header(workspace),
1357-
**default_headers(api_key),
1369+
**default_headers(api_key, module=module),
13581370
**kwargs.pop("headers", {}),
13591371
},
13601372
timeout=timeout,
@@ -1409,7 +1421,7 @@ def put(
14091421
headers={
14101422
"Content-Type": "application/json; charset=utf-8",
14111423
**_workspace_header(workspace),
1412-
**default_headers(api_key),
1424+
**default_headers(api_key, module=get_api_module(cls.__module__)),
14131425
**kwargs.pop("headers", {}),
14141426
},
14151427
timeout=timeout,
@@ -1461,7 +1473,7 @@ def upload( # pylint: disable=unused-argument
14611473
data=js,
14621474
headers={
14631475
**_workspace_header(workspace),
1464-
**default_headers(api_key),
1476+
**default_headers(api_key, module=get_api_module(cls.__module__)),
14651477
**kwargs.pop("headers", {}),
14661478
},
14671479
files=files,
@@ -1511,7 +1523,7 @@ def cancel(
15111523
url,
15121524
headers={
15131525
**_workspace_header(workspace),
1514-
**default_headers(api_key),
1526+
**default_headers(api_key, module=get_api_module(cls.__module__)),
15151527
**kwargs.pop("headers", {}),
15161528
},
15171529
timeout=timeout,
@@ -1618,7 +1630,7 @@ def stream_events(
16181630
url,
16191631
headers={
16201632
**_workspace_header(workspace),
1621-
**default_headers(api_key),
1633+
**default_headers(api_key, module=get_api_module(cls.__module__)),
16221634
**kwargs.pop("headers", {}),
16231635
},
16241636
stream=True,

‎dashscope/common/utils.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ def get_user_agent():
165165

166166
_SDK_CLIENT = "python-sdk"
167167

168+
# Package name -> sdk module tag, for packages whose directory name does
169+
# not match the module segment (threads/messages/runs/steps belong to the
170+
# assistants module).
171+
_SDK_MODULE_PACKAGE_OVERRIDES = {
172+
"threads": "assistants",
173+
}
174+
175+
176+
def get_api_module(module_name: str) -> str:
177+
"""Derive the sdk module tag from an API class's __module__.
178+
179+
e.g. "dashscope.aigc.generation" -> "aigc",
180+
"dashscope.threads.runs.runs" -> "assistants".
181+
"""
182+
parts = module_name.split(".")
183+
if len(parts) < 2 or parts[0] != "dashscope":
184+
return ""
185+
pkg = parts[1]
186+
return _SDK_MODULE_PACKAGE_OVERRIDES.get(pkg, pkg)
187+
168188

169189
def set_sdk_client(client: str) -> None:
170190
"""Override the client identifier in the x-dashscope-sdk-client header.
@@ -196,10 +216,10 @@ def get_sdk_headers(module: str = "") -> Dict[str, str]:
196216
}
197217

198218

199-
def default_headers(api_key: str = None) -> Dict[str, str]:
219+
def default_headers(api_key: str = None, module: str = "") -> Dict[str, str]:
200220
ua = get_user_agent()
201221
headers = {"user-agent": ua}
202-
headers.update(get_sdk_headers())
222+
headers.update(get_sdk_headers(module=module))
203223
if api_key is None:
204224
api_key = get_default_api_key()
205225
headers["Authorization"] = f"Bearer {api_key}"

0 commit comments

Comments
 (0)