Skip to content

Commit f1821c8

Browse files
authored
Merge pull request #124 from bartoszpijet/web-streaming
Implement web streaming api
2 parents 54f6a38 + 29fa27e commit f1821c8

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed

.flutter-plugins-dependencies

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_android-2.2.10/","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2024-09-25 20:59:28.938845","version":"3.24.3","swift_package_manager_enabled":false}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cupertino_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cupertino_http-1.5.1/","native_build":true,"dependencies":[]}],"android":[{"name":"cronet_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cronet_http-1.3.2/","native_build":true,"dependencies":["jni"]},{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"macos":[{"name":"cupertino_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cupertino_http-1.5.1/","native_build":true,"dependencies":[]}],"linux":[{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"windows":[{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"cronet_http","dependencies":["jni"]},{"name":"cupertino_http","dependencies":[]},{"name":"jni","dependencies":[]}],"date_created":"2024-11-12 13:59:27.464677","version":"3.24.3","swift_package_manager_enabled":false}

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,7 @@
251251

252252
## 3.1.2
253253
- Update sdk
254-
- remove path provider
254+
- remove path provider
255+
256+
## 3.1.3
257+
- Implement support for streaming responses from OpenAI on web platform

example_app/openai_app/lib/bloc/openai/openai_bloc.dart

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33

44
import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
55
import 'package:flutter/cupertino.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:flutter_bloc/flutter_bloc.dart';
78
import 'package:get_it/get_it.dart';
89
import 'package:http/http.dart';
@@ -74,14 +75,16 @@ class OpenAIBloc extends Cubit<OpenAIState> {
7475
///[initOpenAISdk]
7576
void initOpenAISdk() async {
7677
_openAI = OpenAI.instance.build(
77-
token: getToken(),
78-
apiUrl:
79-
'https://api.openai.com/v1/', // you can replace with your api url
80-
enableLog: true,
81-
baseOption: HttpSetup(
82-
receiveTimeout: const Duration(seconds: 30),
83-
connectTimeout: const Duration(seconds: 30),
84-
sendTimeout: const Duration(seconds: 30)));
78+
token: getToken(),
79+
apiUrl: 'https://api.openai.com/v1/', // you can replace with your api url
80+
enableLog: true,
81+
baseOption: HttpSetup(
82+
receiveTimeout: const Duration(seconds: 30),
83+
connectTimeout: const Duration(seconds: 30),
84+
sendTimeout: const Duration(seconds: 30),
85+
streamingWebApi: kIsWeb,
86+
),
87+
);
8588
}
8689

8790
void openAIEvent(String event, {required Function() error}) {

example_app/openai_app/pubspec.lock

+24
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ packages:
9696
url: "https://pub.dev"
9797
source: hosted
9898
version: "5.7.0"
99+
dio_compatibility_layer:
100+
dependency: transitive
101+
description:
102+
name: dio_compatibility_layer
103+
sha256: bb7ea1dd6fe98b8f5e3d90da408802fc3abf14d4485416e882cf1b2c8fb4b209
104+
url: "https://pub.dev"
105+
source: hosted
106+
version: "0.1.0"
99107
dio_web_adapter:
100108
dependency: transitive
101109
description:
@@ -112,6 +120,22 @@ packages:
112120
url: "https://pub.dev"
113121
source: hosted
114122
version: "1.3.1"
123+
fetch_api:
124+
dependency: transitive
125+
description:
126+
name: fetch_api
127+
sha256: "97f46c25b480aad74f7cc2ad7ccba2c5c6f08d008e68f95c1077286ce243d0e6"
128+
url: "https://pub.dev"
129+
source: hosted
130+
version: "2.2.0"
131+
fetch_client:
132+
dependency: transitive
133+
description:
134+
name: fetch_client
135+
sha256: "9666ee14536778474072245ed5cba07db81ae8eb5de3b7bf4a2d1e2c49696092"
136+
url: "https://pub.dev"
137+
source: hosted
138+
version: "1.1.2"
115139
ffi:
116140
dependency: transitive
117141
description:

lib/src/model/client/http_setup.dart

+4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ class HttpSetup {
44
Duration receiveTimeout;
55
String proxy;
66

7+
// Experimental streaming web api with fetch as dio adapter
8+
bool streamingWebApi;
9+
710
HttpSetup({
811
this.sendTimeout = const Duration(seconds: 6),
912
this.connectTimeout = const Duration(seconds: 6),
1013
this.receiveTimeout = const Duration(seconds: 6),
1114
this.proxy = '',
15+
this.streamingWebApi = false,
1216
});
1317
}

lib/src/openai.dart

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import 'package:chat_gpt_sdk/src/utils/constants.dart';
2323
import 'package:chat_gpt_sdk/src/utils/token_builder.dart';
2424
import 'package:dio/dio.dart';
2525
import 'package:dio/io.dart';
26+
import 'package:dio_compatibility_layer/dio_compatibility_layer.dart';
27+
import 'package:fetch_client/fetch_client.dart';
2628
import 'client/interceptor/interceptor_wrapper.dart';
2729
import 'edit.dart';
2830
import 'i_openai.dart';
@@ -76,6 +78,19 @@ class OpenAI implements IOpenAI {
7678
connectTimeout: setup.connectTimeout,
7779
receiveTimeout: setup.receiveTimeout,
7880
));
81+
82+
const bool kIsWeb = bool.fromEnvironment('dart.library.js_util');
83+
84+
assert(setup.proxy.isEmpty || !setup.streamingWebApi,
85+
'You can\'t provide both proxy and experimental streaming api support currently');
86+
assert(
87+
!setup.streamingWebApi || kIsWeb,
88+
'You can\'t run web specific API on other platforms');
89+
90+
if (setup.streamingWebApi) {
91+
dio.httpClientAdapter =
92+
ConversionLayerAdapter(FetchClient(mode: RequestMode.cors));
93+
}
7994
if (setup.proxy.isNotEmpty) {
8095
dio.httpClientAdapter = IOHttpClientAdapter(createHttpClient: () {
8196
final client = HttpClient();

pubspec.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: chat_gpt_sdk
22
description: create chat bot and other bot with ChatGPT SDK Support GPT-4 , 3.5 and SSE Generate Prompt (Stream)
3-
version: 3.1.2
3+
version: 3.1.3
44
homepage: https://www.facebook.com/REDEVRX
55
repository: https://github.com/redevRx/Flutter-ChatGPT
66

@@ -10,6 +10,8 @@ environment:
1010

1111
dependencies:
1212
dio: ^5.7.0
13+
dio_compatibility_layer: ^0.1.0
14+
fetch_client: ^1.1.2
1315
http_parser: ^4.0.0
1416
# path_provider: ^2.1.4
1517

0 commit comments

Comments
 (0)