Skip to content

Commit 4984ab8

Browse files
committed
feat: support tool choice
1 parent 83dba6f commit 4984ab8

19 files changed

+342
-77
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Marks parameter supports setting maximum number of output tokens.
5+
/// </summary>
6+
public interface IMaxTokenParameter
7+
{
8+
/// <summary>
9+
/// The maximum number of tokens the model can generate.
10+
/// </summary>
11+
/// <remarks>
12+
/// Default and maximum number of tokens is 1500(qwen-turbo) or 2000(qwen-max, qwen-max-1201, qwen-max-longcontext, qwen-plus).
13+
/// </remarks>
14+
public int? MaxTokens { get; }
15+
}

src/Cnblogs.DashScope.Core/IMultimodalParameters.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@
33
/// <summary>
44
/// Optional parameters for multi-model generation request.
55
/// </summary>
6-
public interface IMultimodalParameters : IProbabilityParameter, ISeedParameter, IIncrementalOutputParameter;
6+
public interface IMultimodalParameters
7+
: IProbabilityParameter, ISeedParameter, IIncrementalOutputParameter, IPenaltyParameter, IMaxTokenParameter,
8+
IStopTokenParameter
9+
{
10+
/// <summary>
11+
/// Allow higher resolution for inputs. When setting to <c>true</c>, increases the maximum input token from 1280 to 16384. Defaults to <c>false</c>.
12+
/// </summary>
13+
public bool? VlHighResolutionImages { get; }
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Marks parameter accepts presence_penalty and repetition_penalty options.
5+
/// </summary>
6+
public interface IPenaltyParameter
7+
{
8+
/// <summary>
9+
/// Increasing the repetition penalty can reduce the amount of repetition in the model’s output. A value of 1.0 indicates no penalty, with the default set at 1.1.
10+
/// </summary>
11+
public float? RepetitionPenalty { get; }
12+
13+
/// <summary>
14+
/// Control the content repetition in text generated by the model.
15+
/// </summary>
16+
/// <remarks>Must be in [-2.0, 2.0]. Use higher penalty for batter creativity.</remarks>
17+
public float? PresencePenalty { get; }
18+
}

src/Cnblogs.DashScope.Core/IProbabilityParameter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Cnblogs.DashScope.Core;
22

33
/// <summary>
4-
/// Marks parameter accepts top_p and top_k options.
4+
/// Marks parameter accepts top_p, top_k and temperature options.
55
/// </summary>
66
public interface IProbabilityParameter
77
{
@@ -16,4 +16,10 @@ public interface IProbabilityParameter
1616
/// </summary>
1717
/// <remarks>top_k would been disabled if applied null or any value larger than 100.</remarks>
1818
public int? TopK { get; }
19+
20+
/// <summary>
21+
/// Controls the diversity of generations. Lower temperature leads to more consistent result.
22+
/// </summary>
23+
/// <remarks>Must be in [0,2), qwen-max defaults to 0.7.</remarks>
24+
public float? Temperature { get; }
1925
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Core;
2+
3+
/// <summary>
4+
/// Marks parameter supports setting stop tokens.
5+
/// </summary>
6+
public interface IStopTokenParameter
7+
{
8+
/// <summary>
9+
/// Stop generation when next token or string is in given range.
10+
/// </summary>
11+
public TextGenerationStop? Stop { get; }
12+
}

src/Cnblogs.DashScope.Core/ITextGenerationParameters.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/// <summary>
44
/// The text generation options.
55
/// </summary>
6-
public interface ITextGenerationParameters : IIncrementalOutputParameter, ISeedParameter, IProbabilityParameter
6+
public interface ITextGenerationParameters
7+
: IIncrementalOutputParameter, ISeedParameter, IProbabilityParameter, IPenaltyParameter, IMaxTokenParameter, IStopTokenParameter
78
{
89
/// <summary>
910
/// The format of the result message, must be <c>text</c> or <c>message</c>.
@@ -14,30 +15,6 @@ public interface ITextGenerationParameters : IIncrementalOutputParameter, ISeedP
1415
/// </remarks>
1516
public string? ResultFormat { get; }
1617

17-
/// <summary>
18-
/// The maximum number of tokens the model can generate.
19-
/// </summary>
20-
/// <remarks>
21-
/// Default and maximum number of tokens is 1500(qwen-turbo) or 2000(qwen-max, qwen-max-1201, qwen-max-longcontext, qwen-plus).
22-
/// </remarks>
23-
public int? MaxTokens { get; }
24-
25-
/// <summary>
26-
/// Increasing the repetition penalty can reduce the amount of repetition in the model’s output. A value of 1.0 indicates no penalty, with the default set at 1.1.
27-
/// </summary>
28-
public float? RepetitionPenalty { get; }
29-
30-
/// <summary>
31-
/// Controls the diversity of generations. Lower temperature leads to more consistent result.
32-
/// </summary>
33-
/// <remarks>Must be in [0,2), defaults to 0.85.</remarks>
34-
public float? Temperature { get; }
35-
36-
/// <summary>
37-
/// Stop generation when next token or string is in given range.
38-
/// </summary>
39-
public TextGenerationStop? Stop { get; }
40-
4118
/// <summary>
4219
/// Enable internet search when generation. Defaults to false.
4320
/// </summary>
@@ -49,7 +26,7 @@ public interface ITextGenerationParameters : IIncrementalOutputParameter, ISeedP
4926
public IEnumerable<ToolDefinition>? Tools { get; }
5027

5128
/// <summary>
52-
///
29+
/// Behavior when choosing tools.
5330
/// </summary>
5431
public ToolChoice? ToolChoice { get; }
5532
}

src/Cnblogs.DashScope.Core/MultimodalParameters.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,27 @@ public class MultimodalParameters : IMultimodalParameters
1111
/// <inheritdoc />
1212
public int? TopK { get; set; }
1313

14+
/// <inheritdoc />
15+
public float? Temperature { get; set; }
16+
1417
/// <inheritdoc />
1518
public ulong? Seed { get; set; }
1619

1720
/// <inheritdoc />
1821
public bool? IncrementalOutput { get; set; }
22+
23+
/// <inheritdoc />
24+
public bool? VlHighResolutionImages { get; set; }
25+
26+
/// <inheritdoc />
27+
public float? RepetitionPenalty { get; set; }
28+
29+
/// <inheritdoc />
30+
public float? PresencePenalty { get; set; }
31+
32+
/// <inheritdoc />
33+
public int? MaxTokens { get; set; }
34+
35+
/// <inheritdoc />
36+
public TextGenerationStop? Stop { get; set; }
1937
}

src/Cnblogs.DashScope.Core/TextGenerationParameters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class TextGenerationParameters : ITextGenerationParameters
2323
/// <inheritdoc />
2424
public float? RepetitionPenalty { get; set; }
2525

26+
/// <inheritdoc />
27+
public float? PresencePenalty { get; set; }
28+
2629
/// <inheritdoc />
2730
public float? Temperature { get; set; }
2831

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1-
namespace Cnblogs.DashScope.Core;
1+
using System.Text.Json.Serialization;
2+
3+
namespace Cnblogs.DashScope.Core;
24

35
/// <summary>
4-
/// Specify behavior when model
6+
/// Specify the behavior for model when tools are applied.
57
/// </summary>
6-
public record ToolChoice();
8+
[JsonConverter(typeof(ToolChoiceJsonConverter))]
9+
public record ToolChoice
10+
{
11+
/// <summary>
12+
/// Make sure tool choices can not be initiated directly.
13+
/// </summary>
14+
private ToolChoice(string type, ToolChoiceFunction? function = null)
15+
{
16+
Type = type;
17+
Function = function;
18+
}
19+
20+
/// <summary>
21+
/// The type of tool choice.
22+
/// </summary>
23+
public string Type { get; }
24+
25+
/// <summary>
26+
/// The function that model must call.
27+
/// </summary>
28+
public ToolChoiceFunction? Function { get; }
29+
30+
/// <summary>
31+
/// Model can not call any tools.
32+
/// </summary>
33+
public static readonly ToolChoice NoneChoice = new("none");
34+
35+
/// <summary>
36+
/// Model can freely pick between generating a message or calling one or more tools.
37+
/// </summary>
38+
public static readonly ToolChoice AutoChoice = new("auto");
39+
40+
/// <summary>
41+
/// Model must call function with specified name.
42+
/// </summary>
43+
/// <param name="functionName">Name of function.</param>
44+
/// <returns></returns>
45+
public static ToolChoice FunctionChoice(string functionName)
46+
=> new("function", new ToolChoiceFunction(functionName));
47+
}
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+
/// Represents functions that model must call.
5+
/// </summary>
6+
/// <param name="Name">The name of the function.</param>
7+
public record ToolChoiceFunction(string Name);

0 commit comments

Comments
 (0)