diff --git a/OpenAI.SDK/Interfaces/IChatCompletionService.cs b/OpenAI.SDK/Interfaces/IChatCompletionService.cs index 19ec3b6c..6ff0dc58 100644 --- a/OpenAI.SDK/Interfaces/IChatCompletionService.cs +++ b/OpenAI.SDK/Interfaces/IChatCompletionService.cs @@ -23,9 +23,10 @@ public interface IChatCompletionService /// /// The ID of the model to use for this request /// + /// Ignore stream lines if they don’t start with "data:". If you don't know what it means, probably you shouldn't change this. /// Propagates notification that operations should be canceled. /// - IAsyncEnumerable CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreate, string? modelId = null, CancellationToken cancellationToken = default); + IAsyncEnumerable CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreate, string? modelId = null, bool justDataMode = true,CancellationToken cancellationToken = default); } public static class IChatCompletionServiceExtension diff --git a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs index 8018c604..ffd52b08 100644 --- a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs +++ b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs @@ -17,7 +17,7 @@ public async Task CreateCompletion(ChatCompletionC } /// - public async IAsyncEnumerable CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreateRequest, string? modelId = null, + public async IAsyncEnumerable CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreateRequest, string? modelId = null, bool justDataMode = true, [EnumeratorCancellation] CancellationToken cancellationToken = default) { // Helper data in case we need to reassemble a multi-packet response @@ -39,12 +39,18 @@ public async IAsyncEnumerable CreateCompletionAsSt cancellationToken.ThrowIfCancellationRequested(); var line = await reader.ReadLineAsync(); + // Skip empty lines if (string.IsNullOrEmpty(line)) { continue; } + if (justDataMode && !line.StartsWith("data: ")) + { + continue; + } + line = line.RemoveIfStartWith("data: "); // Exit the loop if the stream is done