Skip to content

Commit

Permalink
Start reading chat response stream immediately after headers are rece…
Browse files Browse the repository at this point in the history
…ived (#13)
  • Loading branch information
magols committed Jun 16, 2024
1 parent bbf161e commit aa77487
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa
options = chatExecutionSettings
};

var response = await Http.PostAsJsonAsync($"{Attributes["base_url"]}/api/chat", data, cancellationToken).ConfigureAwait(false);
var request = new HttpRequestMessage(HttpMethod.Post, $"{Attributes["base_url"]}/api/chat");
request.Content = JsonContent.Create(data);
using var response = await Http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

ValidateOllamaResponse(response);

Expand All @@ -64,7 +66,7 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa

while (!done)
{
string jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
string jsonResponse = await reader.ReadLineAsync();

var chatResponseMessage = JsonSerializer.Deserialize<OllamaChatResponseMessage>(jsonResponse);
done = chatResponseMessage!.Done;
Expand Down

0 comments on commit aa77487

Please sign in to comment.