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

Ignore lines that start with : ping #472

Merged
merged 5 commits into from
Feb 1, 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
3 changes: 2 additions & 1 deletion OpenAI.SDK/Interfaces/IChatCompletionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public interface IChatCompletionService
/// </summary>
/// <param name="modelId">The ID of the model to use for this request</param>
/// <param name="chatCompletionCreate"></param>
/// <param name="justDataMode">Ignore stream lines if they don’t start with "data:". If you don't know what it means, probably you shouldn't change this.</param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns></returns>
IAsyncEnumerable<ChatCompletionCreateResponse> CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreate, string? modelId = null, CancellationToken cancellationToken = default);
IAsyncEnumerable<ChatCompletionCreateResponse> CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreate, string? modelId = null, bool justDataMode = true,CancellationToken cancellationToken = default);
}

public static class IChatCompletionServiceExtension
Expand Down
8 changes: 7 additions & 1 deletion OpenAI.SDK/Managers/OpenAIChatCompletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task<ChatCompletionCreateResponse> CreateCompletion(ChatCompletionC
}

/// <inheritdoc />
public async IAsyncEnumerable<ChatCompletionCreateResponse> CreateCompletionAsStream(ChatCompletionCreateRequest chatCompletionCreateRequest, string? modelId = null,
public async IAsyncEnumerable<ChatCompletionCreateResponse> 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
Expand All @@ -39,12 +39,18 @@ public async IAsyncEnumerable<ChatCompletionCreateResponse> 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
Expand Down