Skip to content

Commit 8d188b7

Browse files
committed
feat: support tasks and tokenization api
1 parent 0083cf6 commit 8d188b7

36 files changed

+742
-30
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents input of batch embedding.
5+
/// </summary>
6+
public class BatchGetEmbeddingsInput
7+
{
8+
/// <summary>
9+
/// The url of text file to compute embeddings from.
10+
/// </summary>
11+
public required string Url { get; set; }
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Cnblogs.DashScope.Sdk.Internals;
2+
3+
namespace Cnblogs.DashScope.Sdk;
4+
5+
/// <summary>
6+
/// The output of batch get embeddings api.
7+
/// </summary>
8+
public record BatchGetEmbeddingsOutput : DashScopeTaskOutput
9+
{
10+
/// <summary>
11+
/// The url of embedding file.
12+
/// </summary>
13+
public string? Url { get; set; }
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Optional parameter of batch get embeddings request.
5+
/// </summary>
6+
public class BatchGetEmbeddingsParameters
7+
{
8+
/// <summary>
9+
/// Text type of input. Use <see cref="TextTypes"/> to get available options. Defaults to 'document'.
10+
/// </summary>
11+
public string? TextType { get; set; }
12+
}

src/Cnblogs.DashScope.Sdk/DashScopeClientCore.cs

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,131 @@ public async Task<ModelResponse<TextEmbeddingOutput, TextEmbeddingTokenUsage>> G
8080
cancellationToken))!;
8181
}
8282

83+
/// <inheritdoc />
84+
public async
85+
Task<ModelResponse<BatchGetEmbeddingsOutput, TextEmbeddingTokenUsage>> BatchGetEmbeddingsAsync(
86+
ModelRequest<BatchGetEmbeddingsInput, BatchGetEmbeddingsParameters> input,
87+
CancellationToken cancellationToken = default)
88+
{
89+
var request = BuildRequest(HttpMethod.Post, ApiLinks.TextEmbedding, input, isTask: true);
90+
return (await SendAsync<ModelResponse<BatchGetEmbeddingsOutput, TextEmbeddingTokenUsage>>(
91+
request,
92+
cancellationToken))!;
93+
}
94+
95+
/// <inheritdoc />
96+
public async Task<DashScopeTask<TOutput, TUsage>> GetTaskAsync<TOutput, TUsage>(
97+
string taskId,
98+
CancellationToken cancellationToken = default)
99+
where TUsage : class
100+
{
101+
var request = BuildRequest(HttpMethod.Get, $"{ApiLinks.Tasks}{taskId}");
102+
return (await SendAsync<DashScopeTask<TOutput, TUsage>>(request, cancellationToken))!;
103+
}
104+
105+
/// <inheritdoc />
106+
public async Task<DashScopeTaskList> ListTasksAsync(
107+
string? taskId = null,
108+
DateTime? startTime = null,
109+
DateTime? endTime = null,
110+
string? modelName = null,
111+
DashScopeTaskStatus? status = null,
112+
int? pageNo = null,
113+
int? pageSize = null,
114+
CancellationToken cancellationToken = default)
115+
{
116+
var queryString = new StringBuilder();
117+
if (string.IsNullOrEmpty(taskId) == false)
118+
{
119+
queryString.Append($"task_id={taskId}");
120+
}
121+
122+
if (startTime.HasValue)
123+
{
124+
queryString.Append($"start_time={startTime:YYYYMMDDhhmmss}");
125+
}
126+
127+
if (endTime.HasValue)
128+
{
129+
queryString.Append($"end_time={endTime:YYYYMMDDhhmmss}");
130+
}
131+
132+
if (string.IsNullOrEmpty(modelName) == false)
133+
{
134+
queryString.Append($"model_name={modelName}");
135+
}
136+
137+
if (status.HasValue)
138+
{
139+
queryString.Append($"status={status}");
140+
}
141+
142+
if (pageNo.HasValue)
143+
{
144+
queryString.Append($"page_no={pageNo}");
145+
}
146+
147+
if (pageSize.HasValue)
148+
{
149+
queryString.Append($"page_size={pageSize}");
150+
}
151+
152+
var request = BuildRequest(HttpMethod.Get, $"{ApiLinks.Tasks}?{queryString}");
153+
return (await SendAsync<DashScopeTaskList>(request, cancellationToken))!;
154+
}
155+
156+
/// <inheritdoc />
157+
public async Task<DashScopeTaskOperationResponse> CancelTaskAsync(
158+
string taskId,
159+
CancellationToken cancellationToken = default)
160+
{
161+
var request = BuildRequest(HttpMethod.Post, $"{ApiLinks.Tasks}{taskId}/cancel");
162+
return (await SendAsync<DashScopeTaskOperationResponse>(request, cancellationToken))!;
163+
}
164+
165+
/// <inheritdoc />
166+
public async Task<ModelResponse<TokenizationOutput, TokenizationUsage>> TokenizeAsync(
167+
ModelRequest<TextGenerationInput, TextGenerationParameters> input,
168+
CancellationToken cancellationToken = default)
169+
{
170+
var request = BuildRequest(HttpMethod.Post, ApiLinks.Tokenizer, input);
171+
return (await SendAsync<ModelResponse<TokenizationOutput, TokenizationUsage>>(request, cancellationToken))!;
172+
}
173+
83174
private static HttpRequestMessage BuildSseRequest<TPayload>(HttpMethod method, string url, TPayload payload)
175+
where TPayload : class
84176
{
85177
return BuildRequest(method, url, payload, true);
86178
}
87179

180+
private static HttpRequestMessage BuildRequest(HttpMethod method, string url)
181+
{
182+
return BuildRequest(method, url, (string?)null);
183+
}
184+
88185
private static HttpRequestMessage BuildRequest<TPayload>(
89186
HttpMethod method,
90187
string url,
91-
TPayload payload,
92-
bool sse = false)
188+
TPayload? payload = null,
189+
bool sse = false,
190+
bool isTask = false)
191+
where TPayload : class
93192
{
94193
var message = new HttpRequestMessage(method, url)
95194
{
96-
Content = JsonContent.Create(payload, options: SerializationOptions)
195+
Content = payload != null ? JsonContent.Create(payload, options: SerializationOptions) : null
97196
};
98197

99198
if (sse)
100199
{
101200
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
102201
}
103202

203+
if (isTask)
204+
{
205+
message.Headers.Add("X-DashScope-Async", "enable");
206+
}
207+
104208
return message;
105209
}
106210

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents the fetch result of an asynchronous task.
5+
/// </summary>
6+
/// <param name="RequestId">The unique id of this query request.</param>
7+
/// <param name="Output">The task output.</param>
8+
/// <param name="Usage">The task usage.</param>
9+
/// <typeparam name="TOutput">The type of task output.</typeparam>
10+
/// <typeparam name="TUsage">The type of task usage.</typeparam>
11+
public record DashScopeTask<TOutput, TUsage>(string RequestId, TOutput Output, TUsage? Usage = null)
12+
where TUsage : class;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents one page of DashScope task list.
5+
/// </summary>
6+
/// <param name="RequestId">The unique id of this request.</param>
7+
/// <param name="Data">The query result data.</param>
8+
/// <param name="TotalPage">Total number of pages.</param>
9+
/// <param name="Total">Total number of items.</param>
10+
/// <param name="PageNo">Page index of this request.</param>
11+
/// <param name="PageSize">Page size of this request.</param>
12+
public record DashScopeTaskList(
13+
string RequestId,
14+
DashScopeTaskListItem[] Data,
15+
int TotalPage,
16+
int Total,
17+
int PageNo,
18+
int PageSize);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Represents one list item of dash scope task list.
5+
/// </summary>
6+
/// <param name="ApiKeyId">The id of api key.</param>
7+
/// <param name="CallerParentId">The aliyun parent uid of task creator.</param>
8+
/// <param name="CallerUid">The aliyun uid of task creator.</param>
9+
/// <param name="GmtCreate">The timestamp of task creation.</param>
10+
/// <param name="StartTime">The timestamp of task start.</param>
11+
/// <param name="EndTime">The timestamp of task end.</param>
12+
/// <param name="Region">The region of this task belongs.</param>
13+
/// <param name="RequestId">The request id this task created by</param>
14+
/// <param name="Status">Status of this task.</param>
15+
/// <param name="TaskId">Id of this task.</param>
16+
/// <param name="UserApiUniqueKey">Unique key of model api information.</param>
17+
/// <param name="ModelName">The model name of this task using.</param>
18+
public record DashScopeTaskListItem(
19+
string ApiKeyId,
20+
string CallerParentId,
21+
string CallerUid,
22+
long GmtCreate,
23+
long? StartTime,
24+
long? EndTime,
25+
string Region,
26+
string RequestId,
27+
DashScopeTaskStatus Status,
28+
string TaskId,
29+
string UserApiUniqueKey,
30+
string ModelName);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Cnblogs.DashScope.Sdk;
4+
5+
/// <summary>
6+
/// The metrics of one DashScope task.
7+
/// </summary>
8+
/// <param name="Total">The total number of subtasks.</param>
9+
/// <param name="Succeeded">The number of succeeded subtasks.</param>
10+
/// <param name="Failed">The number of failed subtasks.</param>
11+
public record DashScopeTaskMetrics(
12+
[property: JsonPropertyName("TOTAL")] int Total,
13+
[property: JsonPropertyName("SUCCEEDED")]
14+
int Succeeded,
15+
[property: JsonPropertyName("FAILED")]
16+
int Failed);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The operation result of operation to DashScope task.
5+
/// </summary>
6+
/// <param name="RequestId">The id of this request.</param>
7+
/// <param name="Code">The error code, not null if operation failed.</param>
8+
/// <param name="Message">The error message, not null if operation failed.</param>
9+
public record DashScopeTaskOperationResponse(string RequestId, string? Code, string? Message);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Cnblogs.DashScope.Sdk;
4+
5+
/// <summary>
6+
/// Represents status of DashScope task.
7+
/// </summary>
8+
[JsonConverter(typeof(JsonStringEnumConverter<DashScopeTaskStatus>))]
9+
public enum DashScopeTaskStatus
10+
{
11+
/// <summary>
12+
/// Task not running yet.
13+
/// </summary>
14+
Pending = 1,
15+
16+
/// <summary>
17+
/// Task is running.
18+
/// </summary>
19+
Running = 2,
20+
21+
/// <summary>
22+
/// Task has succeeded.
23+
/// </summary>
24+
/// <remarks>If task contains multiple subtask, then this means at lease one subtask has been succeeded.</remarks>
25+
Succeeded = 3,
26+
27+
/// <summary>
28+
/// Task has failed.
29+
/// </summary>
30+
Failed = 4,
31+
32+
/// <summary>
33+
/// Task status unknown, usually because the task is not exits.
34+
/// </summary>
35+
Unknown = 5
36+
}

0 commit comments

Comments
 (0)