From 021f203eebafe129ba8d526379ba623d4dfc43ce Mon Sep 17 00:00:00 2001 From: Gavi Narra Date: Mon, 8 Jan 2024 20:23:16 -0500 Subject: [PATCH 1/3] Ignore lines that start with : ping in Chat Streaming --- OpenAI.SDK/Managers/OpenAIChatCompletions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs index 8018c604..337ade6c 100644 --- a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs +++ b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs @@ -40,7 +40,7 @@ public async IAsyncEnumerable CreateCompletionAsSt var line = await reader.ReadLineAsync(); // Skip empty lines - if (string.IsNullOrEmpty(line)) + if (string.IsNullOrEmpty(line) || line.StartsWith(": ping")) { continue; } From 5270b6218b3b455e737ccfde93ac55ae241ee85b Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Wed, 31 Jan 2024 21:21:32 +0000 Subject: [PATCH 2/3] Update for skippping stream chunks --- OpenAI.SDK/Interfaces/IChatCompletionService.cs | 3 ++- OpenAI.SDK/Managers/OpenAIChatCompletions.cs | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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 337ade6c..946c27ab 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) || line.StartsWith(": ping")) + if (string.IsNullOrEmpty(line)) { continue; } + if (justDataMode && !line.StartsWith("data: ")) + { + ; + } + line = line.RemoveIfStartWith("data: "); // Exit the loop if the stream is done From 5edba6927b83a35a82dd5a797bbff46c7e672b5a Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Wed, 31 Jan 2024 21:22:50 +0000 Subject: [PATCH 3/3] Update for skippping stream chunks --- OpenAI.SDK/Managers/OpenAIChatCompletions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs index 946c27ab..ffd52b08 100644 --- a/OpenAI.SDK/Managers/OpenAIChatCompletions.cs +++ b/OpenAI.SDK/Managers/OpenAIChatCompletions.cs @@ -48,7 +48,7 @@ public async IAsyncEnumerable CreateCompletionAsSt if (justDataMode && !line.StartsWith("data: ")) { - ; + continue; } line = line.RemoveIfStartWith("data: ");