Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for max_completion_tokens property #168

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ builder.Services.AddChatGpt(options =>
options.DefaultParameters = new ChatGptParameters
{
MaxTokens = 800,
//MaxCompletionTokens = 800, // o1 series models support this property instead of MaxTokens
Temperature = 0.7
};
});
Expand Down Expand Up @@ -85,6 +86,8 @@ Currently available models are:
- gpt-4-turbo
- gpt-4o
- gpt-4o-mini
- o1-preview
- o1-mini

They have fixed names, available in the [OpenAIChatGptModels.cs file](https://github.com/marcominerva/ChatGptNet/blob/master/src/ChatGptNet/Models/OpenAIChatGptModels.cs).

Expand Down Expand Up @@ -163,6 +166,7 @@ The configuration can be automatically read from [IConfiguration](https://learn.
// "Temperature": 0.8,
// "TopP": 1,
// "MaxTokens": 500,
// "MaxCompletionTokens": null, // o1 series models support this property instead of MaxTokens
// "PresencePenalty": 0,
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
Expand Down
4 changes: 2 additions & 2 deletions samples/ChatGptApi/ChatGptApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.17" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.19" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/ChatGptApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// "Temperature": 0.8,
// "TopP": 1,
// "MaxTokens": 500,
// "MaxCompletionTokens": null, // o1 series models supports this property instead of MaxTokens
// "PresencePenalty": 0,
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
Expand Down
2 changes: 1 addition & 1 deletion samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Markdig" Version="0.37.0" />
<PackageReference Include="Markdig" Version="0.38.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
Expand Down
1 change: 1 addition & 0 deletions samples/ChatGptConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// "Temperature": 0.8,
// "TopP": 1,
// "MaxTokens": 500,
// "MaxCompletionTokens": null, // o1 series models support this property instead of MaxTokens
// "PresencePenalty": 0,
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
Expand Down
1 change: 1 addition & 0 deletions samples/ChatGptFunctionCallingConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// "Temperature": 0.8,
// "TopP": 1,
// "MaxTokens": 500,
// "MaxCompletionTokens": null, // o1 series models support this property instead of MaxTokens
// "PresencePenalty": 0,
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
Expand Down
1 change: 1 addition & 0 deletions samples/ChatGptStreamConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// "Temperature": 0.8,
// "TopP": 1,
// "MaxTokens": 500,
// "MaxCompletionTokens": null, // o1 series models support this property instead of MaxTokens
// "PresencePenalty": 0,
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
Expand Down
17 changes: 16 additions & 1 deletion src/ChatGptNet/ChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ private async Task<IList<ChatGptMessage>> CreateMessageListAsync(Guid conversati
}

private ChatGptRequest CreateChatGptRequest(IEnumerable<ChatGptMessage> messages, ChatGptToolParameters? toolParameters, bool stream, ChatGptParameters? parameters, string? model)
=> new()
{
var request = new ChatGptRequest()
{
Model = model ?? options.DefaultModel,
Messages = messages,
Expand Down Expand Up @@ -362,6 +363,7 @@ private ChatGptRequest CreateChatGptRequest(IEnumerable<ChatGptMessage> messages
Temperature = parameters?.Temperature ?? options.DefaultParameters.Temperature,
TopP = parameters?.TopP ?? options.DefaultParameters.TopP,
MaxTokens = parameters?.MaxTokens ?? options.DefaultParameters.MaxTokens,
MaxCompletionTokens = parameters?.MaxCompletionTokens ?? options.DefaultParameters.MaxCompletionTokens,
PresencePenalty = parameters?.PresencePenalty ?? options.DefaultParameters.PresencePenalty,
FrequencyPenalty = parameters?.FrequencyPenalty ?? options.DefaultParameters.FrequencyPenalty,
ResponseFormat = parameters?.ResponseFormat ?? options.DefaultParameters.ResponseFormat,
Expand All @@ -370,6 +372,19 @@ private ChatGptRequest CreateChatGptRequest(IEnumerable<ChatGptMessage> messages
User = options.User
};

/*
* As of 2024-09-01-preview, Azure OpenAI conditionally supports the use of the new max_completion_tokens property:
* - The o1-mini and o1-preview models accept max_completion_tokens and reject max_tokens
* - All other models reject max_completion_tokens and accept max_tokens
*/
if (request.MaxCompletionTokens is not null)
{
request.MaxTokens = null;
}

return request;
}

private EmbeddingRequest CreateEmbeddingRequest(IEnumerable<string> messages, EmbeddingParameters? parameters, string? model)
=> new()
{
Expand Down
13 changes: 13 additions & 0 deletions src/ChatGptNet/Models/ChatGptParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,21 @@ public class ChatGptParameters
/// <summary>
/// Gets or sets the maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length.
/// </summary>
/// <remarks>
/// This value is now deprecated in favor of <see cref="MaxCompletionTokens"/>, and is not compatible with <see href="https://platform.openai.com/docs/guides/reasoning">o1 series models</see>.
/// </remarks>
/// <seealso cref="MaxCompletionTokens"/>
[JsonPropertyName("max_tokens")]
public int? MaxTokens { get; set; }

/// <summary>
/// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and <see href="https://platform.openai.com/docs/guides/reasoning">reasoning tokens</see>.
/// </summary>
/// <remarks>o1 series models must use this property instead of <see cref="MaxTokens"/>.</remarks>
/// <seealso cref="MaxTokens"/>
[JsonPropertyName("max_completion_tokens")]
public int? MaxCompletionTokens { get; set; }

/// <summary>
/// Gets or sets the presence penalties for chat completion. Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics (default: 0).
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/ChatGptNet/Models/ChatGptRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,21 @@ internal class ChatGptRequest
/// <summary>
/// Gets or sets the maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length.
/// </summary>
/// <remarks>
/// This value is now deprecated in favor of <see cref="MaxCompletionTokens"/>, and is not compatible with <see href="https://platform.openai.com/docs/guides/reasoning">o1 series models</see>.
/// </remarks>
/// <seealso cref="MaxCompletionTokens"/>
[JsonPropertyName("max_tokens")]
public int? MaxTokens { get; set; }

/// <summary>
/// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and <see href="https://platform.openai.com/docs/guides/reasoning">reasoning tokens</see>.
/// </summary>
/// <remarks>o1 series models must use this property instead of <see cref="MaxTokens"/>.</remarks>
/// <seealso cref="MaxTokens"/>
[JsonPropertyName("max_completion_tokens")]
public int? MaxCompletionTokens { get; set; }

/// <summary>
/// Gets or sets the presence penalties for chat completion. A number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics (default: 0).
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/ChatGptNet/Models/OpenAIChatGptModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ public static class OpenAIChatGptModels
/// See <see href="https://platform.openai.com/docs/models/gpt-4o-mini">GPT-4</see> for more information.
/// </remarks>
public const string Gpt4_o_mini = "gpt-4o-mini";

/// <summary>
/// Reasoning model designed to solve hard problems across domains.
/// </summary>
/// <remarks>
/// This model supports 128.000 tokens and returns a maximum of 32.768 outpout tokens.
/// See <see href="https://platform.openai.com/docs/models#o1">o1-preview and o1-mini</see> for more information.
/// </remarks>
public const string O1_preview = "o1-preview";

/// <summary>
/// Faster and cheaper reasoning model particularly good at coding, math, and science.
/// </summary>
/// <remarks>
/// This model supports 128.000 tokens and returns a maximum of 32.768 outpout tokens.
/// See <see href="https://platform.openai.com/docs/models#o1">o1-preview and o1-mini</see> for more information.
/// </remarks>
public const string O1_mini = "o1-mini";
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.143" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.146" PrivateAssets="All" />
</ItemGroup>

</Project>
Loading