Skip to content

Commit ebdc1e6

Browse files
authored
Add support for stream_options (#161)
2 parents ffe9b12 + 8df90d3 commit ebdc1e6

File tree

17 files changed

+95
-71
lines changed

17 files changed

+95
-71
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ builder.Services.AddChatGpt(options =>
6161
- 2024-04-01-preview
6262
- 2024-05-01-preview
6363
- 2024-02-01
64-
- 2024-06-01 (default)
64+
- 2024-06-01
65+
- 2024-07-01-preview (default)
6566
- _AuthenticationType_: it specifies if the key is an actual API Key or an [Azure Active Directory token](https://learn.microsoft.com/azure/cognitive-services/openai/how-to/managed-identity) (optional, default: "ApiKey").
6667

6768
### DefaultModel and DefaultEmbeddingModel

samples/ChatGptApi/ChatGptApi.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
11-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
12-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
13-
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.6" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
11+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.0" />
12+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
13+
<PackageReference Include="TinyHelpers.AspNetCore" Version="3.1.9" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

samples/ChatGptApi/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"ApiKey": "", // Required
55
//"Organization": "", // Optional, used only by OpenAI
66
"ResourceName": "", // Required when using Azure OpenAI Service
7-
"ApiVersion": "2024-06-01", // Optional, used only by Azure OpenAI Service (default: 2024-06-01)
7+
"ApiVersion": "2024-07-01-preview", // Optional, used only by Azure OpenAI Service (default: 2024-07-01-preview)
88
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory
99

1010
"DefaultModel": "my-model",

samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Markdig" Version="0.37.0" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.7" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.7" PrivateAssets="all" />
14-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.8" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.8" PrivateAssets="all" />
14+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.0" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

samples/ChatGptConsole/ChatGptConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
12+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.0" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

samples/ChatGptConsole/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"ApiKey": "", // Required
55
//"Organization": "", // Optional, used only by OpenAI
66
"ResourceName": "", // Required when using Azure OpenAI Service
7-
"ApiVersion": "2024-06-01", // Optional, used only by Azure OpenAI Service (default: 2024-06-01)
7+
"ApiVersion": "2024-07-01-preview", // Optional, used only by Azure OpenAI Service (default: 2024-07-01-preview)
88
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory
99

1010
"DefaultModel": "my-model",

samples/ChatGptFunctionCallingConsole/ChatGptFunctionCallingConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
12+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.0" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

samples/ChatGptFunctionCallingConsole/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"ApiKey": "", // Required
55
//"Organization": "", // Optional, used only by OpenAI
66
"ResourceName": "", // Required when using Azure OpenAI Service
7-
"ApiVersion": "2024-06-01", // Optional, used only by Azure OpenAI Service (default: 2024-06-01)
7+
"ApiVersion": "2024-07-01-preview", // Optional, used only by Azure OpenAI Service (default: 2024-07-01-preview)
88
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory
99

1010
"DefaultModel": "my-model",

samples/ChatGptStreamConsole/ChatGptStreamConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.7.0" />
12+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.9.0" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
{
2-
"ChatGPT": {
3-
"Provider": "OpenAI", // Optional. Allowed values: OpenAI (default) or Azure
4-
"ApiKey": "", // Required
5-
//"Organization": "", // Optional, used only by OpenAI
6-
"ResourceName": "", // Required when using Azure OpenAI Service
7-
"ApiVersion": "2024-06-01", // Optional, used only by Azure OpenAI Service (default: 2024-06-01)
8-
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory
9-
10-
"DefaultModel": "my-model",
11-
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, set it if you want to use embeddings
12-
"MessageLimit": 20,
13-
"MessageExpiration": "00:30:00",
14-
"ThrowExceptionOnError": true // Optional, default: true
15-
//"User": "UserName",
16-
//"DefaultParameters": {
17-
// "Temperature": 0.8,
18-
// "TopP": 1,
19-
// "MaxTokens": 500,
20-
// "PresencePenalty": 0,
21-
// "FrequencyPenalty": 0,
22-
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
23-
// "Seed": 42 // Optional (any integer value)
24-
//},
25-
//"DefaultEmbeddingParameters": {
26-
// "Dimensions": 1536
27-
//}
28-
},
29-
"Logging": {
30-
"LogLevel": {
31-
"Default": "Error",
32-
"Microsoft": "Warning"
33-
}
34-
}
35-
}
1+
{
2+
"ChatGPT": {
3+
"Provider": "OpenAI", // Optional. Allowed values: OpenAI (default) or Azure
4+
"ApiKey": "", // Required
5+
//"Organization": "", // Optional, used only by OpenAI
6+
"ResourceName": "", // Required when using Azure OpenAI Service
7+
"ApiVersion": "2024-07-01-preview", // Optional, used only by Azure OpenAI Service (default: 2024-07-01-preview)
8+
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory
9+
10+
"DefaultModel": "my-model",
11+
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, set it if you want to use embeddings
12+
"MessageLimit": 20,
13+
"MessageExpiration": "00:30:00",
14+
"ThrowExceptionOnError": true // Optional, default: true
15+
//"User": "UserName",
16+
//"DefaultParameters": {
17+
// "Temperature": 0.8,
18+
// "TopP": 1,
19+
// "MaxTokens": 500,
20+
// "PresencePenalty": 0,
21+
// "FrequencyPenalty": 0,
22+
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
23+
// "Seed": 42 // Optional (any integer value)
24+
//},
25+
//"DefaultEmbeddingParameters": {
26+
// "Dimensions": 1536
27+
//}
28+
},
29+
"Logging": {
30+
"LogLevel": {
31+
"Default": "Error",
32+
"Microsoft": "Warning"
33+
}
34+
}
35+
}

src/ChatGptNet/ChatGptClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ public async IAsyncEnumerable<ChatGptResponse> AskStreamAsync(Guid conversationI
158158
}
159159
}
160160
}
161+
else if (response.Usage is not null)
162+
{
163+
// The usage field on this chunk shows the token usage statistics for the entire request, and the choices field will always be an empty array.
164+
// All other chunks will also include a usage field, but with a null value.
165+
yield return response;
166+
}
161167
}
162168
else if (line.StartsWith("data: [DONE]"))
163169
{
@@ -350,6 +356,8 @@ private ChatGptRequest CreateChatGptRequest(IEnumerable<ChatGptMessage> messages
350356
},
351357

352358
Stream = stream,
359+
StreamOptions = stream ? new() { IncludeUsage = true } : null,
360+
353361
Seed = parameters?.Seed ?? options.DefaultParameters.Seed,
354362
Temperature = parameters?.Temperature ?? options.DefaultParameters.Temperature,
355363
TopP = parameters?.TopP ?? options.DefaultParameters.TopP,

src/ChatGptNet/Models/ChatGptRequest.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ChatGptNet.Models;
44

55
/// <summary>
6-
/// Represents a request for a chat completions.
6+
/// Represents a request for chat completions.
77
/// </summary>
88
/// <remarks>
99
/// See <see href="https://platform.openai.com/docs/api-reference/chat/create">Create chat completion (OpenAI)</see> or <see href="https://learn.microsoft.com/azure/cognitive-services/openai/reference#chat-completions">Chat Completions (Azure)</see> for more information.
@@ -32,7 +32,7 @@ internal class ChatGptRequest
3232
/// Controls which (if any) function is called by the model.
3333
/// </summary>
3434
/// <remarks>
35-
/// <list type = "bullet" >
35+
/// <list type="bullet">
3636
/// <item>
3737
/// <term><see cref="ChatGptToolChoices.None"/></term>
3838
/// <description>Model will not call a function and instead generates a message.</description>
@@ -46,7 +46,7 @@ internal class ChatGptRequest
4646
/// <description>Specifying a particular function name forces the model to call that function.</description>
4747
/// </item>
4848
/// </list>
49-
/// <see cref="ChatGptToolChoices.None"/> is the default when no functions are present. <see cref="ChatGptToolChoices.None"/> is the default if functions are present.
49+
/// <para><see cref="ChatGptToolChoices.None"/> is the default when no functions are present. <see cref="ChatGptToolChoices.None"/> is the default if functions are present.</para>
5050
/// </remarks>
5151
/// <seealso cref="ChatGptFunction"/>
5252
[JsonPropertyName("function_call")]
@@ -64,7 +64,7 @@ internal class ChatGptRequest
6464
/// Controls which (if any) function is called by the model.
6565
/// </summary>
6666
/// <remarks>
67-
/// <list type = "bullet" >
67+
/// <list type="bullet">
6868
/// <item>
6969
/// <term><see cref="ChatGptToolChoices.None"/></term>
7070
/// <description>Model will not call a function and instead generates a message.</description>
@@ -78,17 +78,23 @@ internal class ChatGptRequest
7878
/// <description>Specifying a particular function name forces the model to call that function.</description>
7979
/// </item>
8080
/// </list>
81-
/// <see cref="ChatGptToolChoices.None"/> is the default when no functions are present. <see cref="ChatGptToolChoices.None"/> is the default if functions are present.
81+
/// <para><see cref="ChatGptToolChoices.None"/> is the default when no functions are present. <see cref="ChatGptToolChoices.None"/> is the default if functions are present.</para>
8282
/// </remarks>
8383
/// <seealso cref="ChatGptFunction"/>
8484
[JsonPropertyName("tool_choice")]
8585
public object? ToolChoice { get; set; }
8686

8787
/// <summary>
88-
/// Gets or sets a value that specify if response will be sent in streaming as partial message deltas.
88+
/// Gets or sets a value that specifies if the response will be sent in streaming as partial message deltas.
8989
/// </summary>
9090
public bool Stream { get; set; }
9191

92+
/// <summary>
93+
/// Gets or sets the stream options for chat completions.
94+
/// </summary>
95+
[JsonPropertyName("stream_options")]
96+
public ChatGptStreamOptions? StreamOptions { get; set; }
97+
9298
/// <summary>
9399
/// Gets or sets a value such that, if specified, the system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
94100
/// </summary>
@@ -99,10 +105,10 @@ internal class ChatGptRequest
99105
public int? Seed { get; set; }
100106

101107
/// <summary>
102-
/// Gets or sets what sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic (default: 1).
108+
/// Gets or sets the sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic (default: 1).
103109
/// </summary>
104110
/// <remarks>
105-
/// It is generally recommend altering this value or <see cref="TopP"/> but not both.
111+
/// It is generally recommended to alter this value or <see cref="TopP"/> but not both.
106112
/// </remarks>
107113
/// <seealso cref="TopP"/>
108114
public double? Temperature { get; set; }
@@ -111,7 +117,7 @@ internal class ChatGptRequest
111117
/// Gets or sets an alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with <see cref="TopP"/> probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered (default: 1).
112118
/// </summary>
113119
/// <remarks>
114-
/// It is generally recommend altering this value or <see cref="Temperature"/> but not both.
120+
/// It is generally recommended to alter this value or <see cref="Temperature"/> but not both.
115121
/// </remarks>
116122
/// <seealso cref="Temperature"/>
117123
[JsonPropertyName("top_p")]
@@ -124,7 +130,7 @@ internal class ChatGptRequest
124130
public int? MaxTokens { get; set; }
125131

126132
/// <summary>
127-
/// 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).
133+
/// 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).
128134
/// </summary>
129135
/// <remarks>
130136
/// See <see href="https://platform.openai.com/docs/api-reference/parameter-details">Parameter details</see> for more information.
@@ -133,7 +139,7 @@ internal class ChatGptRequest
133139
public double? PresencePenalty { get; set; }
134140

135141
/// <summary>
136-
/// Gets or sets the frequency penalties for chat completion. Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim (default: 0).
142+
/// Gets or sets the frequency penalties for chat completion. A number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim (default: 0).
137143
/// </summary>
138144
/// <remarks>
139145
/// See <see href="https://platform.openai.com/docs/api-reference/parameter-details">Parameter details</see> for more information.
@@ -149,7 +155,7 @@ internal class ChatGptRequest
149155
public ChatGptResponseFormat? ResponseFormat { get; set; }
150156

151157
/// <summary>
152-
/// Gets or set a value that determines whether to return log probabilities of the output tokens or not. If <see langword="true"/>, returns the log probabilities of each output token returned in the content of message (default: <see langword="false"/>).
158+
/// Gets or sets a value that determines whether to return log probabilities of the output tokens or not. If <see langword="true"/>, returns the log probabilities of each output token returned in the content of the message (default: <see langword="false"/>).
153159
/// </summary>
154160
/// <seealso cref="TopLogProbabilities"/>
155161
[JsonPropertyName("logprobs")]
@@ -159,7 +165,7 @@ internal class ChatGptRequest
159165
/// Gets or sets a value between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
160166
/// </summary>
161167
/// <remarks>
162-
/// <see cref="LogProbabilities"/>must be set to <see langword="true"/> if this parameter is used.
168+
/// <see cref="LogProbabilities"/> must be set to <see langword="true"/> if this parameter is used.
163169
/// </remarks>
164170
/// <seealso cref="LogProbabilities"/>
165171
[JsonPropertyName("top_logprobs")]
@@ -172,4 +178,4 @@ internal class ChatGptRequest
172178
/// See <see href="https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids">Safety best practices</see> for more information.
173179
/// </remarks>
174180
public string? User { get; set; }
175-
}
181+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace ChatGptNet.Models;
4+
5+
internal class ChatGptStreamOptions
6+
{
7+
[JsonPropertyName("include_usage")]
8+
public bool IncludeUsage { get; set; } = true;
9+
}

src/ChatGptNet/Models/Common/Response.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public abstract class Response
2020
/// <summary>
2121
/// Gets or sets information about token usage.
2222
/// </summary>
23-
/// <remarks>
24-
/// The <see cref="Usage"/> property is always <see langword="null"/> when requesting response streaming with <see cref="ChatGptClient.AskStreamAsync(Guid, string, ChatGptParameters?, string?, bool, CancellationToken)"/>.
25-
/// </remarks>
2623
public ChatGptUsage? Usage { get; set; }
2724

2825
/// <summary>

src/ChatGptNet/ServiceConfigurations/AzureChatGptServiceConfiguration.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ internal class AzureChatGptServiceConfiguration : ChatGptServiceConfiguration
1010
/// <summary>
1111
/// The default API version for Azure OpenAI service.
1212
/// </summary>
13-
public const string DefaultApiVersion = "2024-06-01";
13+
public const string DefaultApiVersion = "2024-07-01-preview";
1414

1515
/// <summary>
1616
/// Gets or sets the name of the Azure OpenAI Resource.
1717
/// </summary>
1818
public string? ResourceName { get; set; }
1919

2020
/// <summary>
21-
/// Gets or sets the API version of the Azure OpenAI service (Default: 2024-06-01).
21+
/// Gets or sets the API version of the Azure OpenAI service (Default: 2024-07-01-preview).
2222
/// </summary>
2323
/// <remarks>
2424
/// Currently supported versions are:
@@ -59,7 +59,10 @@ internal class AzureChatGptServiceConfiguration : ChatGptServiceConfiguration
5959
/// <term>2024-06-01</term>
6060
/// <description><see href="https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/2024-06-01/inference.json">Swagger spec</see></description>
6161
/// </item>
62-
/// </list>
62+
/// <item>
63+
/// <term>2024-07-01-preview</term>
64+
/// <description><see href="https://github.com/Azure/azure-rest-api-specs/blob/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2024-07-01-preview/inference.json">Swagger spec</see></description>
65+
/// </item>/// </list>
6366
/// </remarks>
6467
public string ApiVersion { get; set; } = DefaultApiVersion;
6568

src/ChatGptNet/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "3.2",
3+
"version": "3.3",
44
"publicReleaseRefSpec": [
55
"^refs/heads/master$" // we release out of master
66
],

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139" PrivateAssets="All" />
12+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.143" PrivateAssets="All" />
1313
</ItemGroup>
1414

1515
</Project>

0 commit comments

Comments
 (0)