Skip to content

Commit 96ba00d

Browse files
committed
feat: support wanx image generation
1 parent 649b0c5 commit 96ba00d

21 files changed

+361
-6
lines changed

Cnblogs.DashScope.Sdk.sln.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESample_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESample_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESdk_002EUnitTests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Cnblogs.DashScope.Sdk/DashScopeClientCore.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ public async Task<ModelResponse<TokenizationOutput, TokenizationUsage>> Tokenize
180180
return (await SendAsync<ModelResponse<TokenizationOutput, TokenizationUsage>>(request, cancellationToken))!;
181181
}
182182

183+
/// <inheritdoc />
184+
public async Task<ModelResponse<ImageGenerationOutput, ImageGenerationUsage>> CreateImageGenerationTaskAsync(
185+
ModelRequest<ImageGenerationInput> input,
186+
CancellationToken cancellationToken = default)
187+
{
188+
var request = BuildRequest(HttpMethod.Post, ApiLinks.ImageGeneration, input, isTask: true);
189+
return (await SendAsync<ModelResponse<ImageGenerationOutput, ImageGenerationUsage>>(request, cancellationToken))
190+
!;
191+
}
192+
183193
private static HttpRequestMessage BuildSseRequest<TPayload>(HttpMethod method, string url, TPayload payload)
184194
where TPayload : class
185195
{

src/Cnblogs.DashScope.Sdk/IDashScopeClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,14 @@ Task<DashScopeTaskList> ListTasksAsync(
128128
Task<ModelResponse<TokenizationOutput, TokenizationUsage>> TokenizeAsync(
129129
ModelRequest<TextGenerationInput, TextGenerationParameters> input,
130130
CancellationToken cancellationToken = default);
131+
132+
/// <summary>
133+
/// Create a image generation task.
134+
/// </summary>
135+
/// <param name="input">The input of task.</param>
136+
/// <param name="cancellationToken">The cancellation token to use.</param>
137+
/// <returns></returns>
138+
Task<ModelResponse<ImageGenerationOutput, ImageGenerationUsage>> CreateImageGenerationTaskAsync(
139+
ModelRequest<ImageGenerationInput> input,
140+
CancellationToken cancellationToken = default);
131141
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The input of image generation.
5+
/// </summary>
6+
public class ImageGenerationInput
7+
{
8+
/// <summary>
9+
/// The image url to generation new image from.
10+
/// </summary>
11+
public required string ImageUrl { get; set; }
12+
13+
/// <summary>
14+
/// The style the new image should use, checkout docs for sample image of different indexes: https://help.aliyun.com/zh/dashscope/developer-reference/tongyi-wanxiang-style-repaint
15+
/// </summary>
16+
public required int StyleIndex { get; set; }
17+
}
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+
using Cnblogs.DashScope.Sdk.Internals;
3+
4+
namespace Cnblogs.DashScope.Sdk;
5+
6+
/// <summary>
7+
/// The output of image generation task.
8+
/// </summary>
9+
public record ImageGenerationOutput : DashScopeTaskOutput
10+
{
11+
/// <summary>
12+
/// The generated image url.
13+
/// </summary>
14+
public List<ImageGenerationResult>? Results { get; set; }
15+
16+
/// <summary>
17+
/// The style index of generated image.
18+
/// </summary>
19+
public int StyleIndex { get; set; }
20+
21+
/// <summary>
22+
/// The generation start time.
23+
/// </summary>
24+
[JsonConverter(typeof(DashScopeDateTimeConvertor))]
25+
public DateTime? StartTime { get; set; }
26+
27+
/// <summary>
28+
/// The error code, will be 0 when succeeded.
29+
/// </summary>
30+
public int? ErrorCode { get; set; }
31+
32+
/// <summary>
33+
/// The error message, will be 'Success' when succeeded.
34+
/// </summary>
35+
public string? ErrorMessage { get; set; }
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The result of image generation task.
5+
/// </summary>
6+
/// <param name="Url">The url of generated image.</param>
7+
public record ImageGenerationResult(string Url);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The usage of one image generation request.
5+
/// </summary>
6+
/// <param name="ImageCount">The generated image count.</param>
7+
public record ImageGenerationUsage(int ImageCount);

src/Cnblogs.DashScope.Sdk/ModelRequest.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
/// Represents a request for model generation.
55
/// </summary>
66
/// <typeparam name="TInput">The input type for this request.</typeparam>
7-
/// <typeparam name="TParameter">The option type for this request.</typeparam>
8-
public class ModelRequest<TInput, TParameter>
7+
public class ModelRequest<TInput>
98
where TInput : class
10-
where TParameter : class
119
{
1210
/// <summary>
1311
/// The model to use.
@@ -18,7 +16,17 @@ public class ModelRequest<TInput, TParameter>
1816
/// Input of this request.
1917
/// </summary>
2018
public required TInput Input { get; init; }
19+
}
2120

21+
/// <summary>
22+
/// Represents a request for model generation.
23+
/// </summary>
24+
/// <typeparam name="TInput">The input type for this request.</typeparam>
25+
/// <typeparam name="TParameter">The option type for this request.</typeparam>
26+
public class ModelRequest<TInput, TParameter> : ModelRequest<TInput>
27+
where TInput : class
28+
where TParameter : class
29+
{
2230
/// <summary>
2331
/// Optional configuration of this request.
2432
/// </summary>

src/Cnblogs.DashScope.Sdk/Wanx/WanxGenerationApi.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,57 @@ public static Task<DashScopeTask<ImageSynthesisOutput, ImageSynthesisUsage>> Get
7575
{
7676
return dashScopeClient.GetTaskAsync<ImageSynthesisOutput, ImageSynthesisUsage>(taskId, cancellationToken);
7777
}
78+
79+
/// <summary>
80+
/// Creates a wanx image generation task with given model and input.
81+
/// </summary>
82+
/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param>
83+
/// <param name="model">The model to use.</param>
84+
/// <param name="input">The input for image to be generate from.</param>
85+
/// <param name="cancellationToken">The cancellation token to use.</param>
86+
/// <returns>The generated task.</returns>
87+
public static Task<DashScopeTaskOutput> CreateWanxImageGenerationTaskAsync(
88+
this IDashScopeClient dashScopeClient,
89+
WanxStyleRepaintModel model,
90+
ImageGenerationInput input,
91+
CancellationToken cancellationToken = default)
92+
{
93+
return dashScopeClient.CreateWanxImageGenerationTaskAsync(model.GetModelName(), input, cancellationToken);
94+
}
95+
96+
/// <summary>
97+
/// Creates a wanx image generation task with given model and input.
98+
/// </summary>
99+
/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param>
100+
/// <param name="model">The model to use.</param>
101+
/// <param name="input">The input for image to be generate from.</param>
102+
/// <param name="cancellationToken">The cancellation token to use.</param>
103+
/// <returns>The generated task.</returns>
104+
public static async Task<DashScopeTaskOutput> CreateWanxImageGenerationTaskAsync(
105+
this IDashScopeClient dashScopeClient,
106+
string model,
107+
ImageGenerationInput input,
108+
CancellationToken cancellationToken = default)
109+
{
110+
var response = await dashScopeClient.CreateImageGenerationTaskAsync(
111+
new ModelRequest<ImageGenerationInput> { Model = model, Input = input },
112+
cancellationToken);
113+
return response.Output;
114+
}
115+
116+
/// <summary>
117+
/// Get wanx image generation task detail.
118+
/// </summary>
119+
/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param>
120+
/// <param name="taskId">The task id to query.</param>
121+
/// <param name="cancellationToken">The cancellation token to use.</param>
122+
/// <returns></returns>
123+
public static Task<DashScopeTask<ImageGenerationOutput, ImageGenerationUsage>>
124+
GetWanxImageGenerationTaskAsync(
125+
this IDashScopeClient dashScopeClient,
126+
string taskId,
127+
CancellationToken cancellationToken = default)
128+
{
129+
return dashScopeClient.GetTaskAsync<ImageGenerationOutput, ImageGenerationUsage>(taskId, cancellationToken);
130+
}
78131
}

src/Cnblogs.DashScope.Sdk/Wanx/WanxModelNames.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ public static string GetModelName(this WanxModel model)
1212
_ => ThrowHelper.UnknownModelName(nameof(model), model)
1313
};
1414
}
15+
16+
public static string GetModelName(this WanxStyleRepaintModel model)
17+
{
18+
return model switch
19+
{
20+
WanxStyleRepaintModel.WanxStyleRepaintingV1 => "wanx-style-repaint-v1",
21+
_ => ThrowHelper.UnknownModelName(nameof(model), model)
22+
};
23+
}
1524
}

0 commit comments

Comments
 (0)