fetchStream method

  1. @override
Stream<String> fetchStream({
  1. int retries = 2,
  2. FutureOr<void> onError(
    1. CompletionException error,
    2. int retriesRemaning
    )?,
  3. void onJsonComplete(
    1. Map<String, dynamic> chunk
    )?,
})
override

Implementation

@override
Stream<String> fetchStream({
  int retries = 2,
  FutureOr<void> Function(CompletionException error, int retriesRemaning)?
      onError,
  void Function(Map<String, dynamic> chunk)? onJsonComplete,
}) async* {
  var response = await dio.post(
    url,
    data: {
      'model': model.toString(),
      'messages': messages,
      'stream': true,
    },
    options: Options(
      responseType: ResponseType.stream,
    ),
  );

  // Fetches the content of the message
  var stream = getJsonStreamFromResponse(response);
  await for (var json in stream) {
    if (onJsonComplete != null) onJsonComplete(json);
    var content = readMessageChunk(json);
    if (content != null) {
      yield content;
    }
  }
}