Skip to content

Commit

Permalink
[ Fix ] fixed the doubled stream responses from completion and fine-t…
Browse files Browse the repository at this point in the history
…unes events
  • Loading branch information
anasfik committed Feb 17, 2023
1 parent 2d39392 commit f01720f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.4.8

- Fix issues
- Fix doubled stream snapshots than expected.

# 1.4.6

- Changed the use of dotenv to envied package.
Expand Down
13 changes: 9 additions & 4 deletions lib/src/core/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class OpenAINetworkingClient {
OpenAILogger.log("starting decoding response body");
Utf8Decoder utf8decoder = Utf8Decoder();
final Map<String, dynamic> decodedBody =
jsonDecode(utf8decoder.convert(response.bodyBytes)) as Map<String, dynamic>;
jsonDecode(utf8decoder.convert(response.bodyBytes))
as Map<String, dynamic>;

OpenAILogger.log("response body decoded successfully");

Expand Down Expand Up @@ -67,7 +68,8 @@ class OpenAINetworkingClient {
streamedResponse.stream.listen((value) {
final String data = utf8.decode(value);

final List<String> dataLines = data.split("\n");
final List<String> dataLines =
data.split("\n").where((element) => element.isNotEmpty).toList();

for (String line in dataLines) {
if (line.startsWith("data: ")) {
Expand Down Expand Up @@ -111,7 +113,8 @@ class OpenAINetworkingClient {
OpenAILogger.log("starting decoding response body");
Utf8Decoder utf8decoder = Utf8Decoder();
final Map<String, dynamic> decodedBody =
jsonDecode(utf8decoder.convert(response.bodyBytes)) as Map<String, dynamic>;
jsonDecode(utf8decoder.convert(response.bodyBytes))
as Map<String, dynamic>;
OpenAILogger.log("response body decoded successfully");

if (decodedBody['error'] != null) {
Expand Down Expand Up @@ -192,7 +195,8 @@ class OpenAINetworkingClient {
respond.stream.listen((value) {
final String data = utf8.decode(value);

final List<String> dataLines = data.split("\n");
final List<String> dataLines =
data.split("\n").where((element) => element.isNotEmpty).toList();

for (String line in dataLines) {
if (line.startsWith("data: ")) {
Expand All @@ -203,6 +207,7 @@ class OpenAINetworkingClient {
}

final decoded = jsonDecode(data) as Map<String, dynamic>;

controller.add(onSuccess(decoded));
}
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: dart_openai
description: Dart SDK for openAI Apis (GPT-3 & DALL-E), integrate easily the power of OpenAI's state-of-the-art AI models into their Dart applications.
version: 1.4.6
version: 1.4.8
homepage: https://github.com/anasfik/openai
repository: https://github.com/anasfik/openai
documentation: https://github.com/anasfik/openai/blob/main/README.md

issue_tracker: https://github.com/anasfik/openai/issues
environment:
sdk: ">=2.18.5 <3.0.0"

Expand Down

0 comments on commit f01720f

Please sign in to comment.