Skip to content

Commit 649b0c5

Browse files
committed
feat: support wanx image synthesis api
1 parent 8d188b7 commit 649b0c5

32 files changed

+555
-11
lines changed

src/Cnblogs.DashScope.Sdk/BatchGetEmbeddingsOutput.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Cnblogs.DashScope.Sdk.Internals;
2-
3-
namespace Cnblogs.DashScope.Sdk;
1+
namespace Cnblogs.DashScope.Sdk;
42

53
/// <summary>
64
/// The output of batch get embeddings api.

src/Cnblogs.DashScope.Sdk/DashScopeClientCore.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ Task<ModelResponse<BatchGetEmbeddingsOutput, TextEmbeddingTokenUsage>> BatchGetE
9292
cancellationToken))!;
9393
}
9494

95+
/// <inheritdoc />
96+
public async Task<ModelResponse<ImageSynthesisOutput, ImageSynthesisUsage>> CreateImageSynthesisTaskAsync(
97+
ModelRequest<ImageSynthesisInput, ImageSynthesisParameters> input,
98+
CancellationToken cancellationToken = default)
99+
{
100+
var request = BuildRequest(HttpMethod.Post, ApiLinks.ImageSynthesis, input, isTask: true);
101+
return (await SendAsync<ModelResponse<ImageSynthesisOutput, ImageSynthesisUsage>>(request, cancellationToken))!;
102+
}
103+
95104
/// <inheritdoc />
96105
public async Task<DashScopeTask<TOutput, TUsage>> GetTaskAsync<TOutput, TUsage>(
97106
string taskId,

src/Cnblogs.DashScope.Sdk/Internals/DashScopeTaskOutput.cs renamed to src/Cnblogs.DashScope.Sdk/DashScopeTaskOutput.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Sdk.Internals;
23

3-
namespace Cnblogs.DashScope.Sdk.Internals;
4+
namespace Cnblogs.DashScope.Sdk;
45

56
/// <summary>
67
/// The common properties of DashScope task.
@@ -35,6 +36,11 @@ public abstract record DashScopeTaskOutput
3536
[JsonConverter(typeof(DashScopeDateTimeConvertor))]
3637
public DateTime? EndTime { get; set; }
3738

39+
/// <summary>
40+
/// The metrics of subtasks.
41+
/// </summary>
42+
public DashScopeTaskMetrics? TaskMetrics { get; set; }
43+
3844
/// <summary>
3945
/// Error code, not null when <see cref="TaskStatus"/> is Failed.
4046
/// </summary>

src/Cnblogs.DashScope.Sdk/IDashScopeClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ Task<ModelResponse<TextEmbeddingOutput, TextEmbeddingTokenUsage>> GetEmbeddingsA
6565
ModelRequest<BatchGetEmbeddingsInput, BatchGetEmbeddingsParameters> input,
6666
CancellationToken cancellationToken = default);
6767

68+
/// <summary>
69+
/// Create a image synthesis task.
70+
/// </summary>
71+
/// <param name="input">The input of image synthesis task.</param>
72+
/// <param name="cancellationToken">The cancellation token to use.</param>
73+
/// <returns></returns>
74+
Task<ModelResponse<ImageSynthesisOutput, ImageSynthesisUsage>> CreateImageSynthesisTaskAsync(
75+
ModelRequest<ImageSynthesisInput, ImageSynthesisParameters> input,
76+
CancellationToken cancellationToken = default);
77+
6878
/// <summary>
6979
/// Get the task status of given id.
7080
/// </summary>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Available styles for image synthesis task.
5+
/// </summary>
6+
public static class ImageStyles
7+
{
8+
/// <summary>
9+
/// Use the image style determined by model.
10+
/// </summary>
11+
public const string Auto = "<auto>";
12+
13+
/// <summary>
14+
/// Generate 3d image in cartoon style.
15+
/// </summary>
16+
public const string Cartoon3D = "<3d cartoon>";
17+
18+
/// <summary>
19+
/// Generate image in anime style.
20+
/// </summary>
21+
public const string Anime = "<anime>";
22+
23+
/// <summary>
24+
/// Generate oil painting like image.
25+
/// </summary>
26+
public const string OilPainting = "<oil painting>";
27+
28+
/// <summary>
29+
/// Generate water color style image.
30+
/// </summary>
31+
public const string WaterColor = "<watercolor>";
32+
33+
/// <summary>
34+
/// Generate sketch-like image.
35+
/// </summary>
36+
public const string Sketch = "<sketch>";
37+
38+
/// <summary>
39+
/// Generate image in chinese painting style.
40+
/// </summary>
41+
public const string ChinesePainting = "<chinese painting>";
42+
43+
/// <summary>
44+
/// Generate image in flat illustration style.
45+
/// </summary>
46+
public const string FlatIllustration = "<flat illustration>";
47+
}
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 inputs of image synthesis task
5+
/// </summary>
6+
public class ImageSynthesisInput
7+
{
8+
/// <summary>
9+
/// The prompt to generate image from. This will be chopped at max length of 500 characters.
10+
/// </summary>
11+
public required string Prompt { get; set; }
12+
13+
/// <summary>
14+
/// The negative prompt to generate image from. This will be chopped at max length of 500 characters.
15+
/// </summary>
16+
public string? NegativePrompt { get; set; }
17+
}
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+
/// The output of one image synthesis task.
5+
/// </summary>
6+
public record ImageSynthesisOutput : DashScopeTaskOutput
7+
{
8+
/// <summary>
9+
/// The generated image results.
10+
/// </summary>
11+
public List<ImageSynthesisResult>? Results { get; set; }
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// Optional parameters for image synthesis task.
5+
/// </summary>
6+
public class ImageSynthesisParameters
7+
{
8+
/// <summary>
9+
/// Generated image style, defaults to '&lt;auto&gt;'. Use <see cref="ImageStyles"/> to get all available options.
10+
/// </summary>
11+
public string? Style { get; set; }
12+
13+
/// <summary>
14+
/// Generated image size, defaults to 1024*1024. Another options are: 1280*720 and 720*1280.
15+
/// </summary>
16+
public string? Size { get; set; }
17+
18+
/// <summary>
19+
/// Number of images requested. Max number is 4, defaults to 1.
20+
/// </summary>
21+
public int? N { get; set; }
22+
23+
/// <summary>
24+
/// Seed for randomizer. Once set, generated image will use seed, seed+1, seed+2, seed+3 depends on <see cref="N"/>.
25+
/// </summary>
26+
public int? Seed { get; set; }
27+
}
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 result of one image synthesis subtask.
5+
/// </summary>
6+
/// <param name="Url">The url of generated image.</param>
7+
/// <param name="Code">The error code.</param>
8+
/// <param name="Message">The error message.</param>
9+
public record ImageSynthesisResult(string? Url = null, string? Code = null, string? Message = null);
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 synthesis request.
5+
/// </summary>
6+
/// <param name="ImageCount">Generated image count.</param>
7+
public record ImageSynthesisUsage(int ImageCount);

0 commit comments

Comments
 (0)