Skip to content

Commit 5661b20

Browse files
committed
feat: support logprobs
1 parent 2237bbd commit 5661b20

14 files changed

+185
-134
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
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ public class TextGenerationChoice
1818
/// <summary>
1919
/// Token array with log possibility info.
2020
/// </summary>
21-
public TextGenerationLogProbs? Logprobs { get; set; }
21+
public TextGenerationLogprobs? Logprobs { get; set; }
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Cnblogs.DashScope.Core;
1+
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Core.Internals;
3+
4+
namespace Cnblogs.DashScope.Core;
25

36
/// <summary>
47
/// Represents a possible choice of token.
@@ -7,8 +10,9 @@
710
/// <param name="Bytes">Token content in UTF-8 byte array.</param>
811
/// <param name="Logprob">Possibility, <c>null</c> when it's too low.</param>
912
/// <param name="TopLogprobs">The most possible alternatives.</param>
10-
public record TextGenerationLogProbContent(
13+
public record TextGenerationLogprobContent(
1114
string Token,
15+
[property: JsonConverter(typeof(ByteArrayLiteralConvertor))]
1216
byte[] Bytes,
1317
float? Logprob,
14-
List<TextGenerationTopLogProbContent> TopLogprobs);
18+
List<TextGenerationTopLogprobContent> TopLogprobs);

src/Cnblogs.DashScope.Core/TextGenerationLogProbs.cs renamed to src/Cnblogs.DashScope.Core/TextGenerationLogprobs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
/// Possibilities of token choices.
55
/// </summary>
66
/// <param name="Content">The choices with their possibility.</param>
7-
public record TextGenerationLogProbs(List<TextGenerationLogProbContent> Content);
7+
public record TextGenerationLogprobs(List<TextGenerationLogprobContent> Content);
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
namespace Cnblogs.DashScope.Core;
1+
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Core.Internals;
3+
4+
namespace Cnblogs.DashScope.Core;
25

36
/// <summary>
47
/// Represents one choice of most possibility alternative tokens.
58
/// </summary>
69
/// <param name="Token">The token content.</param>
710
/// <param name="Bytes">The token content in UTF-8 byte array.</param>
811
/// <param name="Logprob">Possibility, <c>null</c> when possibility is too low.</param>
9-
public record TextGenerationTopLogProbContent(string Token, byte[] Bytes, float? Logprob);
12+
public record TextGenerationTopLogprobContent(
13+
string Token,
14+
[property: JsonConverter(typeof(ByteArrayLiteralConvertor))]
15+
byte[] Bytes,
16+
float? Logprob);

test/Cnblogs.DashScope.Sdk.SnapshotGenerator/Cnblogs.DashScope.Sdk.SnapshotGenerator.csproj

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/Cnblogs.DashScope.Sdk.SnapshotGenerator/Program.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

test/Cnblogs.DashScope.Sdk.UnitTests/TextGenerationSerializationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public async Task ConversationCompletion_MessageFormatSse_SuccessAsync(
155155
Snapshots.TextGeneration.MessageFormat.SingleMessage,
156156
Snapshots.TextGeneration.MessageFormat.SingleMessageReasoning,
157157
Snapshots.TextGeneration.MessageFormat.SingleMessageWithTools,
158-
Snapshots.TextGeneration.MessageFormat.SingleMessageJson);
158+
Snapshots.TextGeneration.MessageFormat.SingleMessageJson,
159+
Snapshots.TextGeneration.MessageFormat.SingleMessageLogprobs);
159160

160161
public static readonly TheoryData<RequestSnapshot<ModelRequest<TextGenerationInput, ITextGenerationParameters>,
161162
ModelResponse<TextGenerationOutput, TextGenerationTokenUsage>>> SingleGenerationMessageSseFormatData = new(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"model": "qwen-max",
3+
"input": {
4+
"messages": [
5+
{
6+
"role": "user",
7+
"content": "请问 1+1 是多少?请直接输出结果"
8+
}
9+
]
10+
},
11+
"parameters": {
12+
"result_format": "message",
13+
"seed": 1234,
14+
"max_tokens": 1500,
15+
"top_p": 0.8,
16+
"top_k": 100,
17+
"repetition_penalty": 1.1,
18+
"temperature": 0.85,
19+
"logprobs": true,
20+
"top_logprobs": 2
21+
}
22+
}

0 commit comments

Comments
 (0)