Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dashscope/acli/providers/tongyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ def __init__(
request_timeout: int = 60,
protocol: str = "openai",
base_url: str | None = None,
module: str = "app",
):
self.model = model
self.api_key = api_key
self.request_timeout = request_timeout
self.protocol = protocol
self.base_url = (base_url or DASHSCOPE_BASE_URL).rstrip("/")
self.module = module

def _convert_tools(self, tools: list[dict] | None) -> list[dict] | None:
if not tools:
Expand Down Expand Up @@ -126,8 +128,10 @@ def _get_headers(self) -> dict:
if self.api_key:
headers["Authorization"] = f"Bearer {self.api_key}"
if not os.environ.get("DASHSCOPE_DISABLE_SDK_HEADERS"):
headers["x-dashscope-sdk-client"] = "acli"
headers["x-dashscope-sdk-version"] = __version__
parts = ["acli", __version__]
if self.module:
parts.append(self.module)
headers["x-dashscope-sdk-client"] = "/".join(parts)
headers["x-dashscope-sdk-session-id"] = SDK_SESSION_ID
return headers

Expand Down
2 changes: 1 addition & 1 deletion dashscope/agentstudio/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def build_headers(
HEADER_AUTHORIZATION: f"Bearer {api_key}",
HEADER_USER_AGENT: user_agent,
}
headers.update(get_sdk_headers())
headers.update(get_sdk_headers(module="agentstudio"))
if json_body:
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
Expand Down
4 changes: 3 additions & 1 deletion dashscope/api_entities/api_request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def _build_api_request( # pylint: disable=too-many-branches
task_id: Optional[str] = None,
enable_encryption: bool = False,
pre_task_id: Optional[str] = None,
# SDK module tag for the x-dashscope-sdk-client header
sdk_module: str = "",
# Additional parameters for API request data
**kwargs,
):
Expand Down Expand Up @@ -192,7 +194,7 @@ def _build_api_request( # pylint: disable=too-many-branches
"websocket]",
)

merged_headers = dict(get_sdk_headers())
merged_headers = dict(get_sdk_headers(module=sdk_module))
if headers is not None:
merged_headers.update(headers)
if merged_headers:
Expand Down
2 changes: 2 additions & 0 deletions dashscope/api_entities/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DEFAULT_REQUEST_TIMEOUT_SECONDS,
)
from dashscope.common.logging import logger
from dashscope.common.utils import get_sdk_headers


class Encryption:
Expand Down Expand Up @@ -90,6 +91,7 @@ def _get_public_keys():
url = dashscope.base_http_api_url + "/public-keys/latest"
headers = {
"Authorization": f"Bearer {dashscope.api_key}",
**get_sdk_headers(module="utils"),
}

response = requests.get(
Expand Down
1 change: 1 addition & 0 deletions dashscope/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def call( # type: ignore[override]
workspace=workspace,
api_key=api_key,
is_service=False,
sdk_module="app",
**parameters,
)
# call request service.
Expand Down
5 changes: 4 additions & 1 deletion dashscope/audio/http_tts/http_speech_synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _http_call(

# Get base URL
import dashscope
from dashscope.common.utils import join_url
from dashscope.common.utils import get_sdk_headers, join_url

if url:
base_url = url
Expand Down Expand Up @@ -233,6 +233,9 @@ def _http_call(
stream=stream,
)

# Tag sdk module (custom headers below may still override)
request.add_headers(get_sdk_headers(module="audio"))

# Add custom headers
if headers:
request.add_headers(headers)
Expand Down
3 changes: 2 additions & 1 deletion dashscope/audio/qwen_omni/omni_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import dashscope
from dashscope.common.error import ModelRequired
from dashscope.common.logging import logger
from dashscope.common.utils import get_user_agent
from dashscope.common.utils import get_sdk_headers, get_user_agent


class OmniRealtimeCallback:
Expand Down Expand Up @@ -259,6 +259,7 @@ def _get_websocket_header(self):
headers = {
"user-agent": ua,
"Authorization": "Bearer " + self.apikey,
**get_sdk_headers(module="audio"),
}
if self.user_headers:
headers = {**self.user_headers, **headers}
Expand Down
3 changes: 2 additions & 1 deletion dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import dashscope
from dashscope.common.error import ModelRequired
from dashscope.common.logging import logger
from dashscope.common.utils import get_user_agent
from dashscope.common.utils import get_sdk_headers, get_user_agent


class QwenTtsRealtimeCallback:
Expand Down Expand Up @@ -117,6 +117,7 @@ def _get_websocket_header(self):
headers = {
"user-agent": ua,
"Authorization": "Bearer " + self.apikey,
**get_sdk_headers(module="audio"),
}
if self.user_headers:
headers = {**self.user_headers, **headers}
Expand Down
3 changes: 2 additions & 1 deletion dashscope/audio/tts_v2/speech_synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
RequestFailure,
)
from dashscope.common.logging import logger
from dashscope.common.utils import get_user_agent
from dashscope.common.utils import get_sdk_headers, get_user_agent
from dashscope.protocol.websocket import (
ACTION_KEY,
ERROR_MESSAGE,
Expand Down Expand Up @@ -183,6 +183,7 @@ def get_websocket_headers(self, headers, workspace):
self.headers = {
"user-agent": ua,
"Authorization": "Bearer " + self.apikey,
**get_sdk_headers(module="audio"),
}
if headers:
self.headers = {**self.headers, **headers}
Expand Down
3 changes: 3 additions & 0 deletions dashscope/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dashscope # noqa: E402
from dashscope.cli.common import err_console # noqa: E402
from dashscope.common.error import AuthenticationError # noqa: E402
from dashscope.common.utils import set_sdk_client # noqa: E402
from dashscope.cli import ( # noqa: E402
application,
auth,
Expand All @@ -43,6 +44,8 @@
video_synthesis,
)

set_sdk_client("python-cli")


# ---------------------------------------------------------------------------
# Legacy command compatibility layer
Expand Down
17 changes: 12 additions & 5 deletions dashscope/cli/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ def create(
"--capacity",
help="The target capacity",
),
plan: Optional[str] = typer.Option(
None,
plan: str = typer.Option(
...,
"--plan",
help="Deployment plan or template ID",
help="Required. Deployment plan: ptu (PTU reserved), "
"mu (model unit), or Lora (LoRA deployment).",
),
template_id: Optional[str] = typer.Option(
None,
Expand All @@ -145,9 +146,8 @@ def create(
"model": model,
"capacity": capacity,
"suffix": suffix,
"plan": plan,
}
if plan is not None:
kwargs["plan"] = plan
if template_id is not None:
kwargs["template_id"] = template_id

Expand Down Expand Up @@ -194,12 +194,19 @@ def call(
"--capacity",
help="The target capacity",
),
plan: str = typer.Option(
...,
"--plan",
help="Required. Deployment plan: ptu (PTU reserved), "
"mu (model unit), or Lora (LoRA deployment).",
),
):
"""(Deprecated: use 'create' instead) Create a model deployment."""
create(
model=model,
suffix=suffix,
capacity=capacity,
plan=plan,
)


Expand Down
105 changes: 105 additions & 0 deletions dashscope/cli/image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ensure_ok,
handle_sdk_error,
normalize_local_path_or_url,
success,
)

app = typer.Typer(
Expand Down Expand Up @@ -86,3 +87,107 @@ def create(
usage = getattr(response, "usage", None)
if usage:
console.print_json(json.dumps(usage, ensure_ascii=False))


@app.command("fetch")
@handle_sdk_error("Fetch image generation task failed")
def fetch(
task_id: str = typer.Argument(..., help="The image generation task id"),
workspace: Optional[str] = typer.Option(
None,
"-w",
"--workspace",
help="The DashScope workspace id",
),
):
"""Fetch image generation task status or result."""
response = ImageGeneration.fetch(task_id, workspace=workspace)
output = ensure_ok(response)
console.print_json(json.dumps(output, ensure_ascii=False))


@app.command("wait")
@handle_sdk_error("Wait image generation task failed")
def wait(
task_id: str = typer.Argument(..., help="The image generation task id"),
workspace: Optional[str] = typer.Option(
None,
"-w",
"--workspace",
help="The DashScope workspace id",
),
):
"""Wait for an image generation task to complete."""
response = ImageGeneration.wait(task_id, workspace=workspace)
output = ensure_ok(response)
console.print_json(json.dumps(output, ensure_ascii=False))


@app.command("cancel")
@handle_sdk_error("Cancel image generation task failed")
def cancel(
task_id: str = typer.Argument(..., help="The image generation task id"),
workspace: Optional[str] = typer.Option(
None,
"-w",
"--workspace",
help="The DashScope workspace id",
),
):
"""Cancel a pending image generation task."""
ensure_ok(ImageGeneration.cancel(task_id, workspace=workspace))
success(f"Cancel image generation task: {task_id} success")


@app.command("list")
@handle_sdk_error("List image generation tasks failed")
def list_tasks(
start_time: Optional[str] = typer.Option(
None,
"--start-time",
help="Task start time",
),
end_time: Optional[str] = typer.Option(
None,
"--end-time",
help="Task end time",
),
model_name: Optional[str] = typer.Option(
None,
"--model-name",
help="Model name",
),
api_key_id: Optional[str] = typer.Option(
None,
"--api-key-id",
help="API key id",
),
region: Optional[str] = typer.Option(
None,
"--region",
help="Service region",
),
status: Optional[str] = typer.Option(None, "--status", help="Task status"),
page: int = typer.Option(1, "-p", "--page", help="Page number"),
size: int = typer.Option(10, "-s", "--size", help="Page size"),
workspace: Optional[str] = typer.Option(
None,
"-w",
"--workspace",
help="The DashScope workspace id",
),
):
"""List image generation tasks."""
response = ImageGeneration.list(
start_time=start_time,
end_time=end_time,
model_name=model_name,
api_key_id=api_key_id,
region=region,
status=status,
page_no=page,
page_size=size,
workspace=workspace,
)
output = ensure_ok(response)
console.print_json(json.dumps(output, ensure_ascii=False))
Loading
Loading