Skip to content

Commit ae9208e

Browse files
authored
Merge pull request #109 from cnblogs/add-text-generation-parameters
feat: add text generation parameters
2 parents 6ad384a + 14ed2eb commit ae9208e

File tree

32 files changed

+594
-579
lines changed

32 files changed

+594
-579
lines changed

Cnblogs.DashScope.Sdk.sln

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cnblogs.DashScope.AspNetCor
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cnblogs.DashScope.Core", "src\Cnblogs.DashScope.Core\Cnblogs.DashScope.Core.csproj", "{CC389455-A3EA-4F09-B524-4DC351A1E1AA}"
1818
EndProject
19-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cnblogs.DashScope.Sdk.SnapshotGenerator", "test\Cnblogs.DashScope.Sdk.SnapshotGenerator\Cnblogs.DashScope.Sdk.SnapshotGenerator.csproj", "{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1}"
20-
EndProject
2119
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cnblogs.DashScope.AI", "src\Cnblogs.DashScope.AI\Cnblogs.DashScope.AI.csproj", "{5D5AD75A-8084-4738-AC56-B8A23E649452}"
2220
EndProject
2321
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cnblogs.DashScope.AI.UnitTests", "test\Cnblogs.DashScope.AI.UnitTests\Cnblogs.DashScope.AI.UnitTests.csproj", "{25EE79E1-147B-42FD-AFEA-E1550EDD1D36}"
@@ -35,7 +33,6 @@ Global
3533
{8885149A-78F0-4C8E-B9AA-87A46EA69219} = {2E15D1EC-4A07-416E-8BE6-D907F509FD35}
3634
{C910495B-87AB-4AC1-989C-B6720695A139} = {008988ED-0A3B-4272-BCC3-7B4110699345}
3735
{CC389455-A3EA-4F09-B524-4DC351A1E1AA} = {008988ED-0A3B-4272-BCC3-7B4110699345}
38-
{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1} = {CFC8ECB3-5248-46CD-A56C-EC088F2A3804}
3936
{5D5AD75A-8084-4738-AC56-B8A23E649452} = {008988ED-0A3B-4272-BCC3-7B4110699345}
4037
{25EE79E1-147B-42FD-AFEA-E1550EDD1D36} = {CFC8ECB3-5248-46CD-A56C-EC088F2A3804}
4138
{06F0AF23-445B-4C6F-9E19-570DA9B7435D} = {CFC8ECB3-5248-46CD-A56C-EC088F2A3804}
@@ -61,10 +58,6 @@ Global
6158
{CC389455-A3EA-4F09-B524-4DC351A1E1AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
6259
{CC389455-A3EA-4F09-B524-4DC351A1E1AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
6360
{CC389455-A3EA-4F09-B524-4DC351A1E1AA}.Release|Any CPU.Build.0 = Release|Any CPU
64-
{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65-
{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1}.Debug|Any CPU.Build.0 = Debug|Any CPU
66-
{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1}.Release|Any CPU.ActiveCfg = Release|Any CPU
67-
{5088DE77-1CE3-46FB-B9D0-27A6C9A5EED1}.Release|Any CPU.Build.0 = Release|Any CPU
6861
{5D5AD75A-8084-4738-AC56-B8A23E649452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6962
{5D5AD75A-8084-4738-AC56-B8A23E649452}.Debug|Any CPU.Build.0 = Debug|Any CPU
7063
{5D5AD75A-8084-4738-AC56-B8A23E649452}.Release|Any CPU.ActiveCfg = Release|Any CPU

src/Cnblogs.DashScope.Core/ITextGenerationParameters.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ namespace Cnblogs.DashScope.Core;
44
/// The text generation options.
55
/// </summary>
66
public interface ITextGenerationParameters
7-
: IIncrementalOutputParameter, ISeedParameter, IProbabilityParameter, IPenaltyParameter, IMaxTokenParameter, IStopTokenParameter
7+
: IIncrementalOutputParameter, ISeedParameter, IProbabilityParameter, IPenaltyParameter, IMaxTokenParameter,
8+
IStopTokenParameter
89
{
910
/// <summary>
1011
/// The format of the result, must be <c>text</c> or <c>message</c>.
@@ -40,11 +41,31 @@ public interface ITextGenerationParameters
4041
/// </summary>
4142
public bool? EnableSearch { get; }
4243

44+
/// <summary>
45+
/// Search options. <see cref="EnableSearch"/> should set to true.
46+
/// </summary>
47+
public TextGenerationSearchOptions? SearchOptions { get; set; }
48+
4349
/// <summary>
4450
/// Thinking option. Valid for supported models.(e.g. qwen3)
4551
/// </summary>
4652
public bool? EnableThinking { get; }
4753

54+
/// <summary>
55+
/// Maximum length of thinking content. Valid for supported models.(e.g. qwen3)
56+
/// </summary>
57+
public int? ThinkingBudget { get; set; }
58+
59+
/// <summary>
60+
/// Include log possibilities in response.
61+
/// </summary>
62+
public bool? Logprobs { get; set; }
63+
64+
/// <summary>
65+
/// How many choices should be returned. Range: [0, 5]
66+
/// </summary>
67+
public int? TopLogprobs { get; set; }
68+
4869
/// <summary>
4970
/// Available tools for model to call.
5071
/// </summary>
@@ -59,4 +80,9 @@ public interface ITextGenerationParameters
5980
/// Whether to enable parallel tool calling
6081
/// </summary>
6182
public bool? ParallelToolCalls { get; }
83+
84+
/// <summary>
85+
/// Options when using QWen-MT models.
86+
/// </summary>
87+
public TextGenerationTranslationOptions? TranslationOptions { get; set; }
6288
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Cnblogs.DashScope.Core.Internals;
5+
6+
internal class ByteArrayLiteralConvertor : JsonConverter<byte[]>
7+
{
8+
/// <inheritdoc />
9+
public override byte[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType == JsonTokenType.StartArray)
12+
{
13+
reader.Read(); // read out start of array
14+
var list = new List<byte>(8); // should fit most tokens
15+
while (reader.TokenType != JsonTokenType.EndArray)
16+
{
17+
list.Add(reader.GetByte());
18+
reader.Read();
19+
}
20+
21+
return list.ToArray();
22+
}
23+
24+
if (reader.TokenType == JsonTokenType.Null)
25+
{
26+
return null;
27+
}
28+
29+
return reader.GetBytesFromBase64();
30+
}
31+
32+
/// <inheritdoc />
33+
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
34+
{
35+
writer.WriteStartArray();
36+
foreach (var b in value)
37+
{
38+
writer.WriteNumberValue(b);
39+
}
40+
41+
writer.WriteEndArray();
42+
}
43+
}

src/Cnblogs.DashScope.Core/TextGenerationChoice.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public class TextGenerationChoice
1414
/// The generated message.
1515
/// </summary>
1616
public TextChatMessage Message { get; set; } = new(Array.Empty<DashScopeFileId>());
17+
18+
/// <summary>
19+
/// Token array with log possibility info.
20+
/// </summary>
21+
public TextGenerationLogprobs? Logprobs { get; set; }
1722
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Core.Internals;
3+
4+
namespace Cnblogs.DashScope.Core;
5+
6+
/// <summary>
7+
/// Represents a possible choice of token.
8+
/// </summary>
9+
/// <param name="Token">Token content.</param>
10+
/// <param name="Bytes">Token content in UTF-8 byte array.</param>
11+
/// <param name="Logprob">Possibility, <c>null</c> when it's too low.</param>
12+
/// <param name="TopLogprobs">The most possible alternatives.</param>
13+
public record TextGenerationLogprobContent(
14+
string Token,
15+
[property: JsonConverter(typeof(ByteArrayLiteralConvertor))]
16+
byte[] Bytes,
17+
float? Logprob,
18+
List<TextGenerationTopLogprobContent> TopLogprobs);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Possibilities of token choices.
5+
/// </summary>
6+
/// <param name="Content">The choices with their possibility.</param>
7+
public record TextGenerationLogprobs(List<TextGenerationLogprobContent> Content);

src/Cnblogs.DashScope.Core/TextGenerationOutput.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public class TextGenerationOutput
1919
/// Not null when <see cref="TextGenerationParameters"/>.<see cref="TextGenerationParameters.ResultFormat"/> is "message".
2020
/// </summary>
2121
public List<TextGenerationChoice>? Choices { get; set; }
22+
23+
/// <summary>
24+
/// Not null when <see cref="TextGenerationParameters"/>.<see cref="TextGenerationParameters.SearchOptions"/> configured to show source.
25+
/// </summary>
26+
public TextGenerationWebSearchInfo? SearchInfo { get; set; }
2227
}

src/Cnblogs.DashScope.Core/TextGenerationParameters.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,21 @@ public class TextGenerationParameters : ITextGenerationParameters
3838
/// <inheritdoc />
3939
public bool? EnableSearch { get; set; }
4040

41+
/// <inheritdoc />
42+
public TextGenerationSearchOptions? SearchOptions { get; set; }
43+
4144
/// <inheritdoc />
4245
public bool? EnableThinking { get; set; }
4346

47+
/// <inheritdoc />
48+
public int? ThinkingBudget { get; set; }
49+
50+
/// <inheritdoc />
51+
public bool? Logprobs { get; set; }
52+
53+
/// <inheritdoc />
54+
public int? TopLogprobs { get; set; }
55+
4456
/// <inheritdoc />
4557
public IEnumerable<ToolDefinition>? Tools { get; set; }
4658

@@ -50,6 +62,9 @@ public class TextGenerationParameters : ITextGenerationParameters
5062
/// <inheritdoc />
5163
public bool? ParallelToolCalls { get; set; }
5264

65+
/// <inheritdoc />
66+
public TextGenerationTranslationOptions? TranslationOptions { get; set; }
67+
5368
/// <inheritdoc />
5469
public bool? IncrementalOutput { get; set; }
5570
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Web search options
5+
/// </summary>
6+
public class TextGenerationSearchOptions
7+
{
8+
/// <summary>
9+
/// Show search result in response. Defaults to false.
10+
/// </summary>
11+
public bool? EnableSource { get; set; }
12+
13+
/// <summary>
14+
/// Include citation in output. Defaults to false.
15+
/// </summary>
16+
public bool? EnableCitation { get; set; }
17+
18+
/// <summary>
19+
/// Citation format. Defaults to "[&lt;number&gt;]"
20+
/// </summary>
21+
public string? CitationFormat { get; set; }
22+
23+
/// <summary>
24+
/// Force model to use web search. Defaults to false.
25+
/// </summary>
26+
public bool? ForcedSearch { get; set; }
27+
28+
/// <summary>
29+
/// How many search records should be provided to model. "standard" - 5 records. "pro" - 10 records.
30+
/// </summary>
31+
public string? SearchStrategy { get; set; }
32+
}
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+
using Cnblogs.DashScope.Core.Internals;
3+
4+
namespace Cnblogs.DashScope.Core;
5+
6+
/// <summary>
7+
/// Represents one choice of most possibility alternative tokens.
8+
/// </summary>
9+
/// <param name="Token">The token content.</param>
10+
/// <param name="Bytes">The token content in UTF-8 byte array.</param>
11+
/// <param name="Logprob">Possibility, <c>null</c> when possibility is too low.</param>
12+
public record TextGenerationTopLogprobContent(
13+
string Token,
14+
[property: JsonConverter(typeof(ByteArrayLiteralConvertor))]
15+
byte[] Bytes,
16+
float? Logprob);

0 commit comments

Comments
 (0)