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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,6 @@ cython_debug/
.pypirc

# acli
.acli/
.acli

.idea
1 change: 1 addition & 0 deletions dashscope/aigc/code_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def call( # type: ignore[override]
api_key=api_key,
input=input,
workspace=workspace,
is_service=False,
**parameters,
)

Expand Down
9 changes: 6 additions & 3 deletions dashscope/api_entities/aiohttp_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
self.async_request = async_request
self._external_aio_session = session
self.headers = {
"Accept": "application/json",
"Accept": "application/json; charset=utf-8",
"Authorization": f"Bearer {api_key}",
"Cache-Control": "no-cache",
**self.headers, # type: ignore[has-type]
Expand All @@ -70,7 +70,7 @@ def __init__(
}
self.method = http_method
if self.method == HTTPMethod.POST:
self.headers["Content-Type"] = "application/json"
self.headers["Content-Type"] = "application/json; charset=utf-8"

self.stream = stream
if self.stream:
Expand Down Expand Up @@ -286,10 +286,13 @@ async def _handle_request(self):
timeout=request_timeout,
)
else:
body = json.dumps(obj, ensure_ascii=False).encode(
"utf-8",
)
response = await session.request(
"POST",
url=self.url,
json=obj,
data=body,
headers=self.headers,
timeout=request_timeout,
)
Expand Down
10 changes: 8 additions & 2 deletions dashscope/api_entities/api_request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ def get_aiohttp_payload(self):
form.add_field(key, value)
form.add_field("model", data["model"])
if "input" in data:
form.add_field("input", json.dumps(data["input"]))
form.add_field("parameters", json.dumps(data["parameters"]))
form.add_field(
"input",
json.dumps(data["input"], ensure_ascii=False),
)
form.add_field(
"parameters",
json.dumps(data["parameters"], ensure_ascii=False),
)
return True, form()
# pylint: disable=unreachable,pointless-string-statement
"""
Expand Down
14 changes: 10 additions & 4 deletions dashscope/api_entities/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(
self._external_session = None
self._external_aio_session = None
self.headers: Dict = {
"Accept": "application/json",
"Accept": "application/json; charset=utf-8",
"Authorization": f"Bearer {api_key}",
**self.headers,
}
Expand All @@ -115,7 +115,7 @@ def __init__(
}
self.method = http_method
if self.method == HTTPMethod.POST:
self.headers["Content-Type"] = "application/json"
self.headers["Content-Type"] = "application/json; charset=utf-8"

self.stream = stream
if self.stream:
Expand Down Expand Up @@ -194,10 +194,13 @@ async def _handle_aio_request(self): # pylint: disable=too-many-branches
timeout=request_timeout,
)
else:
body = json.dumps(obj, ensure_ascii=False).encode(
"utf-8",
)
response = await session.request(
"POST",
url=self.url,
json=obj,
data=body,
headers=self.headers,
timeout=request_timeout,
)
Expand Down Expand Up @@ -483,10 +486,13 @@ def _handle_request(self): # pylint: disable=too-many-branches
)
else:
logger.debug("Request body: %s", obj)
body = json.dumps(obj, ensure_ascii=False).encode(
"utf-8",
)
response = session.post(
url=self.url,
stream=self.stream,
json=obj,
data=body,
headers={**self.headers},
timeout=self.timeout,
)
Expand Down
4 changes: 2 additions & 2 deletions dashscope/api_entities/websocket_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(

self.headers = {
"Authorization": f"bearer {api_key}",
**self.headers,
**self.headers, # type: ignore[has-type]
}

self.task_headers = {
Expand Down Expand Up @@ -422,4 +422,4 @@ async def _check_websocket_unexpected_message(self, msg):

def _build_up_message(self, headers, payload):
message = {"header": headers, "payload": payload}
return json.dumps(message)
return json.dumps(message, ensure_ascii=False)
2 changes: 1 addition & 1 deletion dashscope/cli/agentic_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def logs(
)

format_output(
{"job_id": job_id, "logs": result.output.get("logs", "")},
{"job_id": job_id, "logs": getattr(result.output, "logs", "")},
fmt=output_format,
)
except Exception as e:
Expand Down
32 changes: 14 additions & 18 deletions dashscope/cli/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _wait_for_deployment(deployed_model: str):
while True:
rsp = dashscope.Deployments.get(deployed_model)
output = ensure_ok(rsp)
status = output["status"]
status = output.status

if status in (
DeploymentStatus.PENDING,
Expand All @@ -67,18 +67,14 @@ def _wait_for_deployment(deployed_model: str):

def _print_deployments(output):
"""Pretty-print a list of deployments from *output*."""
if (
output is None
or "deployments" not in output
or not output["deployments"]
):
if output is None or not output.deployments:
console.print("There is no deployed model!")
return
for dep in output["deployments"]:
for dep in output.deployments:
console.print(
f"Deployed_model: {dep['deployed_model']}, "
f"model: {dep['model_name']}, "
f"status: {dep['status']}",
f"Deployed_model: {dep.deployed_model}, "
f"model: {dep.model_name}, "
f"status: {dep.status}",
)


Expand Down Expand Up @@ -111,7 +107,7 @@ def create(
suffix=suffix, # type: ignore[arg-type]
)
output = ensure_ok(rsp)
deployed_model = output["deployed_model"]
deployed_model = output.deployed_model
success(f"Create model: {deployed_model} deployment")
_wait_for_deployment(deployed_model)

Expand Down Expand Up @@ -151,9 +147,9 @@ def get(
rsp = dashscope.Deployments.get(deployed_model)
output = ensure_ok(rsp)
console.print(
f"Deployed model: {output['deployed_model']} "
f"capacity: {output['capacity']} "
f"status: {output['status']}",
f"Deployed model: {output.deployed_model} "
f"capacity: {output.capacity} "
f"status: {output.status}",
)


Expand All @@ -166,7 +162,7 @@ def list_deployments(
"""List model deployments."""
rsp = dashscope.Deployments.list(page_no=page, page_size=size)
output = ensure_ok(rsp)
if output is None or not output.get("deployments"):
if output is None or not output.deployments:
console.print("There is no deployed model.")
return
_print_deployments(output)
Expand All @@ -193,9 +189,9 @@ def scale(
console.print("There is no deployed model.")
return
console.print(
f"Deployed_model: {output['deployed_model']}, "
f"model: {output['model_name']}, "
f"status: {output['status']}",
f"Deployed_model: {output.deployed_model}, "
f"model: {output.model_name}, "
f"status: {output.status}",
)


Expand Down
30 changes: 15 additions & 15 deletions dashscope/cli/fine_tunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _wait_for_job(job_id: str):
while True:
rsp = dashscope.FineTunes.get(job_id)
output = ensure_ok(rsp)
status = output["status"]
status = output.status

if status == TaskStatus.FAILED:
err_console.print("[red]Fine-tune FAILED![/red]")
Expand All @@ -62,7 +62,7 @@ def _wait_for_job(job_id: str):
if status == TaskStatus.SUCCEEDED:
success(
f"Fine-tune task success, fine-tuned model: "
f"{output['finetuned_output']}",
f"{output.finetuned_output}",
)
return

Expand All @@ -86,12 +86,12 @@ def _stream_events(job_id: str):
print_failed_message(rsp)
return

if rsp.output["status"] in (
if rsp.output.status in (
TaskStatus.FAILED,
TaskStatus.CANCELED,
TaskStatus.SUCCEEDED,
):
console.print(f"Fine-tune job: {job_id} is {rsp.output['status']}")
console.print(f"Fine-tune job: {job_id} is {rsp.output.status}")
_dump_logs(job_id)
return

Expand Down Expand Up @@ -120,9 +120,9 @@ def _dump_logs(job_id: str):
line=LOG_PAGE_SIZE,
)
output = ensure_ok(rsp)
for line in output["logs"]:
for line in output.logs:
console.print(line, highlight=False)
if len(output["logs"]) < LOG_PAGE_SIZE:
if len(output.logs) < LOG_PAGE_SIZE:
break
offset += LOG_PAGE_SIZE

Expand Down Expand Up @@ -202,7 +202,7 @@ def create(
hyper_parameters=params if params else None, # type: ignore[arg-type]
)
output = ensure_ok(rsp)
job_id = output["job_id"]
job_id = output.job_id
success(f"Create fine-tune job success, job_id: {job_id}")
_wait_for_job(job_id)

Expand Down Expand Up @@ -280,14 +280,14 @@ def get(
"""Get fine-tune job status."""
rsp = dashscope.FineTunes.get(job_id)
output = ensure_ok(rsp)
status = output["status"]
status = output.status

if status == TaskStatus.FAILED:
err_console.print("[red]Fine-tune failed![/red]")
elif status == TaskStatus.CANCELED:
console.print("Fine-tune task canceled")
elif status == TaskStatus.SUCCEEDED:
model_name = output["finetuned_output"]
model_name = output.finetuned_output
success(f"Fine-tune task success, fine-tuned model: {model_name}")
else:
console.print(f"The fine-tune task is: {status}")
Expand All @@ -302,17 +302,17 @@ def list_jobs(
"""List fine-tune jobs."""
rsp = dashscope.FineTunes.list(page=page, page_size=size)
output = ensure_ok(rsp)
if output is None or not output.get("jobs"):
if output is None or not output.jobs:
console.print("There is no fine-tuned model.")
return

for job in output["jobs"]:
for job in output.jobs:
line = (
f"job: {job['job_id']}, status: {job['status']}, "
f"base model: {job['model']}"
f"job: {job.job_id}, status: {job.status}, "
f"base model: {job.model}"
)
if job["status"] == TaskStatus.SUCCEEDED:
line += f", fine-tuned model: {job['finetuned_output']}"
if job.status == TaskStatus.SUCCEEDED:
line += f", fine-tuned model: {job.finetuned_output}"
console.print(line)


Expand Down
24 changes: 18 additions & 6 deletions dashscope/client/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
import asyncio
import collections
import json
import time
from http import HTTPStatus
from typing import Any, Dict, Iterator, List, Union
Expand Down Expand Up @@ -1266,11 +1267,13 @@ def call(
flattened_output = kwargs.pop("flattened_output", False)
with requests.Session() as session:
logger.debug("Starting request: %s", url)
body = json.dumps(data, ensure_ascii=False).encode("utf-8")
response = session.post(
url,
json=data,
data=body,
stream=stream,
headers={
"Content-Type": "application/json; charset=utf-8",
**_workspace_header(workspace),
**default_headers(api_key),
**kwargs.pop("headers", {}),
Expand All @@ -1295,7 +1298,7 @@ class UpdateMixin:
def update(
cls,
target: str,
json: object,
json: object, # pylint: disable=redefined-outer-name
api_key: str = None,
path: str = None,
workspace: str = None,
Comment thread
lzsweb marked this conversation as resolved.
Expand Down Expand Up @@ -1327,13 +1330,17 @@ def update(
DEFAULT_REQUEST_TIMEOUT_SECONDS,
)
flattened_output = kwargs.pop("flattened_output", False)
import json as _json # pylint: disable=reimported

with requests.Session() as session:
logger.debug("Starting request: %s", url)
body = _json.dumps(json, ensure_ascii=False).encode("utf-8")
if method == "post":
response = session.post(
url,
json=json,
data=body,
headers={
"Content-Type": "application/json; charset=utf-8",
**_workspace_header(workspace),
**default_headers(api_key),
**kwargs.pop("headers", {}),
Expand All @@ -1343,8 +1350,9 @@ def update(
else:
response = session.patch(
url,
json=json,
data=body,
headers={
"Content-Type": "application/json; charset=utf-8",
**_workspace_header(workspace),
**default_headers(api_key),
**kwargs.pop("headers", {}),
Expand All @@ -1360,7 +1368,7 @@ class PutMixin:
def put(
cls,
target: str,
json: object,
json: object, # pylint: disable=redefined-outer-name
path: str = None,
api_key: str = None,
workspace: str = None,
Comment thread
lzsweb marked this conversation as resolved.
Expand Down Expand Up @@ -1390,12 +1398,16 @@ def put(
REQUEST_TIMEOUT_KEYWORD,
DEFAULT_REQUEST_TIMEOUT_SECONDS,
)
import json as _json # pylint: disable=reimported

with requests.Session() as session:
logger.debug("Starting request: %s", url)
body = _json.dumps(json, ensure_ascii=False).encode("utf-8")
response = session.put(
url,
json=json,
data=body,
headers={
"Content-Type": "application/json; charset=utf-8",
**_workspace_header(workspace),
**default_headers(api_key),
**kwargs.pop("headers", {}),
Expand Down
Loading
Loading