Skip to content

Commit

Permalink
fix(openai_dart): Fetch requests with big payloads dropping connection (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Nov 17, 2023
1 parent eb6f602 commit 1e77109
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/openai_dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ class OpenAIClient extends g.OpenAIClient {
CreateChatCompletionStreamResponse.fromJson(json.decode(d)),
);
}

@override
Future<http.BaseRequest> onRequest(final http.BaseRequest request) {
return onRequestHandler(request);
}
}

class _OpenAIStreamTransformer
Expand Down
10 changes: 10 additions & 0 deletions packages/openai_dart/lib/src/http_client/http_client_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(fetch.FetchClient(mode: fetch.RequestMode.cors));
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
// If the request if bigger than 60KiB set persistentConnection to false
// Ref: https://github.com/Zekfad/fetch_client#large-payload
if ((request.contentLength ?? 0) > 61440) {
request.persistentConnection = false;
}
return Future.value(request);
}
5 changes: 5 additions & 0 deletions packages/openai_dart/lib/src/http_client/http_client_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ import 'package:http/retry.dart';
http.Client createDefaultHttpClient() {
return RetryClient(http.Client());
}

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) {
return Future.value(request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import 'package:http/http.dart' as http;
http.Client createDefaultHttpClient() => throw UnsupportedError(
'Cannot create a client without dart:html or dart:io.',
);

/// Middleware for HTTP requests.
Future<http.BaseRequest> onRequestHandler(final http.BaseRequest request) =>
throw UnsupportedError('stub');

0 comments on commit 1e77109

Please sign in to comment.