Skip to content

Commit 06db106

Browse files
feat: add content generation task sync methods
1 parent 1686f2e commit 06db106

File tree

12 files changed

+310
-1
lines changed

12 files changed

+310
-1
lines changed

volcenginesdkarkruntime/_base_client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,44 @@ def post(
557557
ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
558558
)
559559

560+
def get(
561+
self,
562+
path: str,
563+
*,
564+
cast_to: Type[ResponseT],
565+
params: list[tuple[str, str]] | None = None,
566+
options: ExtraRequestOptions = {},
567+
stream: bool = False,
568+
stream_cls: type[_StreamT] | None = None,
569+
) -> ResponseT | _StreamT:
570+
opts = RequestOptions.construct(
571+
method="get",
572+
url=path,
573+
params=params,
574+
**options,
575+
)
576+
577+
return cast(
578+
ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
579+
)
580+
581+
def delete(
582+
self,
583+
path: str,
584+
*,
585+
cast_to: Type[ResponseT],
586+
params: list[tuple[str, str]] | None = None,
587+
options: ExtraRequestOptions = {},
588+
) -> ResponseT:
589+
opts = RequestOptions.construct( # type: ignore
590+
method="delete",
591+
url=path,
592+
params=params,
593+
**options,
594+
)
595+
596+
return cast(ResponseT, self.request(cast_to, opts))
597+
560598
def request(
561599
self,
562600
cast_to: Type[ResponseT],

volcenginesdkarkruntime/_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Ark(SyncAPIClient):
3838
embeddings: resources.Embeddings
3939
tokenization: resources.Tokenization
4040
context: resources.Context
41+
content_generation: resources.ContentGeneration
4142

4243
def __init__(
4344
self,
@@ -96,6 +97,7 @@ def __init__(
9697
self.embeddings = resources.Embeddings(self)
9798
self.tokenization = resources.Tokenization(self)
9899
self.context = resources.Context(self)
100+
self.content_generation = resources.ContentGeneration(self)
99101
# self.classification = resources.Classification(self)
100102

101103
def _get_endpoint_sts_token(self, endpoint_id: str):

volcenginesdkarkruntime/_resource.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class SyncAPIResource:
1010
def __init__(self, client: "Ark") -> None:
1111
self._client = client
1212
self._post = client.post
13+
self._get = client.get
14+
self._delete = client.delete
1315

1416

1517
class AsyncAPIResource:

volcenginesdkarkruntime/resources/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .classification import Classification, AsyncClassification
55
from .bot import BotChat, AsyncBotChat
66
from .context import Context, AsyncContext
7+
from .content_generation import ContentGeneration
78

89
__all__ = [
910
"Chat",
@@ -15,5 +16,6 @@
1516
"Tokenization",
1617
"AsyncTokenization",
1718
"Context",
18-
"AsyncContext"
19+
"AsyncContext",
20+
"ContentGeneration"
1921
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from volcenginesdkarkruntime.resources.content_generation.content_generation import ContentGeneration
2+
3+
__all__ = ["ContentGeneration"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from __future__ import annotations
2+
3+
from ..._resource import SyncAPIResource
4+
from ..._compat import cached_property
5+
6+
from .tasks import Tasks
7+
8+
__all__ = ["ContentGeneration"]
9+
10+
11+
class ContentGeneration(SyncAPIResource):
12+
@cached_property
13+
def tasks(self) -> Tasks:
14+
return Tasks(self._client)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from __future__ import annotations
2+
3+
from typing import Iterable, Union, List
4+
import httpx
5+
6+
from ..._types import Body, Query, Headers
7+
from ...types.content_generation.content_generation_task import ContentGenerationTask
8+
from ...types.content_generation.content_generation_task_id import ContentGenerationTaskID
9+
from volcenginesdkarkruntime._base_client import make_request_options
10+
from volcenginesdkarkruntime._resource import SyncAPIResource
11+
from volcenginesdkarkruntime.types.content_generation.create_task_content_param import CreateTaskContentParam
12+
from ...types.content_generation.list_content_generation_tasks_response import ListContentGenerationTasksResponse
13+
14+
15+
class Tasks(SyncAPIResource):
16+
def create(
17+
self,
18+
*,
19+
model: str,
20+
content: Iterable[CreateTaskContentParam],
21+
extra_headers: Headers | None = None,
22+
extra_query: Query | None = None,
23+
extra_body: Body | None = None,
24+
timeout: float | httpx.Timeout | None = None,
25+
) -> ContentGenerationTaskID:
26+
resp = self._post(
27+
"/contents/generations/tasks",
28+
body={
29+
"model": model,
30+
"content": content,
31+
},
32+
options=make_request_options(
33+
extra_headers=extra_headers,
34+
extra_query=extra_query,
35+
extra_body=extra_body,
36+
timeout=timeout,
37+
),
38+
cast_to=ContentGenerationTaskID,
39+
)
40+
return resp
41+
42+
43+
def get(
44+
self,
45+
task_id: str,
46+
extra_headers: Headers | None = None,
47+
extra_query: Query | None = None,
48+
extra_body: Body | None = None,
49+
timeout: float | httpx.Timeout | None = None,
50+
) -> ContentGenerationTask:
51+
resp = self._get(
52+
path=f"/contents/generations/tasks/{task_id}",
53+
options=make_request_options(
54+
extra_headers=extra_headers,
55+
extra_query=extra_query,
56+
extra_body=extra_body,
57+
timeout=timeout,
58+
),
59+
cast_to=ContentGenerationTask,
60+
)
61+
return resp
62+
63+
def list(
64+
self,
65+
page_num: int | None = None,
66+
page_size: int | None = None,
67+
status: str | None = None,
68+
task_ids : Union[List[str], str] | None = None,
69+
model: str | None = None,
70+
extra_headers: Headers | None = None,
71+
extra_body: Body | None = None,
72+
extra_query: Query | None = None,
73+
timeout: float | httpx.Timeout | None = None,
74+
) -> ListContentGenerationTasksResponse:
75+
76+
query_params = []
77+
if page_num:
78+
query_params.append(("page_num", page_num))
79+
if page_size:
80+
query_params.append(("page_size", page_size))
81+
if status:
82+
query_params.append(("filter.status", status))
83+
if model:
84+
query_params.append(("filter.model", model))
85+
if task_ids:
86+
if isinstance(task_ids, str):
87+
task_ids = [task_ids]
88+
for task_id in task_ids:
89+
query_params.append(("filter.task_ids", task_id))
90+
91+
resp = self._get(
92+
path="/contents/generations/tasks",
93+
params=query_params,
94+
options=make_request_options(
95+
extra_headers=extra_headers,
96+
extra_query=extra_query,
97+
extra_body=extra_body,
98+
timeout=timeout,
99+
),
100+
cast_to=ListContentGenerationTasksResponse,
101+
)
102+
return resp
103+
104+
def delete(
105+
self,
106+
task_id: str,
107+
extra_headers: Headers | None = None,
108+
extra_query: Query | None = None,
109+
extra_body: Body | None = None,
110+
timeout: float | httpx.Timeout | None = None,
111+
):
112+
resp = self._delete(
113+
path=f"/contents/generations/tasks/{task_id}",
114+
options=make_request_options(
115+
extra_headers=extra_headers,
116+
extra_query=extra_query,
117+
extra_body=extra_body,
118+
timeout=timeout,
119+
),
120+
cast_to=ContentGenerationTask,
121+
)
122+
return resp

volcenginesdkarkruntime/types/content_generation/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
__all__ = ["ContentGenerationTask"]
3+
4+
from volcenginesdkarkruntime._models import BaseModel
5+
6+
7+
class Usage(BaseModel):
8+
completion_tokens: int
9+
"""The number of tokens used for completion."""
10+
11+
class Content(BaseModel):
12+
video_url: str
13+
"""The URL of the generated video, if any."""
14+
15+
class ContentGenerationTask(BaseModel):
16+
id: str
17+
"""A unique identifier for the task."""
18+
19+
model: str
20+
"""The model used for the task."""
21+
22+
status: str
23+
"""The status of the task (running, failed, queued, succeeded, cancelled)."""
24+
25+
failure_reason: str
26+
"""The reason for failure, if applicable."""
27+
28+
content: Content
29+
"""The content generated by the task."""
30+
31+
usage: Usage
32+
"""The usage information for the task."""
33+
34+
created_at: int
35+
"""The Unix timestamp when the task was created."""
36+
37+
updated_at: int
38+
"""The Unix timestamp when the task was last updated."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from volcenginesdkarkruntime._models import BaseModel
2+
3+
4+
class ContentGenerationTaskID(BaseModel):
5+
id: str
6+
"""A unique identifier for the task."""

0 commit comments

Comments
 (0)